From aa3f3acfcd9c8625be13ddf936bf27029fe8758e Mon Sep 17 00:00:00 2001 From: Xudong Liu Date: Tue, 24 Oct 2023 01:40:41 -0700 Subject: [PATCH] use patch to avoid auto fill default value Signed-off-by: Xudong Liu --- ...kodeploymentconfig_controller_avi_phase.go | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/controllers/akodeploymentconfig/akodeploymentconfig_controller_avi_phase.go b/controllers/akodeploymentconfig/akodeploymentconfig_controller_avi_phase.go index ab8fea85..1f6c8b1d 100644 --- a/controllers/akodeploymentconfig/akodeploymentconfig_controller_avi_phase.go +++ b/controllers/akodeploymentconfig/akodeploymentconfig_controller_avi_phase.go @@ -7,9 +7,10 @@ import ( "bytes" "context" "errors" - "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/utils" "sync" + "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/utils" + "net" "sort" @@ -19,6 +20,8 @@ import ( corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -290,25 +293,24 @@ func (r *AKODeploymentConfigReconciler) reconcileAviInfraSetting( log.Info("ControlPlaneNetwork is empty in akoDeploymentConfig, skip creating AVIInfraSetting") return res, nil } + return res, r.Patch(ctx, r.createAviInfraSetting(adc), client.Apply, client.ForceOwnership, client.FieldOwner("field-owner")) +} - newAviInfraSetting := r.createAviInfraSetting(adc) - aviInfraSetting := &akov1beta1.AviInfraSetting{} +// AviInfraSetting is a top-level type +type AviInfraSetting struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + Spec AviInfraSettingSpec `json:"spec,omitempty"` +} - if err := r.Get(ctx, client.ObjectKey{ - Name: haprovider.GetAviInfraSettingName(adc), - }, aviInfraSetting); err != nil { - if apierrors.IsNotFound(err) { - log.Info("AVIInfraSetting doesn't exist, start creating it") - return res, r.Create(ctx, newAviInfraSetting) - } - log.Error(err, "Failed to get AVIInfraSetting, requeue") - return res, err - } - newAviInfraSetting.Spec.DeepCopyInto(&aviInfraSetting.Spec) - return res, r.Update(ctx, aviInfraSetting) +type AviInfraSettingSpec struct { + Network akov1beta1.AviInfraSettingNetwork `json:"network,omitempty"` + SeGroup akov1beta1.AviInfraSettingSeGroup `json:"seGroup,omitempty"` + L7Settings akov1beta1.AviInfraL7Settings `json:"l7Settings,omitempty"` + NSXSettings *akov1beta1.AviInfraNSXSettings `json:"nsxSettings,omitempty"` } -func (r *AKODeploymentConfigReconciler) createAviInfraSetting(adc *akoov1alpha1.AKODeploymentConfig) *akov1beta1.AviInfraSetting { +func (r *AKODeploymentConfigReconciler) createAviInfraSetting(adc *akoov1alpha1.AKODeploymentConfig) *unstructured.Unstructured { // ShardVSSize describes ingress shared virtual service size, default value is SMALL shardSize := "SMALL" if adc.Spec.ExtraConfigs.IngressConfigs.ShardVSSize != "" { @@ -327,11 +329,15 @@ func (r *AKODeploymentConfigReconciler) createAviInfraSetting(adc *akoov1alpha1. }} } - return &akov1beta1.AviInfraSetting{ + aviinfraSetting := &AviInfraSetting{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "ako.vmware.com/v1beta1", + Kind: "AviInfraSetting", + }, ObjectMeta: metav1.ObjectMeta{ Name: haprovider.GetAviInfraSettingName(adc), }, - Spec: akov1beta1.AviInfraSettingSpec{ + Spec: AviInfraSettingSpec{ SeGroup: akov1beta1.AviInfraSettingSeGroup{ Name: adc.Spec.ServiceEngineGroup, }, @@ -343,6 +349,9 @@ func (r *AKODeploymentConfigReconciler) createAviInfraSetting(adc *akoov1alpha1. }, }, } + u := &unstructured.Unstructured{} + u.Object, _ = runtime.DefaultUnstructuredConverter.ToUnstructured(aviinfraSetting) + return u } func (r *AKODeploymentConfigReconciler) reconcileAviInfraSettingDelete(