Skip to content

Commit

Permalink
Add annotation support to util.CloneTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
schrej committed May 4, 2021
1 parent 261d231 commit 17486c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
19 changes: 16 additions & 3 deletions controllers/external/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ type CloneTemplateInput struct {
// Labels is an optional map of labels to be added to the object.
// +optional
Labels map[string]string

// Annotations is an optional map of annotations to be added to the object.
// +optional
Annotations map[string]string
}

// CloneTemplate uses the client and the reference to create a new object from the template.
Expand All @@ -88,6 +92,7 @@ func CloneTemplate(ctx context.Context, in *CloneTemplateInput) (*corev1.ObjectR
ClusterName: in.ClusterName,
OwnerRef: in.OwnerRef,
Labels: in.Labels,
Annotations: in.Annotations,
}
to, err := GenerateTemplate(generateTemplateInput)
if err != nil {
Expand Down Expand Up @@ -127,6 +132,10 @@ type GenerateTemplateInput struct {
// Labels is an optional map of labels to be added to the object.
// +optional
Labels map[string]string

// Annotations is an optional map of annotations to be added to the object.
// +optional
Annotations map[string]string
}

func GenerateTemplate(in *GenerateTemplateInput) (*unstructured.Unstructured, error) {
Expand All @@ -146,10 +155,14 @@ func GenerateTemplate(in *GenerateTemplateInput) (*unstructured.Unstructured, er
to.SetName(names.SimpleNameGenerator.GenerateName(in.Template.GetName() + "-"))
to.SetNamespace(in.Namespace)

if to.GetAnnotations() == nil {
to.SetAnnotations(map[string]string{})
}
// Set annotations.
annotations := to.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
for key, value := range in.Annotations {
annotations[key] = value
}
annotations[clusterv1.TemplateClonedFromNameAnnotation] = in.TemplateRef.Name
annotations[clusterv1.TemplateClonedFromGroupKindAnnotation] = in.TemplateRef.GroupVersionKind().GroupKind().String()
to.SetAnnotations(annotations)
Expand Down
20 changes: 16 additions & 4 deletions controllers/external/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ func TestCloneTemplateResourceFound(t *testing.T) {
"template": map[string]interface{}{
"metadata": map[string]interface{}{
"annotations": map[string]interface{}{
"test": "annotations",
"test-template": "annotations",
"precedence": "template",
},
"labels": map[string]interface{}{
"test-template": "label",
"precedence": "template",
},
},
"spec": map[string]interface{}{
Expand Down Expand Up @@ -175,7 +180,12 @@ func TestCloneTemplateResourceFound(t *testing.T) {
ClusterName: testClusterName,
OwnerRef: owner.DeepCopy(),
Labels: map[string]string{
"test-label-1": "value-1",
"precedence": "input",
clusterv1.ClusterLabelName: "should-be-overwritten",
},
Annotations: map[string]string{
"precedence": "input",
clusterv1.TemplateClonedFromNameAnnotation: "should-be-overwritten",
},
})
g.Expect(err).NotTo(HaveOccurred())
Expand All @@ -201,10 +211,12 @@ func TestCloneTemplateResourceFound(t *testing.T) {

cloneLabels := clone.GetLabels()
g.Expect(cloneLabels).To(HaveKeyWithValue(clusterv1.ClusterLabelName, testClusterName))
g.Expect(cloneLabels).To(HaveKeyWithValue("test-label-1", "value-1"))
g.Expect(cloneLabels).To(HaveKeyWithValue("test-template", "label"))
g.Expect(cloneLabels).To(HaveKeyWithValue("precedence", "input"))

cloneAnnotations := clone.GetAnnotations()
g.Expect(cloneAnnotations).To(HaveKeyWithValue("test", "annotations"))
g.Expect(cloneAnnotations).To(HaveKeyWithValue("test-template", "annotations"))
g.Expect(cloneAnnotations).To(HaveKeyWithValue("precedence", "input"))

g.Expect(cloneAnnotations).To(HaveKeyWithValue(clusterv1.TemplateClonedFromNameAnnotation, templateRef.Name))
g.Expect(cloneAnnotations).To(HaveKeyWithValue(clusterv1.TemplateClonedFromGroupKindAnnotation, templateRef.GroupVersionKind().GroupKind().String()))
Expand Down
1 change: 1 addition & 0 deletions controllers/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ func (r *MachineSetReconciler) syncReplicas(ctx context.Context, ms *clusterv1.M
Namespace: machine.Namespace,
ClusterName: machine.Spec.ClusterName,
Labels: machine.Labels,
Annotations: machine.Annotations,
})
if err != nil {
return errors.Wrapf(err, "failed to clone infrastructure configuration for MachineSet %q in namespace %q", ms.Name, ms.Namespace)
Expand Down

0 comments on commit 17486c4

Please sign in to comment.