diff --git a/pkg/registration/spoke/registration/creating_controller.go b/pkg/registration/spoke/registration/creating_controller.go index 9a6c6931d..11b8fc12e 100644 --- a/pkg/registration/spoke/registration/creating_controller.go +++ b/pkg/registration/spoke/registration/creating_controller.go @@ -52,10 +52,11 @@ func NewManagedClusterCreatingController( } func (c *managedClusterCreatingController) sync(ctx context.Context, syncCtx factory.SyncContext) error { + logger := klog.FromContext(ctx) existingCluster, err := c.hubClusterClient.ClusterV1().ManagedClusters().Get(ctx, c.clusterName, metav1.GetOptions{}) // ManagedCluster is only allowed created during bootstrap. After bootstrap secret expired, an unauthorized error will be got, output log at the debug level if err != nil && skipUnauthorizedError(err) == nil { - klog.V(4).Infof("unable to get the managed cluster %q from hub: %v", c.clusterName, err) + logger.V(4).Info("unable to get the managed cluster from hub", "clusterName", c.clusterName, "err", err) return nil } @@ -76,8 +77,16 @@ func (c *managedClusterCreatingController) sync(ctx context.Context, syncCtx fac } _, err = c.hubClusterClient.ClusterV1().ManagedClusters().Create(ctx, managedCluster, metav1.CreateOptions{}) - // ManagedCluster is only allowed created during bootstrap. After bootstrap secret expired, an unauthorized error will be got, skip it - if skipUnauthorizedError(err) != nil { + if errors.IsAlreadyExists(err) { + logger.V(4).Info("managed cluster already exists", "clusterName", c.clusterName) + return nil + } + + if err != nil { + // Unauthorized/Forbidden after bootstrap: skip without emitting a create event. + if skipUnauthorizedError(err) == nil { + return nil + } return fmt.Errorf("unable to create managed cluster with name %q on hub: %w", c.clusterName, err) } syncCtx.Recorder().Eventf("ManagedClusterCreated", "Managed cluster %q created on hub", c.clusterName) diff --git a/test/e2e/addon_lease_test.go b/test/e2e/addon_lease_test.go index c7e124703..33e526d02 100644 --- a/test/e2e/addon_lease_test.go +++ b/test/e2e/addon_lease_test.go @@ -68,7 +68,7 @@ var _ = ginkgo.Describe("Addon Health Check", ginkgo.Label("addon-lease"), func( return err } if !meta.IsStatusConditionTrue(found.Status.Conditions, "Available") { - return fmt.Errorf("condition should be available") + return fmt.Errorf("condition should be available, got %v", found.Status.Conditions) } return nil }).Should(gomega.Succeed()) diff --git a/test/e2e/addonmanagement_test.go b/test/e2e/addonmanagement_test.go index e40c1865d..413e9eb3e 100644 --- a/test/e2e/addonmanagement_test.go +++ b/test/e2e/addonmanagement_test.go @@ -130,7 +130,7 @@ var _ = ginkgo.Describe("Enable addon management feature gate", ginkgo.Ordered, gomega.Expect(err).ToNot(gomega.HaveOccurred()) ginkgo.By(fmt.Sprintf("create the addon %v on the managed cluster namespace %v", addOnName, universalClusterName)) - err = hub.CreateManagedClusterAddOn(universalClusterName, addOnName, "test-ns") // the install namespace will be ignored + err = hub.CreateManagedClusterAddOn(universalClusterName, addOnName, addonInstallNamespace) if err != nil { klog.Errorf("failed to create managed cluster addon %v on the managed cluster namespace %v: %v", addOnName, universalClusterName, err) gomega.Expect(errors.IsAlreadyExists(err)).To(gomega.BeTrue()) diff --git a/test/framework/managedclusteraddon.go b/test/framework/managedclusteraddon.go index cdd35ed44..991e445ab 100644 --- a/test/framework/managedclusteraddon.go +++ b/test/framework/managedclusteraddon.go @@ -9,6 +9,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/util/retry" addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" ) @@ -21,13 +22,30 @@ func (hub *Hub) CreateManagedClusterAddOn(managedClusterNamespace, addOnName, in Namespace: managedClusterNamespace, Name: addOnName, }, - Spec: addonv1alpha1.ManagedClusterAddOnSpec{ - InstallNamespace: installNamespace, - }, + Spec: addonv1alpha1.ManagedClusterAddOnSpec{}, }, metav1.CreateOptions{}, ) - return err + + if err != nil { + return err + } + + return retry.RetryOnConflict(retry.DefaultBackoff, func() error { + addOn, err := hub.AddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterNamespace).Get( + context.TODO(), addOnName, metav1.GetOptions{}) + if err != nil { + return err + } + + if addOn.Status.Namespace == installNamespace { + return nil + } + addOn.Status.Namespace = installNamespace + _, err = hub.AddonClient.AddonV1alpha1().ManagedClusterAddOns(managedClusterNamespace).UpdateStatus( + context.TODO(), addOn, metav1.UpdateOptions{}) + return err + }) } func (hub *Hub) CreateManagedClusterAddOnLease(addOnInstallNamespace, addOnName string) error {