k8s.io/registry.k8s.io@v0.3.1/pkg/net/cloudcidrs/internal/ranges2go/main.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  // ranges2go generates a go source file with pre-parsed AWS IP ranges data.
    18  // See also genrawdata.sh for downloading the raw data to this binary.
    19  package main
    20  
    21  import (
    22  	"os"
    23  	"path/filepath"
    24  )
    25  
    26  func main() {
    27  	// overridable for make verify
    28  	outputPath := os.Getenv("OUT_FILE")
    29  	dataDir := os.Getenv("DATA_DIR")
    30  	if outputPath == "" {
    31  		outputPath = "./zz_generated_range_data.go"
    32  	}
    33  	if dataDir == "" {
    34  		dataDir = "./internal/ranges2go/data"
    35  	}
    36  	// read in data
    37  	awsRaw := mustReadFile(filepath.Join(dataDir, "aws-ip-ranges.json"))
    38  	gcpRaw := mustReadFile(filepath.Join(dataDir, "gcp-cloud.json"))
    39  	// parse raw AWS IP range data
    40  	awsRTP, err := parseAWS(awsRaw)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	// parse GCP IP range data
    45  	gcpRTP, err := parseGCP(gcpRaw)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	// emit file
    50  	f, err := os.Create(outputPath)
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  	cloudToRTP := map[string]regionsToPrefixes{
    55  		"AWS": awsRTP,
    56  		"GCP": gcpRTP,
    57  	}
    58  	if err := generateRangesGo(f, cloudToRTP); err != nil {
    59  		panic(err)
    60  	}
    61  }
    62  
    63  func mustReadFile(filePath string) string {
    64  	contents, err := os.ReadFile(filePath)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  	return string(contents)
    69  }