Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
richardchen331 committed Jan 25, 2023
1 parent f13e97e commit bee121d
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 71 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/v1alpha4/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions cloud/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (s *ManagedControlPlaneScope) GetCredential() *Credential {
return s.credential
}

// GetAllNodePools gets all node pools for the control plane.
func (s *ManagedControlPlaneScope) GetAllNodePools(ctx context.Context) ([]infrav1exp.GCPManagedMachinePool, error) {
if s.AllNodePools == nil {
opt1 := client.InNamespace(s.GCPManagedControlPlane.Namespace)
Expand All @@ -190,18 +191,15 @@ func (s *ManagedControlPlaneScope) GetAllNodePools(ctx context.Context) ([]infra
return s.AllNodePools, nil
}

func parseLocation(location string) (region string, zone *string) {
func parseLocation(location string) string {
parts := strings.Split(location, "-")
region = strings.Join(parts[:2], "-")
if len(parts) == 3 {
return region, &parts[2]
}
return region, nil
region := strings.Join(parts[:2], "-")
return region
}

// Region returns the region of the GKE cluster.
func (s *ManagedControlPlaneScope) Region() string {
region, _ := parseLocation(s.GCPManagedControlPlane.Spec.Location)
region := parseLocation(s.GCPManagedControlPlane.Spec.Location)
return region
}

Expand Down
61 changes: 36 additions & 25 deletions cloud/scope/managedmachinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package scope
import (
"context"
"fmt"
"strings"

"google.golang.org/api/option"
"sigs.k8s.io/cluster-api/util/conditions"
"strings"

"cloud.google.com/go/compute/apiv1"
"cloud.google.com/go/container/apiv1"
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 @@ -35,13 +36,13 @@ import (

// ManagedMachinePoolScopeParams defines the input parameters used to create a new Scope.
type ManagedMachinePoolScopeParams struct {
ManagedClusterClient *container.ClusterManagerClient
ManagedClusterClient *container.ClusterManagerClient
InstanceGroupManagersClient *compute.InstanceGroupManagersClient
Client client.Client
Cluster *clusterv1.Cluster
GCPManagedCluster *infrav1exp.GCPManagedCluster
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
Client client.Client
Cluster *clusterv1.Cluster
GCPManagedCluster *infrav1exp.GCPManagedCluster
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
}

// NewManagedMachinePoolScope creates a new Scope from the supplied parameters.
Expand Down Expand Up @@ -94,13 +95,13 @@ func NewManagedMachinePoolScope(ctx context.Context, params ManagedMachinePoolSc
}

return &ManagedMachinePoolScope{
client: params.Client,
Cluster: params.Cluster,
client: params.Client,
Cluster: params.Cluster,
GCPManagedControlPlane: params.GCPManagedControlPlane,
GCPManagedMachinePool: params.GCPManagedMachinePool,
mcClient: params.ManagedClusterClient,
migClient: params.InstanceGroupManagersClient,
patchHelper: helper,
mcClient: params.ManagedClusterClient,
migClient: params.InstanceGroupManagersClient,
patchHelper: helper,
}, nil
}

Expand All @@ -109,12 +110,12 @@ type ManagedMachinePoolScope struct {
client client.Client
patchHelper *patch.Helper

Cluster *clusterv1.Cluster
Cluster *clusterv1.Cluster
GCPManagedCluster *infrav1exp.GCPManagedCluster
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
mcClient *container.ClusterManagerClient
migClient *compute.InstanceGroupManagersClient
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
mcClient *container.ClusterManagerClient
migClient *compute.InstanceGroupManagersClient
}

// PatchObject persists the managed control plane configuration and status.
Expand All @@ -137,37 +138,42 @@ func (s *ManagedMachinePoolScope) Close() error {
return s.PatchObject()
}

// ConditionSetter return a condition setter (which is GCPManagedMachinePool itself).
func (s *ManagedMachinePoolScope) ConditionSetter() conditions.Setter {
return s.GCPManagedMachinePool
}

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

// InstanceGroupManagersClient returns a client used to interact with GCP MIG.
func (s *ManagedMachinePoolScope) InstanceGroupManagersClient() *compute.InstanceGroupManagersClient {
return s.migClient
}

func ParseInstanceGroupUrl(url string) (project string, zone string, mig string) {
// ParseInstanceGroupURL parses an instance group URL.
func ParseInstanceGroupURL(url string) (project string, zone string, mig string) {
parts := strings.Split(url, "/")
if len(parts) < 11 {
return "", "", ""
}
return parts[6], parts[8], parts[10]
}

// ConvertToSdkNodePool convertsSetReplicas a node pool to format that is used by GCP SDK.
func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool) *containerpb.NodePool {
nodePoolName := nodePool.Spec.NodePoolName
if len(nodePoolName) == 0 {
nodePoolName = nodePool.Name
}
sdkNodePool := containerpb.NodePool{
Name: nodePoolName,
Name: nodePoolName,
InitialNodeCount: nodePool.Spec.NodeCount,
Config: &containerpb.NodeConfig{
Labels: nodePool.Spec.KubernetesLabels,
Taints: infrav1exp.ConvertToSdkTaint(nodePool.Spec.KubernetesTaints),
Labels: nodePool.Spec.KubernetesLabels,
Taints: infrav1exp.ConvertToSdkTaint(nodePool.Spec.KubernetesTaints),
Metadata: nodePool.Spec.AdditionalLabels,
},
}
Expand All @@ -177,6 +183,7 @@ func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool) *containerp
return &sdkNodePool
}

// ConvertToSdkNodePools converts node pools to format that is used by GCP SDK.
func ConvertToSdkNodePools(nodePools []infrav1exp.GCPManagedMachinePool) []*containerpb.NodePool {
res := []*containerpb.NodePool{}
for _, nodePool := range nodePools {
Expand All @@ -185,27 +192,31 @@ func ConvertToSdkNodePools(nodePools []infrav1exp.GCPManagedMachinePool) []*cont
return res
}

// SetReplicas sets the replicas count in status.
func (s *ManagedMachinePoolScope) SetReplicas(replicas int32) {
s.GCPManagedMachinePool.Status.Replicas = replicas
}

// NodePoolName returns the node pool name.
func (s *ManagedMachinePoolScope) NodePoolName() string {
if len(s.GCPManagedMachinePool.Spec.NodePoolName) > 0 {
return s.GCPManagedMachinePool.Spec.NodePoolName
} else {
return s.GCPManagedMachinePool.Name
}
return s.GCPManagedMachinePool.Name
}

// Region returns the region of the GKE node pool.
func (s *ManagedMachinePoolScope) Region() string {
region, _ := parseLocation(s.GCPManagedControlPlane.Spec.Location)
region := parseLocation(s.GCPManagedControlPlane.Spec.Location)
return region
}

// NodePoolLocation returns the location of the node pool.
func (s *ManagedMachinePoolScope) NodePoolLocation() string {
return fmt.Sprintf("projects/%s/locations/%s/clusters/%s", s.GCPManagedControlPlane.Spec.Project, s.Region(), s.GCPManagedControlPlane.Spec.ClusterName)
}

// NodePoolFullName returns the full name of the node pool.
func (s *ManagedMachinePoolScope) NodePoolFullName() string {
return fmt.Sprintf("%s/nodePools/%s", s.NodePoolLocation(), s.NodePoolName())
}
5 changes: 3 additions & 2 deletions cloud/services/container/clusters/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package clusters
import (
"context"
"fmt"

"sigs.k8s.io/cluster-api-provider-gcp/cloud/scope"

"cloud.google.com/go/container/apiv1/containerpb"
Expand Down Expand Up @@ -216,7 +217,7 @@ func (s *Service) createCluster(ctx context.Context) error {

nodePools, _ := s.scope.GetAllNodePools(ctx)
cluster := &containerpb.Cluster{
Name: s.scope.GCPManagedControlPlane.Spec.ClusterName,
Name: s.scope.GCPManagedControlPlane.Spec.ClusterName,
Network: *s.scope.GCPManagedCluster.Spec.Network.Name,
Autopilot: &containerpb.Autopilot{
Enabled: false,
Expand All @@ -231,7 +232,7 @@ func (s *Service) createCluster(ctx context.Context) error {
}
createClusterRequest := &containerpb.CreateClusterRequest{
Cluster: cluster,
Parent: s.scope.ClusterLocation(),
Parent: s.scope.ClusterLocation(),
}
_, err := s.scope.ManagedControlPlaneClient().CreateCluster(ctx, createClusterRequest)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cloud/services/container/nodepools/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package instances implements reconciler for GKE node pool components.
// Package nodepools implements reconciler for GKE node pool components.
package nodepools
30 changes: 16 additions & 14 deletions cloud/services/container/nodepools/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ limitations under the License.
package nodepools

import (
"cloud.google.com/go/container/apiv1/containerpb"
"context"
"fmt"
"github.com/googleapis/gax-go/v2/apierror"
"github.com/pkg/errors"
"reflect"

"google.golang.org/api/iterator"
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
"google.golang.org/grpc/codes"
"reflect"

"cloud.google.com/go/compute/apiv1/computepb"
"cloud.google.com/go/container/apiv1/containerpb"
"github.com/googleapis/gax-go/v2/apierror"
"github.com/pkg/errors"
"sigs.k8s.io/cluster-api-provider-gcp/cloud/scope"
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
Expand Down Expand Up @@ -110,7 +112,7 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
return ctrl.Result{}, nil
}

needUpdateVersionOrImage, nodePoolUpdateVersionOrImage := s.checkDiffAndPrepareUpdateVersionOrImage(*nodePool)
needUpdateVersionOrImage, nodePoolUpdateVersionOrImage := s.checkDiffAndPrepareUpdateVersionOrImage(nodePool)
if needUpdateVersionOrImage {
log.Info("Version/image update required")
err = s.updateNodePoolVersionOrImage(ctx, nodePoolUpdateVersionOrImage)
Expand All @@ -123,7 +125,7 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
return ctrl.Result{}, nil
}

needUpdateSize, setNodePoolSizeRequest := s.checkDiffAndPrepareUpdateSize(*nodePool)
needUpdateSize, setNodePoolSizeRequest := s.checkDiffAndPrepareUpdateSize(nodePool)
if needUpdateSize {
log.Info("Size update required")
err = s.updateNodePoolSize(ctx, setNodePoolSizeRequest)
Expand Down Expand Up @@ -217,14 +219,14 @@ func (s *Service) getInstances(ctx context.Context, nodePool *containerpb.NodePo
instances := []*computepb.ManagedInstance{}

for _, url := range nodePool.InstanceGroupUrls {
project, zone, mig := scope.ParseInstanceGroupUrl(url)
project, zone, mig := scope.ParseInstanceGroupURL(url)
if mig == "" {
return nil, errors.New(fmt.Sprintf("fail to parse instance group url %s", url))
return nil, fmt.Errorf("fail to parse instance group url %s", url)
}
listManagedInstancesRequest := &computepb.ListManagedInstancesInstanceGroupManagersRequest{
InstanceGroupManager: mig,
Project: project,
Zone: zone,
Project: project,
Zone: zone,
}
iter := s.scope.InstanceGroupManagersClient().ListManagedInstances(ctx, listManagedInstancesRequest)
for {
Expand All @@ -245,7 +247,7 @@ func (s *Service) getInstances(ctx context.Context, nodePool *containerpb.NodePo
func (s *Service) createNodePool(ctx context.Context) error {
createNodePoolRequest := &containerpb.CreateNodePoolRequest{
NodePool: scope.ConvertToSdkNodePool(*s.scope.GCPManagedMachinePool),
Parent: s.scope.NodePoolLocation(),
Parent: s.scope.NodePoolLocation(),
}
_, err := s.scope.ManagedMachinePoolClient().CreateNodePool(ctx, createNodePoolRequest)
if err != nil {
Expand Down Expand Up @@ -285,7 +287,7 @@ func (s *Service) deleteNodePool(ctx context.Context) error {
return nil
}

func (s *Service) checkDiffAndPrepareUpdateVersionOrImage(existingNodePool containerpb.NodePool) (bool, *containerpb.UpdateNodePoolRequest) {
func (s *Service) checkDiffAndPrepareUpdateVersionOrImage(existingNodePool *containerpb.NodePool) (bool, *containerpb.UpdateNodePoolRequest) {
needUpdate := false
updateNodePoolRequest := containerpb.UpdateNodePoolRequest{
Name: s.scope.NodePoolFullName(),
Expand Down Expand Up @@ -313,7 +315,7 @@ func (s *Service) checkDiffAndPrepareUpdateVersionOrImage(existingNodePool conta
return needUpdate, &updateNodePoolRequest
}

func (s *Service) checkDiffAndPrepareUpdateSize(existingNodePool containerpb.NodePool) (bool, *containerpb.SetNodePoolSizeRequest) {
func (s *Service) checkDiffAndPrepareUpdateSize(existingNodePool *containerpb.NodePool) (bool, *containerpb.SetNodePoolSizeRequest) {
needUpdate := false
setNodePoolSizeRequest := containerpb.SetNodePoolSizeRequest{
Name: s.scope.NodePoolFullName(),
Expand Down
15 changes: 3 additions & 12 deletions exp/api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ type Taint struct {
// Taints is an array of Taints.
type Taints []Taint

// MachinePoolMode represents the mode of operation for the machine pool.
type MachinePoolMode string

const (
// MachinePoolModeSystem represents a system machine pool.
MachinePoolModeSystem MachinePoolMode = "system"
// MachinePoolModeUser represenyts a user machine pool.
MachinePoolModeUser MachinePoolMode = "user"
)

func convertToSdkTaintEffect(effect TaintEffect) containerpb.NodeTaint_Effect {
switch effect {
case "NoSchedule":
Expand All @@ -58,15 +48,16 @@ func convertToSdkTaintEffect(effect TaintEffect) containerpb.NodeTaint_Effect {
}
}

// ConvertToSdkTaint converts taints to format that is used by GCP SDK.
func ConvertToSdkTaint(taints Taints) []*containerpb.NodeTaint {
if taints == nil {
return nil
}
res := []*containerpb.NodeTaint{}
for _, taint := range taints {
res = append(res, &containerpb.NodeTaint{
Key: taint.Key,
Value: taint.Value,
Key: taint.Key,
Value: taint.Value,
Effect: convertToSdkTaintEffect(taint.Effect),
})
}
Expand Down
Loading

0 comments on commit bee121d

Please sign in to comment.