Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed May 13, 2022
1 parent d137a60 commit af5337f
Show file tree
Hide file tree
Showing 9 changed files with 704 additions and 494 deletions.
190 changes: 190 additions & 0 deletions exp/runtime/hooks/api/v1alpha1/topologymutation_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/cluster-api/internal/runtime/catalog"
)

// GeneratePatchesRequest is the request of the GeneratePatches hook.
// +kubebuilder:object:root=true
type GeneratePatchesRequest struct {
metav1.TypeMeta `json:",inline"`

// Variables are global variables for all templates.
Variables []Variable `json:"variables"`

// Items is the list of templates to generate patches for.
Items []GeneratePatchesRequestItem `json:"items"`
}

// GeneratePatchesRequestItem represents a template to generate patches for.
type GeneratePatchesRequestItem struct {
// UID is an identifier for this template. It allows us to correlate the template in the request
// with the corresponding generates patches in the response.
UID types.UID `json:"uid"`

// HolderReference is a reference to the object where the template is used.
HolderReference HolderReference `json:"holderReference"`

// Object contains the template as a raw object.
Object runtime.RawExtension `json:"object"`

// Variables are variables specific for the current template.
// For example some builtin variables like MachineDeployment replicas and version are context-sensitive
// and thus are only added to templates for MachineDeployments and with values which correspond to the
// current MachineDeployment.
Variables []Variable `json:"variables"`
}

// GeneratePatchesResponse is the response of the GeneratePatches hook.
// NOTE: The patches in GeneratePatchesResponse will be applied in the order in which they are defined to the
// templates of the request. Thus applying changes consecutively when iterating through internal and external patches.
// +kubebuilder:object:root=true
type GeneratePatchesResponse struct {
metav1.TypeMeta `json:",inline"`

// Status of the call.
// One of: "Success" or "Failure".
Status ResponseStatus `json:"status"`

// A human-readable description of the status of the call.
Message string `json:"message,omitempty"`

// Items is the list of generated patches.
Items []GeneratePatchesResponseItem `json:"items"`
}

// GeneratePatchesResponseItem is a generated patch.
type GeneratePatchesResponseItem struct {
// UID identifies the corresponding template in the request on which
// the patch should be applied.
UID types.UID `json:"uid"`

// PatchType defines the type of the patch.
// One of: "JSONPatch" or "JSONMergePatch".
PatchType PatchType `json:"patchType"`

// Patch contains the patch which should be applied to the template.
// It must be of the corresponding PatchType.
Patch []byte `json:"patch"`
}

// PatchType defines the supported patch types.
// +enum
type PatchType string

const (
// JSONPatchType identifies a https://datatracker.ietf.org/doc/html/rfc6902 JSON patch.
JSONPatchType PatchType = "JSONPatch"

// JSONMergePatchType identifies a https://datatracker.ietf.org/doc/html/rfc7386 JSON merge patch.
JSONMergePatchType PatchType = "JSONMergePatch"
)

func GeneratePatches(*GeneratePatchesRequest, *GeneratePatchesResponse) {}

// ValidateTopologyRequest is the request of the ValidateTopology hook.
// +kubebuilder:object:root=true
type ValidateTopologyRequest struct {
metav1.TypeMeta `json:",inline"`

// Variables are global variables for all templates.
Variables []Variable `json:"variables"`

// Items is the list of templates to validate.
Items []*ValidateTopologyRequestItem `json:"items"`
}

// ValidateTopologyRequestItem represents a template to validate.
type ValidateTopologyRequestItem struct {
// HolderReference is a reference to the object where the template is used.
HolderReference HolderReference `json:"holderReference"`

// Object contains the template as a raw object.
Object runtime.RawExtension `json:"object"`

// Variables are variables specific for the current template.
// For example some builtin variables like MachineDeployment replicas and version are context-sensitive
// and thus are only added to templates for MachineDeployments and with values which correspond to the
// current MachineDeployment.
Variables []Variable `json:"variables"`
}

// ValidateTopologyResponse is the response of the ValidateTopology hook.
// +kubebuilder:object:root=true
type ValidateTopologyResponse struct {
metav1.TypeMeta `json:",inline"`

// Status of the call.
// One of: "Success" or "Failure".
Status ResponseStatus `json:"status"`

// A human-readable description of the status of the call.
Message string `json:"message"`
}

// Variable represents a variable value.
type Variable struct {
// Name of the variable.
Name string `json:"name"`

// Value of the variable.
Value apiextensionsv1.JSON `json:"value"`
}

// HolderReference represents a reference to an object which holds a template.
type HolderReference struct {
// API version of the referent.
APIVersion string `json:"apiVersion"`

// Kind of the referent.
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
Kind string `json:"kind"`

// Namespace of the referent.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
Namespace string `json:"namespace"`

// Name of the referent.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
Name string `json:"name"`

// FieldPath is the path to the field of the object which references the template.
FieldPath string `json:"fieldPath"`
}

func ValidateTopology(*GeneratePatchesRequest, *GeneratePatchesResponse) {}

func init() {
catalogBuilder.RegisterHook(GeneratePatches, &catalog.HookMeta{
Tags: []string{"Topology Mutation Hook"},
Summary: "GeneratePatches generates patches during topology reconciliation for the entire Cluster topology.",
Description: "A GeneratePatches call generates patches for the entire Cluster topology. Accordingly the request contains all templates, the global variables and the template-specific variables. The response contains generated patches.",
})

catalogBuilder.RegisterHook(ValidateTopology, &catalog.HookMeta{
Tags: []string{"Topology Mutation Hook"},
Summary: "ValidateTopology validates the Cluster topology after all patches have been applied.",
Description: "A ValidateTopology call validates the Cluster topology after all patches have been applied. The request contains all templates of the Cluster topology, the global variables and the template-specific variables. The response contains the result of the validation.",
})
}
122 changes: 4 additions & 118 deletions internal/controllers/topology/cluster/patches/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,128 +23,14 @@ package api

import (
"context"
"fmt"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
)

// Generator defines a component that can generate patches for ClusterClass templates.
type Generator interface {
// Generate generates patches for templates.
// GenerateRequest contains templates and the corresponding variables.
// GenerateResponse contains the patches which should be applied to the templates of the GenerateRequest.
Generate(ctx context.Context, request *GenerateRequest) (*GenerateResponse, error)
}

// GenerateRequest defines the input for a Generate request.
type GenerateRequest struct {
// Variables is a name/value map containing variables.
Variables map[string]apiextensionsv1.JSON

// Items contains the list of templates to generate patches for.
Items []*GenerateRequestTemplate
}

// GenerateRequestTemplate defines one of the ClusterClass templates to generate patches for.
type GenerateRequestTemplate struct {
// TemplateRef identifies a template to generate patches for;
// the same TemplateRef must be used when specifying to which template a generated patch should be applied to.
TemplateRef TemplateRef

// Variables is a name/value map containing variables specifically for the current template.
// For example some builtin variables like MachineDeployment replicas and version are context-sensitive
// and thus are only added to templates for MachineDeployments and with values which correspond to the
// current MachineDeployment.
Variables map[string]apiextensionsv1.JSON

// Template contains the template.
Template apiextensionsv1.JSON
}

// TemplateRef identifies one of the ClusterClass templates to generate patches for;
// the same TemplateRef must be used when specifying where a generated patch should apply to.
type TemplateRef struct {
// APIVersion of the current template.
APIVersion string

// Kind of the current template.
Kind string

// TemplateType defines where the template is used.
TemplateType TemplateType

// MachineDeployment specifies the MachineDeployment in which the template is used.
// This field is only set if the template is used in the context of a MachineDeployment.
MachineDeploymentRef MachineDeploymentRef
}

func (t TemplateRef) String() string {
ret := fmt.Sprintf("%s %s/%s", t.TemplateType, t.APIVersion, t.Kind)
if t.MachineDeploymentRef.TopologyName != "" {
ret = fmt.Sprintf("%s, MachineDeployment topology %s", ret, t.MachineDeploymentRef.TopologyName)
}
if t.MachineDeploymentRef.Class != "" {
ret = fmt.Sprintf("%s, MachineDeployment class %s", ret, t.MachineDeploymentRef.Class)
}
return ret
}

// MachineDeploymentRef specifies the MachineDeployment in which the template is used.
type MachineDeploymentRef struct {
// TopologyName is the name of the MachineDeploymentTopology.
TopologyName string

// Class is the name of the MachineDeploymentClass.
Class string
}

// TemplateType define the type for target types enum.
type TemplateType string

const (
// InfrastructureClusterTemplateType identifies a template for the InfrastructureCluster object.
InfrastructureClusterTemplateType TemplateType = "InfrastructureClusterTemplate"

// ControlPlaneTemplateType identifies a template for the ControlPlane object.
ControlPlaneTemplateType TemplateType = "ControlPlaneTemplate"

// ControlPlaneInfrastructureMachineTemplateType identifies a template for the InfrastructureMachines to be used for the ControlPlane object.
ControlPlaneInfrastructureMachineTemplateType TemplateType = "ControlPlane/InfrastructureMachineTemplate"

// MachineDeploymentBootstrapConfigTemplateType identifies a template for the BootstrapConfig to be used for a MachineDeployment object.
MachineDeploymentBootstrapConfigTemplateType TemplateType = "MachineDeployment/BootstrapConfigTemplate"

// MachineDeploymentInfrastructureMachineTemplateType identifies a template for the InfrastructureMachines to be used for a MachineDeployment object.
MachineDeploymentInfrastructureMachineTemplateType TemplateType = "MachineDeployment/InfrastructureMachineTemplate"
)

// PatchType define the type for patch types enum.
type PatchType string

const (
// JSONPatchType identifies a https://datatracker.ietf.org/doc/html/rfc6902 json patch.
JSONPatchType PatchType = "JSONPatch"

// JSONMergePatchType identifies a https://datatracker.ietf.org/doc/html/rfc7386 json merge patch.
JSONMergePatchType PatchType = "JSONMergePatch"
)

// GenerateResponse defines the response of a Generate request.
// NOTE: Patches defined in GenerateResponse will be applied in the same order to the original
// GenerateRequest object, thus adding changes on templates across all the subsequent Generate calls.
type GenerateResponse struct {
// Items contains the list of generated patches.
Items []GenerateResponsePatch
}

// GenerateResponsePatch defines a Patch targeting a specific GenerateRequestTemplate.
type GenerateResponsePatch struct {
// TemplateRef identifies the template the patch should apply to.
TemplateRef TemplateRef

// Patch contains the patch.
Patch apiextensionsv1.JSON

// Patch defines the type of the JSON patch.
PatchType PatchType
// GeneratePatchesRequest contains templates and the corresponding variables.
// GeneratePatchesResponse contains the patches which should be applied to the templates of the GenerateRequest.
Generate(context.Context, *runtimehooksv1.GeneratePatchesRequest) *runtimehooksv1.GeneratePatchesResponse
}
Loading

0 comments on commit af5337f

Please sign in to comment.