Skip to content

Commit

Permalink
The original label of the object is preserved in Patch
Browse files Browse the repository at this point in the history
Signed-off-by: LiZhenCheng9527 <lizhencheng6@huawei.com>
  • Loading branch information
LiZhenCheng9527 committed Mar 5, 2024
1 parent 5de9703 commit 421010a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
12 changes: 10 additions & 2 deletions e2e/resources/attachedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ func CreateOrUpdateAttachedCluster(client kurator.Interface, attachedCluster *cl
if getErr != nil {
return getErr
}
attachedCluster.ResourceVersion = originalAttachedCluster.ResourceVersion
attachedClusterPatchData, createPatchErr := CreatePatchData(originalAttachedCluster, attachedCluster)
modifiedObjectMeta := ModifiedObjectMeta(originalAttachedCluster.ObjectMeta, attachedCluster.ObjectMeta)
oldAttachedCluster := clusterv1a1.AttachedCluster{
ObjectMeta: originalAttachedCluster.ObjectMeta,
Spec: originalAttachedCluster.Spec,
}
modAttachedCluster := clusterv1a1.AttachedCluster{
ObjectMeta: modifiedObjectMeta,
Spec: attachedCluster.Spec,
}
attachedClusterPatchData, createPatchErr := CreatePatchData(oldAttachedCluster, modAttachedCluster)
if createPatchErr != nil {
return createPatchErr
}
Expand Down
12 changes: 10 additions & 2 deletions e2e/resources/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ func CreateOrUpdateFleet(client kurator.Interface, fleet *fleetv1a1.Fleet) error
if getErr != nil {
return getErr
}
fleet.ResourceVersion = originalFleet.ResourceVersion
fleetPatchData, createPatchErr := CreatePatchData(originalFleet, fleet)
modifiedObjectMeta := ModifiedObjectMeta(originalFleet.ObjectMeta, fleet.ObjectMeta)
oldFleet := fleetv1a1.Fleet{
ObjectMeta: originalFleet.ObjectMeta,
Spec: originalFleet.Spec,
}
modFleet := fleetv1a1.Fleet{
ObjectMeta: modifiedObjectMeta,
Spec: fleet.Spec,
}
fleetPatchData, createPatchErr := CreatePatchData(oldFleet, modFleet)
if createPatchErr != nil {
return createPatchErr
}
Expand Down
12 changes: 10 additions & 2 deletions e2e/resources/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ func CreateOrUpdateSecret(client kubernetes.Interface, secret *corev1.Secret) er
if getErr != nil {
return getErr
}
secret.ResourceVersion = originalSecret.ResourceVersion
secretPatchData, createPatchErr := CreatePatchData(originalSecret, secret)
modifiedObjectMeta := ModifiedObjectMeta(originalSecret.ObjectMeta, secret.ObjectMeta)
oldSecret := corev1.Secret{
ObjectMeta: originalSecret.ObjectMeta,
Data: originalSecret.Data,
}
modSecret := corev1.Secret{
ObjectMeta: modifiedObjectMeta,
Data: secret.Data,
}
secretPatchData, createPatchErr := CreatePatchData(oldSecret, modSecret)
if createPatchErr != nil {
return createPatchErr
}
Expand Down
37 changes: 34 additions & 3 deletions e2e/resources/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"encoding/json"

jsonpatch "github.com/evanphx/json-patch"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func CreatePatchData(originalAttachedCluster, modifiedAttachedCluster interface{}) ([]byte, error) {
originalData, originalErr := json.Marshal(originalAttachedCluster)
func CreatePatchData(original, modified interface{}) ([]byte, error) {
originalData, originalErr := json.Marshal(original)
if originalErr != nil {
return nil, originalErr
}
modifiedData, modifiedErr := json.Marshal(modifiedAttachedCluster)
modifiedData, modifiedErr := json.Marshal(modified)
if modifiedErr != nil {
return nil, modifiedErr
}
Expand All @@ -37,3 +38,33 @@ func CreatePatchData(originalAttachedCluster, modifiedAttachedCluster interface{
}
return patchData, nil
}

func ModifiedObjectMeta(original, modified metav1.ObjectMeta) metav1.ObjectMeta {
if modified.Labels == nil {
modified.Labels = original.Labels
} else {
for k, v := range original.Labels {
if modified.Labels[k] == "" {
modified.Labels[k] = v
}
}
}

if modified.Annotations == nil {
modified.Annotations = original.Annotations
} else {
for k, v := range original.Annotations {
if modified.Annotations[k] == "" {
modified.Annotations[k] = v
}
}
}

if modified.Finalizers == nil {
modified.Finalizers = original.Finalizers
}
if modified.ResourceVersion == "" {
modified.ResourceVersion = original.ResourceVersion
}
return modified
}

0 comments on commit 421010a

Please sign in to comment.