sigs.k8s.io/cluster-api@v1.7.1/internal/test/builder/infrastructure.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  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    21  	"k8s.io/apimachinery/pkg/runtime/schema"
    22  )
    23  
    24  var (
    25  	// InfrastructureGroupVersion is group version used for infrastructure objects.
    26  	InfrastructureGroupVersion = schema.GroupVersion{Group: "infrastructure.cluster.x-k8s.io", Version: "v1beta1"}
    27  
    28  	// GenericInfrastructureMachineKind is the Kind for the GenericInfrastructureMachine.
    29  	GenericInfrastructureMachineKind = "GenericInfrastructureMachine"
    30  	// GenericInfrastructureMachineCRD is a generic infrastructure machine CRD.
    31  	GenericInfrastructureMachineCRD = untypedCRD(InfrastructureGroupVersion.WithKind(GenericInfrastructureMachineKind))
    32  
    33  	// GenericInfrastructureMachineTemplateKind is the Kind for the GenericInfrastructureMachineTemplate.
    34  	GenericInfrastructureMachineTemplateKind = "GenericInfrastructureMachineTemplate"
    35  	// GenericInfrastructureMachineTemplateCRD is a generic infrastructure machine template CRD.
    36  	GenericInfrastructureMachineTemplateCRD = untypedCRD(InfrastructureGroupVersion.WithKind(GenericInfrastructureMachineTemplateKind))
    37  
    38  	// GenericInfrastructureMachinePoolTemplateKind is the Kind for the GenericInfrastructureMachinePoolTemplate.
    39  	GenericInfrastructureMachinePoolTemplateKind = "GenericInfrastructureMachinePoolTemplate"
    40  	// GenericInfrastructureMachinePoolTemplateCRD is a generic infrastructure machine pool template CRD.
    41  	GenericInfrastructureMachinePoolTemplateCRD = untypedCRD(InfrastructureGroupVersion.WithKind(GenericInfrastructureMachinePoolTemplateKind))
    42  
    43  	// GenericInfrastructureMachinePoolKind is the Kind for the GenericInfrastructureMachinePool.
    44  	GenericInfrastructureMachinePoolKind = "GenericInfrastructureMachinePool"
    45  	// GenericInfrastructureMachinePoolCRD is a generic infrastructure machine pool CRD.
    46  	GenericInfrastructureMachinePoolCRD = untypedCRD(InfrastructureGroupVersion.WithKind(GenericInfrastructureMachinePoolKind))
    47  
    48  	// GenericInfrastructureClusterKind is the kind for the GenericInfrastructureCluster type.
    49  	GenericInfrastructureClusterKind = "GenericInfrastructureCluster"
    50  	// GenericInfrastructureClusterCRD is a generic infrastructure machine CRD.
    51  	GenericInfrastructureClusterCRD = untypedCRD(InfrastructureGroupVersion.WithKind(GenericInfrastructureClusterKind))
    52  
    53  	// GenericInfrastructureClusterTemplateKind is the kind for the GenericInfrastructureClusterTemplate type.
    54  	GenericInfrastructureClusterTemplateKind = "GenericInfrastructureClusterTemplate"
    55  	// GenericInfrastructureClusterTemplateCRD is a generic infrastructure machine template CRD.
    56  	GenericInfrastructureClusterTemplateCRD = untypedCRD(InfrastructureGroupVersion.WithKind(GenericInfrastructureClusterTemplateKind))
    57  
    58  	// TODO: drop generic CRDs in favour of typed test CRDs.
    59  
    60  	// TestInfrastructureClusterTemplateKind is the kind for the TestInfrastructureClusterTemplate type.
    61  	TestInfrastructureClusterTemplateKind = "TestInfrastructureClusterTemplate"
    62  	// TestInfrastructureClusterTemplateCRD is a test infrastructure machine template CRD.
    63  	TestInfrastructureClusterTemplateCRD = testInfrastructureClusterTemplateCRD(InfrastructureGroupVersion.WithKind(TestInfrastructureClusterTemplateKind))
    64  
    65  	// TestInfrastructureClusterKind is the kind for the TestInfrastructureCluster type.
    66  	TestInfrastructureClusterKind = "TestInfrastructureCluster"
    67  	// TestInfrastructureClusterCRD is a test infrastructure machine CRD.
    68  	TestInfrastructureClusterCRD = testInfrastructureClusterCRD(InfrastructureGroupVersion.WithKind(TestInfrastructureClusterKind))
    69  
    70  	// TestInfrastructureMachineTemplateKind is the kind for the TestInfrastructureMachineTemplate type.
    71  	TestInfrastructureMachineTemplateKind = "TestInfrastructureMachineTemplate"
    72  	// TestInfrastructureMachineTemplateCRD is a test infrastructure machine template CRD.
    73  	TestInfrastructureMachineTemplateCRD = testInfrastructureMachineTemplateCRD(InfrastructureGroupVersion.WithKind(TestInfrastructureMachineTemplateKind))
    74  
    75  	// TestInfrastructureMachinePoolTemplateKind is the kind for the TestInfrastructureMachinePoolTemplate type.
    76  	TestInfrastructureMachinePoolTemplateKind = "TestInfrastructureMachinePoolTemplate"
    77  	// TestInfrastructureMachinePoolTemplateCRD is a test infrastructure machine pool template CRD.
    78  	TestInfrastructureMachinePoolTemplateCRD = testInfrastructureMachinePoolTemplateCRD(InfrastructureGroupVersion.WithKind(TestInfrastructureMachinePoolTemplateKind))
    79  
    80  	// TestInfrastructureMachinePoolKind is the kind for the TestInfrastructureMachinePool type.
    81  	TestInfrastructureMachinePoolKind = "TestInfrastructureMachinePool"
    82  	// TestInfrastructureMachinePoolCRD is a test infrastructure machine CRD.
    83  	TestInfrastructureMachinePoolCRD = testInfrastructureMachinePoolCRD(InfrastructureGroupVersion.WithKind(TestInfrastructureMachinePoolKind))
    84  
    85  	// TestInfrastructureMachineKind is the kind for the TestInfrastructureMachine type.
    86  	TestInfrastructureMachineKind = "TestInfrastructureMachine"
    87  	// TestInfrastructureMachineCRD is a test infrastructure machine CRD.
    88  	TestInfrastructureMachineCRD = testInfrastructureMachineCRD(InfrastructureGroupVersion.WithKind(TestInfrastructureMachineKind))
    89  )
    90  
    91  func testInfrastructureClusterTemplateCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
    92  	return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
    93  		"metadata": {
    94  			// NOTE: in CRD there is only a partial definition of metadata schema.
    95  			// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
    96  			Type: "object",
    97  		},
    98  		"spec": {
    99  			Type: "object",
   100  			Properties: map[string]apiextensionsv1.JSONSchemaProps{
   101  				// Mandatory field from the Cluster API contract
   102  				"template": {
   103  					Type: "object",
   104  					Properties: map[string]apiextensionsv1.JSONSchemaProps{
   105  						"spec": clusterSpecSchema,
   106  					},
   107  				},
   108  			},
   109  		},
   110  	})
   111  }
   112  
   113  func testInfrastructureClusterCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
   114  	return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
   115  		"metadata": {
   116  			// NOTE: in CRD there is only a partial definition of metadata schema.
   117  			// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
   118  			Type: "object",
   119  		},
   120  		"spec": clusterSpecSchema,
   121  		"status": {
   122  			Type: "object",
   123  			Properties: map[string]apiextensionsv1.JSONSchemaProps{
   124  				// mandatory field from the Cluster API contract
   125  				"ready": {Type: "boolean"},
   126  				// General purpose fields to be used in different test scenario.
   127  				"foo": {Type: "string"},
   128  				"bar": {Type: "string"},
   129  			},
   130  			Required: []string{"ready"},
   131  		},
   132  	})
   133  }
   134  
   135  func testInfrastructureMachineTemplateCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
   136  	return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
   137  		"metadata": {
   138  			// NOTE: in CRD there is only a partial definition of metadata schema.
   139  			// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
   140  			Type: "object",
   141  		},
   142  		"spec": {
   143  			Type: "object",
   144  			Properties: map[string]apiextensionsv1.JSONSchemaProps{
   145  				// Mandatory field from the Cluster API contract
   146  				"template": {
   147  					Type: "object",
   148  					Properties: map[string]apiextensionsv1.JSONSchemaProps{
   149  						"metadata": metadataSchema,
   150  						"spec":     machineSpecSchema,
   151  					},
   152  				},
   153  			},
   154  		},
   155  	})
   156  }
   157  
   158  func testInfrastructureMachinePoolTemplateCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
   159  	return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
   160  		"metadata": {
   161  			// NOTE: in CRD there is only a partial definition of metadata schema.
   162  			// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
   163  			Type: "object",
   164  		},
   165  		"spec": {
   166  			Type: "object",
   167  			Properties: map[string]apiextensionsv1.JSONSchemaProps{
   168  				// Mandatory field from the Cluster API contract
   169  				"template": {
   170  					Type: "object",
   171  					Properties: map[string]apiextensionsv1.JSONSchemaProps{
   172  						"metadata": metadataSchema,
   173  						"spec":     machinePoolSpecSchema,
   174  					},
   175  				},
   176  			},
   177  		},
   178  	})
   179  }
   180  
   181  func testInfrastructureMachineCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
   182  	return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
   183  		"metadata": {
   184  			// NOTE: in CRD there is only a partial definition of metadata schema.
   185  			// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
   186  			Type: "object",
   187  		},
   188  		"spec": machineSpecSchema,
   189  		"status": {
   190  			Type: "object",
   191  			Properties: map[string]apiextensionsv1.JSONSchemaProps{
   192  				// mandatory field from the Cluster API contract
   193  				"ready": {Type: "boolean"},
   194  				// General purpose fields to be used in different test scenario.
   195  				"foo": {Type: "string"},
   196  				"bar": {Type: "string"},
   197  			},
   198  		},
   199  	})
   200  }
   201  
   202  func testInfrastructureMachinePoolCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
   203  	return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
   204  		"metadata": {
   205  			// NOTE: in CRD there is only a partial definition of metadata schema.
   206  			// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
   207  			Type: "object",
   208  		},
   209  		"spec": machinePoolSpecSchema,
   210  		"status": {
   211  			Type: "object",
   212  			Properties: map[string]apiextensionsv1.JSONSchemaProps{
   213  				// mandatory field from the Cluster API contract
   214  				"ready": {Type: "boolean"},
   215  				// General purpose fields to be used in different test scenario.
   216  				"foo": {Type: "string"},
   217  				"bar": {Type: "string"},
   218  			},
   219  		},
   220  	})
   221  }
   222  
   223  var (
   224  	clusterSpecSchema = apiextensionsv1.JSONSchemaProps{
   225  		Type: "object",
   226  		Properties: map[string]apiextensionsv1.JSONSchemaProps{
   227  			// Mandatory field from the Cluster API contract
   228  			"controlPlaneEndpoint": {
   229  				Type: "object",
   230  				Properties: map[string]apiextensionsv1.JSONSchemaProps{
   231  					"host": {Type: "string"},
   232  					"port": {Type: "integer"},
   233  				},
   234  				Required: []string{"host", "port"},
   235  			},
   236  			// General purpose fields to be used in different test scenario.
   237  			"foo": {Type: "string"},
   238  			"bar": {Type: "string"},
   239  			"fooMap": {
   240  				Type: "object",
   241  				Properties: map[string]apiextensionsv1.JSONSchemaProps{
   242  					"foo": {Type: "string"},
   243  				},
   244  			},
   245  			"fooList": {
   246  				Type: "array",
   247  				Items: &apiextensionsv1.JSONSchemaPropsOrArray{
   248  					Schema: &apiextensionsv1.JSONSchemaProps{
   249  						Type: "object",
   250  						Properties: map[string]apiextensionsv1.JSONSchemaProps{
   251  							"foo": {Type: "string"},
   252  						},
   253  					},
   254  				},
   255  			},
   256  			// Field for testing
   257  		},
   258  	}
   259  
   260  	machineSpecSchema = apiextensionsv1.JSONSchemaProps{
   261  		Type: "object",
   262  		Properties: map[string]apiextensionsv1.JSONSchemaProps{
   263  			// Mandatory field from the Cluster API contract
   264  			"providerID": {Type: "string"},
   265  			// General purpose fields to be used in different test scenario.
   266  			"foo": {Type: "string"},
   267  			"bar": {Type: "string"},
   268  		},
   269  	}
   270  
   271  	machinePoolSpecSchema = apiextensionsv1.JSONSchemaProps{
   272  		Type: "object",
   273  		Properties: map[string]apiextensionsv1.JSONSchemaProps{
   274  			// Mandatory field from the Cluster API contract
   275  			"providerIDList": {
   276  				Type: "array",
   277  				Items: &apiextensionsv1.JSONSchemaPropsOrArray{
   278  					Schema: &apiextensionsv1.JSONSchemaProps{
   279  						Type: "string",
   280  					},
   281  				},
   282  			},
   283  			// General purpose fields to be used in different test scenario.
   284  			"foo": {Type: "string"},
   285  			"bar": {Type: "string"},
   286  		},
   287  	}
   288  )