Skip to content

Commit 578f8d3

Browse files
clusterctl small refactor of fixNamespace
1 parent c246f9b commit 578f8d3

File tree

3 files changed

+98
-55
lines changed

3 files changed

+98
-55
lines changed

cmd/clusterctl/pkg/client/repository/components.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/config"
3232
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/scheme"
3333
"sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/util"
34-
"sigs.k8s.io/yaml"
3534
)
3635

3736
// variableRegEx defines the regexp used for searching variables inside a YAML
@@ -129,16 +128,7 @@ func (c *components) Objs() []unstructured.Unstructured {
129128
}
130129

131130
func (c *components) Yaml() ([]byte, error) {
132-
var ret [][]byte //nolint
133-
for _, o := range c.objs {
134-
content, err := yaml.Marshal(o.UnstructuredContent())
135-
if err != nil {
136-
return nil, errors.Wrapf(err, "failed to marshal yaml for %s/%s", o.GetNamespace(), o.GetName())
137-
}
138-
ret = append(ret, content)
139-
}
140-
141-
return util.JoinYaml(ret...), nil
131+
return util.FromUnstructured(c.objs)
142132
}
143133

144134
// newComponents returns a new objects embedding a component YAML file
@@ -186,6 +176,10 @@ func newComponents(provider config.Provider, version string, rawyaml []byte, con
186176
return nil, errors.New("target namespace can't be defaulted. Please specify a target namespace")
187177
}
188178

179+
// add a Namespace object if missing (ensure the targetNamespace will be created)
180+
objs = addNamespaceIfMissing(objs, targetNamespace)
181+
182+
// fix Namespace name in all the objects
189183
objs = fixTargetNamespace(objs, targetNamespace)
190184

191185
// ensure all the ClusterRoleBinding which are referencing namespaced objects have the name prefixed with the namespace name
@@ -279,21 +273,13 @@ func inspectTargetNamespace(objs []unstructured.Unstructured) (string, error) {
279273
return namespace, nil
280274
}
281275

282-
// fixTargetNamespace ensures all the provider components are deployed in the target namespace (apply only to namespaced objects).
283-
// The func tales case of fixing the namespace object, if any.
284-
func fixTargetNamespace(objs []unstructured.Unstructured, targetNamespace string) []unstructured.Unstructured {
276+
// addNamespaceIfMissing adda a Namespace object if missing (this ensure the targetNamespace will be created)
277+
func addNamespaceIfMissing(objs []unstructured.Unstructured, targetNamespace string) []unstructured.Unstructured {
285278
namespaceObjectFound := false
286-
287279
for _, o := range objs {
288280
// if the object has Kind Namespace, fix the namespace name
289281
if o.GetKind() == namespaceKind {
290282
namespaceObjectFound = true
291-
o.SetName(targetNamespace)
292-
}
293-
294-
// if the object is namespaced, set the namespace name
295-
if isResourceNamespaced(o.GetKind()) {
296-
o.SetNamespace(targetNamespace)
297283
}
298284
}
299285

@@ -312,6 +298,23 @@ func fixTargetNamespace(objs []unstructured.Unstructured, targetNamespace string
312298
return objs
313299
}
314300

301+
// fixTargetNamespace ensures all the provider components are deployed in the target namespace (apply only to namespaced objects).
302+
func fixTargetNamespace(objs []unstructured.Unstructured, targetNamespace string) []unstructured.Unstructured {
303+
for _, o := range objs {
304+
// if the object has Kind Namespace, fix the namespace name
305+
if o.GetKind() == namespaceKind {
306+
o.SetName(targetNamespace)
307+
}
308+
309+
// if the object is namespaced, set the namespace name
310+
if isResourceNamespaced(o.GetKind()) {
311+
o.SetNamespace(targetNamespace)
312+
}
313+
}
314+
315+
return objs
316+
}
317+
315318
func isResourceNamespaced(kind string) bool {
316319
switch kind {
317320
case "Namespace",

cmd/clusterctl/pkg/client/repository/components_test.go

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,15 @@ func Test_inspectTargetNamespace(t *testing.T) {
188188
}
189189
}
190190

191-
func Test_fixTargetNamespace_A(t *testing.T) {
191+
func Test_fixTargetNamespace(t *testing.T) {
192192
type args struct {
193193
objs []unstructured.Unstructured
194194
targetNamespace string
195195
}
196196
tests := []struct {
197197
name string
198198
args args
199+
want []unstructured.Unstructured
199200
}{
200201
{
201202
name: "fix Namespace object if exists",
@@ -212,41 +213,17 @@ func Test_fixTargetNamespace_A(t *testing.T) {
212213
},
213214
targetNamespace: "bar",
214215
},
215-
},
216-
{
217-
name: "add Namespace object if it does not exists",
218-
args: args{
219-
objs: []unstructured.Unstructured{},
220-
targetNamespace: "bar",
216+
want: []unstructured.Unstructured{
217+
{
218+
Object: map[string]interface{}{
219+
"kind": namespaceKind,
220+
"metadata": map[string]interface{}{
221+
"name": "bar",
222+
},
223+
},
224+
},
221225
},
222226
},
223-
}
224-
for _, tt := range tests {
225-
t.Run(tt.name, func(t *testing.T) {
226-
got := fixTargetNamespace(tt.args.objs, tt.args.targetNamespace)
227-
228-
wgot, err := inspectTargetNamespace(got)
229-
if err != nil {
230-
t.Fatalf("inspectTargetNamespace() error = %v", err)
231-
}
232-
233-
if wgot != tt.args.targetNamespace {
234-
t.Errorf("fixTargetNamespace().targetNamespace got = %v, want %v", wgot, tt.args.targetNamespace)
235-
}
236-
})
237-
}
238-
}
239-
240-
func Test_fixTargetNamespace_B(t *testing.T) {
241-
type args struct {
242-
objs []unstructured.Unstructured
243-
targetNamespace string
244-
}
245-
tests := []struct {
246-
name string
247-
args args
248-
want []unstructured.Unstructured
249-
}{
250227
{
251228
name: "fix namespaced objects",
252229
args: args{
@@ -301,6 +278,55 @@ func Test_fixTargetNamespace_B(t *testing.T) {
301278
}
302279
}
303280

281+
func Test_addNamespaceIfMissing(t *testing.T) {
282+
type args struct {
283+
objs []unstructured.Unstructured
284+
targetNamespace string
285+
}
286+
tests := []struct {
287+
name string
288+
args args
289+
}{
290+
{
291+
name: "don't add Namespace object if exists",
292+
args: args{
293+
objs: []unstructured.Unstructured{
294+
{
295+
Object: map[string]interface{}{
296+
"kind": namespaceKind,
297+
"metadata": map[string]interface{}{
298+
"name": "foo",
299+
},
300+
},
301+
},
302+
},
303+
targetNamespace: "foo",
304+
},
305+
},
306+
{
307+
name: "add Namespace object if it does not exists",
308+
args: args{
309+
objs: []unstructured.Unstructured{},
310+
targetNamespace: "bar",
311+
},
312+
},
313+
}
314+
for _, tt := range tests {
315+
t.Run(tt.name, func(t *testing.T) {
316+
got := addNamespaceIfMissing(tt.args.objs, tt.args.targetNamespace)
317+
318+
wgot, err := inspectTargetNamespace(got)
319+
if err != nil {
320+
t.Fatalf("inspectTargetNamespace() error = %v", err)
321+
}
322+
323+
if wgot != tt.args.targetNamespace {
324+
t.Errorf("addNamespaceIfMissing().targetNamespace got = %v, want %v", wgot, tt.args.targetNamespace)
325+
}
326+
})
327+
}
328+
}
329+
304330
func Test_fixClusterRoleBindings(t *testing.T) {
305331
type args struct {
306332
objs []unstructured.Unstructured

cmd/clusterctl/pkg/internal/util/yaml.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,17 @@ func ToUnstructured(rawyaml []byte) ([]unstructured.Unstructured, error) {
6767

6868
return ret, nil
6969
}
70+
71+
// FromUnstructured takes a list of Unstructured objects and converts it into a YAML
72+
func FromUnstructured(objs []unstructured.Unstructured) ([]byte, error) {
73+
var ret [][]byte //nolint
74+
for _, o := range objs {
75+
content, err := yaml.Marshal(o.UnstructuredContent())
76+
if err != nil {
77+
return nil, errors.Wrapf(err, "failed to marshal yaml for %s/%s", o.GetNamespace(), o.GetName())
78+
}
79+
ret = append(ret, content)
80+
}
81+
82+
return JoinYaml(ret...), nil
83+
}

0 commit comments

Comments
 (0)