github.com/koderover/helm@v2.17.0+incompatible/pkg/chartutil/generator/capabilities_default_versions_generate.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  // Generates the default versions to use with capabilities. This cannot be loaded
    16  // dynamically as it uses enough memory to cause out of memory issues in CI.
    17  //
    18  // +build ignore
    19  package main
    20  
    21  import (
    22  	"bytes"
    23  	"fmt"
    24  	"io/ioutil"
    25  	"os"
    26  	"path"
    27  	"sort"
    28  
    29  	"k8s.io/client-go/kubernetes/scheme"
    30  )
    31  
    32  const licenseHeader = `/*
    33  Copyright The Helm Authors.
    34  Licensed under the Apache License, Version 2.0 (the "License");
    35  you may not use this file except in compliance with the License.
    36  You may obtain a copy of the License at
    37  
    38  http://www.apache.org/licenses/LICENSE-2.0
    39  
    40  Unless required by applicable law or agreed to in writing, software
    41  distributed under the License is distributed on an "AS IS" BASIS,
    42  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    43  See the License for the specific language governing permissions and
    44  limitations under the License.
    45  */`
    46  
    47  func main() {
    48  	v := getVersions()
    49  
    50  	o := createOutput(v)
    51  
    52  	err := ioutil.WriteFile("capabilities_versions_generated.go", o, 0644)
    53  	if err != nil {
    54  		fmt.Printf("writing output: %s", err)
    55  		os.Exit(1)
    56  	}
    57  }
    58  
    59  func createOutput(v []string) []byte {
    60  	var out bytes.Buffer
    61  
    62  	fmt.Fprintln(&out, licenseHeader)
    63  	fmt.Fprint(&out, "// Code generated by capabilities_default_versions_generate.go; DO NOT EDIT.\n\n")
    64  	fmt.Fprint(&out, "package chartutil\n\n")
    65  	fmt.Fprintln(&out, "func defaultVersions() []string {")
    66  	fmt.Fprintln(&out, "\treturn []string{")
    67  
    68  	for _, v := range v {
    69  		fmt.Fprintf(&out, "\t\t\"%s\",\n", v)
    70  	}
    71  
    72  	fmt.Fprintln(&out, "\t}")
    73  	fmt.Fprintln(&out, "}")
    74  
    75  	return out.Bytes()
    76  }
    77  
    78  func getVersions() []string {
    79  
    80  	var s []string
    81  	var gv string
    82  	var gvk string
    83  
    84  	// Check is used so that we only add an item once to the return
    85  	check := make(map[string]struct{})
    86  
    87  	// Client go has a default scheme set with everything in it
    88  	// This includes over 500 group versions and group versioned kinds
    89  	for k := range scheme.Scheme.AllKnownTypes() {
    90  		gv = path.Join(k.Group, k.Version)
    91  		gvk = path.Join(k.Group, k.Version, k.Kind)
    92  		if _, ok := check[gv]; !ok {
    93  			check[gv] = struct{}{}
    94  			s = append(s, gv)
    95  		}
    96  		if _, ok := check[gvk]; !ok {
    97  			check[gvk] = struct{}{}
    98  			s = append(s, gvk)
    99  		}
   100  	}
   101  
   102  	// Put the names in a consistent order
   103  	sort.Strings(s)
   104  
   105  	return s
   106  }