Skip to content

Commit 59e5ba3

Browse files
feat(managedmachinepool|managedcontrolplane): Assert for GCPManagedClusterHasNoNetworkDefined when reconciling
Also introduces Container interface which implements bits of https://github.com/googleapis/google-cloud-go/blob/a187451a912835703078e5b6a339c514edebe5de/container/apiv1/cluster_manager_client.go#L468 since it's an internal interface.
1 parent 24fc95f commit 59e5ba3

File tree

9 files changed

+472
-12
lines changed

9 files changed

+472
-12
lines changed

cloud/interfaces.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ package cloud
1919
import (
2020
"context"
2121

22+
"cloud.google.com/go/container/apiv1/containerpb"
23+
24+
"github.com/googleapis/gax-go/v2"
25+
2226
ctrl "sigs.k8s.io/controller-runtime"
2327

2428
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
@@ -49,6 +53,23 @@ type Client interface {
4953
NetworkCloud() Cloud
5054
}
5155

56+
// Container is an interface which implements bits of internalClusterManagerClient from since there is no Public Interface for it.
57+
// ref: https://github.com/googleapis/google-cloud-go/blob/a187451a912835703078e5b6a339c514edebe5de/container/apiv1/cluster_manager_client.go#L468
58+
type Container interface {
59+
Close() error
60+
CreateCluster(context.Context, *containerpb.CreateClusterRequest, ...gax.CallOption) (*containerpb.Operation, error)
61+
UpdateCluster(context.Context, *containerpb.UpdateClusterRequest, ...gax.CallOption) (*containerpb.Operation, error)
62+
DeleteCluster(context.Context, *containerpb.DeleteClusterRequest, ...gax.CallOption) (*containerpb.Operation, error)
63+
GetCluster(context.Context, *containerpb.GetClusterRequest, ...gax.CallOption) (*containerpb.Cluster, error)
64+
ListNodePools(context.Context, *containerpb.ListNodePoolsRequest, ...gax.CallOption) (*containerpb.ListNodePoolsResponse, error)
65+
GetNodePool(context.Context, *containerpb.GetNodePoolRequest, ...gax.CallOption) (*containerpb.NodePool, error)
66+
CreateNodePool(context.Context, *containerpb.CreateNodePoolRequest, ...gax.CallOption) (*containerpb.Operation, error)
67+
DeleteNodePool(context.Context, *containerpb.DeleteNodePoolRequest, ...gax.CallOption) (*containerpb.Operation, error)
68+
SetNodePoolSize(context.Context, *containerpb.SetNodePoolSizeRequest, ...gax.CallOption) (*containerpb.Operation, error)
69+
SetNodePoolAutoscaling(context.Context, *containerpb.SetNodePoolAutoscalingRequest, ...gax.CallOption) (*containerpb.Operation, error)
70+
UpdateNodePool(context.Context, *containerpb.UpdateNodePoolRequest, ...gax.CallOption) (*containerpb.Operation, error)
71+
}
72+
5273
// ClusterGetter is an interface which can get cluster information.
5374
type ClusterGetter interface {
5475
Client

cloud/scope/managedcontrolplane.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ import (
2020
"context"
2121
"fmt"
2222

23-
"sigs.k8s.io/cluster-api-provider-gcp/util/location"
24-
25-
"sigs.k8s.io/cluster-api/util/conditions"
26-
27-
container "cloud.google.com/go/container/apiv1"
2823
credentials "cloud.google.com/go/iam/credentials/apiv1"
2924
resourcemanager "cloud.google.com/go/resourcemanager/apiv3"
3025
"github.com/pkg/errors"
26+
"sigs.k8s.io/cluster-api-provider-gcp/cloud"
3127
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
28+
"sigs.k8s.io/cluster-api-provider-gcp/util/location"
3229
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3330
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
31+
"sigs.k8s.io/cluster-api/util/conditions"
3432
"sigs.k8s.io/cluster-api/util/patch"
3533
"sigs.k8s.io/controller-runtime/pkg/client"
3634
)
@@ -43,7 +41,7 @@ const (
4341
// ManagedControlPlaneScopeParams defines the input parameters used to create a new Scope.
4442
type ManagedControlPlaneScopeParams struct {
4543
CredentialsClient *credentials.IamCredentialsClient
46-
ManagedClusterClient *container.ClusterManagerClient
44+
ManagedClusterClient cloud.Container
4745
TagBindingsClient *resourcemanager.TagBindingsClient
4846
Client client.Client
4947
Cluster *clusterv1.Cluster
@@ -118,7 +116,7 @@ type ManagedControlPlaneScope struct {
118116
Cluster *clusterv1.Cluster
119117
GCPManagedCluster *infrav1exp.GCPManagedCluster
120118
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
121-
mcClient *container.ClusterManagerClient
119+
mcClient cloud.Container
122120
tagBindingsClient *resourcemanager.TagBindingsClient
123121
credentialsClient *credentials.IamCredentialsClient
124122
credential *Credential
@@ -159,7 +157,7 @@ func (s *ManagedControlPlaneScope) Client() client.Client {
159157
}
160158

161159
// ManagedControlPlaneClient returns a client used to interact with GKE.
162-
func (s *ManagedControlPlaneScope) ManagedControlPlaneClient() *container.ClusterManagerClient {
160+
func (s *ManagedControlPlaneScope) ManagedControlPlaneClient() cloud.Container {
163161
return s.mcClient
164162
}
165163

cloud/scope/managedmachinepool.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"sigs.k8s.io/cluster-api/util/conditions"
3030

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

4241
// ManagedMachinePoolScopeParams defines the input parameters used to create a new Scope.
4342
type ManagedMachinePoolScopeParams struct {
44-
ManagedClusterClient *container.ClusterManagerClient
43+
ManagedClusterClient cloud.Container
4544
InstanceGroupManagersClient *compute.InstanceGroupManagersClient
4645
Client client.Client
4746
Cluster *clusterv1.Cluster
@@ -112,7 +111,7 @@ type ManagedMachinePoolScope struct {
112111
GCPManagedCluster *infrav1exp.GCPManagedCluster
113112
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
114113
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
115-
mcClient *container.ClusterManagerClient
114+
mcClient cloud.Container
116115
migClient *compute.InstanceGroupManagersClient
117116
}
118117

@@ -142,7 +141,7 @@ func (s *ManagedMachinePoolScope) ConditionSetter() conditions.Setter {
142141
}
143142

144143
// ManagedMachinePoolClient returns a client used to interact with GKE.
145-
func (s *ManagedMachinePoolScope) ManagedMachinePoolClient() *container.ClusterManagerClient {
144+
func (s *ManagedMachinePoolScope) ManagedMachinePoolClient() cloud.Container {
146145
return s.mcClient
147146
}
148147

cloud/scope/mocks.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package scope
2+
3+
import (
4+
"context"
5+
6+
"github.com/pkg/errors"
7+
)
8+
9+
// NewMockManagedControlPlaneScope creates a new Scope from the supplied parameters.
10+
func NewMockManagedControlPlaneScope(_ context.Context, params ManagedControlPlaneScopeParams) (*ManagedControlPlaneScope, error) {
11+
if params.Cluster == nil {
12+
return nil, errors.New("failed to generate new scope from nil Cluster")
13+
}
14+
if params.GCPManagedCluster == nil {
15+
return nil, errors.New("failed to generate new scope from nil GCPManagedCluster")
16+
}
17+
if params.GCPManagedControlPlane == nil {
18+
return nil, errors.New("failed to generate new scope from nil GCPManagedControlPlane")
19+
}
20+
21+
return &ManagedControlPlaneScope{
22+
client: params.Client,
23+
Cluster: params.Cluster,
24+
GCPManagedCluster: params.GCPManagedCluster,
25+
GCPManagedControlPlane: params.GCPManagedControlPlane,
26+
mcClient: params.ManagedClusterClient,
27+
}, nil
28+
}

0 commit comments

Comments
 (0)