From 0dba5fe4a809e8bce684da77bfb4b6bd19d152d0 Mon Sep 17 00:00:00 2001 From: fabriziopandini Date: Tue, 27 Feb 2024 18:15:43 +0100 Subject: [PATCH] Make Flavor gen templates vcsim friendly --- .../flavors/clusterclass_generators.go | 18 ++++++++++++++++-- .../flavorgen/flavors/env/envsubts_consts.go | 3 ++- packaging/flavorgen/flavors/generators.go | 16 +++++++++++++--- packaging/flavorgen/flavors/util/helpers.go | 6 ++++++ ...cluster-template-external-loadbalancer.yaml | 2 +- templates/cluster-template-ignition.yaml | 2 +- templates/cluster-template-node-ipam.yaml | 2 +- templates/cluster-template-topology.yaml | 2 ++ templates/cluster-template.yaml | 2 +- templates/clusterclass-template.yaml | 8 +++++++- 10 files changed, 50 insertions(+), 11 deletions(-) diff --git a/packaging/flavorgen/flavors/clusterclass_generators.go b/packaging/flavorgen/flavors/clusterclass_generators.go index c03fe0024a..8bf828f4b5 100644 --- a/packaging/flavorgen/flavors/clusterclass_generators.go +++ b/packaging/flavorgen/flavors/clusterclass_generators.go @@ -18,6 +18,7 @@ package flavors import ( "fmt" + "strings" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -128,10 +129,13 @@ func getCredSecretNameTemplate() *string { func getControlPlaneEndpointTemplate() *string { template := map[string]interface{}{ "host": "{{ .controlPlaneIpAddr }}", - "port": 6443, + "port": "{{ .controlPlanePort }}", } templateStr, _ := yaml.Marshal(template) - return ptr.To(string(templateStr)) + + fixTemplateStr := string(templateStr) + fixTemplateStr = strings.Replace(fixTemplateStr, "'{{ .controlPlanePort }}'", "{{ .controlPlanePort }}", -1) + return ptr.To(fixTemplateStr) } func getEnableSSHIntoNodesTemplate() *string { @@ -183,6 +187,16 @@ func getClusterClassVariables() []clusterv1.ClusterClassVariable { }, }, }, + { + Name: "controlPlanePort", + Required: true, + Schema: clusterv1.VariableSchema{ + OpenAPIV3Schema: clusterv1.JSONSchemaProps{ + Type: "integer", + Description: "Port for the control plane endpoint.", + }, + }, + }, { Name: "credsSecretName", Required: true, diff --git a/packaging/flavorgen/flavors/env/envsubts_consts.go b/packaging/flavorgen/flavors/env/envsubts_consts.go index 70a1d734f4..4c0e73d1b4 100644 --- a/packaging/flavorgen/flavors/env/envsubts_consts.go +++ b/packaging/flavorgen/flavors/env/envsubts_consts.go @@ -45,7 +45,8 @@ const ( VSphereStoragePolicyVar = "${VSPHERE_STORAGE_POLICY}" VSphereTemplateVar = "${VSPHERE_TEMPLATE}" WorkerMachineCountVar = "${WORKER_MACHINE_COUNT}" - ControlPlaneEndpointVar = "${CONTROL_PLANE_ENDPOINT_IP}" + ControlPlaneEndpointHostVar = "${CONTROL_PLANE_ENDPOINT_IP}" + ControlPlaneEndpointPortVar = "${CONTROL_PLANE_ENDPOINT_PORT:=6443}" // VipNetworkInterfaceVar defaults to an empty string to let kube-vip autodetect the interface. VipNetworkInterfaceVar = "${VIP_NETWORK_INTERFACE:=\"\"}" VSphereUsername = "${VSPHERE_USERNAME}" diff --git a/packaging/flavorgen/flavors/generators.go b/packaging/flavorgen/flavors/generators.go index 12ebc0b515..d53d1f7415 100644 --- a/packaging/flavorgen/flavors/generators.go +++ b/packaging/flavorgen/flavors/generators.go @@ -139,9 +139,13 @@ func clusterTopologyVariables() ([]clusterv1.ClusterVariable, error) { if err != nil { return nil, errors.Wrapf(err, "failed to json-encode variable VSphereSSHAuthorizedKeysVar: %q", env.VSphereSSHAuthorizedKeysVar) } - controlPlaneIP, err := json.Marshal(env.ControlPlaneEndpointVar) + controlPlaneIP, err := json.Marshal(env.ControlPlaneEndpointHostVar) if err != nil { - return nil, errors.Wrapf(err, "failed to json-encode variable ControlPlaneEndpointVar: %q", env.ControlPlaneEndpointVar) + return nil, errors.Wrapf(err, "failed to json-encode variable ControlPlaneEndpointHostVar: %q", env.ControlPlaneEndpointHostVar) + } + controlPlanePort, err := json.Marshal(env.ControlPlaneEndpointPortVar) + if err != nil { + return nil, errors.Wrapf(err, "failed to json-encode variable ControlPlaneEndpointPortVar: %q", env.ControlPlaneEndpointPortVar) } secretName, err := json.Marshal(env.ClusterNameVar) if err != nil { @@ -176,6 +180,12 @@ func clusterTopologyVariables() ([]clusterv1.ClusterVariable, error) { Raw: controlPlaneIP, }, }, + { + Name: "controlPlanePort", + Value: apiextensionsv1.JSON{ + Raw: controlPlanePort, + }, + }, { Name: "credsSecretName", Value: apiextensionsv1.JSON{ @@ -215,7 +225,7 @@ func newVSphereCluster() infrav1.VSphereCluster { Kind: infrav1.SecretKind, }, ControlPlaneEndpoint: infrav1.APIEndpoint{ - Host: env.ControlPlaneEndpointVar, + Host: env.ControlPlaneEndpointHostVar, Port: 6443, }, }, diff --git a/packaging/flavorgen/flavors/util/helpers.go b/packaging/flavorgen/flavors/util/helpers.go index ba8243a312..7e983913b2 100644 --- a/packaging/flavorgen/flavors/util/helpers.go +++ b/packaging/flavorgen/flavors/util/helpers.go @@ -62,6 +62,12 @@ var ( Value: map[string]interface{}{}, FieldPath: []string{"spec", "template", "spec"}, }, + { + Kind: "VSphereCluster", + Name: "${CLUSTER_NAME}", + Value: env.ControlPlaneEndpointPortVar, + FieldPath: []string{"spec", "controlPlaneEndpoint", "port"}, + }, } stringVars = []string{ diff --git a/templates/cluster-template-external-loadbalancer.yaml b/templates/cluster-template-external-loadbalancer.yaml index 2d046c4fbe..2333f36926 100644 --- a/templates/cluster-template-external-loadbalancer.yaml +++ b/templates/cluster-template-external-loadbalancer.yaml @@ -28,7 +28,7 @@ metadata: spec: controlPlaneEndpoint: host: ${CONTROL_PLANE_ENDPOINT_IP} - port: 6443 + port: ${CONTROL_PLANE_ENDPOINT_PORT:=6443} identityRef: kind: Secret name: '${CLUSTER_NAME}' diff --git a/templates/cluster-template-ignition.yaml b/templates/cluster-template-ignition.yaml index 1eba549cb0..b18f7e99c8 100644 --- a/templates/cluster-template-ignition.yaml +++ b/templates/cluster-template-ignition.yaml @@ -28,7 +28,7 @@ metadata: spec: controlPlaneEndpoint: host: ${CONTROL_PLANE_ENDPOINT_IP} - port: 6443 + port: ${CONTROL_PLANE_ENDPOINT_PORT:=6443} identityRef: kind: Secret name: '${CLUSTER_NAME}' diff --git a/templates/cluster-template-node-ipam.yaml b/templates/cluster-template-node-ipam.yaml index 225fa3766c..6d9d1a6bd8 100644 --- a/templates/cluster-template-node-ipam.yaml +++ b/templates/cluster-template-node-ipam.yaml @@ -28,7 +28,7 @@ metadata: spec: controlPlaneEndpoint: host: ${CONTROL_PLANE_ENDPOINT_IP} - port: 6443 + port: ${CONTROL_PLANE_ENDPOINT_PORT:=6443} identityRef: kind: Secret name: '${CLUSTER_NAME}' diff --git a/templates/cluster-template-topology.yaml b/templates/cluster-template-topology.yaml index 2cc7381762..c4751f70dd 100644 --- a/templates/cluster-template-topology.yaml +++ b/templates/cluster-template-topology.yaml @@ -91,6 +91,8 @@ spec: status: {} - name: controlPlaneIpAddr value: ${CONTROL_PLANE_ENDPOINT_IP} + - name: controlPlanePort + value: ${CONTROL_PLANE_ENDPOINT_PORT:=6443} - name: credsSecretName value: '${CLUSTER_NAME}' version: '${KUBERNETES_VERSION}' diff --git a/templates/cluster-template.yaml b/templates/cluster-template.yaml index 4e5b4536a4..57bf1e0627 100644 --- a/templates/cluster-template.yaml +++ b/templates/cluster-template.yaml @@ -28,7 +28,7 @@ metadata: spec: controlPlaneEndpoint: host: ${CONTROL_PLANE_ENDPOINT_IP} - port: 6443 + port: ${CONTROL_PLANE_ENDPOINT_PORT:=6443} identityRef: kind: Secret name: '${CLUSTER_NAME}' diff --git a/templates/clusterclass-template.yaml b/templates/clusterclass-template.yaml index 3cd7b12715..25523c3674 100644 --- a/templates/clusterclass-template.yaml +++ b/templates/clusterclass-template.yaml @@ -100,7 +100,7 @@ spec: valueFrom: template: | host: '{{ .controlPlaneIpAddr }}' - port: 6443 + port: {{ .controlPlanePort }} - op: add path: /spec/template/spec/identityRef valueFrom: @@ -223,6 +223,12 @@ spec: openAPIV3Schema: description: Floating VIP for the control plane. type: string + - name: controlPlanePort + required: true + schema: + openAPIV3Schema: + description: Port for the control plane endpoint. + type: integer - name: credsSecretName required: true schema: