mirror of
https://github.com/open-cluster-management-io/ocm.git
synced 2026-02-14 10:00:11 +00:00
Adding labels to the resources created by work controller (#1176)
Signed-off-by: suvaanshkumar <suvaanshkumar@gmail.com>
This commit is contained in:
@@ -271,16 +271,25 @@ func CreateManifestWork(
|
||||
return nil, fmt.Errorf("invalid cluster namespace")
|
||||
}
|
||||
|
||||
// Get ManifestWorkReplicaSet labels
|
||||
labels := mwrSet.Labels
|
||||
|
||||
// TODO consider how to trace the manifestworks spec changes for cloudevents work client
|
||||
|
||||
// Merge mwrSet.Labels with the required labels
|
||||
mergedLabels := make(map[string]string)
|
||||
for k, v := range labels {
|
||||
mergedLabels[k] = v
|
||||
}
|
||||
|
||||
mergedLabels[ManifestWorkReplicaSetControllerNameLabelKey] = manifestWorkReplicaSetKey(mwrSet)
|
||||
mergedLabels[ManifestWorkReplicaSetPlacementNameLabelKey] = placementRefName
|
||||
|
||||
return &workv1.ManifestWork{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: mwrSet.Name,
|
||||
Namespace: clusterNS,
|
||||
Labels: map[string]string{
|
||||
ManifestWorkReplicaSetControllerNameLabelKey: manifestWorkReplicaSetKey(mwrSet),
|
||||
ManifestWorkReplicaSetPlacementNameLabelKey: placementRefName,
|
||||
},
|
||||
Labels: mergedLabels,
|
||||
},
|
||||
Spec: mwrSet.Spec.ManifestWorkTemplate,
|
||||
}, nil
|
||||
|
||||
@@ -139,7 +139,7 @@ func (m *ManifestWorkController) sync(ctx context.Context, controllerContext fac
|
||||
}
|
||||
|
||||
// Apply appliedManifestWork
|
||||
appliedManifestWork, err := m.applyAppliedManifestWork(ctx, manifestWork.Name, m.hubHash, m.agentID)
|
||||
appliedManifestWork, err := m.applyAppliedManifestWork(ctx, manifestWork.Name, m.hubHash, m.agentID, manifestWork.ObjectMeta.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -187,11 +187,13 @@ func (m *ManifestWorkController) sync(ctx context.Context, controllerContext fac
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ManifestWorkController) applyAppliedManifestWork(ctx context.Context, workName, hubHash, agentID string) (*workapiv1.AppliedManifestWork, error) {
|
||||
func (m *ManifestWorkController) applyAppliedManifestWork(ctx context.Context, workName,
|
||||
hubHash, agentID string, labels map[string]string) (*workapiv1.AppliedManifestWork, error) {
|
||||
appliedManifestWorkName := fmt.Sprintf("%s-%s", m.hubHash, workName)
|
||||
requiredAppliedWork := &workapiv1.AppliedManifestWork{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: appliedManifestWorkName,
|
||||
Labels: labels,
|
||||
Finalizers: []string{workapiv1.AppliedManifestWorkFinalizer},
|
||||
},
|
||||
Spec: workapiv1.AppliedManifestWorkSpec{
|
||||
|
||||
@@ -129,6 +129,7 @@ var _ = ginkgo.Describe("ManifestWorkReplicaSet", func() {
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
gomega.Eventually(assertWorksByReplicaSet(clusterNames, manifestWorkReplicaSet, 3), eventuallyTimeout, eventuallyInterval).Should(gomega.Succeed())
|
||||
gomega.Eventually(assertWorksSameLabelAsReplicaSet(manifestWorkReplicaSet), eventuallyTimeout, eventuallyInterval).Should(gomega.Succeed())
|
||||
gomega.Eventually(assertSummary(workapiv1alpha1.ManifestWorkReplicaSetSummary{
|
||||
Total: 3,
|
||||
}, manifestWorkReplicaSet), eventuallyTimeout, eventuallyInterval).Should(gomega.Succeed())
|
||||
@@ -452,3 +453,24 @@ func assertWorksByReplicaSet(clusterNames sets.Set[string], mwrs *workapiv1alpha
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func assertWorksSameLabelAsReplicaSet(mwrs *workapiv1alpha1.ManifestWorkReplicaSet) func() error {
|
||||
return func() error {
|
||||
key := fmt.Sprintf("%s.%s", mwrs.Namespace, mwrs.Name)
|
||||
works, err := hubWorkClient.WorkV1().ManifestWorks(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{
|
||||
LabelSelector: fmt.Sprintf("work.open-cluster-management.io/manifestworkreplicaset=%s", key),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, work := range works.Items {
|
||||
for k, v := range mwrs.Labels {
|
||||
if work.Labels[k] != v {
|
||||
return fmt.Errorf("label %s mismatch: expected %s, got %s", k, v, work.Labels[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -868,6 +868,7 @@ var _ = ginkgo.Describe("ManifestWork", func() {
|
||||
})
|
||||
|
||||
ginkgo.Context("Work completion", func() {
|
||||
|
||||
ginkgo.BeforeEach(func() {
|
||||
manifests = []workapiv1.Manifest{
|
||||
util.ToManifest(util.NewConfigmap(clusterName, cm1, map[string]string{"a": "b"}, nil)),
|
||||
@@ -938,5 +939,28 @@ var _ = ginkgo.Describe("ManifestWork", func() {
|
||||
return nil
|
||||
}, eventuallyTimeout, eventuallyInterval).Should(gomega.Succeed())
|
||||
})
|
||||
|
||||
ginkgo.It("should propagate labels from ManifestWork to AppliedManifestWork", func() {
|
||||
// Add labels to the work using Patch instead of Update
|
||||
patchData := `{"metadata":{"labels":{"test-label":"test-value","test-label-2":"test-value-2"}}}`
|
||||
|
||||
_, err := hubWorkClient.WorkV1().ManifestWorks(clusterName).Patch(
|
||||
context.Background(), work.Name, types.MergePatchType,
|
||||
[]byte(patchData), metav1.PatchOptions{})
|
||||
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
||||
|
||||
// Verify AppliedManifestWork has the same labels
|
||||
gomega.Eventually(func() bool {
|
||||
appliedWork, err := spokeWorkClient.WorkV1().AppliedManifestWorks().Get(
|
||||
context.Background(), appliedManifestWorkName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if labels match
|
||||
return appliedWork.Labels["test-label"] == "test-value" &&
|
||||
appliedWork.Labels["test-label-2"] == "test-value-2"
|
||||
}, eventuallyTimeout, eventuallyInterval).Should(gomega.BeTrue())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user