Skip to content

Commit ca392e5

Browse files
committed
fix: Properly set additionalTrustBundle for Nutanix CCM
The helm values template contained an error meaning that the additionalTrustBundle setting would have been templated as part of the Helm install, rather than the CAAPH addon deployment, meaning that specifying an additional trust bundle would not work for Nutanix CCM deployments. This commit fixes that and adds tests that use the same template as the CAAPH addon would do from the Helm chart to ensure that the template is now correct.
1 parent e6b4a83 commit ca392e5

File tree

2 files changed

+73
-20
lines changed

2 files changed

+73
-20
lines changed

charts/cluster-api-runtime-extensions-nutanix/templates/ccm/nutanix/manifests/helm-addon-installation.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ data:
1212
prismCentralEndPoint: {{ `{{ .PrismCentralHost }}` }}
1313
prismCentralPort: {{ `{{ .PrismCentralPort }}` }}
1414
prismCentralInsecure: {{ `{{ .PrismCentralInsecure }}` }}
15-
{{- with .PrismCentralAdditionalTrustBundle }}
16-
prismCentralAdditionalTrustBundle: {{ `{{ . }}` }}
17-
{{- end }}
15+
{{ `{{- with .PrismCentralAdditionalTrustBundle }}` }}
16+
prismCentralAdditionalTrustBundle: {{ `{{ printf "%q" . }}` }}
17+
{{ `{{- end }}` }}
1818
1919
# The Secret containing the credentials will be created by the handler.
2020
createSecret: false

pkg/handlers/generic/lifecycle/ccm/nutanix/handler_test.go

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@
44
package nutanix
55

66
import (
7+
"bytes"
78
"fmt"
9+
"os/exec"
10+
"path/filepath"
11+
"strings"
812
"testing"
13+
"text/template"
914

1015
"github.com/stretchr/testify/assert"
1116
"github.com/stretchr/testify/require"
17+
corev1 "k8s.io/api/core/v1"
18+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
19+
"sigs.k8s.io/yaml"
1220

1321
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1422
apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
1523
)
1624

1725
const (
18-
in = `---
19-
prismCentralEndPoint: {{ .PrismCentralHost }}
20-
prismCentralPort: {{ .PrismCentralPort }}
21-
prismCentralInsecure: {{ .PrismCentralInsecure }}
22-
prismCentralAdditionalTrustBundle: "{{ or .PrismCentralAdditionalTrustBundle "" }}"
23-
24-
# The Secret containing the credentials will be created by the handler.
25-
createSecret: false
26-
secretName: nutanix-ccm-credentials
27-
`
2826
//nolint:lll // just a long string
2927
testCertBundle = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVjekNDQTF1Z0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRUUZBRC4uQWtHQTFVRUJoTUNSMEl4CkV6QVJCZ05WQkFnVENsTnZiV1V0VTNSaGRHVXhGREFTQmdOVkJBb1RDMC4uMEVnVEhSa01UY3dOUVlEClZRUUxFeTVEYkdGemN5QXhJRkIxWW14cFl5QlFjbWx0WVhKNUlFTmxjbi4uWFJwYjI0Z1FYVjBhRzl5CmFYUjVNUlF3RWdZRFZRUURFd3RDWlhOMElFTkJJRXgwWkRBZUZ3MHdNRC4uVFV3TVRaYUZ3MHdNVEF5Ck1EUXhPVFV3TVRaYU1JR0hNUXN3Q1FZRFZRUUdFd0pIUWpFVE1CRUdBMS4uMjl0WlMxVGRHRjBaVEVVCk1CSUdBMVVFQ2hNTFFtVnpkQ0JEUVNCTWRHUXhOekExQmdOVkJBc1RMay4uREVnVUhWaWJHbGpJRkJ5CmFXMWhjbmtnUTJWeWRHbG1hV05oZEdsdmJpQkJkWFJvYjNKcGRIa3hGRC4uQU1UQzBKbGMzUWdRMEVnClRIUmtNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZy4uVHoybXI3U1ppQU1mUXl1CnZCak05T2lKalJhelhCWjFCalA1Q0UvV20vUnI1MDBQUksr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ZCkl0OHVuYTJnWTRsMk8vL29uODhyNUlXSmxtMUwwb0E4ZTRmUjJ5ckJIWC4uYWRzR2VGS2t5TnJ3R2kvCjd2UU1mWGRHc1JyWE5HUkduWCt2V0RaMy96V0kwam9EdENrTm5xRXBWbi4uSG9YCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0="
3028
)
@@ -39,24 +37,61 @@ prismCentralAdditionalTrustBundle: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVj
3937
4038
# The Secret containing the credentials will be created by the handler.
4139
createSecret: false
42-
secretName: nutanix-ccm-credentials
43-
`
40+
secretName: nutanix-ccm-credentials`
4441

4542
expectedWithoutAdditionalTrustBundle = `---
4643
prismCentralEndPoint: prism-central.nutanix.com
4744
prismCentralPort: 9440
4845
prismCentralInsecure: true
49-
prismCentralAdditionalTrustBundle: ""
5046
5147
# The Secret containing the credentials will be created by the handler.
5248
createSecret: false
53-
secretName: nutanix-ccm-credentials
54-
`
49+
secretName: nutanix-ccm-credentials`
50+
)
51+
52+
var templateFile = filepath.Join(
53+
moduleRootDir(),
54+
"charts",
55+
"cluster-api-runtime-extensions-nutanix",
56+
"templates",
57+
"ccm",
58+
"nutanix",
59+
"manifests",
60+
"helm-addon-installation.yaml",
5561
)
5662

5763
func Test_templateValues(t *testing.T) {
5864
t.Parallel()
5965

66+
// Mimic the Helm templating using dummy values.
67+
templateData := map[string]interface{}{}
68+
require.NoError(
69+
t,
70+
unstructured.SetNestedField(
71+
templateData,
72+
true,
73+
"Values",
74+
"hooks",
75+
"ccm",
76+
"nutanix",
77+
"helmAddonStrategy",
78+
"defaultValueTemplateConfigMap",
79+
"create",
80+
),
81+
)
82+
var templatedBytes bytes.Buffer
83+
require.NoError(
84+
t,
85+
template.Must(
86+
template.New(
87+
"helm-addon-installation.yaml").ParseFiles(templateFile),
88+
).Execute(&templatedBytes, templateData),
89+
)
90+
cm := &corev1.ConfigMap{}
91+
require.NoError(t, yaml.UnmarshalStrict(templatedBytes.Bytes(), cm))
92+
93+
valuesTemplate := cm.Data["values.yaml"]
94+
6095
tests := []struct {
6196
name string
6297
clusterConfig *apivariables.ClusterConfigSpec
@@ -88,7 +123,7 @@ func Test_templateValues(t *testing.T) {
88123
},
89124
},
90125
},
91-
in: in,
126+
in: valuesTemplate,
92127
expected: expectedWithAdditionalTrustBundle,
93128
},
94129
{
@@ -115,7 +150,7 @@ func Test_templateValues(t *testing.T) {
115150
},
116151
},
117152
},
118-
in: in,
153+
in: valuesTemplate,
119154
expected: expectedWithoutAdditionalTrustBundle,
120155
},
121156
}
@@ -129,3 +164,21 @@ func Test_templateValues(t *testing.T) {
129164
})
130165
}
131166
}
167+
168+
func moduleRootDir() string {
169+
cmd := exec.Command("go", "list", "-m", "-f", "{{ .Dir }}")
170+
out, err := cmd.CombinedOutput()
171+
if err != nil {
172+
// We include the combined output because the error is usually
173+
// an exit code, which does not explain why the command failed.
174+
panic(
175+
fmt.Sprintf("cmd.Dir=%q, cmd.Env=%q, cmd.Args=%q, err=%q, output=%q",
176+
cmd.Dir,
177+
cmd.Env,
178+
cmd.Args,
179+
err,
180+
out),
181+
)
182+
}
183+
return strings.TrimSpace(string(out))
184+
}

0 commit comments

Comments
 (0)