Skip to content

Commit

Permalink
flavorgen: enforce VSPHERE_USERNAME and VSPHERE_PASSWORD variables to…
Browse files Browse the repository at this point in the history
… be set as string in templates
  • Loading branch information
chrischdi committed Jun 3, 2024
1 parent 6e8e237 commit 7bd1736
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 105 deletions.
48 changes: 31 additions & 17 deletions packaging/flavorgen/flavors/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ package util
import (
"reflect"
"regexp"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"
"k8s.io/apimachinery/pkg/runtime/schema"
utilyaml "sigs.k8s.io/cluster-api/util/yaml"

"sigs.k8s.io/cluster-api-provider-vsphere/packaging/flavorgen/flavors/env"
)
Expand Down Expand Up @@ -85,8 +85,9 @@ var (
regexVar(env.VSphereServerVar),
regexVar(env.VSphereTemplateVar),
regexVar(env.VSphereStoragePolicyVar),
// TODO: Why was thumbprint not here?
regexVar(env.VSphereThumbprint),
regexVar(env.VSphereUsername),
regexVar(env.VSpherePassword),
}
)

Expand Down Expand Up @@ -134,19 +135,11 @@ func deleteZeroValues(o map[string]interface{}) map[string]interface{} {
}

func GenerateObjectYAML(obj runtime.Object, replacements []Replacement) string {
bytes, err := yaml.Marshal(obj)
if err != nil {
panic(err)
}
json, err := yaml.YAMLToJSONStrict(bytes)
data, err := toUnstructured(obj, obj.GetObjectKind().GroupVersionKind())
if err != nil {
panic(err)
}

data := unstructured.Unstructured{}
if err := data.UnmarshalJSON(json); err != nil {
panic(err)
}
data.Object = deleteZeroValues(data.Object)

for _, v := range replacements {
Expand All @@ -167,7 +160,8 @@ func GenerateObjectYAML(obj runtime.Object, replacements []Replacement) string {
_ = unstructured.SetNestedSlice(data.Object, slice, path...)
}
}
bytes, err = yaml.Marshal(data.Object)

bytes, err := utilyaml.FromUnstructured([]unstructured.Unstructured{*data})
if err != nil {
panic(err)
}
Expand All @@ -187,16 +181,36 @@ func GenerateObjectYAML(obj runtime.Object, replacements []Replacement) string {
}

func GenerateManifestYaml(objs []runtime.Object, replacements []Replacement) string {
var sb strings.Builder
// var sb strings.Builder

Check failure on line 184 in packaging/flavorgen/flavors/util/helpers.go

View workflow job for this annotation

GitHub Actions / lint

commentedOutCode: may want to remove commented-out code (gocritic)

Check failure on line 184 in packaging/flavorgen/flavors/util/helpers.go

View workflow job for this annotation

GitHub Actions / lint

commentedOutCode: may want to remove commented-out code (gocritic)

bytes := [][]byte{}

for _, o := range objs {
sb.WriteString("---\n")
sb.WriteString(GenerateObjectYAML(o, replacements))
bytes = append(bytes, []byte(GenerateObjectYAML(o, replacements)))
}

return sb.String()
return string(utilyaml.JoinYaml(bytes...))
}

func TypeToKind(i interface{}) string {
return reflect.ValueOf(i).Elem().Type().Name()
}

// toUnstructured converts an object to Unstructured.
// We have to pass in a gvk as we can't rely on GVK being set in a runtime.Object.
func toUnstructured(obj runtime.Object, gvk schema.GroupVersionKind) (*unstructured.Unstructured, error) {
// If the incoming object is already unstructured, perform a deep copy first
// otherwise DefaultUnstructuredConverter ends up returning the inner map without
// making a copy.
if _, ok := obj.(runtime.Unstructured); ok {
obj = obj.DeepCopyObject()
}
rawMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
u := &unstructured.Unstructured{Object: rawMap}
u.SetGroupVersionKind(gvk)

return u, nil
}
21 changes: 9 additions & 12 deletions templates/cluster-template-external-loadbalancer.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
Expand Down Expand Up @@ -222,16 +221,16 @@ metadata:
name: '${CLUSTER_NAME}'
namespace: '${NAMESPACE}'
stringData:
password: ${VSPHERE_PASSWORD}
username: ${VSPHERE_USERNAME}
password: '${VSPHERE_PASSWORD}'
username: '${VSPHERE_USERNAME}'
---
apiVersion: v1
kind: Secret
metadata:
name: vsphere-config-secret
namespace: '${NAMESPACE}'
stringData:
data: |
data: |-
apiVersion: v1
kind: Secret
metadata:
Expand All @@ -255,8 +254,7 @@ type: addons.cluster.x-k8s.io/resource-set
---
apiVersion: v1
data:
data: |
---
data: |-
apiVersion: v1
kind: Namespace
metadata:
Expand Down Expand Up @@ -1114,7 +1112,7 @@ metadata:
name: cloud-provider-vsphere-credentials
namespace: '${NAMESPACE}'
stringData:
data: |
data: |-
apiVersion: v1
kind: Secret
metadata:
Expand All @@ -1124,14 +1122,14 @@ stringData:
name: cloud-provider-vsphere-credentials
namespace: kube-system
stringData:
${VSPHERE_SERVER}.password: ${VSPHERE_PASSWORD}
${VSPHERE_SERVER}.username: ${VSPHERE_USERNAME}
${VSPHERE_SERVER}.password: '${VSPHERE_PASSWORD}'
${VSPHERE_SERVER}.username: '${VSPHERE_USERNAME}'
type: Opaque
type: addons.cluster.x-k8s.io/resource-set
---
apiVersion: v1
data:
data: |
data: |-
---
# Source: vsphere-cpi/templates/service-account.yaml
apiVersion: v1
Expand Down Expand Up @@ -1354,7 +1352,6 @@ data:
namespace: kube-system
- kind: User
name: cloud-controller-manager
---
apiVersion: v1
data:
vsphere.conf: |
Expand All @@ -1375,4 +1372,4 @@ data:
kind: ConfigMap
metadata:
name: cpi-manifests
namespace: '${NAMESPACE}'
namespace: '${NAMESPACE}'
21 changes: 9 additions & 12 deletions templates/cluster-template-ignition.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
Expand Down Expand Up @@ -436,16 +435,16 @@ metadata:
name: '${CLUSTER_NAME}'
namespace: '${NAMESPACE}'
stringData:
password: ${VSPHERE_PASSWORD}
username: ${VSPHERE_USERNAME}
password: '${VSPHERE_PASSWORD}'
username: '${VSPHERE_USERNAME}'
---
apiVersion: v1
kind: Secret
metadata:
name: vsphere-config-secret
namespace: '${NAMESPACE}'
stringData:
data: |
data: |-
apiVersion: v1
kind: Secret
metadata:
Expand All @@ -469,8 +468,7 @@ type: addons.cluster.x-k8s.io/resource-set
---
apiVersion: v1
data:
data: |
---
data: |-
apiVersion: v1
kind: Namespace
metadata:
Expand Down Expand Up @@ -1328,7 +1326,7 @@ metadata:
name: cloud-provider-vsphere-credentials
namespace: '${NAMESPACE}'
stringData:
data: |
data: |-
apiVersion: v1
kind: Secret
metadata:
Expand All @@ -1338,14 +1336,14 @@ stringData:
name: cloud-provider-vsphere-credentials
namespace: kube-system
stringData:
${VSPHERE_SERVER}.password: ${VSPHERE_PASSWORD}
${VSPHERE_SERVER}.username: ${VSPHERE_USERNAME}
${VSPHERE_SERVER}.password: '${VSPHERE_PASSWORD}'
${VSPHERE_SERVER}.username: '${VSPHERE_USERNAME}'
type: Opaque
type: addons.cluster.x-k8s.io/resource-set
---
apiVersion: v1
data:
data: |
data: |-
---
# Source: vsphere-cpi/templates/service-account.yaml
apiVersion: v1
Expand Down Expand Up @@ -1568,7 +1566,6 @@ data:
namespace: kube-system
- kind: User
name: cloud-controller-manager
---
apiVersion: v1
data:
vsphere.conf: |
Expand All @@ -1589,4 +1586,4 @@ data:
kind: ConfigMap
metadata:
name: cpi-manifests
namespace: '${NAMESPACE}'
namespace: '${NAMESPACE}'
21 changes: 9 additions & 12 deletions templates/cluster-template-node-ipam.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
Expand Down Expand Up @@ -357,16 +356,16 @@ metadata:
name: '${CLUSTER_NAME}'
namespace: '${NAMESPACE}'
stringData:
password: ${VSPHERE_PASSWORD}
username: ${VSPHERE_USERNAME}
password: '${VSPHERE_PASSWORD}'
username: '${VSPHERE_USERNAME}'
---
apiVersion: v1
kind: Secret
metadata:
name: vsphere-config-secret
namespace: '${NAMESPACE}'
stringData:
data: |
data: |-
apiVersion: v1
kind: Secret
metadata:
Expand All @@ -390,8 +389,7 @@ type: addons.cluster.x-k8s.io/resource-set
---
apiVersion: v1
data:
data: |
---
data: |-
apiVersion: v1
kind: Namespace
metadata:
Expand Down Expand Up @@ -1249,7 +1247,7 @@ metadata:
name: cloud-provider-vsphere-credentials
namespace: '${NAMESPACE}'
stringData:
data: |
data: |-
apiVersion: v1
kind: Secret
metadata:
Expand All @@ -1259,14 +1257,14 @@ stringData:
name: cloud-provider-vsphere-credentials
namespace: kube-system
stringData:
${VSPHERE_SERVER}.password: ${VSPHERE_PASSWORD}
${VSPHERE_SERVER}.username: ${VSPHERE_USERNAME}
${VSPHERE_SERVER}.password: '${VSPHERE_PASSWORD}'
${VSPHERE_SERVER}.username: '${VSPHERE_USERNAME}'
type: Opaque
type: addons.cluster.x-k8s.io/resource-set
---
apiVersion: v1
data:
data: |
data: |-
---
# Source: vsphere-cpi/templates/service-account.yaml
apiVersion: v1
Expand Down Expand Up @@ -1489,7 +1487,6 @@ data:
namespace: kube-system
- kind: User
name: cloud-controller-manager
---
apiVersion: v1
data:
vsphere.conf: |
Expand All @@ -1510,4 +1507,4 @@ data:
kind: ConfigMap
metadata:
name: cpi-manifests
namespace: '${NAMESPACE}'
namespace: '${NAMESPACE}'
Loading

0 comments on commit 7bd1736

Please sign in to comment.