github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/meta/testrestmapper/test_restmapper.go (about)

     1  /*
     2  Copyright 2018 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 testrestmapper
    18  
    19  import (
    20  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/api/meta"
    21  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime"
    22  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    23  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/sets"
    24  )
    25  
    26  // TestOnlyStaticRESTMapper returns a union RESTMapper of all known types with priorities chosen in the following order:
    27  //  1. legacy kube group preferred version, extensions preferred version, metrics preferred version, legacy
    28  //     kube any version, extensions any version, metrics any version, all other groups alphabetical preferred version,
    29  //     all other groups alphabetical.
    30  //
    31  // TODO callers of this method should be updated to build their own specific restmapper based on their scheme for their tests
    32  // TODO the things being tested are related to whether various cases are handled, not tied to the particular types being checked.
    33  func TestOnlyStaticRESTMapper(scheme *runtime.Scheme, versionPatterns ...schema.GroupVersion) meta.RESTMapper {
    34  	unionMapper := meta.MultiRESTMapper{}
    35  	unionedGroups := sets.NewString()
    36  	for _, enabledVersion := range scheme.PrioritizedVersionsAllGroups() {
    37  		if !unionedGroups.Has(enabledVersion.Group) {
    38  			unionedGroups.Insert(enabledVersion.Group)
    39  			unionMapper = append(unionMapper, newRESTMapper(enabledVersion.Group, scheme))
    40  		}
    41  	}
    42  
    43  	if len(versionPatterns) != 0 {
    44  		resourcePriority := []schema.GroupVersionResource{}
    45  		kindPriority := []schema.GroupVersionKind{}
    46  		for _, versionPriority := range versionPatterns {
    47  			resourcePriority = append(resourcePriority, versionPriority.WithResource(meta.AnyResource))
    48  			kindPriority = append(kindPriority, versionPriority.WithKind(meta.AnyKind))
    49  		}
    50  
    51  		return meta.PriorityRESTMapper{Delegate: unionMapper, ResourcePriority: resourcePriority, KindPriority: kindPriority}
    52  	}
    53  
    54  	prioritizedGroups := []string{"", "extensions", "metrics"}
    55  	resourcePriority, kindPriority := prioritiesForGroups(scheme, prioritizedGroups...)
    56  
    57  	prioritizedGroupsSet := sets.NewString(prioritizedGroups...)
    58  	remainingGroups := sets.String{}
    59  	for _, enabledVersion := range scheme.PrioritizedVersionsAllGroups() {
    60  		if !prioritizedGroupsSet.Has(enabledVersion.Group) {
    61  			remainingGroups.Insert(enabledVersion.Group)
    62  		}
    63  	}
    64  
    65  	remainingResourcePriority, remainingKindPriority := prioritiesForGroups(scheme, remainingGroups.List()...)
    66  	resourcePriority = append(resourcePriority, remainingResourcePriority...)
    67  	kindPriority = append(kindPriority, remainingKindPriority...)
    68  
    69  	return meta.PriorityRESTMapper{Delegate: unionMapper, ResourcePriority: resourcePriority, KindPriority: kindPriority}
    70  }
    71  
    72  // prioritiesForGroups returns the resource and kind priorities for a PriorityRESTMapper, preferring the preferred version of each group first,
    73  // then any non-preferred version of the group second.
    74  func prioritiesForGroups(scheme *runtime.Scheme, groups ...string) ([]schema.GroupVersionResource, []schema.GroupVersionKind) {
    75  	resourcePriority := []schema.GroupVersionResource{}
    76  	kindPriority := []schema.GroupVersionKind{}
    77  
    78  	for _, group := range groups {
    79  		availableVersions := scheme.PrioritizedVersionsForGroup(group)
    80  		if len(availableVersions) > 0 {
    81  			resourcePriority = append(resourcePriority, availableVersions[0].WithResource(meta.AnyResource))
    82  			kindPriority = append(kindPriority, availableVersions[0].WithKind(meta.AnyKind))
    83  		}
    84  	}
    85  	for _, group := range groups {
    86  		resourcePriority = append(resourcePriority, schema.GroupVersionResource{Group: group, Version: meta.AnyVersion, Resource: meta.AnyResource})
    87  		kindPriority = append(kindPriority, schema.GroupVersionKind{Group: group, Version: meta.AnyVersion, Kind: meta.AnyKind})
    88  	}
    89  
    90  	return resourcePriority, kindPriority
    91  }
    92  
    93  func newRESTMapper(group string, scheme *runtime.Scheme) meta.RESTMapper {
    94  	mapper := meta.NewDefaultRESTMapper(scheme.PrioritizedVersionsForGroup(group))
    95  	for _, gv := range scheme.PrioritizedVersionsForGroup(group) {
    96  		for kind := range scheme.KnownTypes(gv) {
    97  			if ignoredKinds.Has(kind) {
    98  				continue
    99  			}
   100  			scope := meta.RESTScopeNamespace
   101  			if rootScopedKinds[gv.WithKind(kind).GroupKind()] {
   102  				scope = meta.RESTScopeRoot
   103  			}
   104  			mapper.Add(gv.WithKind(kind), scope)
   105  		}
   106  	}
   107  
   108  	return mapper
   109  }
   110  
   111  // hardcoded is good enough for the test we're running
   112  var rootScopedKinds = map[schema.GroupKind]bool{
   113  	{Group: "admission.k8s.io", Kind: "AdmissionReview"}: true,
   114  
   115  	{Group: "admissionregistration.k8s.io", Kind: "ValidatingWebhookConfiguration"}: true,
   116  	{Group: "admissionregistration.k8s.io", Kind: "MutatingWebhookConfiguration"}:   true,
   117  
   118  	{Group: "authentication.k8s.io", Kind: "TokenReview"}: true,
   119  
   120  	{Group: "authorization.k8s.io", Kind: "SubjectAccessReview"}:     true,
   121  	{Group: "authorization.k8s.io", Kind: "SelfSubjectAccessReview"}: true,
   122  	{Group: "authorization.k8s.io", Kind: "SelfSubjectRulesReview"}:  true,
   123  
   124  	{Group: "certificates.k8s.io", Kind: "CertificateSigningRequest"}: true,
   125  
   126  	{Group: "", Kind: "Node"}:             true,
   127  	{Group: "", Kind: "Namespace"}:        true,
   128  	{Group: "", Kind: "PersistentVolume"}: true,
   129  	{Group: "", Kind: "ComponentStatus"}:  true,
   130  
   131  	{Group: "extensions", Kind: "PodSecurityPolicy"}: true,
   132  
   133  	{Group: "policy", Kind: "PodSecurityPolicy"}: true,
   134  
   135  	{Group: "extensions", Kind: "PodSecurityPolicy"}: true,
   136  
   137  	{Group: "rbac.authorization.k8s.io", Kind: "ClusterRole"}:        true,
   138  	{Group: "rbac.authorization.k8s.io", Kind: "ClusterRoleBinding"}: true,
   139  
   140  	{Group: "scheduling.k8s.io", Kind: "PriorityClass"}: true,
   141  
   142  	{Group: "storage.k8s.io", Kind: "StorageClass"}:     true,
   143  	{Group: "storage.k8s.io", Kind: "VolumeAttachment"}: true,
   144  
   145  	{Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"}: true,
   146  
   147  	{Group: "apiserver.k8s.io", Kind: "AdmissionConfiguration"}: true,
   148  
   149  	{Group: "audit.k8s.io", Kind: "Event"}:  true,
   150  	{Group: "audit.k8s.io", Kind: "Policy"}: true,
   151  
   152  	{Group: "apiregistration.k8s.io", Kind: "APIService"}: true,
   153  
   154  	{Group: "metrics.k8s.io", Kind: "NodeMetrics"}: true,
   155  
   156  	{Group: "wardle.example.com", Kind: "Fischer"}: true,
   157  }
   158  
   159  // hardcoded is good enough for the test we're running
   160  var ignoredKinds = sets.NewString(
   161  	"ListOptions",
   162  	"DeleteOptions",
   163  	"Status",
   164  	"PodLogOptions",
   165  	"PodExecOptions",
   166  	"PodAttachOptions",
   167  	"PodPortForwardOptions",
   168  	"PodProxyOptions",
   169  	"NodeProxyOptions",
   170  	"ServiceProxyOptions",
   171  )