From fb917907e25f1f7c80efe396bd398470f6798956 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Thu, 6 Jan 2022 15:54:57 -0500 Subject: [PATCH 1/2] add templateOverride field Signed-off-by: Abhinav Pandey --- api/v1beta1/tinkerbellmachine_types.go | 4 ++ ...e.cluster.x-k8s.io_tinkerbellmachines.yaml | 4 ++ ...r.x-k8s.io_tinkerbellmachinetemplates.yaml | 4 ++ controllers/machine.go | 51 ++++++++++--------- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/api/v1beta1/tinkerbellmachine_types.go b/api/v1beta1/tinkerbellmachine_types.go index d8958c25..1f8b5eb9 100644 --- a/api/v1beta1/tinkerbellmachine_types.go +++ b/api/v1beta1/tinkerbellmachine_types.go @@ -60,6 +60,10 @@ type TinkerbellMachineSpec struct { // +optional ImageLookupOSVersion string `json:"imageLookupOSVersion,omitempty"` + // TemplateOverride overrides the default CAPT hardware template + // +optional + TemplateOverride string `json:"templateOverride,omitempty"` + // Those fields are set programmatically, but they cannot be re-constructed from "state of the world", so // we put them in spec instead of status. HardwareName string `json:"hardwareName,omitempty"` diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachines.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachines.yaml index fcef9bb3..e76e4f65 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachines.yaml @@ -97,6 +97,10 @@ spec: type: string providerID: type: string + templateOverride: + description: TemplateOverride overrides the default CAPT hardware + template + type: string type: object status: description: TinkerbellMachineStatus defines the observed state of TinkerbellMachine. diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachinetemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachinetemplates.yaml index 3b2bc78f..ce0ddd93 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachinetemplates.yaml @@ -88,6 +88,10 @@ spec: type: string providerID: type: string + templateOverride: + description: TemplateOverride overrides the default CAPT hardware + template + type: string type: object required: - spec diff --git a/controllers/machine.go b/controllers/machine.go index a091c8ea..79a1c910 100644 --- a/controllers/machine.go +++ b/controllers/machine.go @@ -169,36 +169,39 @@ func (mrc *machineReconcileContext) imageURL() (string, error) { } func (mrc *machineReconcileContext) createTemplate(hardware *tinkv1.Hardware) error { - if len(hardware.Status.Disks) < 1 { - return ErrHardwareMissingDiskConfiguration - } + templateData := mrc.tinkerbellMachine.Spec.TemplateOverride + if templateData == "" { + if len(hardware.Status.Disks) < 1 { + return ErrHardwareMissingDiskConfiguration + } - targetDisk := hardware.Status.Disks[0].Device - targetDevice := firstPartitionFromDevice(targetDisk) + targetDisk := hardware.Status.Disks[0].Device + targetDevice := firstPartitionFromDevice(targetDisk) - imageURL, err := mrc.imageURL() - if err != nil { - return fmt.Errorf("failed to generate imageURL: %w", err) - } + imageURL, err := mrc.imageURL() + if err != nil { + return fmt.Errorf("failed to generate imageURL: %w", err) + } - metadataIP := os.Getenv("TINKERBELL_IP") - if metadataIP == "" { - metadataIP = "192.168.1.1" - } + metadataIP := os.Getenv("TINKERBELL_IP") + if metadataIP == "" { + metadataIP = "192.168.1.1" + } - metadataURL := fmt.Sprintf("http://%s:50061", metadataIP) + metadataURL := fmt.Sprintf("http://%s:50061", metadataIP) - workflowTemplate := templates.WorkflowTemplate{ - Name: mrc.tinkerbellMachine.Name, - MetadataURL: metadataURL, - ImageURL: imageURL, - DestDisk: targetDisk, - DestPartition: targetDevice, - } + workflowTemplate := templates.WorkflowTemplate{ + Name: mrc.tinkerbellMachine.Name, + MetadataURL: metadataURL, + ImageURL: imageURL, + DestDisk: targetDisk, + DestPartition: targetDevice, + } - templateData, err := workflowTemplate.Render() - if err != nil { - return fmt.Errorf("rendering template: %w", err) + templateData, err = workflowTemplate.Render() + if err != nil { + return fmt.Errorf("rendering template: %w", err) + } } templateObject := &tinkv1.Template{ From 753d39d527b24c90c3606155fc21639fcf5ee063 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Tue, 1 Feb 2022 09:37:19 -0800 Subject: [PATCH 2/2] Address comments Signed-off-by: Abhinav Pandey --- api/v1beta1/tinkerbellmachine_types.go | 3 ++- ...nfrastructure.cluster.x-k8s.io_tinkerbellmachines.yaml | 5 +++-- ...cture.cluster.x-k8s.io_tinkerbellmachinetemplates.yaml | 5 +++-- controllers/machine.go | 8 ++++---- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/api/v1beta1/tinkerbellmachine_types.go b/api/v1beta1/tinkerbellmachine_types.go index 1f8b5eb9..ab4b8491 100644 --- a/api/v1beta1/tinkerbellmachine_types.go +++ b/api/v1beta1/tinkerbellmachine_types.go @@ -60,7 +60,8 @@ type TinkerbellMachineSpec struct { // +optional ImageLookupOSVersion string `json:"imageLookupOSVersion,omitempty"` - // TemplateOverride overrides the default CAPT hardware template + // TemplateOverride overrides the default Tinkerbell template used by CAPT. + // You can learn more about Tinkerbell templates here: https://docs.tinkerbell.org/templates/ // +optional TemplateOverride string `json:"templateOverride,omitempty"` diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachines.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachines.yaml index e76e4f65..0e75f0b4 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachines.yaml @@ -98,8 +98,9 @@ spec: providerID: type: string templateOverride: - description: TemplateOverride overrides the default CAPT hardware - template + description: 'TemplateOverride overrides the default Tinkerbell template + used by CAPT. You can learn more about Tinkerbell templates here: + https://docs.tinkerbell.org/templates/' type: string type: object status: diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachinetemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachinetemplates.yaml index ce0ddd93..2a75843d 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_tinkerbellmachinetemplates.yaml @@ -89,8 +89,9 @@ spec: providerID: type: string templateOverride: - description: TemplateOverride overrides the default CAPT hardware - template + description: 'TemplateOverride overrides the default Tinkerbell + template used by CAPT. You can learn more about Tinkerbell + templates here: https://docs.tinkerbell.org/templates/' type: string type: object required: diff --git a/controllers/machine.go b/controllers/machine.go index 79a1c910..7f1eae4e 100644 --- a/controllers/machine.go +++ b/controllers/machine.go @@ -169,12 +169,12 @@ func (mrc *machineReconcileContext) imageURL() (string, error) { } func (mrc *machineReconcileContext) createTemplate(hardware *tinkv1.Hardware) error { + if len(hardware.Status.Disks) < 1 { + return ErrHardwareMissingDiskConfiguration + } + templateData := mrc.tinkerbellMachine.Spec.TemplateOverride if templateData == "" { - if len(hardware.Status.Disks) < 1 { - return ErrHardwareMissingDiskConfiguration - } - targetDisk := hardware.Status.Disks[0].Device targetDevice := firstPartitionFromDevice(targetDisk)