k8s.io/kubernetes@v1.29.3/pkg/kubectl/cmd/convert/import_known_versions_test.go (about)

     1  /*
     2  Copyright 2021 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 convert
    18  
    19  import (
    20  	"sort"
    21  	"strings"
    22  	"testing"
    23  
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"k8s.io/client-go/kubernetes/scheme"
    27  	"k8s.io/kubernetes/pkg/api/legacyscheme"
    28  )
    29  
    30  func TestKnownVersions(t *testing.T) {
    31  	legacytypes := legacyscheme.Scheme.AllKnownTypes()
    32  	alreadyErroredGroups := map[string]bool{}
    33  	var gvks []schema.GroupVersionKind
    34  	for gvk := range scheme.Scheme.AllKnownTypes() {
    35  		gvks = append(gvks, gvk)
    36  	}
    37  	sort.Slice(gvks, func(i, j int) bool {
    38  		if isWatchEvent1, isWatchEvent2 := gvks[i].Kind == "WatchEvent", gvks[j].Kind == "WatchEvent"; isWatchEvent1 != isWatchEvent2 {
    39  			return isWatchEvent2
    40  		}
    41  		if isList1, isList2 := strings.HasSuffix(gvks[i].Kind, "List"), strings.HasSuffix(gvks[j].Kind, "List"); isList1 != isList2 {
    42  			return isList2
    43  		}
    44  		if isOptions1, isOptions2 := strings.HasSuffix(gvks[i].Kind, "Options"), strings.HasSuffix(gvks[j].Kind, "Options"); isOptions1 != isOptions2 {
    45  			return isOptions2
    46  		}
    47  		if isInternal1, isInternal2 := gvks[i].Group == runtime.APIVersionInternal, gvks[j].Group == runtime.APIVersionInternal; isInternal1 != isInternal2 {
    48  			return isInternal2
    49  		}
    50  		return gvks[i].String() < gvks[j].String()
    51  	})
    52  	for _, gvk := range gvks {
    53  		if alreadyErroredGroups[gvk.Group] {
    54  			continue
    55  		}
    56  		if _, legacyregistered := legacytypes[gvk]; !legacyregistered {
    57  			t.Errorf("%v is not registered in legacyscheme. Add group %q (all internal and external versions) to convert/import_known_versions.go", gvk, gvk.Group)
    58  			alreadyErroredGroups[gvk.Group] = true
    59  		}
    60  	}
    61  }