|
| 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 api contains the API definition for the patch engine. |
| 18 | +// NOTE: We are introducing this API as a decoupling layer between the patch engine and the concrete components |
| 19 | +// responsible for generating patches, because we aim to provide support for external patches in a future iteration. |
| 20 | +// We also assume that this API and all the related types will be moved in a separate (versioned) package thus |
| 21 | +// providing a versioned contract between Cluster API and the components implementing external patch extensions. |
| 22 | +package api |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + |
| 27 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 28 | +) |
| 29 | + |
| 30 | +// Generator defines a component that can generate patches for ClusterClass templates. |
| 31 | +type Generator interface { |
| 32 | + // Generate generates patches for templates. |
| 33 | + // GenerateRequest contains templates and the corresponding variables. |
| 34 | + // GenerateResponse contains the patches which should be applied to the templates of the GenerateRequest. |
| 35 | + Generate(ctx context.Context, request *GenerateRequest) (*GenerateResponse, error) |
| 36 | +} |
| 37 | + |
| 38 | +// GenerateRequest defines the input for a Generate request. |
| 39 | +type GenerateRequest struct { |
| 40 | + // Variables is a name/value map containing variables. |
| 41 | + Variables map[string]apiextensionsv1.JSON |
| 42 | + |
| 43 | + // Items contains the list of templates to generate patches for. |
| 44 | + Items []*GenerateRequestTemplate |
| 45 | +} |
| 46 | + |
| 47 | +// GenerateRequestTemplate defines one of the ClusterClass templates to generate patches for. |
| 48 | +type GenerateRequestTemplate struct { |
| 49 | + // TemplateRef identifies a template to generate patches for; |
| 50 | + // the same TemplateRef must be used when specifying to which template a generated patch should be applied to. |
| 51 | + TemplateRef TemplateRef |
| 52 | + |
| 53 | + // Variables is a name/value map containing variables specifically for the current template. |
| 54 | + // For example some builtin variables like MachineDeployment replicas and version are context-sensitive |
| 55 | + // and thus are only added to templates for MachineDeployments and with values which correspond to the |
| 56 | + // current MachineDeployment. |
| 57 | + Variables map[string]apiextensionsv1.JSON |
| 58 | + |
| 59 | + // Template contains the template. |
| 60 | + Template apiextensionsv1.JSON |
| 61 | +} |
| 62 | + |
| 63 | +// TemplateRef identifies one of the ClusterClass templates to generate patches for; |
| 64 | +// the same TemplateRef must be used when specifying where a generated patch should apply to. |
| 65 | +type TemplateRef struct { |
| 66 | + // APIVersion of the current template. |
| 67 | + APIVersion string |
| 68 | + |
| 69 | + // Kind of the current template. |
| 70 | + Kind string |
| 71 | + |
| 72 | + // TemplateType defines where the template is used. |
| 73 | + TemplateType TemplateType |
| 74 | + |
| 75 | + // MachineDeployment specifies the MachineDeployment in which the template is used. |
| 76 | + // This field is only set if the template is used in the context of a MachineDeployment. |
| 77 | + MachineDeploymentRef MachineDeploymentRef |
| 78 | +} |
| 79 | + |
| 80 | +// MachineDeploymentRef specifies the MachineDeployment in which the template is used. |
| 81 | +type MachineDeploymentRef struct { |
| 82 | + // TopologyName is the name of the MachineDeploymentTopology. |
| 83 | + TopologyName string |
| 84 | + |
| 85 | + // Class is the name of the MachineDeploymentClass. |
| 86 | + Class string |
| 87 | +} |
| 88 | + |
| 89 | +// TemplateType define the type for target types enum. |
| 90 | +type TemplateType string |
| 91 | + |
| 92 | +const ( |
| 93 | + // InfrastructureClusterTemplateType identifies a template for the InfrastructureCluster object. |
| 94 | + InfrastructureClusterTemplateType TemplateType = "InfrastructureClusterTemplate" |
| 95 | + |
| 96 | + // ControlPlaneTemplateType identifies a template for the ControlPlane object. |
| 97 | + ControlPlaneTemplateType TemplateType = "ControlPlaneTemplate" |
| 98 | + |
| 99 | + // ControlPlaneInfrastructureMachineTemplateType identifies a template for the InfrastructureMachines to be used for the ControlPlane object. |
| 100 | + ControlPlaneInfrastructureMachineTemplateType TemplateType = "ControlPlane/InfrastructureMachineTemplate" |
| 101 | + |
| 102 | + // MachineDeploymentBootstrapConfigTemplateType identifies a template for the BootstrapConfig to be used for a MachineDeployment object. |
| 103 | + MachineDeploymentBootstrapConfigTemplateType TemplateType = "MachineDeployment/BootstrapConfigTemplate" |
| 104 | + |
| 105 | + // MachineDeploymentInfrastructureMachineTemplateType identifies a template for the InfrastructureMachines to be used for a MachineDeployment object. |
| 106 | + MachineDeploymentInfrastructureMachineTemplateType TemplateType = "MachineDeployment/InfrastructureMachineTemplate" |
| 107 | +) |
| 108 | + |
| 109 | +// PatchType define the type for patch types enum. |
| 110 | +type PatchType string |
| 111 | + |
| 112 | +const ( |
| 113 | + // JSONPatchType identifies a https://datatracker.ietf.org/doc/html/rfc6902 json patch. |
| 114 | + JSONPatchType PatchType = "JSONPatch" |
| 115 | + |
| 116 | + // JSONMergePatchType identifies a https://datatracker.ietf.org/doc/html/rfc7386 json merge patch. |
| 117 | + JSONMergePatchType PatchType = "JSONMergePatch" |
| 118 | +) |
| 119 | + |
| 120 | +// GenerateResponse defines the response of a Generate request. |
| 121 | +// NOTE: Patches defined in GenerateResponse will be applied in the same order to the original |
| 122 | +// GenerateRequest object, thus adding changes on templates across all the subsequent Generate calls. |
| 123 | +type GenerateResponse struct { |
| 124 | + // Items contains the list of generated patches. |
| 125 | + Items []GenerateResponsePatch |
| 126 | +} |
| 127 | + |
| 128 | +// GenerateResponsePatch defines a Patch targeting a specific GenerateRequestTemplate. |
| 129 | +type GenerateResponsePatch struct { |
| 130 | + // TemplateRef identifies the template the patch should apply to. |
| 131 | + TemplateRef TemplateRef |
| 132 | + |
| 133 | + // Patch contains the patch. |
| 134 | + Patch apiextensionsv1.JSON |
| 135 | + |
| 136 | + // Patch defines the type of the JSON patch. |
| 137 | + PatchType PatchType |
| 138 | +} |
0 commit comments