Skip to content

Commit c82a3dd

Browse files
authored
Merge pull request #29 from weaveworks/fix-leading-comma
Don't add empty elements to the list of configs.
2 parents 163f947 + ebcd335 commit c82a3dd

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

controllers/clusterbootstrapconfig_controller.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (r *ClusterBootstrapConfigReconciler) Reconcile(ctx context.Context, req ct
105105
return ctrl.Result{RequeueAfter: clusterBootstrapConfig.ClusterReadinessRequeue()}, nil
106106
}
107107
}
108+
108109
if err := bootstrapClusterWithConfig(ctx, logger, r.Client, cluster, &clusterBootstrapConfig); err != nil {
109110
return ctrl.Result{}, fmt.Errorf("failed to bootstrap cluster config: %w", err)
110111
}
@@ -116,9 +117,11 @@ func (r *ClusterBootstrapConfigReconciler) Reconcile(ctx context.Context, req ct
116117
},
117118
},
118119
})
120+
119121
if err != nil {
120122
return ctrl.Result{}, fmt.Errorf("failed to create a patch to update the cluster annotations: %w", err)
121123
}
124+
122125
if err := r.Client.Patch(ctx, cluster, client.RawPatch(types.MergePatchType, mergePatch)); err != nil {
123126
return ctrl.Result{}, fmt.Errorf("failed to annotate cluster %s/%s as bootstrapped: %w", cluster.ObjectMeta.Name, cluster.ObjectMeta.Namespace, err)
124127
}
@@ -127,10 +130,22 @@ func (r *ClusterBootstrapConfigReconciler) Reconcile(ctx context.Context, req ct
127130
}
128131

129132
func appendClusterConfigToBootstrappedList(config capiv1alpha1.ClusterBootstrapConfig, cluster *clustersv1.GitopsCluster) string {
130-
current := cluster.GetAnnotations()[capiv1alpha1.BootstrapConfigsAnnotation]
131-
set := sets.NewString(strings.Split(current, ",")...)
132-
id := fmt.Sprintf("%s/%s", config.GetNamespace(), config.GetName())
133-
set.Insert(id)
133+
set := sets.NewString()
134+
135+
current := func(ann string) []string {
136+
nonempty := []string{}
137+
for _, s := range strings.Split(ann, ",") {
138+
if s != "" {
139+
nonempty = append(nonempty, s)
140+
}
141+
}
142+
143+
return nonempty
144+
}(cluster.GetAnnotations()[capiv1alpha1.BootstrapConfigsAnnotation])
145+
set.Insert(current...)
146+
147+
set.Insert(fmt.Sprintf("%s/%s", config.GetNamespace(), config.GetName()))
148+
134149
return strings.Join(set.List(), ",")
135150
}
136151

controllers/clusterbootstrapconfig_controller_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ func TestReconcile_when_cluster_ready(t *testing.T) {
139139
if l := len(jobs.Items); l != 1 {
140140
t.Fatalf("found %d jobs, want %d", l, 1)
141141
}
142+
143+
// reload the Cluster to check the state
144+
if err := reconciler.Get(context.TODO(), client.ObjectKeyFromObject(cl), cl); err != nil {
145+
t.Fatal(err)
146+
}
147+
148+
if v := cl.ObjectMeta.Annotations[capiv1alpha1.BootstrapConfigsAnnotation]; v != "testing/test-config" {
149+
t.Fatalf("got bootstrapped configs %q, want %q", v, "testing/test-config")
150+
}
142151
}
143152

144153
func TestReconcile_when_cluster_ready_bootstrapped_with_same_config(t *testing.T) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/weaveworks/cluster-bootstrap-controller
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/fluxcd/pkg/runtime v0.35.0

0 commit comments

Comments
 (0)