...

Source file src/k8s.io/kubernetes/test/integration/apiserver/no_new_betas_test.go

Documentation: k8s.io/kubernetes/test/integration/apiserver

     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 apiserver
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime/schema"
    24  )
    25  
    26  func TestNoNewBetaAPIsByDefault(t *testing.T) {
    27  	// yes, this *is* a copy/paste from somewhere else.  We really do mean it when we say you shouldn't be modifying
    28  	// this list and this test was created to make it more painful.
    29  	// legacyBetaEnabledByDefaultResources is the list of beta resources we enable.  You may not add to this list
    30  	legacyBetaEnabledByDefaultResources := map[schema.GroupVersionResource]bool{
    31  		gvr("flowcontrol.apiserver.k8s.io", "v1beta2", "flowschemas"):                 true, // remove in 1.29
    32  		gvr("flowcontrol.apiserver.k8s.io", "v1beta2", "prioritylevelconfigurations"): true, // remove in 1.29
    33  	}
    34  
    35  	// legacyBetaResourcesWithoutStableEquivalents contains those groupresources that were enabled by default as beta
    36  	// before we changed that policy and do not have stable versions. These resources are allowed to have additional
    37  	// beta versions enabled by default.  Nothing new should be added here.  There are no future exceptions because there
    38  	// are no more beta resources enabled by default.
    39  	legacyBetaResourcesWithoutStableEquivalents := map[schema.GroupResource]bool{
    40  		gvr("flowcontrol.apiserver.k8s.io", "v1beta1", "flowschemas").GroupResource():                 true,
    41  		gvr("flowcontrol.apiserver.k8s.io", "v1beta1", "prioritylevelconfigurations").GroupResource(): true,
    42  	}
    43  
    44  	// if you found this because you want to create an integration test for your new beta API, the method you're looking for
    45  	// is this setupWithResources method and you need to pass the resource you want to enable into it.
    46  	_, kubeClient, _, tearDownFn := setupWithResources(t,
    47  		[]schema.GroupVersion{},
    48  		[]schema.GroupVersionResource{},
    49  	)
    50  	defer tearDownFn()
    51  
    52  	_, allResourceLists, err := kubeClient.Discovery().ServerGroupsAndResources()
    53  	if err != nil {
    54  		t.Error(err)
    55  	}
    56  
    57  	for _, currResourceList := range allResourceLists {
    58  		for _, currResource := range currResourceList.APIResources {
    59  			if !strings.Contains(currResource.Version, "beta") {
    60  				continue // skip non-beta apis
    61  			}
    62  			if strings.Contains(currResource.Name, "/") {
    63  				continue // skip subresources
    64  			}
    65  			enabledGVR := schema.GroupVersionResource{
    66  				Group:    currResource.Group,
    67  				Version:  currResource.Version,
    68  				Resource: currResource.Name,
    69  			}
    70  			if legacyBetaEnabledByDefaultResources[enabledGVR] {
    71  				continue
    72  			}
    73  			if legacyBetaResourcesWithoutStableEquivalents[enabledGVR.GroupResource()] {
    74  				continue
    75  			}
    76  
    77  			t.Errorf("%v is a new beta API.  New beta APIs may not be enabled by default.  "+
    78  				"See https://github.com/kubernetes/enhancements/blob/0ad0fc8269165ca300d05ca51c7ce190a79976a5/keps/sig-architecture/3136-beta-apis-off-by-default/README.md "+
    79  				"for more details.", enabledGVR)
    80  		}
    81  	}
    82  }
    83  

View as plain text