Skip to content

Commit 0e920d2

Browse files
committed
fix: handle runtime.Rawextension properly
1 parent 9d3d842 commit 0e920d2

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

bootstrap/eks/internal/userdata/nodeadm.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ spec:
9797
{{- end }}
9898
kubelet:
9999
{{- if .KubeletConfig }}
100-
config: |
101-
{{ Indent 6 .KubeletConfig }}
100+
config:
101+
{{ Indent 6 (toYaml .KubeletConfig) }}
102102
{{- end }}
103103
flags:
104104
{{- range $flag := .KubeletFlags }}
@@ -107,12 +107,12 @@ spec:
107107
{{- if or .ContainerdConfig .ContainerdBaseRuntimeSpec }}
108108
containerd:
109109
{{- if .ContainerdConfig }}
110-
config: |
110+
config:
111111
{{ Indent 6 .ContainerdConfig }}
112112
{{- end }}
113113
{{- if .ContainerdBaseRuntimeSpec }}
114-
baseRuntimeSpec: |
115-
{{ Indent 6 .ContainerdBaseRuntimeSpec}}
114+
baseRuntimeSpec:
115+
{{ Indent 6 (toYaml .ContainerdBaseRuntimeSpec) }}
116116
{{- end }}
117117
{{- end }}
118118
{{- if .Instance }}
@@ -238,7 +238,6 @@ func validateNodeadmInput(input *NodeadmInput) error {
238238
if input.NodeGroupName == "" {
239239
return fmt.Errorf("node group name is required for nodeadm")
240240
}
241-
242241
if input.Boundary == "" {
243242
input.Boundary = boundary
244243
}

bootstrap/eks/internal/userdata/nodeadm_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
. "github.com/onsi/gomega"
2525
"github.com/onsi/gomega/format"
26+
"k8s.io/apimachinery/pkg/runtime"
2627
"k8s.io/utils/ptr"
2728

2829
eksbootstrapv1 "sigs.k8s.io/cluster-api-provider-aws/v2/bootstrap/eks/api/v1beta2"
@@ -168,11 +169,12 @@ func TestNodeadmUserdata(t *testing.T) {
168169
APIServerEndpoint: "https://example.com",
169170
CACert: "test-ca-cert",
170171
NodeGroupName: "test-nodegroup",
171-
KubeletConfig: `
172+
KubeletConfig: &runtime.RawExtension{
173+
Raw: []byte(`
172174
evictionHard:
173175
memory.available: "2000Mi"
174-
175-
`,
176+
`),
177+
},
176178
},
177179
},
178180
expectErr: false,

bootstrap/eks/internal/userdata/utils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ limitations under the License.
1717
package userdata
1818

1919
import (
20+
"fmt"
2021
"strings"
2122
"text/template"
23+
24+
"github.com/pkg/errors"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
"sigs.k8s.io/yaml"
2227
)
2328

2429
var (
2530
defaultTemplateFuncMap = template.FuncMap{
2631
"Indent": templateYAMLIndent,
32+
"toYaml": templateToYAML,
2733
}
2834
)
2935

@@ -32,3 +38,28 @@ func templateYAMLIndent(i int, input string) string {
3238
ident := "\n" + strings.Repeat(" ", i)
3339
return strings.Repeat(" ", i) + strings.Join(split, ident)
3440
}
41+
42+
func templateToYAML(r *runtime.RawExtension) (string, error) {
43+
if r == nil {
44+
return "", nil
45+
}
46+
if r.Object != nil {
47+
b, err := yaml.Marshal(r.Object)
48+
if err != nil {
49+
return "", errors.Wrap(err, "failed to convert to yaml")
50+
}
51+
return string(b), nil
52+
}
53+
if len(r.Raw) > 0 {
54+
if yb, err := yaml.JSONToYAML(r.Raw); err == nil {
55+
return string(yb), nil
56+
}
57+
var temp interface{}
58+
err := yaml.Unmarshal(r.Raw, &temp)
59+
if err == nil {
60+
return string(r.Raw), nil
61+
}
62+
return "", fmt.Errorf("runtime object raw is neither json nor yaml %s", string(r.Raw))
63+
}
64+
return "", nil
65+
}

0 commit comments

Comments
 (0)