Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
clusterbootrap controller adds owner reference to existing objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Adolfo Duarte committed Nov 7, 2022
1 parent 2e7f961 commit 04c568d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
8 changes: 8 additions & 0 deletions addons/controllers/clusterbootstrap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ func (r *ClusterBootstrapReconciler) reconcileNormal(cluster *clusterapiv1beta1.
return ctrl.Result{}, nil
}

clusterBootstrapHelper := clusterbootstrapclone.NewHelper(
r.context, r.Client, r.aggregatedAPIResourcesClient, r.dynamicClient, r.gvrHelper, r.Log)

if err := clusterBootstrapHelper.AddClusterOwnerRefToExistingProviders(cluster, clusterBootstrap); err != nil {
log.Error(err, "could not add cluster as owner to existing providers")
return ctrl.Result{}, err
}

// reconcile the proxy settings of the cluster
if err := r.reconcileClusterProxyAndNetworkSettings(cluster, log); err != nil {
return ctrl.Result{}, err
Expand Down
38 changes: 38 additions & 0 deletions addons/pkg/util/clusterbootstrapclone/clusterbootstrapclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,44 @@ func (h *Helper) cloneEmbeddedLocalObjectRef(cluster *clusterapiv1beta1.Cluster,
return nil
}

func (h *Helper) getListOfExistingProviders(clusterBootstrap *runtanzuv1alpha3.ClusterBootstrap) ([]*unstructured.Unstructured, error) {
// returns a slice of providers listed in the clusterbootstrap and that have been verified to exist.
providers := []*unstructured.Unstructured{}
cbPkgs, err := h.getListOfPkgsWithProviderRefNames(clusterBootstrap)
if err != nil {
return nil, err
}
for _, pkg := range cbPkgs {
gvr, err := h.GVRHelper.GetGVR(schema.GroupKind{Group: *pkg.ValuesFrom.ProviderRef.APIGroup, Kind: pkg.ValuesFrom.ProviderRef.Kind})
if err != nil {
h.Logger.Error(err, "failed to getGVR")
return nil, err
}
provider, err := h.DynamicClient.Resource(*gvr).Namespace(clusterBootstrap.Namespace).Get(h.Ctx, pkg.ValuesFrom.ProviderRef.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
continue
}
if err != nil {
h.Logger.Error(err, fmt.Sprintf("unable to fetch provider %s/%s", clusterBootstrap.Namespace, pkg.ValuesFrom.ProviderRef.Name), "gvr", gvr)
return nil, err
}
providers = append(providers, provider)
}
return providers, nil
}

func (h *Helper) AddClusterOwnerRefToExistingProviders(cluster *clusterapiv1beta1.Cluster, clusterBootstrap *runtanzuv1alpha3.ClusterBootstrap) error {

exsitingProviders, err := h.getListOfExistingProviders(clusterBootstrap)
if err != nil {
return err
}
if err = h.AddClusterOwnerRef(cluster, exsitingProviders, nil, nil); err != nil {
return err
}
return nil
}

// AddClusterOwnerRef adds cluster as an owner reference to the children with given controller and blockownerdeletion settings
func (h *Helper) AddClusterOwnerRef(cluster *clusterapiv1beta1.Cluster, children []*unstructured.Unstructured, controller, blockownerdeletion *bool) error {
ownerRef := metav1.OwnerReference{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,50 @@ var _ = Describe("ClusterbootstrapClone", func() {
})
})

Context("Verify AddclusterOwnerRefToExistingProviders()", func() {
It("should succeed on adding cluster as owner to existing orphan config", func() {
fakeCluster := constructFakeCluster()
fakeBootstrap := constructFakeClusterBootstrap()
fakeDynamicClient = dynamicfake.NewSimpleDynamicClient(scheme, constructFakeAntreaConfig())
helper.DynamicClient = fakeDynamicClient

err := helper.AddClusterOwnerRefToExistingProviders(fakeCluster, fakeBootstrap)
Expect(err).ToNot(HaveOccurred())
})
It("should succeed on adding cluster as owner to config which already lists same cluster as owner", func() {
fakeBootstrap := constructFakeClusterBootstrap()
fakeAntreaConfig := constructFakeAntreaConfig()
fakeCluster := constructNamespacedFakeCluster("same-cluster", "fake-ns")
fakeAntreaConfigWithOwner := constructFakeAntreaConfigWithClusterOwner(fakeAntreaConfig.Name, fakeCluster.Name)
fakeDynamicClient = dynamicfake.NewSimpleDynamicClient(scheme, fakeAntreaConfigWithOwner)
helper.DynamicClient = fakeDynamicClient

err := helper.AddClusterOwnerRefToExistingProviders(fakeCluster, fakeBootstrap)
Expect(err).ToNot(HaveOccurred())
})
It("should throw error if config has different cluster owner", func() {
fakeBootstrap := constructFakeClusterBootstrap()
fakeAntreaConfig := constructFakeAntreaConfig()
fakeCluster := constructNamespacedFakeCluster("same-cluster", "fake-ns")
fakeAntreaConfigWithOwner := constructFakeAntreaConfigWithClusterOwner(fakeAntreaConfig.Name, "someothercluster")
fakeDynamicClient = dynamicfake.NewSimpleDynamicClient(scheme, fakeAntreaConfigWithOwner)
helper.DynamicClient = fakeDynamicClient

err := helper.AddClusterOwnerRefToExistingProviders(fakeCluster, fakeBootstrap)
Expect(err).To(HaveOccurred())
})
It("should handle non existing config ", func() {
fakeBootstrap := constructFakeClusterBootstrap()
fakeCluster := constructNamespacedFakeCluster("same-cluster", "fake-ns")
fakeDynamicClient = dynamicfake.NewSimpleDynamicClient(scheme)
helper.DynamicClient = fakeDynamicClient

err := helper.AddClusterOwnerRefToExistingProviders(fakeCluster, fakeBootstrap)
Expect(err).ToNot(HaveOccurred())
})

})

Context("Verify cloneEmbeddedLocalObjectRef()", func() {
BeforeEach(func() {
fakeDynamicClient = dynamicfake.NewSimpleDynamicClient(scheme, constructFakeAntreaConfig(), constructFakeVSphereCPIConfig(), constructFakeSecret())
Expand Down Expand Up @@ -789,6 +833,23 @@ func constructFakeClusterBootstrapTemplate() *v1alpha3.ClusterBootstrapTemplate
}
}

func constructFakeClusterBootstrap() *v1alpha3.ClusterBootstrap {
return &v1alpha3.ClusterBootstrap{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-clusterbootstrap",
Namespace: "fake-ns",
},
Spec: &v1alpha3.ClusterBootstrapTemplateSpec{
CNI: constructFakeClusterBootstrapPackageWithAntreaProviderRef(),
CSI: constructFakeClusterBootstrapPackageWithCSIInlineRef(),
AdditionalPackages: []*v1alpha3.ClusterBootstrapPackage{
{RefName: fakePinnipedCBPackageRefName, ValuesFrom: &v1alpha3.ValuesFrom{Inline: map[string]interface{}{"identity_management_type": "oidc"}}},
{RefName: fakeMetricsServerCBPackageRefName},
},
},
}
}

func prepareCarvelPackages(client client.Client, namespace string) {
err := client.Create(context.TODO(), constructAntreaCarvelPackage(namespace))
Expect(err).To(BeNil())
Expand Down

0 comments on commit 04c568d

Please # to comment.