k8s.io/registry.k8s.io@v0.3.1/pkg/net/cloudcidrs/internal/ranges2go/gen.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "io" 22 "sort" 23 ) 24 25 const fileHeader = `/* 26 Copyright The Kubernetes Authors. 27 28 Licensed under the Apache License, Version 2.0 (the "License"); 29 you may not use this file except in compliance with the License. 30 You may obtain a copy of the License at 31 32 http://www.apache.org/licenses/LICENSE-2.0 33 34 Unless required by applicable law or agreed to in writing, software 35 distributed under the License is distributed on an "AS IS" BASIS, 36 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 37 See the License for the specific language governing permissions and 38 limitations under the License. 39 */ 40 41 // File generated by ranges2go DO NOT EDIT 42 43 package cloudcidrs 44 45 import ( 46 "net/netip" 47 ) 48 49 ` 50 51 func generateRangesGo(w io.Writer, cloudToRTP map[string]regionsToPrefixes) error { 52 // generate source file header 53 if _, err := io.WriteString(w, fileHeader); err != nil { 54 return err 55 } 56 57 // ensure iteration order is predictable for reproducible codegen 58 clouds := make([]string, 0, len(cloudToRTP)) 59 for cloud := range cloudToRTP { 60 clouds = append(clouds, cloud) 61 } 62 sort.Strings(clouds) 63 64 // generate constants for each cloud 65 for _, cloud := range clouds { 66 if _, err := fmt.Fprintf(w, "// %s cloud\nconst %s = %q\n", cloud, cloud, cloud); err != nil { 67 return err 68 } 69 } 70 71 // generate main data variable 72 if _, err := io.WriteString(w, ` 73 // regionToRanges contains a preparsed map of cloud IPInfo to netip.Prefix 74 var regionToRanges = map[IPInfo][]netip.Prefix{ 75 `, 76 ); err != nil { 77 return err 78 } 79 for _, cloud := range clouds { 80 rtp := cloudToRTP[cloud] 81 if err := genCloud(w, cloud, rtp); err != nil { 82 return err 83 } 84 } 85 if _, err := io.WriteString(w, "}\n"); err != nil { 86 return err 87 } 88 89 return nil 90 } 91 92 func genCloud(w io.Writer, cloud string, rtp regionsToPrefixes) error { 93 // ensure iteration order is predictable for reproducible codegen 94 regions := make([]string, 0, len(rtp)) 95 for region := range rtp { 96 regions = append(regions, region) 97 } 98 sort.Strings(regions) 99 for _, region := range regions { 100 prefixes := rtp[region] 101 if _, err := fmt.Fprintf(w, "\t{Cloud: %s, Region: %q}: {\n", cloud, region); err != nil { 102 return err 103 } 104 for _, prefix := range prefixes { 105 addr := prefix.Addr() 106 bits := prefix.Bits() 107 // Using netip.*From avoids additional runtime allocation. 108 // 109 // It also means we don't need error checking / parsing cannot fail 110 // at runtime, we've already parsed these and re-emitted them 111 // as pre-computed IP address / bit mask values. 112 if addr.Is4() { 113 b := addr.As4() 114 if _, err := fmt.Fprintf(w, 115 "\t\tnetip.PrefixFrom(netip.AddrFrom4([4]byte{%d, %d, %d, %d}), %d),\n", 116 b[0], b[1], b[2], b[3], bits, 117 ); err != nil { 118 return err 119 } 120 } else { 121 b := addr.As16() 122 if _, err := fmt.Fprintf(w, 123 "\t\tnetip.PrefixFrom(netip.AddrFrom16([16]byte{%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d}), %d),\n", 124 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15], bits, 125 ); err != nil { 126 return err 127 } 128 } 129 } 130 if _, err := io.WriteString(w, "\t},\n"); err != nil { 131 return err 132 } 133 } 134 return nil 135 }