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

Commit

Permalink
Add missing labels for antreaconfig from clusterbootstrap (#4555)
Browse files Browse the repository at this point in the history
Signed-off-by: Hang Yan <yhang@vmware.com>
  • Loading branch information
hangyan committed Apr 7, 2023
1 parent 462d945 commit bb07e03
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions addons/controllers/calicoconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ var _ = Describe("CalicoConfig Reconciler and Webhooks", func() {
Expect(k8sClient.Get(ctx, configKey, calicoConfig)).To(Succeed())

Expect(calicoConfig.Spec.Calico.Config.VethMTU).Should(Equal(int64(1420)))
Expect(calicoConfig.ObjectMeta.Labels["tkg.tanzu.vmware.com/package-name"]).Should(Equal("calico.tanzu.vmware.com.1.2.5--vmware.12-tkg.1"))
// why int64?? that would make it architecture specific, as oposed to why not just int?
// (defined in apis/addonconfigs/cni/v1alpha1/calicoconfig_types.go)
// seems to also have forced part of this fix: https://github.com/vmware-tanzu/tanzu-framework/pull/2164
Expand Down
5 changes: 5 additions & 0 deletions addons/controllers/clusterbootstrap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ func (r *ClusterBootstrapReconciler) patchClusterBootstrapFromTemplate(
return nil, err
}

if err := clusterBootstrapHelper.AddPackageLabelForCNIProvider(cluster.Name, updatedClusterBootstrap); err != nil {
r.Log.Error(err, fmt.Sprintf("unable to add labels to cni provider"))
return nil, err
}

r.Log.Info("updated clusterBootstrap", "clusterBootstrap", updatedClusterBootstrap)
return updatedClusterBootstrap, nil
}
Expand Down
53 changes: 53 additions & 0 deletions addons/pkg/util/clusterbootstrapclone/clusterbootstrapclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ func (h *Helper) HandleExistingClusterBootstrap(clusterBootstrap *runtanzuv1alph
h.Logger.Error(err, fmt.Sprintf("unable to add cluster %s/%s as owner reference to providers", cluster.Namespace, cluster.Name))
}

if err := h.AddPackageLabelForCNIProvider(cluster.Name, clusterBootstrap); err != nil {
h.Logger.Error(err, fmt.Sprintf("unable to add labels to cni provider"))
return nil, err
}
h.Logger.Info("patched labels for cni provider.")

clusterBootstrap.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: clusterapiv1beta1.GroupVersion.String(),
Expand Down Expand Up @@ -964,6 +970,31 @@ func (h *Helper) AddClusterOwnerRefToExistingProviders(cluster *clusterapiv1beta
return nil
}

// AddPackageLabelForCNIProvider patch the missing labels for AntreaConfig(cni config) when it's related to
// an existing clusterboostrap. To make antreaconfig works with old versions, it relies on the package label now.
func (h *Helper) AddPackageLabelForCNIProvider(clusterName string, clusterBootstrap *runtanzuv1alpha3.ClusterBootstrap) error {
cni := clusterBootstrap.Spec.CNI
if cni != nil {
providers, err := h.getListOfExistingProviders(clusterBootstrap)
if err != nil {
return err
}
for _, p := range providers {
if cni.ValuesFrom != nil && cni.ValuesFrom.ProviderRef != nil && p.GetKind() == cni.ValuesFrom.ProviderRef.Kind {
labels := p.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labels[addontypes.PackageNameLabel] = util.ParseStringForLabel(cni.RefName)
labels[addontypes.ClusterNameLabel] = clusterName
return h.setLabels(labels, p)
}
}

}
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 Expand Up @@ -995,6 +1026,28 @@ func ownedByDifferentCluster(k8SObject *unstructured.Unstructured, cluster *clus
return ""
}

func (h *Helper) setLabels(labels map[string]string, child *unstructured.Unstructured) error {
gvr, err := h.GVRHelper.GetGVR(child.GroupVersionKind().GroupKind())
if err != nil {
h.Logger.Error(err, fmt.Sprintf("unable to get GVR of %s/%s", child.GetNamespace(), child.GetName()))
return err
}

patchObj := unstructured.Unstructured{}
patchObj.SetLabels(labels)
patchData, err := patchObj.MarshalJSON()
if err != nil {
h.Logger.Error(err, fmt.Sprintf("unable to patch provider %s/%s", child.GetNamespace(), child.GetName()), "gvr", gvr)
return err
}
_, err = h.DynamicClient.Resource(*gvr).Namespace(child.GetNamespace()).Patch(h.Ctx, child.GetName(), types.MergePatchType, patchData, metav1.PatchOptions{})
if err != nil {
h.Logger.Error(err, fmt.Sprintf("unable to patch provider %s/%s", child.GetNamespace(), child.GetName()), "gvr", gvr)
}
return err

}

func (h *Helper) setOwnerRef(ownerRef *metav1.OwnerReference, children []*unstructured.Unstructured) error {
for _, child := range children {
gvr, err := h.GVRHelper.GetGVR(child.GroupVersionKind().GroupKind())
Expand Down

0 comments on commit bb07e03

Please sign in to comment.