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

Add missing labels for antreaconfig from clusterbootstrap #4555

Merged
merged 5 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
}

// AddPackageLabelForCNIConfig 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