Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow label values with boolean data type in kustomization patch yaml #407

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/controller/subscription/subscription_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,13 @@ func (r *ReconcileSubscription) Reconcile(ctx context.Context, request reconcile
result := reconcile.Result{RequeueAfter: nextStatusUpateAt}

if err != nil {
klog.Errorf("failed to update status for subscription %v with error %v retry after 1 second", request.NamespacedName, err)
klog.Errorf("failed to update status for subscription %v with error %v, retry after 1 second", request.NamespacedName, err)

result.RequeueAfter = 1 * time.Second
} else if reconcileErr != nil {
klog.Errorf("do Reconcile got error %v, retry after 5 minutes", reconcileErr)

result.RequeueAfter = 5 * time.Minute
}

return result, err
Expand Down
25 changes: 24 additions & 1 deletion pkg/subscriber/git/git_subscriber_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,28 @@ func (ghsi *SubscriberItem) subscribeResource(file []byte) (*unstructured.Unstru
err := yaml.Unmarshal(file, &rsc)

if err != nil {
klog.Error(err, "Failed to unmarshal Kubernetes resource")
klog.Errorf("Failed to unmarshal Kubernetes resource to Unstructured, err:%v ", err)
return nil, nil, err
}

// The labels patched by kustomization could not be decoded to rsc correctly if not all label values are defined as string
// The label type is defined as map[string]string. If a label value is defined as a boolean data type such as true/false, the whole label patch will be ignored
// As a workaround, users can manually update the label value to "true" as string type in the kustomization.yaml
// To resolve it, get all labels from original resource file and explicitly set them to the new resource to be deployed.
t := kubeResource{}
err = yaml.Unmarshal(file, &t)

if err != nil {
klog.Errorf("Failed to unmarshal Kubernetes resource to kubeResource, err:%v ", err)
return nil, nil, err
}

if resourceLabels := t.GetLabels(); resourceLabels != nil {
rsc.SetLabels(resourceLabels)
}

if resourceAnnos := t.GetAnnotations(); resourceAnnos != nil {
rsc.SetAnnotations(resourceAnnos)
}

validgvk := rsc.GetObjectKind().GroupVersionKind()
Expand Down Expand Up @@ -696,6 +717,8 @@ func (ghsi *SubscriberItem) subscribeResource(file []byte) (*unstructured.Unstru
// Set app label
utils.SetPartOfLabel(ghsi.SubscriberItem.Subscription, rsc)

klog.Infof("new resource for deployment: %#v", rsc)

return rsc, &validgvk, nil
}

Expand Down
42 changes: 42 additions & 0 deletions pkg/subscriber/git/git_subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,45 @@ var _ = Describe("should update subscription status", func() {
By(fmt.Sprintf("Philip message %s", subitem.Subscription.Status.Message))
})
})

var _ = Describe("test patching labels via git kustomimzation", func() {
It("should observe the labels patched to the resource via git kustomization", func() {
githubsub2 := &appv1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Name: sharedkey.Name,
Namespace: sharedkey.Namespace,
Annotations: map[string]string{
appv1.AnnotationGitBranch: "main",
},
},
Spec: appv1.SubscriptionSpec{
Channel: sharedkey.String(),
},
}

subitem := &SubscriberItem{}
subitem.Channel = githubchn
subitem.Subscription = githubsub2
subitem.synchronizer = defaultSubscriber.synchronizer

subanno := make(map[string]string)
subanno[appv1.AnnotationGitPath] = "test/github/kustomizeLabels/namctigtd27d"
subanno[appv1.AnnotationGitBranch] = "main"
subanno[appv1.AnnotationClusterAdmin] = "true"
githubsub2.SetAnnotations(subanno)

subitem.doSubscription()

Expect(len(subitem.resources)).To(Equal(3))

for _, resource := range subitem.resources {
template := resource.Resource
if template.GetKind() == "ManagedCluster" {
labels := template.GetLabels()
Expect(labels["test2"]).To(Equal("test2"))
Expect(labels["environment"]).To(Equal("dev"))
Expect(labels["vault-csi-provider-enabled"]).To(Equal("true"))
}
}
})
})
Loading