Skip to content

Commit

Permalink
Merge pull request #262 from mrlihanbo/fix/clusters-to-adopt-all
Browse files Browse the repository at this point in the history
fix: incorrect ping-pong in namespace controller
  • Loading branch information
mrlihanbo authored Nov 8, 2023
2 parents 2f4b1f4 + 43901df commit 68a25a3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/nsautoprop/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (c *Controller) reconcile(ctx context.Context, qualifiedName common.Qualifi
}
needsUpdate = needsUpdate || isDirty

isDirty, err = c.ensureAnnotation(fedNamespace, adoption.ClustersToAdoptAnnotation, AllClustersToAdoptRegexp)
isDirty, err = c.ensureAnnotation(fedNamespace, adoption.ClustersToAdoptInternalAnnotation, AllClustersToAdoptRegexp)
if err != nil {
keyedLogger.Error(err, "Failed to ensure annotation")
return worker.StatusError
Expand Down
15 changes: 11 additions & 4 deletions pkg/util/adoption/conflictresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const (
ConflictResolutionInternalAnnotation = common.InternalPrefix + "conflict-resolution"
// ClustersToAdoptAnnotation specifies the set of clusters where preexisting resources are allowed to be adopted. Defaults to no clusters.
// It will only take effect if adoption is enabled by the conflict resolution annotation.
ClustersToAdoptAnnotation = common.DefaultPrefix + "clusters-to-adopt"
ClustersToAdoptAnnotation = common.DefaultPrefix + "clusters-to-adopt"
ClustersToAdoptInternalAnnotation = common.InternalPrefix + "clusters-to-adopt"
)

type ConflictResolution string
Expand All @@ -58,13 +59,19 @@ type clustersToAdoptAnnotationElement struct {
}

func FilterToAdoptCluster(obj metav1.Object, clusterName string) (bool, error) {
annotation := obj.GetAnnotations()[ClustersToAdoptAnnotation]
if len(annotation) == 0 {
annotations := obj.GetAnnotations()

clustersToAdoptRaw := annotations[ClustersToAdoptAnnotation]
if value, exist := annotations[ClustersToAdoptInternalAnnotation]; exist {
clustersToAdoptRaw = value
}

if len(clustersToAdoptRaw) == 0 {
return false, nil
}

var clustersToAdopt clustersToAdoptAnnotationElement
if err := json.Unmarshal([]byte(annotation), &clustersToAdopt); err != nil {
if err := json.Unmarshal([]byte(clustersToAdoptRaw), &clustersToAdopt); err != nil {
return false, fmt.Errorf("failed to unmarshal %s annotation %w", ClustersToAdoptAnnotation, err)
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/util/adoption/conflictresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ func TestFilterToAdoptCluster(t *testing.T) {
want: true,
wantErr: false,
},
{
name: "with internal adopt annotation",
args: args{
obj: newFederatedObject(map[string]string{
ClustersToAdoptInternalAnnotation: `{"clusters": ["kubeadmiral-member-1"]}`,
}),
clusterName: "kubeadmiral-member-1",
},
want: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 68a25a3

Please sign in to comment.