Skip to content

Commit

Permalink
Use mock gke client in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-demicev committed Jan 3, 2024
1 parent f3440f7 commit 7697bf6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 77 deletions.
124 changes: 57 additions & 67 deletions controller/gke-cluster-config-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Handler struct {
secrets wranglerv1.SecretClient
secretsCache wranglerv1.SecretCache
gkeClient services.GKEClusterService
gkeClientCtx context.Context
}

func Register(
Expand All @@ -90,10 +91,28 @@ func (h *Handler) OnGkeConfigChanged(_ string, config *gkev1.GKEClusterConfig) (
if config == nil {
return nil, nil
}

if config.DeletionTimestamp != nil {
return nil, nil
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

h.gkeClientCtx = ctx

cred, err := GetSecret(h.gkeClientCtx, h.secrets, &config.Spec)
if err != nil {
return config, err
}

gkeClient, err := gke.GetGKEClusterClient(h.gkeClientCtx, cred)
if err != nil {
return config, err
}

h.gkeClient = gkeClient

switch config.Status.Phase {
case gkeConfigImportingPhase:
return h.importCluster(config)
Expand Down Expand Up @@ -157,10 +176,7 @@ func (h *Handler) recordError(onChange func(key string, config *gkev1.GKECluster
// importCluster returns an active cluster spec containing the given config's clusterName and region/zone
// and creates a Secret containing the cluster's CA and endpoint retrieved from the cluster object.
func (h *Handler) importCluster(config *gkev1.GKEClusterConfig) (*gkev1.GKEClusterConfig, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cluster, err := GetCluster(ctx, h.secrets, &config.Spec)
cluster, err := gke.GetCluster(h.gkeClientCtx, h.gkeClient, &config.Spec)
if err != nil {
return config, err
}
Expand All @@ -173,30 +189,35 @@ func (h *Handler) importCluster(config *gkev1.GKEClusterConfig) (*gkev1.GKEClust
}

func (h *Handler) OnGkeConfigRemoved(_ string, config *gkev1.GKEClusterConfig) (*gkev1.GKEClusterConfig, error) {
if config.Spec.Imported {
logrus.Infof("cluster [%s] is imported, will not delete GKE cluster", config.Name)
return config, nil
}
if config.Status.Phase == gkeConfigNotCreatedPhase {
// The most likely context here is that the cluster already existed in GKE, so we shouldn't delete it
logrus.Warnf("cluster [%s] never advanced to creating status, will not delete GKE cluster", config.Name)
return config, nil
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cred, err := GetSecret(ctx, h.secrets, &config.Spec)
h.gkeClientCtx = ctx

cred, err := GetSecret(h.gkeClientCtx, h.secrets, &config.Spec)
if err != nil {
return config, err
}
gkeClient, err := gke.GetGKEClusterClient(ctx, cred)

gkeClient, err := gke.GetGKEClusterClient(h.gkeClientCtx, cred)
if err != nil {
return config, err
}

h.gkeClient = gkeClient

if config.Spec.Imported {
logrus.Infof("cluster [%s] is imported, will not delete GKE cluster", config.Name)
return config, nil
}
if config.Status.Phase == gkeConfigNotCreatedPhase {
// The most likely context here is that the cluster already existed in GKE, so we shouldn't delete it
logrus.Warnf("cluster [%s] never advanced to creating status, will not delete GKE cluster", config.Name)
return config, nil
}

logrus.Infof("removing cluster %v from project %v, region/zone %v", config.Spec.ClusterName, config.Spec.ProjectID, gke.Location(config.Spec.Region, config.Spec.Zone))
if err := gke.RemoveCluster(ctx, gkeClient, config); err != nil {
if err := gke.RemoveCluster(h.gkeClientCtx, h.gkeClient, config); err != nil {
logrus.Debugf("error deleting cluster %s: %v", config.Spec.ClusterName, err)
return config, err
}
Expand All @@ -212,33 +233,17 @@ func (h *Handler) create(config *gkev1.GKEClusterConfig) (*gkev1.GKEClusterConfi
return h.gkeCC.UpdateStatus(config)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cred, err := GetSecret(ctx, h.secrets, &config.Spec)
if err != nil {
return config, err
}
gkeClient, err := gke.GetGKEClusterClient(ctx, cred)
if err != nil {
return config, err
}

if err = gke.Create(ctx, gkeClient, config); err != nil {
if err := gke.Create(h.gkeClientCtx, h.gkeClient, config); err != nil {
return config, err
}

config = config.DeepCopy()
config.Status.Phase = gkeConfigCreatingPhase
config, err = h.gkeCC.UpdateStatus(config)
return config, err
return h.gkeCC.UpdateStatus(config)
}

func (h *Handler) checkAndUpdate(config *gkev1.GKEClusterConfig) (*gkev1.GKEClusterConfig, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cluster, err := GetCluster(ctx, h.secrets, &config.Spec)
cluster, err := gke.GetCluster(h.gkeClientCtx, h.gkeClient, &config.Spec)
if err != nil {
return config, err
}
Expand Down Expand Up @@ -294,27 +299,15 @@ func (h *Handler) enqueueUpdate(config *gkev1.GKEClusterConfig) (*gkev1.GKEClust
}

func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, upstreamSpec *gkev1.GKEClusterConfigSpec) (*gkev1.GKEClusterConfig, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cred, err := GetSecret(ctx, h.secrets, &config.Spec)
if err != nil {
return config, err
}
gkeClient, err := gke.GetGKEClusterClient(ctx, cred)
if err != nil {
return config, err
}

changed, err := gke.UpdateMasterKubernetesVersion(ctx, gkeClient, config, upstreamSpec)
changed, err := gke.UpdateMasterKubernetesVersion(h.gkeClientCtx, h.gkeClient, config, upstreamSpec)
if err != nil {
return config, err
}
if changed == gke.Changed {
return h.enqueueUpdate(config)
}

changed, err = gke.UpdateClusterAddons(ctx, gkeClient, config, upstreamSpec)
changed, err = gke.UpdateClusterAddons(h.gkeClientCtx, h.gkeClient, config, upstreamSpec)
if err != nil {
return config, err
}
Expand All @@ -326,47 +319,47 @@ func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, ups
return h.enqueueUpdate(config)
}

changed, err = gke.UpdateMasterAuthorizedNetworks(ctx, gkeClient, config, upstreamSpec)
changed, err = gke.UpdateMasterAuthorizedNetworks(h.gkeClientCtx, h.gkeClient, config, upstreamSpec)
if err != nil {
return config, err
}
if changed == gke.Changed {
return h.enqueueUpdate(config)
}

changed, err = gke.UpdateLoggingMonitoringService(ctx, gkeClient, config, upstreamSpec)
changed, err = gke.UpdateLoggingMonitoringService(h.gkeClientCtx, h.gkeClient, config, upstreamSpec)
if err != nil {
return config, err
}
if changed == gke.Changed {
return h.enqueueUpdate(config)
}

changed, err = gke.UpdateNetworkPolicyEnabled(ctx, gkeClient, config, upstreamSpec)
changed, err = gke.UpdateNetworkPolicyEnabled(h.gkeClientCtx, h.gkeClient, config, upstreamSpec)
if err != nil {
return config, err
}
if changed == gke.Changed {
return h.enqueueUpdate(config)
}

changed, err = gke.UpdateLocations(ctx, gkeClient, config, upstreamSpec)
changed, err = gke.UpdateLocations(h.gkeClientCtx, h.gkeClient, config, upstreamSpec)
if err != nil {
return config, err
}
if changed == gke.Changed {
return h.enqueueUpdate(config)
}

changed, err = gke.UpdateMaintenanceWindow(ctx, gkeClient, config, upstreamSpec)
changed, err = gke.UpdateMaintenanceWindow(h.gkeClientCtx, h.gkeClient, config, upstreamSpec)
if err != nil {
return config, err
}
if changed == gke.Changed {
return h.enqueueUpdate(config)
}

changed, err = gke.UpdateLabels(ctx, gkeClient, config, upstreamSpec)
changed, err = gke.UpdateLabels(h.gkeClientCtx, h.gkeClient, config, upstreamSpec)
if err != nil {
return config, err
}
Expand All @@ -386,7 +379,7 @@ func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, ups
upstreamNodePool, ok := upstreamNodePools[npName]
if ok {
// There is a matching nodepool in the cluster already, so update it if needed
changed, err = gke.UpdateNodePoolKubernetesVersionOrImageType(ctx, gkeClient, np, config, upstreamNodePool)
changed, err = gke.UpdateNodePoolKubernetesVersionOrImageType(h.gkeClientCtx, h.gkeClient, np, config, upstreamNodePool)
if err != nil {
return config, err
}
Expand All @@ -397,7 +390,7 @@ func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, ups
continue
}

changed, err = gke.UpdateNodePoolSize(ctx, gkeClient, np, config, upstreamNodePool)
changed, err = gke.UpdateNodePoolSize(h.gkeClientCtx, h.gkeClient, np, config, upstreamNodePool)
if err != nil {
return config, err
}
Expand All @@ -408,7 +401,7 @@ func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, ups
continue
}

changed, err = gke.UpdateNodePoolAutoscaling(ctx, gkeClient, np, config, upstreamNodePool)
changed, err = gke.UpdateNodePoolAutoscaling(h.gkeClientCtx, h.gkeClient, np, config, upstreamNodePool)
if err != nil {
return config, err
}
Expand All @@ -419,7 +412,7 @@ func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, ups
continue
}

changed, err = gke.UpdateNodePoolManagement(ctx, gkeClient, np, config, upstreamNodePool)
changed, err = gke.UpdateNodePoolManagement(h.gkeClientCtx, h.gkeClient, np, config, upstreamNodePool)
if err != nil {
return config, err
}
Expand All @@ -430,7 +423,7 @@ func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, ups
continue
}

changed, err = gke.UpdateNodePoolConfig(ctx, gkeClient, np, config, upstreamNodePool)
changed, err = gke.UpdateNodePoolConfig(h.gkeClientCtx, h.gkeClient, np, config, upstreamNodePool)
if err != nil {
return config, err
}
Expand All @@ -443,7 +436,7 @@ func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, ups
} else {
// There is no nodepool with this name yet, create it
logrus.Infof("adding node pool [%s] to cluster [%s]", *np.Name, config.Name)
if changed, err = gke.CreateNodePool(ctx, gkeClient, config, np); err != nil {
if changed, err = gke.CreateNodePool(h.gkeClientCtx, h.gkeClient, config, np); err != nil {
return config, err
}
if changed == gke.Changed || changed == gke.Retry {
Expand All @@ -455,7 +448,7 @@ func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, ups
for npName := range upstreamNodePools {
if _, ok := downstreamNodePools[npName]; !ok {
logrus.Infof("removing node pool [%s] from cluster [%s]", npName, config.Name)
if changed, err = gke.RemoveNodePool(ctx, gkeClient, config, npName); err != nil {
if changed, err = gke.RemoveNodePool(h.gkeClientCtx, h.gkeClient, config, npName); err != nil {
return config, err
}
if changed == gke.Changed || changed == gke.Retry {
Expand All @@ -480,10 +473,7 @@ func (h *Handler) updateUpstreamClusterState(config *gkev1.GKEClusterConfig, ups
}

func (h *Handler) waitForCreationComplete(config *gkev1.GKEClusterConfig) (*gkev1.GKEClusterConfig, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cluster, err := GetCluster(ctx, h.secrets, &config.Spec)
cluster, err := gke.GetCluster(h.gkeClientCtx, h.gkeClient, &config.Spec)
if err != nil {
return config, err
}
Expand Down
23 changes: 13 additions & 10 deletions controller/gke-cluster-config-handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"context"
"fmt"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -40,6 +41,13 @@ var _ = Describe("createCASecret", func() {
}
Expect(cl.Create(ctx, gkeConfig)).To(Succeed())

caSecret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: gkeConfig.Name,
Namespace: gkeConfig.Namespace,
},
}

clusterState = &gkeapi.Cluster{
Name: "test-cluster",
Endpoint: "https://test.com",
Expand All @@ -56,7 +64,7 @@ var _ = Describe("createCASecret", func() {
})

AfterEach(func() {
Expect(test.CleanupAndWait(ctx, cl, gkeConfig)).To(Succeed())
Expect(test.CleanupAndWait(ctx, cl, gkeConfig, caSecret)).To(Succeed())
})

It("should create CA secret", func() {
Expand All @@ -65,12 +73,6 @@ var _ = Describe("createCASecret", func() {
})

It("should return nil if caSecret exist", func() {
caSecret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: gkeConfig.Name,
Namespace: gkeConfig.Namespace,
},
}
Expect(cl.Create(ctx, caSecret)).To(Succeed())

err := handler.createCASecret(gkeConfig, clusterState)
Expand Down Expand Up @@ -367,23 +369,24 @@ var _ = Describe("importCluster", func() {
Namespace: gkeConfig.Namespace,
},
}
Expect(cl.Create(ctx, caSecret)).To(Succeed())

handler = &Handler{
gkeCC: gkeFactory.Gke().V1().GKEClusterConfig(),
secrets: coreFactory.Core().V1().Secret(),
secretsCache: coreFactory.Core().V1().Secret().Cache(),
gkeClient: gkeServiceMock,
gkeClientCtx: context.Background(),
}
})

AfterEach(func() {
Expect(test.CleanupAndWait(ctx, cl, testNamespace, credentialSecret, gkeConfig, caSecret)).To(Succeed())
Expect(test.CleanupAndWait(ctx, cl, credentialSecret, gkeConfig, caSecret)).To(Succeed())
})

It("should import cluster and update status", func() {
gkeServiceMock.EXPECT().
ClusterGet(
ctx,
context.Background(),
gke.ClusterRRN(gkeConfig.Spec.ProjectID, gke.Location(gkeConfig.Spec.Region, gkeConfig.Spec.Zone),
gkeConfig.Spec.ClusterName)).
Return(clusterState, nil)
Expand Down

0 comments on commit 7697bf6

Please sign in to comment.