|
| 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 | +//nolint:lll // Code is copied from upstream. |
| 18 | +package builder |
| 19 | + |
| 20 | +import ( |
| 21 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 22 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + // BootstrapGroupVersion is group version used for bootstrap objects. |
| 27 | + BootstrapGroupVersion = schema.GroupVersion{ |
| 28 | + Group: "bootstrap.cluster.x-k8s.io", |
| 29 | + Version: "v1beta1", |
| 30 | + } |
| 31 | + |
| 32 | + // GenericBootstrapConfigKind is the Kind for the GenericBootstrapConfig. |
| 33 | + GenericBootstrapConfigKind = "GenericBootstrapConfig" |
| 34 | + // GenericBootstrapConfigCRD is a generic bootstrap CRD. |
| 35 | + GenericBootstrapConfigCRD = untypedCRD( |
| 36 | + BootstrapGroupVersion.WithKind(GenericBootstrapConfigKind), |
| 37 | + ) |
| 38 | + |
| 39 | + // GenericBootstrapConfigTemplateKind is the Kind for the GenericBootstrapConfigTemplate. |
| 40 | + GenericBootstrapConfigTemplateKind = "GenericBootstrapConfigTemplate" |
| 41 | + // GenericBootstrapConfigTemplateCRD is a generic bootstrap template CRD. |
| 42 | + GenericBootstrapConfigTemplateCRD = untypedCRD( |
| 43 | + BootstrapGroupVersion.WithKind(GenericBootstrapConfigTemplateKind), |
| 44 | + ) |
| 45 | + |
| 46 | + // TODO: drop generic CRDs in favour of typed test CRDs. |
| 47 | + |
| 48 | + // TestBootstrapConfigTemplateKind is the kind for the TestBootstrapConfigTemplate type. |
| 49 | + TestBootstrapConfigTemplateKind = "TestBootstrapConfigTemplate" |
| 50 | + // TestBootstrapConfigTemplateCRD is a test bootstrap config template CRD. |
| 51 | + TestBootstrapConfigTemplateCRD = testBootstrapConfigTemplateCRD( |
| 52 | + BootstrapGroupVersion.WithKind(TestBootstrapConfigTemplateKind), |
| 53 | + ) |
| 54 | + |
| 55 | + // TestBootstrapConfigKind is the kind for the TestBootstrapConfig type. |
| 56 | + TestBootstrapConfigKind = "TestBootstrapConfig" |
| 57 | + // TestBootstrapConfigCRD is a test bootstrap config CRD. |
| 58 | + TestBootstrapConfigCRD = testBootstrapConfigCRD( |
| 59 | + BootstrapGroupVersion.WithKind(TestBootstrapConfigKind), |
| 60 | + ) |
| 61 | +) |
| 62 | + |
| 63 | +func testBootstrapConfigTemplateCRD( |
| 64 | + gvk schema.GroupVersionKind, |
| 65 | +) *apiextensionsv1.CustomResourceDefinition { |
| 66 | + return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{ |
| 67 | + "metadata": { |
| 68 | + // NOTE: in CRD there is only a partial definition of metadata schema. |
| 69 | + // Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185 |
| 70 | + Type: "object", |
| 71 | + }, |
| 72 | + "spec": { |
| 73 | + Type: "object", |
| 74 | + Properties: map[string]apiextensionsv1.JSONSchemaProps{ |
| 75 | + // Mandatory field from the Cluster API contract |
| 76 | + "template": { |
| 77 | + Type: "object", |
| 78 | + Properties: map[string]apiextensionsv1.JSONSchemaProps{ |
| 79 | + "spec": bootstrapConfigSpecSchema, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +func testBootstrapConfigCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition { |
| 88 | + return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{ |
| 89 | + "metadata": { |
| 90 | + // NOTE: in CRD there is only a partial definition of metadata schema. |
| 91 | + // Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185 |
| 92 | + Type: "object", |
| 93 | + }, |
| 94 | + "spec": bootstrapConfigSpecSchema, |
| 95 | + "status": { |
| 96 | + Type: "object", |
| 97 | + Properties: map[string]apiextensionsv1.JSONSchemaProps{ |
| 98 | + // mandatory field from the Cluster API contract |
| 99 | + "ready": {Type: "boolean"}, |
| 100 | + "dataSecretName": {Type: "string"}, |
| 101 | + // General purpose fields to be used in different test scenario. |
| 102 | + "foo": {Type: "string"}, |
| 103 | + "bar": {Type: "string"}, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +var bootstrapConfigSpecSchema = apiextensionsv1.JSONSchemaProps{ |
| 110 | + Type: "object", |
| 111 | + Properties: map[string]apiextensionsv1.JSONSchemaProps{ |
| 112 | + // General purpose fields to be used in different test scenario. |
| 113 | + "foo": {Type: "string"}, |
| 114 | + "bar": {Type: "string"}, |
| 115 | + }, |
| 116 | +} |
0 commit comments