Skip to content

Commit b4e6d64

Browse files
Fix akodeploymentconfig cluster selector didn't select the right clusters issue (#91)
* Fix akodeploymentconfig cluster selector didn't select the right clusters issue * Remove duplicated add-on secrete deletion code Signed-off-by: Xudong Liu <xudongl@vmware.com>
1 parent b73d355 commit b4e6d64

15 files changed

+216
-255
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ PUBLISH?=publish
1313
BUILD_VERSION ?= $(shell git describe --always --match "v*" | sed 's/v//')
1414

1515
# TKG Version
16-
TKG_VERSION ?= v1.6.0+vmware.11
16+
TKG_VERSION ?= v1.6.0+vmware.12
1717

1818
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
1919
ifeq (,$(shell go env GOBIN))

api/v1alpha1/constants.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const (
3737

3838
AVI_VERSION = "20.1.3"
3939
AviClusterLabel = "networking.tkg.tanzu.vmware.com/avi"
40-
AviClusterSelectedLabel = "networking.tkg.tanzu.vmware.com/avi-skip-default-adc"
4140
AviClusterDeleteConfigLabel = "networking.tkg.tanzu.vmware.com/avi-config-delete"
4241
AviClusterSecretType = "avi.cluster.x-k8s.io/secret"
4342
AviNamespace = "avi-system"

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controllers/akodeploymentconfig/akodeploymentconfig_controller_avi_phase.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727

2828
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
2929
akov1alpha1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/apis/ako/v1alpha1"
30+
31+
ako_operator "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/ako-operator"
3032
)
3133

3234
func getAviCAFromADC(c client.Client, ctx context.Context,
@@ -291,7 +293,7 @@ func (r *AKODeploymentConfigReconciler) reconcileAviInfraSettingDelete(
291293
log.Info("Start reconciling AVIInfraSetting Delete")
292294

293295
// Get the list of clusters managed by the AKODeploymentConfig
294-
clusters, err := phases.ListAkoDeplymentConfigDeployedClusters(ctx, r.Client, adc)
296+
clusters, err := ako_operator.ListAkoDeploymentConfigSelectClusters(ctx, r.Client, log, adc)
295297
if err != nil {
296298
log.Error(err, "Fail to list clusters deployed by current AKODeploymentConfig")
297299
return res, err

controllers/akodeploymentconfig/akodeploymentconfig_controller_cluster_phase.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func (r *AKODeploymentConfigReconciler) reconcileClusters(
3737

3838
return phases.ReconcileClustersPhases(ctx, r.Client, log, obj,
3939
[]phases.ReconcileClusterPhase{
40-
r.applyClusterLabel,
4140
r.addClusterFinalizer,
4241
r.ClusterReconciler.ReconcileAddonSecret,
4342
},
@@ -63,7 +62,6 @@ func (r *AKODeploymentConfigReconciler) reconcileClustersDelete(
6362
// cluster is in normal state, remove the label and finalizer to
6463
// stop managing it
6564
[]phases.ReconcileClusterPhase{
66-
r.removeClusterLabel,
6765
r.removeClusterFinalizer,
6866
r.ClusterReconciler.ReconcileAddonSecretDelete,
6967
},
@@ -74,49 +72,6 @@ func (r *AKODeploymentConfigReconciler) reconcileClustersDelete(
7472
)
7573
}
7674

77-
// applyClusterLabel is a reconcileClusterPhase. It applies the AVI label to a
78-
// Cluster
79-
func (r *AKODeploymentConfigReconciler) applyClusterLabel(
80-
_ context.Context,
81-
log logr.Logger,
82-
cluster *clusterv1.Cluster,
83-
obj *akoov1alpha1.AKODeploymentConfig,
84-
) (ctrl.Result, error) {
85-
if cluster.Labels == nil {
86-
cluster.Labels = make(map[string]string)
87-
}
88-
if _, exists := cluster.Labels[akoov1alpha1.AviClusterLabel]; !exists {
89-
log.Info("Adding label to cluster", "label", akoov1alpha1.AviClusterLabel)
90-
} else {
91-
log.Info("Label already applied to cluster", "label", akoov1alpha1.AviClusterLabel)
92-
}
93-
// When the cluster is selected by this AKODeploymentConfig, but its is not install-ako-for-all,
94-
// this indicates that the cluster is managed by a customized ADC and the default ADC shall be skipped.
95-
if obj.Name != akoov1alpha1.WorkloadClusterAkoDeploymentConfig {
96-
cluster.Labels[akoov1alpha1.AviClusterSelectedLabel] = ""
97-
}
98-
// Always set avi label on managed cluster
99-
cluster.Labels[akoov1alpha1.AviClusterLabel] = ""
100-
return ctrl.Result{}, nil
101-
}
102-
103-
// removeClusterLabel is a reconcileClusterPhase. It removes the AVI label from a
104-
// Cluster
105-
func (r *AKODeploymentConfigReconciler) removeClusterLabel(
106-
_ context.Context,
107-
log logr.Logger,
108-
cluster *clusterv1.Cluster,
109-
_ *akoov1alpha1.AKODeploymentConfig,
110-
) (ctrl.Result, error) {
111-
if _, exists := cluster.Labels[akoov1alpha1.AviClusterLabel]; exists {
112-
log.Info("Removing label from cluster", "label", akoov1alpha1.AviClusterLabel)
113-
}
114-
// Always deletes avi label on managed cluster
115-
delete(cluster.Labels, akoov1alpha1.AviClusterLabel)
116-
delete(cluster.Labels, akoov1alpha1.AviClusterSelectedLabel)
117-
return ctrl.Result{}, nil
118-
}
119-
12075
// addClusterFinalizer is a reconcileClusterPhase. It adds the AVI
12176
// finalizer to a Cluster.
12277
func (r *AKODeploymentConfigReconciler) addClusterFinalizer(

controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ func intgTestAkoDeploymentConfigController() {
759759
ensureClusterAviLabelMatchExpectation(client.ObjectKey{
760760
Name: cluster.Name,
761761
Namespace: cluster.Namespace,
762-
}, akoov1alpha1.AviClusterSelectedLabel, true)
762+
}, akoov1alpha1.AviClusterLabel, true)
763763
})
764764

765765
When("no longer selected by a customized ADC", func() {
@@ -775,11 +775,11 @@ func intgTestAkoDeploymentConfigController() {
775775
}, "test", false)
776776
})
777777

778-
It("should drop the skip-default-adc label (AviClusterSelectedLabel)", func() {
778+
It("should drop the AviClusterLabel)", func() {
779779
ensureClusterAviLabelMatchExpectation(client.ObjectKey{
780780
Name: cluster.Name,
781781
Namespace: cluster.Namespace,
782-
}, akoov1alpha1.AviClusterSelectedLabel, false)
782+
}, akoov1alpha1.AviClusterLabel, false)
783783
})
784784
})
785785
})

controllers/akodeploymentconfig/phases/phases.go

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/go-logr/logr"
1010

1111
"github.com/pkg/errors"
12-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1312
kerrors "k8s.io/apimachinery/pkg/util/errors"
1413
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1514
"sigs.k8s.io/cluster-api/util"
@@ -18,7 +17,7 @@ import (
1817
"sigs.k8s.io/controller-runtime/pkg/client"
1918

2019
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
21-
handlers "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/handlers"
20+
ako_operator "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/ako-operator"
2221
)
2322

2423
// ReconcilePhase defines a function that reconciles one aspect of
@@ -67,7 +66,7 @@ func ReconcileClustersPhases(
6766
res := ctrl.Result{}
6867

6968
// Get the list of clusters managed by the AKODeploymentConfig
70-
clusters, err := ListAkoDeplymentConfigDeployedClusters(ctx, client, obj)
69+
clusters, err := ako_operator.ListAkoDeploymentConfigSelectClusters(ctx, client, log, obj)
7170
if err != nil {
7271
log.Error(err, "Fail to list clusters deployed by current AKODeploymentConfig")
7372
return res, err
@@ -92,6 +91,9 @@ func ReconcileClustersPhases(
9291
cluster.GroupVersionKind(), cluster.Namespace+"/"+cluster.Name)
9392
}
9493

94+
// update cluster avi label before run any phase functions
95+
ako_operator.ApplyClusterLabel(log, &cluster, obj)
96+
9597
phases := normalPhases
9698
if !cluster.GetDeletionTimestamp().IsZero() {
9799
phases = deletePhases
@@ -127,40 +129,3 @@ func ReconcileClustersPhases(
127129

128130
return res, kerrors.NewAggregate(allErrs)
129131
}
130-
131-
// ListAkoDeplymentConfigDeployedClusters list all clusters enabled current akodeploymentconfig
132-
func ListAkoDeplymentConfigDeployedClusters(ctx context.Context, kclient client.Client, obj *akoov1alpha1.AKODeploymentConfig) (*clusterv1.ClusterList, error) {
133-
selector, err := metav1.LabelSelectorAsSelector(&obj.Spec.ClusterSelector)
134-
if err != nil {
135-
return nil, err
136-
}
137-
listOptions := []client.ListOption{
138-
client.MatchingLabelsSelector{Selector: selector},
139-
}
140-
var clusters clusterv1.ClusterList
141-
if err := kclient.List(ctx, &clusters, listOptions...); err != nil {
142-
return nil, err
143-
}
144-
145-
var newItems []clusterv1.Cluster
146-
for _, c := range clusters.Items {
147-
if !handlers.SkipCluster(&c) {
148-
_, selected := c.Labels[akoov1alpha1.AviClusterSelectedLabel]
149-
// when cluster selected by non-default AKODeploymentConfig,
150-
// skip default select all AKODeploymentConfig
151-
if selector.Empty() && selected {
152-
continue
153-
}
154-
// management cluster can't be selected by other AKODeploymentConfig
155-
// instead of management cluster AKODeploymentConfig
156-
if c.Namespace == akoov1alpha1.TKGSystemNamespace &&
157-
obj.Name != akoov1alpha1.ManagementClusterAkoDeploymentConfig {
158-
continue
159-
}
160-
newItems = append(newItems, c)
161-
}
162-
}
163-
clusters.Items = newItems
164-
165-
return &clusters, nil
166-
}

controllers/akodeploymentconfig/phases/phases_unit_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
package phases
55

66
import (
7+
ako_operator "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/ako-operator"
8+
9+
"github.com/go-logr/logr"
710
. "github.com/onsi/ginkgo"
811
. "github.com/onsi/gomega"
912
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
1013
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/builder"
1114
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1215
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
16+
ctrl "sigs.k8s.io/controller-runtime"
1317
)
1418

1519
func ReconcilePhaseUnitTest() {
1620
var (
1721
err error
22+
log logr.Logger
1823
ctx *builder.IntegrationTestContext
1924
akoDeploymentConfig *akoov1alpha1.AKODeploymentConfig
2025
)
@@ -47,6 +52,7 @@ func ReconcilePhaseUnitTest() {
4752
},
4853
}
4954
ctx = suite.NewIntegrationTestContext()
55+
log = ctrl.Log.WithName("controllers").WithName("AKODeploymentConfig")
5056
})
5157

5258
Context("Should be able to list all workload clusters", func() {
@@ -59,7 +65,7 @@ func ReconcilePhaseUnitTest() {
5965
Name: "test-cluster",
6066
Namespace: "default",
6167
Labels: map[string]string{
62-
akoov1alpha1.AviClusterLabel: "",
68+
akoov1alpha1.AviClusterLabel: "test-ako-deployment-config",
6369
"test": "test",
6470
},
6571
},
@@ -75,7 +81,7 @@ func ReconcilePhaseUnitTest() {
7581
})
7682

7783
It("list all selected workload clusters", func() {
78-
clusterList, err := ListAkoDeplymentConfigDeployedClusters(ctx.Context, ctx.Client, akoDeploymentConfig)
84+
clusterList, err := ako_operator.ListAkoDeploymentConfigSelectClusters(ctx.Context, ctx.Client, log, akoDeploymentConfig)
7985
Expect(err).ShouldNot(HaveOccurred())
8086
Expect(len(clusterList.Items)).To(Equal(1))
8187
Expect(clusterList.Items[0].Name).To(Equal("test-cluster"))

controllers/cluster/cluster_controller.go

Lines changed: 13 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
1919
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/haprovider"
2020
apierrors "k8s.io/apimachinery/pkg/api/errors"
21-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22-
"k8s.io/apimachinery/pkg/labels"
2321
"k8s.io/apimachinery/pkg/runtime"
2422
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2523
ctrl "sigs.k8s.io/controller-runtime"
@@ -76,6 +74,8 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
7674
}
7775
}()
7876

77+
log = log.WithValues("Cluster", cluster.Namespace+"/"+cluster.Name)
78+
7979
if ako_operator.IsHAProvider() {
8080
log.Info("AVI is control plane HA provider")
8181
r.Haprovider = haprovider.NewProvider(r.Client, r.Log)
@@ -88,89 +88,22 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
8888
}
8989
}
9090

91-
log = log.WithValues("Cluster", cluster.Namespace+"/"+cluster.Name)
92-
93-
if _, exist := cluster.Labels[akoov1alpha1.AviClusterLabel]; !exist {
94-
log.Info("Cluster doesn't have AVI enabled, skip Cluster reconciling")
95-
return res, nil
96-
}
97-
98-
log.Info("Cluster has AVI enabled, start Cluster reconciling")
99-
// Getting all akodeploymentconfigs
100-
var akoDeploymentConfigs akoov1alpha1.AKODeploymentConfigList
101-
if err := r.Client.List(ctx, &akoDeploymentConfigs); err != nil {
91+
akoDeploymentConfig, err := ako_operator.GetAKODeploymentConfigForCluster(ctx, r.Client, log, cluster)
92+
if err != nil {
93+
log.Error(err, "failed to get cluster matched akodeploymentconfig")
10294
return res, err
10395
}
10496

105-
// Matches current cluster with all the akoDeploymentConfigs
106-
clusterLabels := cluster.GetLabels()
107-
matchedAkoDeploymentConfigs := make([]akoov1alpha1.AKODeploymentConfig, 0)
108-
for _, akoDeploymentConfig := range akoDeploymentConfigs.Items {
109-
if selector, err := metav1.LabelSelectorAsSelector(&akoDeploymentConfig.Spec.ClusterSelector); err != nil {
110-
log.Error(err, "Failed to convert label sector to selector when matching ", "cluster", cluster.Name, " with ", akoDeploymentConfig.Name)
111-
} else if selector.Matches(labels.Set(clusterLabels)) {
112-
log.Info("Cluster ", cluster.Name, " is selected by", "Akodeploymentconfig", (akoDeploymentConfig.Namespace + "/" + akoDeploymentConfig.Name))
113-
matchedAkoDeploymentConfigs = append(matchedAkoDeploymentConfigs, akoDeploymentConfig)
114-
}
115-
}
116-
117-
// When the cluster is not selected or only selected by the default ADC, a.k.a. install-ako-for-all
118-
// We drop its skip-default-adc label, allowing cluster to use default ADC's config.
119-
// It happens when a cluster is no longer selected by a customized ADC
120-
if len(matchedAkoDeploymentConfigs) == 0 ||
121-
(len(matchedAkoDeploymentConfigs) == 1 && matchedAkoDeploymentConfigs[0].Name == akoov1alpha1.WorkloadClusterAkoDeploymentConfig) {
122-
delete(cluster.Labels, akoov1alpha1.AviClusterSelectedLabel)
123-
}
124-
125-
// If the cluster is selected by some AKODeploymentConfig objects, skip removing the finalizer
126-
if len(matchedAkoDeploymentConfigs) > 0 {
97+
if akoDeploymentConfig == nil {
98+
// Removing finalizer if current cluster can't be selected by any akoDeploymentConfig
99+
log.Info("Not find cluster matched akodeploymentconfig, removing finalizer", "finalizer", akoov1alpha1.ClusterFinalizer)
100+
ctrlutil.RemoveFinalizer(cluster, akoov1alpha1.ClusterFinalizer)
101+
// Removing avi label after deleting all the resources
102+
delete(cluster.Labels, akoov1alpha1.AviClusterLabel)
103+
log.Info("Cluster doesn't have AVI enabled, skip Cluster reconciling")
127104
return res, nil
128105
}
129106

130-
// Removing finalizer if current cluster can't be selected by any akoDeploymentConfig
131-
log.Info("Removing finalizer", "finalizer", akoov1alpha1.ClusterFinalizer)
132-
ctrlutil.RemoveFinalizer(cluster, akoov1alpha1.ClusterFinalizer)
133-
134-
// Removing add on secret and its associated resources for a AKO
135-
if _, err := r.deleteAddonSecret(ctx, log, cluster); err != nil {
136-
log.Error(err, "Failed to remove secret", "secret", cluster.Name)
137-
return res, err
138-
}
139-
140-
// Removing avi label after deleting all the resources
141-
delete(cluster.Labels, akoov1alpha1.AviClusterLabel)
142-
107+
log.Info("Cluster has AVI enabled")
143108
return res, nil
144109
}
145-
146-
// deleteAddonSecret delete cluster related add on secret
147-
func (r *ClusterReconciler) deleteAddonSecret(
148-
ctx context.Context,
149-
log logr.Logger,
150-
cluster *clusterv1.Cluster,
151-
) (ctrl.Result, error) {
152-
log.Info("Starts reconciling add on secret deletion")
153-
res := ctrl.Result{}
154-
secret := &corev1.Secret{}
155-
if err := r.Get(ctx, client.ObjectKey{
156-
Name: r.akoAddonSecretName(cluster),
157-
Namespace: cluster.Namespace,
158-
}, secret); err != nil {
159-
if apierrors.IsNotFound(err) {
160-
log.V(3).Info("add on secret is already deleted")
161-
return res, nil
162-
}
163-
log.Error(err, "Failed to get add on secret, requeue")
164-
return res, err
165-
}
166-
167-
if err := r.Delete(ctx, secret); err != nil {
168-
log.Error(err, "Failed to delete add on secret, requeue")
169-
return res, err
170-
}
171-
return res, nil
172-
}
173-
174-
func (r *ClusterReconciler) akoAddonSecretName(cluster *clusterv1.Cluster) string {
175-
return cluster.Name + "-load-balancer-and-ingress-service-addon"
176-
}

0 commit comments

Comments
 (0)