From f25eb506f3a0f7c2024220f817d593a2ccb5fe5f Mon Sep 17 00:00:00 2001 From: Lubron Date: Tue, 25 Jun 2024 20:32:25 -0700 Subject: [PATCH] Cherry pick #312 and #310 (#316) * Trigger adc reconciliation if credentials is updated in ADC (#310) Signed-off-by: Lubron Zhan * Change the logger (#312) Signed-off-by: Lubron Zhan --------- Signed-off-by: Lubron Zhan --- .../akodeploymentconfig_controller.go | 5 +- ...kodeploymentconfig_controller_intg_test.go | 13 ++-- .../cluster/cluster_controller.go | 18 +++--- .../cluster_controller_addon_secret.go | 24 ++----- .../user/user_controller.go | 13 ++-- go.mod | 8 ++- go.sum | 6 ++ main.go | 63 ++++++++++++------- pkg/haprovider/haprovider.go | 2 +- pkg/utils/get_objects.go | 20 ++++++ 10 files changed, 104 insertions(+), 68 deletions(-) create mode 100644 pkg/utils/get_objects.go diff --git a/controllers/akodeploymentconfig/akodeploymentconfig_controller.go b/controllers/akodeploymentconfig/akodeploymentconfig_controller.go index 1d8004a2..607a33d6 100644 --- a/controllers/akodeploymentconfig/akodeploymentconfig_controller.go +++ b/controllers/akodeploymentconfig/akodeploymentconfig_controller.go @@ -182,9 +182,12 @@ func (r *AKODeploymentConfigReconciler) secretToAKODeploymentConfig(c client.Cli } var requests []ctrl.Request + // enqueue if credentials or certificate of akoo is updated for _, akoDeploymentConfig := range akoDeploymentConfigs.Items { if akoDeploymentConfig.Spec.CertificateAuthorityRef.Name == secret.Name && - akoDeploymentConfig.Spec.CertificateAuthorityRef.Namespace == secret.Namespace { + akoDeploymentConfig.Spec.CertificateAuthorityRef.Namespace == secret.Namespace || + akoDeploymentConfig.Spec.AdminCredentialRef.Name == secret.Name && + akoDeploymentConfig.Spec.AdminCredentialRef.Namespace == secret.Namespace { requests = append(requests, ctrl.Request{ NamespacedName: types.NamespacedName{ Namespace: akoDeploymentConfig.Namespace, diff --git a/controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go b/controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go index 9e31ae2a..50aa8b7c 100644 --- a/controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go +++ b/controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go @@ -26,7 +26,6 @@ import ( "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako" ako_operator "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako-operator" "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/test/builder" - "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/test/util" testutil "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/test/util" ) @@ -352,7 +351,7 @@ func intgTestAkoDeploymentConfigController() { err := os.Setenv(ako_operator.IsControlPlaneHAProvider, "False") Expect(err).ShouldNot(HaveOccurred()) }) - It("shouldn't wait AIS if controlplane and dataplane has the same CIDR", func() { + It("shouldn't wait for AIS if controlplane and dataplane has the same CIDR", func() { akoDeploymentConfig.Spec.ControlPlaneNetwork.CIDR = akoDeploymentConfig.Spec.DataNetwork.CIDR createObjects(akoDeploymentConfig, cluster, controllerCredentials, controllerCA) aviInfraSettingName = akoDeploymentConfig.Name + "-ais" @@ -369,7 +368,7 @@ func intgTestAkoDeploymentConfigController() { Expect(service.Annotations[akoov1alpha1.HAAVIInfraSettingAnnotationsKey]).To(BeEmpty()) }) - It("should wait AIS before adding annotation to service", func() { + It("should wait for AIS before adding annotation to service", func() { createObjects(akoDeploymentConfig, cluster, controllerCredentials, controllerCA) aviInfraSettingName = akoDeploymentConfig.Name + "-ais" ensureRuntimeObjectMatchExpectation(client.ObjectKey{ @@ -756,7 +755,7 @@ func intgTestAkoDeploymentConfigController() { }, &akoov1alpha1.AKODeploymentConfig{}, true) ensureRuntimeObjectMatchExpectation(client.ObjectKey{ - Name: util.CustomADCName, + Name: testutil.CustomADCName, }, &akoov1alpha1.AKODeploymentConfig{}, true) }) @@ -775,7 +774,7 @@ func intgTestAkoDeploymentConfigController() { ensureClusterAviLabelValueMatchExpectation(client.ObjectKey{ Name: cluster.Name, Namespace: cluster.Namespace, - }, akoov1alpha1.AviClusterLabel, util.CustomADCName, true) + }, akoov1alpha1.AviClusterLabel, testutil.CustomADCName, true) By("removing cluster's label") latestCluster := &clusterv1.Cluster{ @@ -814,7 +813,7 @@ func intgTestAkoDeploymentConfigController() { }, &akoov1alpha1.AKODeploymentConfig{}, true) ensureRuntimeObjectMatchExpectation(client.ObjectKey{ - Name: util.CustomADCName, + Name: testutil.CustomADCName, }, &akoov1alpha1.AKODeploymentConfig{}, true) }) @@ -833,7 +832,7 @@ func intgTestAkoDeploymentConfigController() { ensureClusterAviLabelValueMatchExpectation(client.ObjectKey{ Name: cluster.Name, Namespace: cluster.Namespace, - }, akoov1alpha1.AviClusterLabel, util.CustomADCName, true) + }, akoov1alpha1.AviClusterLabel, testutil.CustomADCName, true) By("removing cluster's label") latestCluster := &clusterv1.Cluster{ diff --git a/controllers/akodeploymentconfig/cluster/cluster_controller.go b/controllers/akodeploymentconfig/cluster/cluster_controller.go index 04cc10aa..74c30b1e 100644 --- a/controllers/akodeploymentconfig/cluster/cluster_controller.go +++ b/controllers/akodeploymentconfig/cluster/cluster_controller.go @@ -8,24 +8,24 @@ import ( "fmt" "time" - "k8s.io/client-go/kubernetes/scheme" - "github.com/go-logr/logr" "github.com/pkg/errors" - akoov1alpha1 "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1" corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - - "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako" - akoo "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako-operator" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/kubernetes/scheme" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/controllers/remote" "sigs.k8s.io/cluster-api/util/conditions" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + + akoov1alpha1 "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1" + "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako" + akoo "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako-operator" + "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/utils" ) const ( @@ -122,7 +122,7 @@ func (r *ClusterReconciler) cleanup( // - secret is -load-balancer-and-ingress-service-data-values secretName := r.akoAddonDataValueName() if akoo.IsClusterClassBasedCluster(obj) { - secretName = r.akoAddonSecretNameForClusterClass(obj) + secretName = utils.AKOAddonSecretNameForClusterClass(obj) } if err := remoteClient.Get(ctx, client.ObjectKey{ Name: secretName, diff --git a/controllers/akodeploymentconfig/cluster/cluster_controller_addon_secret.go b/controllers/akodeploymentconfig/cluster/cluster_controller_addon_secret.go index 4d38bf7d..6be16c69 100644 --- a/controllers/akodeploymentconfig/cluster/cluster_controller_addon_secret.go +++ b/controllers/akodeploymentconfig/cluster/cluster_controller_addon_secret.go @@ -87,7 +87,7 @@ func (r *ClusterReconciler) ReconcileAddonSecret( } secret := &corev1.Secret{} if err = r.Get(ctx, client.ObjectKey{ - Name: r.akoAddonSecretName(cluster), + Name: utils.AKOAddonSecretName(cluster), Namespace: cluster.Namespace, }, secret); err != nil { if apierrors.IsNotFound(err) { @@ -125,7 +125,7 @@ func (r *ClusterReconciler) ReconcileAddonSecretDelete( secret := &corev1.Secret{} if err := r.Get(ctx, client.ObjectKey{ - Name: r.akoAddonSecretName(cluster), + Name: utils.AKOAddonSecretName(cluster), Namespace: cluster.Namespace, }, secret); err != nil { if apierrors.IsNotFound(err) { @@ -153,18 +153,6 @@ func (r *ClusterReconciler) ReconcileAddonSecretDelete( return res, nil } -func (r *ClusterReconciler) aviUserSecretName(cluster *clusterv1.Cluster) string { - return cluster.Name + "-avi-credentials" -} - -func (r *ClusterReconciler) akoAddonSecretName(cluster *clusterv1.Cluster) string { - return cluster.Name + "-load-balancer-and-ingress-service-addon" -} - -func (r *ClusterReconciler) akoAddonSecretNameForClusterClass(cluster *clusterv1.Cluster) string { - return cluster.Name + "-load-balancer-and-ingress-service-data-values" -} - func (r *ClusterReconciler) createAKOAddonSecret(cluster *clusterv1.Cluster, obj *akoov1alpha1.AKODeploymentConfig, aviUsersecret *corev1.Secret) (*corev1.Secret, error) { secretStringData, err := AkoAddonSecretDataYaml(cluster, obj, aviUsersecret) if err != nil { @@ -172,7 +160,7 @@ func (r *ClusterReconciler) createAKOAddonSecret(cluster *clusterv1.Cluster, obj } secret := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ - Name: r.akoAddonSecretName(cluster), + Name: utils.AKOAddonSecretName(cluster), Namespace: cluster.Namespace, Annotations: map[string]string{ akoov1alpha1.TKGAddonAnnotationKey: "networking/load-balancer-and-ingress-service", @@ -223,7 +211,7 @@ func AkoAddonSecretDataYaml(cluster *clusterv1.Cluster, obj *akoov1alpha1.AKODep func (r *ClusterReconciler) getClusterAviUserSecret(cluster *clusterv1.Cluster, ctx context.Context) (*corev1.Secret, error) { secret := &corev1.Secret{} if err := r.Get(ctx, client.ObjectKey{ - Name: r.aviUserSecretName(cluster), + Name: utils.AVIUserSecretName(cluster), Namespace: cluster.Namespace, }, secret); err != nil { return secret, err @@ -264,7 +252,7 @@ func (r *ClusterReconciler) patchAkoPackageRefToClusterBootstrap(ctx context.Con expectedAKOClusterBootstrapPackage := &runv1alpha3.ClusterBootstrapPackage{ RefName: akoPackageRefName, ValuesFrom: &runv1alpha3.ValuesFrom{ - SecretRef: r.akoAddonSecretName(cluster), + SecretRef: utils.AKOAddonSecretName(cluster), }, } @@ -390,7 +378,7 @@ func ValidateClusterIpFamily(cluster *clusterv1.Cluster, adc *akoov1alpha1.AKODe // When enable avi as control plane ha, backend server shouldn't use secondary ip type // TODO:(chenlin) Remove validation after AKO supports configurable ip pool if isVIPProvider && adcIpFamily == IPv6IpFamily && clusterIpFamily == DualStackIPv4Primary { - return errors.New("When enabling avi as control plane HA, AKO with IP family V6 can not work together with ipv4 primary dual-stack cluster") + return errors.New("when enabling avi as control plane HA, AKO with IP family V6 can not work together with ipv4 primary dual-stack cluster") } return nil } diff --git a/controllers/akodeploymentconfig/user/user_controller.go b/controllers/akodeploymentconfig/user/user_controller.go index cc261cd5..12062153 100644 --- a/controllers/akodeploymentconfig/user/user_controller.go +++ b/controllers/akodeploymentconfig/user/user_controller.go @@ -8,9 +8,6 @@ import ( "github.com/go-logr/logr" "github.com/pkg/errors" - akoov1alpha1 "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1" - "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/aviclient" - "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/utils" "github.com/vmware/alb-sdk/go/models" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -22,6 +19,10 @@ import ( "sigs.k8s.io/cluster-api/util/conditions" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + + akoov1alpha1 "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1" + "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/aviclient" + "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/utils" ) // AkoUserReconciler reconcile avi user related resources @@ -431,12 +432,12 @@ func (r *AkoUserReconciler) deployManagementClusterSecret( Name: obj.Spec.AdminCredentialRef.Name, Namespace: obj.Spec.AdminCredentialRef.Namespace, }, adminCredential); err != nil { - log.Error(err, "Failed to find referenced AdminCredential Secret") + log.Error(err, "Failed to find referenced AdminCredential Secret", "secret namespace", obj.Spec.AdminCredentialRef.Namespace, "secret name", obj.Spec.AdminCredentialRef.Name) return err } secret := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ - Name: cluster.Name + "-avi-credentials", + Name: utils.AVIUserSecretName(cluster), Namespace: cluster.Namespace, }, Type: akoov1alpha1.AviClusterSecretType, @@ -448,7 +449,7 @@ func (r *AkoUserReconciler) deployManagementClusterSecret( } err := r.Client.Create(ctx, secret) if apierrors.IsAlreadyExists(err) { - log.Info("avi secret already exists, update avi-secret") + log.Info("avi secret already exists, update avi-secret", "secret namespace", secret.Namespace, "secret name", secret.Name) return r.Client.Update(ctx, secret) } return err diff --git a/go.mod b/go.mod index 687d322c..3abfd473 100644 --- a/go.mod +++ b/go.mod @@ -10,15 +10,16 @@ require ( github.com/onsi/gomega v1.33.1 github.com/pkg/errors v0.9.1 github.com/satori/go.uuid v1.2.0 + github.com/spf13/pflag v1.0.5 github.com/vmware-tanzu/tanzu-framework/apis/run v0.0.0-20221104044415-a462bbe793b9 github.com/vmware/alb-sdk v0.0.0-20240502042605-947bfcf176dd github.com/vmware/load-balancer-and-ingress-services-for-kubernetes v0.0.0-20231012053946-537d99c1eba2 - go.uber.org/zap v1.27.0 gopkg.in/yaml.v3 v3.0.1 k8s.io/api v0.29.6 k8s.io/apiextensions-apiserver v0.29.6 k8s.io/apimachinery v0.29.6 k8s.io/client-go v0.29.6 + k8s.io/component-base v0.29.6 k8s.io/klog/v2 v2.110.1 k8s.io/utils v0.0.0-20231127182322-b307cd553661 sigs.k8s.io/cluster-api v1.7.3 @@ -48,6 +49,7 @@ require ( github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.4.0 // indirect github.com/imdario/mergo v0.3.13 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/mailru/easyjson v0.7.7 // indirect @@ -60,8 +62,9 @@ require ( github.com/prometheus/client_model v0.5.0 // indirect github.com/prometheus/common v0.45.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/cobra v1.8.0 // indirect go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.24.0 // indirect golang.org/x/oauth2 v0.18.0 // indirect @@ -76,7 +79,6 @@ require ( gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect k8s.io/cluster-bootstrap v0.29.3 // indirect - k8s.io/component-base v0.29.6 // indirect k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect diff --git a/go.sum b/go.sum index 80bc9d74..c58be353 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,7 @@ github.com/coredns/caddy v1.1.0 h1:ezvsPrT/tA/7pYDBZxu0cT0VmWk75AfIaf6GSYCNMf0= github.com/coredns/caddy v1.1.0/go.mod h1:A6ntJQlAWuQfFlsd9hvigKbo2WS0VUs2l1e2F+BawD4= github.com/coredns/corefile-migration v1.0.21 h1:W/DCETrHDiFo0Wj03EyMkaQ9fwsmSgqTCQDHpceaSsE= github.com/coredns/corefile-migration v1.0.21/go.mod h1:XnhgULOEouimnzgn0t4WPuFDN2/PJQcTxdWKC5eXNGE= +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -96,6 +97,8 @@ github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4 github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= @@ -155,12 +158,15 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stoewer/go-strcase v1.2.0 h1:Z2iHWqGXH00XYgqDmNgQbIBxf3wrNq0F3feEy0ainaU= diff --git a/main.go b/main.go index fc787879..c1f49921 100644 --- a/main.go +++ b/main.go @@ -9,39 +9,39 @@ import ( "net/http/pprof" "os" + "github.com/spf13/pflag" + runv1alpha3 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha3" akov1beta1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/apis/ako/v1beta1" - "go.uber.org/zap/zapcore" - - akoov1alpha1 "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" + cliflag "k8s.io/component-base/cli/flag" + "k8s.io/component-base/logs" + logsv1 "k8s.io/component-base/logs/api/v1" + "k8s.io/klog/v2" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/config" + "sigs.k8s.io/controller-runtime/pkg/manager" + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" + akoov1alpha1 "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1" "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/controllers" - ako_operator "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako-operator" - runv1alpha3 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha3" - "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/zap" - "sigs.k8s.io/controller-runtime/pkg/manager" - metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" ) var ( - scheme = runtime.NewScheme() - setupLog = log.Log + scheme = runtime.NewScheme() + setupLog = ctrl.Log.WithName("setup") + logOptions = logs.NewOptions() + metricsAddr string + enableLeaderElection bool + profilerAddress string ) func initLog() { - f := func(ecfg *zapcore.EncoderConfig) { - ecfg.EncodeTime = zapcore.ISO8601TimeEncoder - } - ctrl.SetLogger(zap.New(zap.UseDevMode(true), - zap.ConsoleEncoder(zap.EncoderConfigOption(f)))) + ctrl.SetLogger(klog.Background()) } func init() { @@ -54,14 +54,31 @@ func init() { _ = runv1alpha3.AddToScheme(scheme) } +// InitFlags initializes the flags. +func InitFlags(fs *pflag.FlagSet) { + logsv1.AddFlags(logOptions, fs) + + fs.StringVar(&metricsAddr, "metrics-addr", "localhost:8080", "The address the metric endpoint binds to.") + fs.BoolVar(&enableLeaderElection, "enable-leader-election", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.") + fs.StringVar(&profilerAddress, "profiler-addr", "", "Bind address to expose the pprof profiler") +} + func main() { - var metricsAddr string - var enableLeaderElection bool - var profilerAddress string - flag.StringVar(&metricsAddr, "metrics-addr", "localhost:8080", "The address the metric endpoint binds to.") - flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.") - flag.StringVar(&profilerAddress, "profiler-addr", "", "Bind address to expose the pprof profiler") - flag.Parse() + + InitFlags(pflag.CommandLine) + pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc) + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) + // Set log level 2 as default. + if err := pflag.CommandLine.Set("v", "2"); err != nil { + setupLog.Error(err, "Failed to set default log level") + os.Exit(1) + } + pflag.Parse() + + if err := logsv1.ValidateAndApply(logOptions, nil); err != nil { + setupLog.Error(err, "Unable to start manager") + os.Exit(1) + } if profilerAddress != "" { setupLog.Info( diff --git a/pkg/haprovider/haprovider.go b/pkg/haprovider/haprovider.go index b7c5e31e..0f31b0cd 100644 --- a/pkg/haprovider/haprovider.go +++ b/pkg/haprovider/haprovider.go @@ -67,7 +67,7 @@ func (r *HAProvider) CreateOrUpdateHAService(ctx context.Context, cluster *clust Namespace: cluster.Namespace, }, service); err != nil { if apierrors.IsNotFound(err) { - r.log.Info(serviceName + "service doesn't exist, start creating it...") + r.log.Info(serviceName + " service doesn't exist, start creating it...") service, err = r.createService(ctx, cluster) if err != nil { return err diff --git a/pkg/utils/get_objects.go b/pkg/utils/get_objects.go new file mode 100644 index 00000000..d798da4b --- /dev/null +++ b/pkg/utils/get_objects.go @@ -0,0 +1,20 @@ +// Copyright 2024 VMware, Inc. +// SPDX-License-Identifier: Apache-2.0 + +package utils + +import ( + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" +) + +func AVIUserSecretName(cluster *clusterv1.Cluster) string { + return cluster.Name + "-avi-credentials" +} + +func AKOAddonSecretName(cluster *clusterv1.Cluster) string { + return cluster.Name + "-load-balancer-and-ingress-service-addon" +} + +func AKOAddonSecretNameForClusterClass(cluster *clusterv1.Cluster) string { + return cluster.Name + "-load-balancer-and-ingress-service-data-values" +}