Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent GCPManagedCluster reconciler to not crash if Network.Name and Network.Subnets is not passed to it #1284

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions cloud/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ package cloud
import (
"context"

"cloud.google.com/go/container/apiv1/containerpb"

"github.com/googleapis/gax-go/v2"

ctrl "sigs.k8s.io/controller-runtime"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
Expand Down Expand Up @@ -49,6 +53,23 @@ type Client interface {
NetworkCloud() Cloud
}

// Container is an interface which implements bits of internalClusterManagerClient from since there is no Public Interface for it.
// ref: https://github.com/googleapis/google-cloud-go/blob/a187451a912835703078e5b6a339c514edebe5de/container/apiv1/cluster_manager_client.go#L468
type Container interface {
Close() error
CreateCluster(context.Context, *containerpb.CreateClusterRequest, ...gax.CallOption) (*containerpb.Operation, error)
UpdateCluster(context.Context, *containerpb.UpdateClusterRequest, ...gax.CallOption) (*containerpb.Operation, error)
DeleteCluster(context.Context, *containerpb.DeleteClusterRequest, ...gax.CallOption) (*containerpb.Operation, error)
GetCluster(context.Context, *containerpb.GetClusterRequest, ...gax.CallOption) (*containerpb.Cluster, error)
ListNodePools(context.Context, *containerpb.ListNodePoolsRequest, ...gax.CallOption) (*containerpb.ListNodePoolsResponse, error)
GetNodePool(context.Context, *containerpb.GetNodePoolRequest, ...gax.CallOption) (*containerpb.NodePool, error)
CreateNodePool(context.Context, *containerpb.CreateNodePoolRequest, ...gax.CallOption) (*containerpb.Operation, error)
DeleteNodePool(context.Context, *containerpb.DeleteNodePoolRequest, ...gax.CallOption) (*containerpb.Operation, error)
SetNodePoolSize(context.Context, *containerpb.SetNodePoolSizeRequest, ...gax.CallOption) (*containerpb.Operation, error)
SetNodePoolAutoscaling(context.Context, *containerpb.SetNodePoolAutoscalingRequest, ...gax.CallOption) (*containerpb.Operation, error)
UpdateNodePool(context.Context, *containerpb.UpdateNodePoolRequest, ...gax.CallOption) (*containerpb.Operation, error)
}

// ClusterGetter is an interface which can get cluster information.
type ClusterGetter interface {
Client
Expand Down
14 changes: 6 additions & 8 deletions cloud/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ import (
"context"
"fmt"

"sigs.k8s.io/cluster-api-provider-gcp/util/location"

"sigs.k8s.io/cluster-api/util/conditions"

container "cloud.google.com/go/container/apiv1"
credentials "cloud.google.com/go/iam/credentials/apiv1"
resourcemanager "cloud.google.com/go/resourcemanager/apiv3"
"github.com/pkg/errors"
"sigs.k8s.io/cluster-api-provider-gcp/cloud"
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-gcp/util/location"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -43,7 +41,7 @@ const (
// ManagedControlPlaneScopeParams defines the input parameters used to create a new Scope.
type ManagedControlPlaneScopeParams struct {
CredentialsClient *credentials.IamCredentialsClient
ManagedClusterClient *container.ClusterManagerClient
ManagedClusterClient cloud.Container
TagBindingsClient *resourcemanager.TagBindingsClient
Client client.Client
Cluster *clusterv1.Cluster
Expand Down Expand Up @@ -118,7 +116,7 @@ type ManagedControlPlaneScope struct {
Cluster *clusterv1.Cluster
GCPManagedCluster *infrav1exp.GCPManagedCluster
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
mcClient *container.ClusterManagerClient
mcClient cloud.Container
tagBindingsClient *resourcemanager.TagBindingsClient
credentialsClient *credentials.IamCredentialsClient
credential *Credential
Expand Down Expand Up @@ -159,7 +157,7 @@ func (s *ManagedControlPlaneScope) Client() client.Client {
}

// ManagedControlPlaneClient returns a client used to interact with GKE.
func (s *ManagedControlPlaneScope) ManagedControlPlaneClient() *container.ClusterManagerClient {
func (s *ManagedControlPlaneScope) ManagedControlPlaneClient() cloud.Container {
return s.mcClient
}

Expand Down
7 changes: 3 additions & 4 deletions cloud/scope/managedmachinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"sigs.k8s.io/cluster-api/util/conditions"

compute "cloud.google.com/go/compute/apiv1"
container "cloud.google.com/go/container/apiv1"
"cloud.google.com/go/container/apiv1/containerpb"
"github.com/pkg/errors"
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
Expand All @@ -41,7 +40,7 @@ import (

// ManagedMachinePoolScopeParams defines the input parameters used to create a new Scope.
type ManagedMachinePoolScopeParams struct {
ManagedClusterClient *container.ClusterManagerClient
ManagedClusterClient cloud.Container
InstanceGroupManagersClient *compute.InstanceGroupManagersClient
Client client.Client
Cluster *clusterv1.Cluster
Expand Down Expand Up @@ -112,7 +111,7 @@ type ManagedMachinePoolScope struct {
GCPManagedCluster *infrav1exp.GCPManagedCluster
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
mcClient *container.ClusterManagerClient
mcClient cloud.Container
migClient *compute.InstanceGroupManagersClient
}

Expand Down Expand Up @@ -142,7 +141,7 @@ func (s *ManagedMachinePoolScope) ConditionSetter() conditions.Setter {
}

// ManagedMachinePoolClient returns a client used to interact with GKE.
func (s *ManagedMachinePoolScope) ManagedMachinePoolClient() *container.ClusterManagerClient {
func (s *ManagedMachinePoolScope) ManagedMachinePoolClient() cloud.Container {
return s.mcClient
}

Expand Down
28 changes: 28 additions & 0 deletions cloud/scope/mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package scope

import (
"context"

"github.com/pkg/errors"
)

// NewMockManagedControlPlaneScope creates a new Scope from the supplied parameters.
func NewMockManagedControlPlaneScope(_ context.Context, params ManagedControlPlaneScopeParams) (*ManagedControlPlaneScope, error) {
if params.Cluster == nil {
return nil, errors.New("failed to generate new scope from nil Cluster")
}
if params.GCPManagedCluster == nil {
return nil, errors.New("failed to generate new scope from nil GCPManagedCluster")
}
if params.GCPManagedControlPlane == nil {
return nil, errors.New("failed to generate new scope from nil GCPManagedControlPlane")
}

return &ManagedControlPlaneScope{
client: params.Client,
Cluster: params.Cluster,
GCPManagedCluster: params.GCPManagedCluster,
GCPManagedControlPlane: params.GCPManagedControlPlane,
mcClient: params.ManagedClusterClient,
}, nil
}
Loading