Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Jun 21, 2024
1 parent 683c35b commit d2aed18
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
- op: remove
path: /spec/patches/0
- op: remove
path: /spec/patches/0
- op: remove
path: /spec/patches/0
- op: remove
path: /spec/patches/0
- op: add
path: /spec/patches/0
- op: replace
path: /spec/patches
value:
- name: test-patch
external:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
- op: remove
path: /spec/patches/0
- op: remove
path: /spec/patches/0
- op: remove
path: /spec/patches/0
- op: remove
path: /spec/patches/0
- op: add
path: /spec/patches/0
- op: replace
path: /spec/patches
value:
- name: test-patch
external:
Expand Down
107 changes: 94 additions & 13 deletions test/infrastructure/test-extension/topologymutation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ func (h *ExtensionHandlers) GeneratePatches(ctx context.Context, req *runtimehoo
// By using WalkTemplates it is possible to implement patches using typed API objects, which makes code
// easier to read and less error prone than using unstructured or working with raw json/yaml.
// IMPORTANT: by unit testing this func/nested func properly, it is possible to prevent unexpected rollouts when patches are modified.
topologymutation.WalkTemplates(ctx, h.decoder, req, resp, func(ctx context.Context, obj runtime.Object, variables map[string]apiextensionsv1.JSON, _ runtimehooksv1.HolderReference) error {
topologymutation.WalkTemplates(ctx, h.decoder, req, resp, func(ctx context.Context, obj runtime.Object, variables map[string]apiextensionsv1.JSON, holderRef runtimehooksv1.HolderReference) error {
log := ctrl.LoggerFrom(ctx)

isControlPlane := holderRef.Kind == "KubeadmControlPlane"

switch obj := obj.(type) {
case *controlplanev1.KubeadmControlPlaneTemplate:
if err := patchKubeadmControlPlaneTemplate(ctx, obj, variables); err != nil {
Expand All @@ -98,7 +100,7 @@ func (h *ExtensionHandlers) GeneratePatches(ctx context.Context, req *runtimehoo
return errors.Wrap(err, "error patching VSphereClusterTemplate")
}
case *infrav1.VSphereMachineTemplate:
if err := patchGovmomiMachineTemplate(ctx, obj, variables); err != nil {
if err := patchGovmomiMachineTemplate(ctx, obj, variables, isControlPlane); err != nil {
log.Error(err, "Error patching VSphereMachineTemplate")
return errors.Wrap(err, "error patching VSphereMachineTemplate")
}
Expand All @@ -108,7 +110,7 @@ func (h *ExtensionHandlers) GeneratePatches(ctx context.Context, req *runtimehoo
return errors.Wrap(err, "error patching VSphereClusterTemplate")
}
case *vmwarev1.VSphereMachineTemplate:
if err := patchSupervisorMachineTemplate(ctx, obj, variables); err != nil {
if err := patchSupervisorMachineTemplate(ctx, obj, variables, isControlPlane); err != nil {
log.Error(err, "Error patching VSphereMachineTemplate")
return errors.Wrap(err, "error patching VSphereMachineTemplate")
}
Expand Down Expand Up @@ -137,7 +139,7 @@ func patchKubeadmControlPlaneTemplate(_ context.Context, tpl *controlplanev1.Kub
if err != nil {
return err
}
kubeVipPodManifestModified := regexp.MustCompile("(name: address\n +value:).*").ReplaceAll([]byte(kubeVipPodManifest), []byte(fmt.Sprintf("$1 %s", controlPlaneIPAddr)))
kubeVipPodManifestModified := regexp.MustCompile("(name: address\n +value:).*").ReplaceAllString(kubeVipPodManifest, fmt.Sprintf("$1 %s", controlPlaneIPAddr))

for _, file := range kubevip.NewKubeVIPFiles() {
if file.Path == "/etc/kubernetes/manifests/kube-vip.yaml" {
Expand All @@ -146,17 +148,56 @@ func patchKubeadmControlPlaneTemplate(_ context.Context, tpl *controlplanev1.Kub
tpl.Spec.Template.Spec.KubeadmConfigSpec.Files = append(tpl.Spec.Template.Spec.KubeadmConfigSpec.Files, file)
}

// patch preKubeadmScript
preKubeadmScript, err := topologymutation.GetStringVariable(templateVariables, "preKubeadmScript")
if err != nil {
return err
}
version, err := topologymutation.GetStringVariable(templateVariables, "builtin.controlPlane.version")
if err != nil {
return err
}

versionRegex := regexp.MustCompile("(KUBERNETES_VERSION=.*)")
tpl.Spec.Template.Spec.KubeadmConfigSpec.Files = append(tpl.Spec.Template.Spec.KubeadmConfigSpec.Files,
bootstrapv1.File{
Owner: "root:root",
Path: "/etc/pre-kubeadm-commands/10-prekubeadmscript.sh",
Permissions: "0755",
Content: versionRegex.ReplaceAllString(preKubeadmScript, fmt.Sprintf("KUBERNETES_VERSION=%s", version)),
},
)

return nil
}

// KubeadmConfigTemplate patches the KubeadmConfigTemplate.
func patchKubeadmConfigTemplate(_ context.Context, tpl *bootstrapv1.KubeadmConfigTemplate, templateVariables map[string]apiextensionsv1.JSON) error {
// patch enableSSHIntoNodes
//nolint:revive
if err := patchUsers(&tpl.Spec.Template.Spec, templateVariables); err != nil {
return err
}

// patch preKubeadmScript
preKubeadmScript, err := topologymutation.GetStringVariable(templateVariables, "preKubeadmScript")
if err != nil {
return err
}
version, err := topologymutation.GetStringVariable(templateVariables, "builtin.machineDeployment.version")
if err != nil {
return err
}

versionRegex := regexp.MustCompile("(KUBERNETES_VERSION=.*)")
tpl.Spec.Template.Spec.Files = append(tpl.Spec.Template.Spec.Files,
bootstrapv1.File{
Owner: "root:root",
Path: "/etc/pre-kubeadm-commands/10-prekubeadmscript.sh",
Permissions: "0755",
Content: versionRegex.ReplaceAllString(preKubeadmScript, fmt.Sprintf("KUBERNETES_VERSION=%s", version)),
},
)

return nil
}

Expand Down Expand Up @@ -224,23 +265,63 @@ func patchGovmomiClusterTemplate(_ context.Context, vsphereCluster *infrav1.VSph

// patchSupervisorClusterTemplate patches the supervisor VSphereClusterTemplate.
// NOTE: this patch is not required for any special reason, it is used for testing the patch machinery itself.
func patchSupervisorClusterTemplate(_ context.Context, _ *vmwarev1.VSphereClusterTemplate, _ map[string]apiextensionsv1.JSON) error {
// TODO(chrischdi): implement some patch
func patchSupervisorClusterTemplate(_ context.Context, vsphereCluster *vmwarev1.VSphereClusterTemplate, templateVariables map[string]apiextensionsv1.JSON) error {
// patch infraClusterSubstitutions
controlPlaneIPAddr, err := topologymutation.GetStringVariable(templateVariables, "controlPlaneIpAddr")
if err != nil {
return err
}
var controlPlanePort int32
if err := topologymutation.GetObjectVariableInto(templateVariables, "controlPlanePort", &controlPlanePort); err != nil {
return err
}

vsphereCluster.Spec.Template.Spec.ControlPlaneEndpoint.Host = controlPlaneIPAddr
vsphereCluster.Spec.Template.Spec.ControlPlaneEndpoint.Port = controlPlanePort

return nil
}

// patchGovmomiMachineTemplate patches the govmomi VSphereMachineTemplate.
// NOTE: this patch is not required for any special reason, it is used for testing the patch machinery itself.
func patchGovmomiMachineTemplate(_ context.Context, _ *infrav1.VSphereMachineTemplate, _ map[string]apiextensionsv1.JSON) error {
// TODO(chrischdi): implement some patch
return nil
func patchGovmomiMachineTemplate(_ context.Context, vsphereMachineTemplate *infrav1.VSphereMachineTemplate, templateVariables map[string]apiextensionsv1.JSON, isControlPlane bool) error {
// patch vSphereTemplate

var err error
vsphereMachineTemplate.Spec.Template.Spec.Template, err = calculateImageName(templateVariables, isControlPlane)

return err
}

// patchSupervisorMachineTemplate patches the supervisor VSphereMachineTemplate.
// NOTE: this patch is not required for any special reason, it is used for testing the patch machinery itself.
func patchSupervisorMachineTemplate(_ context.Context, _ *vmwarev1.VSphereMachineTemplate, _ map[string]apiextensionsv1.JSON) error {
// TODO(chrischdi): implement some patch
return nil
func patchSupervisorMachineTemplate(_ context.Context, vsphereMachineTemplate *vmwarev1.VSphereMachineTemplate, templateVariables map[string]apiextensionsv1.JSON, isControlPlane bool) error {
// patch vSphereTemplate

var err error
vsphereMachineTemplate.Spec.Template.Spec.ImageName, err = calculateImageName(templateVariables, isControlPlane)

return err
}

func calculateImageName(templateVariables map[string]apiextensionsv1.JSON, isControlPlane bool) (string, error) {
// patch vSphereTemplate
versionVariable := "builtin.controlPlane.version"
if !isControlPlane {
versionVariable = "builtin.machineDeployment.version"
}

version, err := topologymutation.GetStringVariable(templateVariables, versionVariable)
if err != nil {
return "", err
}

// Fallback to the v1.30.0 image, except for v1.28.0 and v1.29.0.
if version != "v1.28.0" && version != "v1.29.0" {
version = "v1.30.0"
}

return fmt.Sprintf("ubuntu-2204-kube-%s", version), nil
}

// ValidateTopology implements the HandlerFunc for the ValidateTopology hook.
Expand Down

0 comments on commit d2aed18

Please sign in to comment.