Skip to content

Commit

Permalink
Add 'tenant-' prefix to tenant name in CCM, if not provided (#75)
Browse files Browse the repository at this point in the history
Signed-off-by: Waleed Malik <[email protected]>
  • Loading branch information
ahmedwaleedmalik authored Nov 7, 2024
1 parent 5ede47c commit b9cd5a3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
6 changes: 6 additions & 0 deletions cmd/ccm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -113,6 +114,11 @@ func main() {
opts.BindFlags(flag.CommandLine)
flag.Parse()

// If clusterName is not prefixed with "tenant-" then prefix it
if !strings.HasPrefix(clusterName, "tenant-") {
clusterName = "tenant-" + clusterName
}

if enableGatewayAPI {
utilruntime.Must(gwapiv1.Install(scheme))
}
Expand Down
4 changes: 2 additions & 2 deletions hack/ci/e2e/config/ccm/tenant-1/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
resources:
- ../base
namespace: cluster-tenant1
namespace: tenant-primary
patches:
- patch: |-
- op: add
path: "/spec/template/spec/containers/0/args/-"
value: "--cluster-name=cluster-tenant1"
value: "--cluster-name=tenant-primary"
target:
kind: Deployment
4 changes: 2 additions & 2 deletions hack/ci/e2e/config/ccm/tenant-2/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
resources:
- ../base
namespace: cluster-tenant2
namespace: tenant-secondary
patches:
- patch: |-
- op: add
path: "/spec/template/spec/containers/0/args/-"
value: "--cluster-name=cluster-tenant2"
value: "--cluster-name=tenant-secondary"
target:
kind: Deployment
14 changes: 9 additions & 5 deletions hack/ci/e2e/deploy-kubelb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ kind load docker-image --name=kubelb kubermatic.io/ccm:e2e
echodate "Install kubelb"
make install
make e2e-deploy-kubelb
for i in 1 2; do
echodate "Install ccm for cluster-tenant${i}"
kubectl create ns "cluster-tenant${i}"
kubectl label ns "cluster-tenant${i}" kubelb.k8c.io/managed-by=kubelb
kubectl config set-context $(kubectl config current-context) --namespace="cluster-tenant${i}"

tenants=("tenant-primary" "tenant-secondary")
i=1
for tenant in "${tenants[@]}"; do
echodate "Install ccm for $tenant"
kubectl create ns "$tenant"
kubectl label ns "$tenant" kubelb.k8c.io/managed-by=kubelb
kubectl config set-context $(kubectl config current-context) --namespace="$tenant"
kubectl create secret generic kubelb-cluster --from-file=kubelb="${TMPDIR}"/kubelb.kubeconfig --from-file=tenant="${TMPDIR}/tenant${i}.kubeconfig"
make "e2e-deploy-ccm-tenant-${i}"
i=$((i + 1))
done
32 changes: 16 additions & 16 deletions hack/ci/e2e/tests/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func TestSimpleService(t *testing.T) {
expectHTTPGet(testServiceURL, "nginx/1.24.0")

lb := v1alpha1.LoadBalancer{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant1", Name: string(svc.UID)}, &lb)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-primary", Name: string(svc.UID)}, &lb)).To(Succeed())
Expect(len(lb.Spec.Endpoints)).To(Equal(1))
Expect(len(lb.Spec.Endpoints[0].Ports)).To(Equal(1))

Expect(lb.Spec.Endpoints[0].AddressesReference).ToNot(BeNil())
// Retrieve the endpoint addresses and make sure they are correct
addresses := v1alpha1.Addresses{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant1", Name: string(*&lb.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-primary", Name: string(*&lb.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(len(addresses.Spec.Addresses)).To(Equal(1))
}

Expand All @@ -95,14 +95,14 @@ func TestMultiNodeService(t *testing.T) {
expectHTTPGet(testServiceURL, "nginx/1.24.0")

lb := v1alpha1.LoadBalancer{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(svc.UID)}, &lb)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(svc.UID)}, &lb)).To(Succeed())
Expect(len(lb.Spec.Endpoints)).To(Equal(1))
Expect(len(lb.Spec.Endpoints[0].Ports)).To(Equal(1))

Expect(lb.Spec.Endpoints[0].AddressesReference).ToNot(BeNil())
// Retrieve the endpoint addresses and make sure they are correct
addresses := v1alpha1.Addresses{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(*&lb.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(*&lb.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(len(addresses.Spec.Addresses)).To(Equal(4))
}

Expand All @@ -123,14 +123,14 @@ func TestMultiPortService(t *testing.T) {
expectHTTPGet(fmt.Sprintf("%v:9901", testServiceURL), "envoy")

lb := v1alpha1.LoadBalancer{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant1", Name: string(svc.UID)}, &lb)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-primary", Name: string(svc.UID)}, &lb)).To(Succeed())
Expect(len(lb.Spec.Endpoints)).To(Equal(1))
Expect(len(lb.Spec.Endpoints[0].Ports)).To(Equal(2))

Expect(lb.Spec.Endpoints[0].AddressesReference).ToNot(BeNil())
// Retrieve the endpoint addresses and make sure they are correct
addresses := v1alpha1.Addresses{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(*&lb.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(*&lb.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(len(addresses.Spec.Addresses)).To(Equal(4))
}

Expand All @@ -151,14 +151,14 @@ func TestMultiPortMultiNodeService(t *testing.T) {
expectHTTPGet(fmt.Sprintf("%v:9901", testServiceURL), "envoy")

lb := v1alpha1.LoadBalancer{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(svc.UID)}, &lb)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(svc.UID)}, &lb)).To(Succeed())
Expect(len(lb.Spec.Endpoints)).To(Equal(1))
Expect(len(lb.Spec.Endpoints[0].Ports)).To(Equal(2))

Expect(lb.Spec.Endpoints[0].AddressesReference).ToNot(BeNil())
// Retrieve the endpoint addresses and make sure they are correct
addresses := v1alpha1.Addresses{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(*&lb.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(*&lb.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(len(addresses.Spec.Addresses)).To(Equal(4))
}

Expand Down Expand Up @@ -190,25 +190,25 @@ func TestDuplicateService(t *testing.T) {
expectHTTPGet(fmt.Sprintf("%v:8080", testServiceURL2), "envoy")

lb1 := v1alpha1.LoadBalancer{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant1", Name: string(svc1.UID)}, &lb1)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-primary", Name: string(svc1.UID)}, &lb1)).To(Succeed())
Expect(len(lb1.Spec.Endpoints)).To(Equal(1))
Expect(len(lb1.Spec.Endpoints[0].Ports)).To(Equal(2))

Expect(lb1.Spec.Endpoints[0].AddressesReference).ToNot(BeNil())
// Retrieve the endpoint addresses and make sure they are correct
addresses := v1alpha1.Addresses{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant1", Name: string(*&lb1.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-primary", Name: string(*&lb1.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(len(addresses.Spec.Addresses)).To(Equal(1))

lb2 := v1alpha1.LoadBalancer{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(svc2.UID)}, &lb2)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(svc2.UID)}, &lb2)).To(Succeed())
Expect(len(lb2.Spec.Endpoints)).To(Equal(1))
Expect(len(lb2.Spec.Endpoints[0].Ports)).To(Equal(2))

Expect(lb2.Spec.Endpoints[0].AddressesReference).ToNot(BeNil())
// Retrieve the endpoint addresses and make sure they are correct
addresses = v1alpha1.Addresses{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(*&lb2.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(*&lb2.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(len(addresses.Spec.Addresses)).To(Equal(4))
}

Expand Down Expand Up @@ -252,24 +252,24 @@ func TestMultipleServices(t *testing.T) {
wg.Wait()

lb1 := v1alpha1.LoadBalancer{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(svc1.UID)}, &lb1)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(svc1.UID)}, &lb1)).To(Succeed())
Expect(len(lb1.Spec.Endpoints)).To(Equal(1))
Expect(len(lb1.Spec.Endpoints[0].Ports)).To(Equal(2))

Expect(lb1.Spec.Endpoints[0].AddressesReference).ToNot(BeNil())
// Retrieve the endpoint addresses and make sure they are correct
addresses := v1alpha1.Addresses{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(*&lb1.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(*&lb1.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(len(addresses.Spec.Addresses)).To(Equal(4))

lb2 := v1alpha1.LoadBalancer{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(svc2.UID)}, &lb2)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(svc2.UID)}, &lb2)).To(Succeed())
Expect(len(lb2.Spec.Endpoints)).To(Equal(1))
Expect(len(lb2.Spec.Endpoints[0].Ports)).To(Equal(2))

Expect(lb2.Spec.Endpoints[0].AddressesReference).ToNot(BeNil())
// Retrieve the endpoint addresses and make sure they are correct
addresses = v1alpha1.Addresses{}
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "cluster-tenant2", Name: string(*&lb2.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(kubelbK8sClient.Get(ctx, types.NamespacedName{Namespace: "tenant-secondary", Name: string(*&lb2.Spec.Endpoints[0].AddressesReference.Name)}, &addresses)).To(Succeed())
Expect(len(addresses.Spec.Addresses)).To(Equal(4))
}

0 comments on commit b9cd5a3

Please sign in to comment.