sigs.k8s.io/cluster-api@v1.7.1/internal/test/builder/crds.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 builder
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/gobuffalo/flect"
    23  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"k8s.io/utils/ptr"
    27  
    28  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    29  	"sigs.k8s.io/cluster-api/util/contract"
    30  )
    31  
    32  func untypedCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
    33  	return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
    34  		"spec": {
    35  			Type:                   "object",
    36  			XPreserveUnknownFields: ptr.To(true),
    37  		},
    38  		"status": {
    39  			Type:                   "object",
    40  			XPreserveUnknownFields: ptr.To(true),
    41  		},
    42  	})
    43  }
    44  
    45  func generateCRD(gvk schema.GroupVersionKind, properties map[string]apiextensionsv1.JSONSchemaProps) *apiextensionsv1.CustomResourceDefinition {
    46  	return &apiextensionsv1.CustomResourceDefinition{
    47  		TypeMeta: metav1.TypeMeta{
    48  			APIVersion: apiextensionsv1.SchemeGroupVersion.String(),
    49  			Kind:       "CustomResourceDefinition",
    50  		},
    51  		ObjectMeta: metav1.ObjectMeta{
    52  			Name: contract.CalculateCRDName(gvk.Group, gvk.Kind),
    53  			Labels: map[string]string{
    54  				clusterv1.GroupVersion.String(): "v1beta1",
    55  			},
    56  		},
    57  		Spec: apiextensionsv1.CustomResourceDefinitionSpec{
    58  			Group: gvk.Group,
    59  			Scope: apiextensionsv1.NamespaceScoped,
    60  			Names: apiextensionsv1.CustomResourceDefinitionNames{
    61  				Kind:   gvk.Kind,
    62  				Plural: flect.Pluralize(strings.ToLower(gvk.Kind)),
    63  			},
    64  			Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
    65  				{
    66  					Name:    gvk.Version,
    67  					Served:  true,
    68  					Storage: true,
    69  					Subresources: &apiextensionsv1.CustomResourceSubresources{
    70  						Status: &apiextensionsv1.CustomResourceSubresourceStatus{},
    71  					},
    72  					Schema: &apiextensionsv1.CustomResourceValidation{
    73  						OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
    74  							Type:       "object",
    75  							Properties: properties,
    76  						},
    77  					},
    78  				},
    79  			},
    80  		},
    81  	}
    82  }
    83  
    84  var (
    85  	refSchema = apiextensionsv1.JSONSchemaProps{
    86  		Type: "object",
    87  		Properties: map[string]apiextensionsv1.JSONSchemaProps{
    88  			"apiVersion": {Type: "string"},
    89  			"kind":       {Type: "string"},
    90  			"name":       {Type: "string"},
    91  			"namespace":  {Type: "string"},
    92  			// NOTE: omitting fields not used for sake of simplicity.
    93  		},
    94  	}
    95  
    96  	metadataSchema = apiextensionsv1.JSONSchemaProps{
    97  		Type: "object",
    98  		Properties: map[string]apiextensionsv1.JSONSchemaProps{
    99  			"labels": {
   100  				Type: "object",
   101  				AdditionalProperties: &apiextensionsv1.JSONSchemaPropsOrBool{
   102  					Schema: &apiextensionsv1.JSONSchemaProps{Type: "string"},
   103  				},
   104  			},
   105  			"annotations": {
   106  				Type: "object",
   107  				AdditionalProperties: &apiextensionsv1.JSONSchemaPropsOrBool{
   108  					Schema: &apiextensionsv1.JSONSchemaProps{Type: "string"},
   109  				},
   110  			},
   111  		},
   112  	}
   113  )