...

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

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

     1  /*
     2  Copyright 2024 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 cel
    18  
    19  import (
    20  	"fmt"
    21  	"slices"
    22  	"strings"
    23  	"testing"
    24  
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/apimachinery/pkg/util/sets"
    28  	"k8s.io/client-go/discovery"
    29  	apiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
    30  	"k8s.io/kubernetes/pkg/kubeapiserver/admission/exclusion"
    31  	"k8s.io/kubernetes/test/integration/framework"
    32  )
    33  
    34  // TestExcludedResources is an open-box test to ensure that a resource that is not persisted
    35  // is either in the allow-list or block-list of the CEL admission.
    36  //
    37  // see staging/src/k8s.io/apiserver/pkg/admission/exclusion/exclusion.go
    38  // for the lists that the test is about.
    39  func TestExcludedResources(t *testing.T) {
    40  	server, err := apiservertesting.StartTestServer(t,
    41  		nil, // no extra options
    42  		// enable all APIs to cover all defined resources
    43  		// note that it does not require feature flags enabled to
    44  		// discovery GVRs of disabled features.
    45  		[]string{"--runtime-config=api/all=true"},
    46  		framework.SharedEtcd())
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	defer server.TearDownFn()
    51  
    52  	config := server.ClientConfig
    53  
    54  	discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	_, resourceLists, _, err := discoveryClient.GroupsAndMaybeResources()
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	interestedGRs := sets.New[schema.GroupResource]()
    63  	interestedVerbCombinations := []metav1.Verbs{
    64  		{"create"},
    65  		{"create", "get"},
    66  	}
    67  	for _, rl := range resourceLists {
    68  		for _, r := range rl.APIResources {
    69  			slices.Sort(r.Verbs)
    70  			for _, c := range interestedVerbCombinations {
    71  				if slices.Equal(r.Verbs, c) {
    72  					gv, err := schema.ParseGroupVersion(rl.GroupVersion)
    73  					if err != nil {
    74  						t.Fatalf("internal error: cannot parse GV from %q: %v", rl.GroupVersion, err)
    75  					}
    76  					interestedGRs.Insert(gv.WithResource(r.Name).GroupResource())
    77  				}
    78  			}
    79  		}
    80  	}
    81  	existing := sets.New[schema.GroupResource]()
    82  	existing.Insert(exclusion.Included()...)
    83  	existing.Insert(exclusion.Excluded()...)
    84  	shouldAdd, shouldRemove := interestedGRs.Difference(existing), existing.Difference(interestedGRs)
    85  	if shouldAdd.Len() > 0 {
    86  		t.Errorf("the following resources should either be in Included or Excluded in\n"+
    87  			"pkg/kubeapiserver/admission/exclusion/resources.go\n%s",
    88  			formatGRs(shouldAdd.UnsortedList()),
    89  		)
    90  	}
    91  	if shouldRemove.Len() > 0 {
    92  		t.Errorf("the following resources are in pkg/kubeapiserver/admission/exclusion/resources.go\n"+
    93  			"but does not seem to be transient.\n%s",
    94  			formatGRs(shouldRemove.UnsortedList()))
    95  	}
    96  }
    97  
    98  func formatGRs(grs []schema.GroupResource) string {
    99  	lines := make([]string, 0, len(grs))
   100  	for _, gvr := range grs {
   101  		item := fmt.Sprintf("%#v,", gvr)
   102  		lines = append(lines, item)
   103  	}
   104  	slices.Sort(lines)
   105  	return strings.Join(lines, "\n")
   106  }
   107  

View as plain text