Skip to content

Commit

Permalink
Add auto approve anno for accepted cluster (open-cluster-management-i…
Browse files Browse the repository at this point in the history
…o#680)

Signed-off-by: Wei Liu <liuweixa@redhat.com>
  • Loading branch information
skeeey authored Oct 31, 2024
1 parent fa98535 commit d26db93
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pkg/registration/hub/managedcluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ func (c *managedClusterController) sync(ctx context.Context, syncCtx factory.Syn
return nil
}

if !managedCluster.Spec.HubAcceptsClient {
if features.HubMutableFeatureGate.Enabled(ocmfeature.ManagedClusterAutoApproval) {
// If the ManagedClusterAutoApproval feature is enabled, we automatically accept a cluster only
// when it joins for the first time, afterwards users can deny it again.
if features.HubMutableFeatureGate.Enabled(ocmfeature.ManagedClusterAutoApproval) {
if _, ok := managedCluster.Annotations[clusterAcceptedAnnotationKey]; !ok {
return c.acceptCluster(ctx, managedClusterName)
}
if _, ok := managedCluster.Annotations[clusterAcceptedAnnotationKey]; !ok {
return c.acceptCluster(ctx, managedCluster)
}
}

if !managedCluster.Spec.HubAcceptsClient {
// Current spoke cluster is not accepted, do nothing.
if !meta.IsStatusConditionTrue(managedCluster.Status.Conditions, v1.ManagedClusterConditionHubAccepted) {
return nil
Expand Down Expand Up @@ -224,12 +224,20 @@ func (c *managedClusterController) removeManagedClusterResources(ctx context.Con
return operatorhelpers.NewMultiLineAggregate(errs)
}

func (c *managedClusterController) acceptCluster(ctx context.Context, managedClusterName string) error {
// TODO support patching both annotations and spec simultaneously in the patcher
func (c *managedClusterController) acceptCluster(ctx context.Context, managedCluster *v1.ManagedCluster) error {
acceptedTime := time.Now()
patch := fmt.Sprintf(`{"metadata":{"annotations":{"%s":"%s"}},"spec":{"hubAcceptsClient":true}}`,

// If one cluster is already accepted, we only add the cluster accepted annotation, otherwise
// we add the cluster accepted annotation and accept the cluster.
patch := fmt.Sprintf(`{"metadata":{"annotations":{"%s":"%s"}}}`,
clusterAcceptedAnnotationKey, acceptedTime.Format(time.RFC3339))
_, err := c.clusterClient.ClusterV1().ManagedClusters().Patch(ctx, managedClusterName,
if !managedCluster.Spec.HubAcceptsClient {
// TODO support patching both annotations and spec simultaneously in the patcher
patch = fmt.Sprintf(`{"metadata":{"annotations":{"%s":"%s"}},"spec":{"hubAcceptsClient":true}}`,
clusterAcceptedAnnotationKey, acceptedTime.Format(time.RFC3339))
}

_, err := c.clusterClient.ClusterV1().ManagedClusters().Patch(ctx, managedCluster.Name,
types.MergePatchType, []byte(patch), metav1.PatchOptions{})
return err
}
17 changes: 17 additions & 0 deletions pkg/registration/hub/managedcluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ func TestSyncManagedCluster(t *testing.T) {
testingcommon.AssertActions(t, actions, "patch")
},
},
{
name: "should add the auto approval annotation to an accepted cluster when auto approval is enabled",
autoApprovalEnabled: true,
startingObjects: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()},
validateActions: func(t *testing.T, actions []clienttesting.Action) {
testingcommon.AssertActions(t, actions, "patch")
patch := actions[0].(clienttesting.PatchAction).GetPatch()
managedCluster := &v1.ManagedCluster{}
err := json.Unmarshal(patch, managedCluster)
if err != nil {
t.Fatal(err)
}
if _, ok := managedCluster.Annotations[clusterAcceptedAnnotationKey]; !ok {
t.Errorf("expected auto approval annotation, but failed")
}
},
},
}

features.HubMutableFeatureGate.Add(ocmfeature.DefaultHubRegistrationFeatureGates)
Expand Down

0 comments on commit d26db93

Please sign in to comment.