Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
richardchen331 committed Jan 25, 2023
1 parent 2071e51 commit f13e97e
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 98 deletions.
4 changes: 2 additions & 2 deletions cloud/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ func (s *ManagedControlPlaneScope) GetCredential() *Credential {
return s.credential
}

func (s *ManagedControlPlaneScope) GetAllNodePools() ([]infrav1exp.GCPManagedMachinePool, error) {
func (s *ManagedControlPlaneScope) GetAllNodePools(ctx context.Context) ([]infrav1exp.GCPManagedMachinePool, error) {
if s.AllNodePools == nil {
opt1 := client.InNamespace(s.GCPManagedControlPlane.Namespace)
opt2 := client.MatchingLabels(map[string]string{
clusterv1.ClusterLabelName: s.Cluster.Name,
})

machinePoolList := &infrav1exp.GCPManagedMachinePoolList{}
if err := s.client.List(context.TODO(), machinePoolList, opt1, opt2); err != nil {
if err := s.client.List(ctx, machinePoolList, opt1, opt2); err != nil {
return nil, err
}
s.AllNodePools = machinePoolList.Items
Expand Down
33 changes: 26 additions & 7 deletions cloud/scope/managedmachinepool.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright The Kubernetes Authors.
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ package scope
import (
"context"
"fmt"
"google.golang.org/api/option"
"sigs.k8s.io/cluster-api/util/conditions"
"strings"

Expand All @@ -38,35 +39,51 @@ type ManagedMachinePoolScopeParams struct {
InstanceGroupManagersClient *compute.InstanceGroupManagersClient
Client client.Client
Cluster *clusterv1.Cluster
GCPManagedCluster *infrav1exp.GCPManagedCluster
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
}

// NewManagedMachinePoolScope creates a new Scope from the supplied parameters.
// This is meant to be called for each reconcile iteration.
func NewManagedMachinePoolScope(params ManagedMachinePoolScopeParams) (*ManagedMachinePoolScope, error) {
func NewManagedMachinePoolScope(ctx context.Context, params ManagedMachinePoolScopeParams) (*ManagedMachinePoolScope, 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")
}
if params.GCPManagedMachinePool == nil {
return nil, errors.New("failed to generate new scope from nil GCPManagedMachinePool")
}

var credentialData []byte
var err error
if params.GCPManagedCluster.Spec.CredentialsRef != nil {
credentialData, err = getCredentialDataFromRef(ctx, params.GCPManagedCluster.Spec.CredentialsRef, params.Client)
} else {
credentialData, err = getCredentialDataFromMount()
}
if err != nil {
return nil, errors.Errorf("failed to get credential data: %v", err)
}

if params.ManagedClusterClient == nil {
managedClusterClient, err := container.NewClusterManagerClient(context.TODO())
var managedClusterClient *container.ClusterManagerClient
managedClusterClient, err = container.NewClusterManagerClient(ctx, option.WithCredentialsJSON(credentialData))
if err != nil {
return nil, errors.Errorf("failed to create gcp managed cluster client: %v", err)
}
params.ManagedClusterClient = managedClusterClient
}

if params.InstanceGroupManagersClient == nil {
instanceGroupManagersClient, err := compute.NewInstanceGroupManagersRESTClient(context.TODO())
var instanceGroupManagersClient *compute.InstanceGroupManagersClient
instanceGroupManagersClient, err = compute.NewInstanceGroupManagersRESTClient(ctx, option.WithCredentialsJSON(credentialData))
if err != nil {
return nil, errors.Errorf("failed to create gcp instance group managers client: %v", err)
return nil, errors.Errorf("failed to create gcp instance group manager client: %v", err)
}
params.InstanceGroupManagersClient = instanceGroupManagersClient
}
Expand All @@ -93,6 +110,7 @@ type ManagedMachinePoolScope struct {
patchHelper *patch.Helper

Cluster *clusterv1.Cluster
GCPManagedCluster *infrav1exp.GCPManagedCluster
GCPManagedControlPlane *infrav1exp.GCPManagedControlPlane
GCPManagedMachinePool *infrav1exp.GCPManagedMachinePool
mcClient *container.ClusterManagerClient
Expand All @@ -115,6 +133,7 @@ func (s *ManagedMachinePoolScope) PatchObject() error {
// Close closes the current scope persisting the managed control plane configuration and status.
func (s *ManagedMachinePoolScope) Close() error {
s.mcClient.Close()
s.migClient.Close()
return s.PatchObject()
}

Expand Down Expand Up @@ -184,7 +203,7 @@ func (s *ManagedMachinePoolScope) Region() string {
}

func (s *ManagedMachinePoolScope) NodePoolLocation() string {
return fmt.Sprintf("projects/%s/locations/%s/clusters/%s", s.GCPManagedControlPlane.Spec.Project, s.Region(), s.GCPManagedControlPlane.Name)
return fmt.Sprintf("projects/%s/locations/%s/clusters/%s", s.GCPManagedControlPlane.Spec.Project, s.Region(), s.GCPManagedControlPlane.Spec.ClusterName)
}

func (s *ManagedMachinePoolScope) NodePoolFullName() string {
Expand Down
2 changes: 1 addition & 1 deletion cloud/services/container/clusters/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (s *Service) updateCAPIKubeconfigSecret(ctx context.Context, configSecret *
}

func (s *Service) getKubeConfigContextName(isUser bool) string {
contextName := fmt.Sprintf("gke_%s_%s_%s", s.scope.GCPManagedControlPlane.Spec.Project, s.scope.GCPManagedControlPlane.Spec.Location, s.scope.GCPManagedControlPlane.Name)
contextName := fmt.Sprintf("gke_%s_%s_%s", s.scope.GCPManagedControlPlane.Spec.Project, s.scope.GCPManagedControlPlane.Spec.Location, s.scope.GCPManagedControlPlane.Spec.ClusterName)
if isUser {
contextName = fmt.Sprintf("%s-user", contextName)
}
Expand Down
23 changes: 12 additions & 11 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"
"github.com/googleapis/gax-go/v2/apierror"
Expand Down Expand Up @@ -47,19 +48,19 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
log.Info("Cluster not found, creating")
s.scope.GCPManagedControlPlane.Status.Ready = false

nodePools, err := s.scope.GetAllNodePools()
nodePools, err := s.scope.GetAllNodePools(ctx)
if err != nil {
conditions.MarkFalse(s.scope.ConditionSetter(), clusterv1.ReadyCondition, infrav1exp.GKEControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, err.Error())
conditions.MarkFalse(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneReadyCondition, infrav1exp.GKEControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, err.Error())
conditions.MarkFalse(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneCreatingCondition, infrav1exp.GKEControlPlaneReconciliationFailedReason, clusterv1.ConditionSeverityError, err.Error())
return err
return ctrl.Result{}, err
}
if len(nodePools) == 0 {
log.Info("At least 1 node pool is required to create GKE cluster")
conditions.MarkFalse(s.scope.ConditionSetter(), clusterv1.ReadyCondition, infrav1exp.GKEControlPlaneRequiresAtLeastOneNodePoolReason, clusterv1.ConditionSeverityInfo, "")
conditions.MarkFalse(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneReadyCondition, infrav1exp.GKEControlPlaneRequiresAtLeastOneNodePoolReason, clusterv1.ConditionSeverityInfo, "")
conditions.MarkFalse(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneCreatingCondition, infrav1exp.GKEControlPlaneRequiresAtLeastOneNodePoolReason, clusterv1.ConditionSeverityInfo, "")
return nil
return ctrl.Result{RequeueAfter: reconciler.DefaultRetryTime}, nil
}

if err = s.createCluster(ctx); err != nil {
Expand Down Expand Up @@ -100,14 +101,14 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
if len(cluster.Conditions) > 0 {
msg = cluster.Conditions[0].GetMessage()
}
log.Error(errors.New("Cluster in error/degraded state"), msg, "name", s.scope.GCPManagedControlPlane.Name)
log.Error(errors.New("Cluster in error/degraded state"), msg, "name", s.scope.GCPManagedControlPlane.Spec.ClusterName)
conditions.MarkFalse(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneReadyCondition, infrav1exp.GKEControlPlaneErrorReason, clusterv1.ConditionSeverityError, "")
s.scope.GCPManagedControlPlane.Status.Ready = false
return ctrl.Result{}, nil
case containerpb.Cluster_RUNNING:
log.Info("Cluster running")
default:
log.Error(errors.New("Unhandled cluster status"), fmt.Sprintf("Unhandled cluster status %s", cluster.Status), "name", s.scope.GCPManagedControlPlane.Name)
log.Error(errors.New("Unhandled cluster status"), fmt.Sprintf("Unhandled cluster status %s", cluster.Status), "name", s.scope.GCPManagedControlPlane.Spec.ClusterName)
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -203,7 +204,7 @@ func (s *Service) describeCluster(ctx context.Context) (*containerpb.Cluster, er
return nil, nil
}
}
log.Error(err, "Error getting GKE cluster", "name", s.scope.GCPManagedControlPlane.Name)
log.Error(err, "Error getting GKE cluster", "name", s.scope.GCPManagedControlPlane.Spec.ClusterName)
return nil, err
}

Expand All @@ -213,9 +214,9 @@ func (s *Service) describeCluster(ctx context.Context) (*containerpb.Cluster, er
func (s *Service) createCluster(ctx context.Context) error {
log := log.FromContext(ctx)

nodePools, _ := s.scope.GetAllNodePools()
nodePools, _ := s.scope.GetAllNodePools(ctx)
cluster := &containerpb.Cluster{
Name: s.scope.GCPManagedControlPlane.Name,
Name: s.scope.GCPManagedControlPlane.Spec.ClusterName,
Network: *s.scope.GCPManagedCluster.Spec.Network.Name,
Autopilot: &containerpb.Autopilot{
Enabled: false,
Expand All @@ -234,7 +235,7 @@ func (s *Service) createCluster(ctx context.Context) error {
}
_, err := s.scope.ManagedControlPlaneClient().CreateCluster(ctx, createClusterRequest)
if err != nil {
log.Error(err, "Error creating GKE cluster", "name", s.scope.GCPManagedControlPlane.Name)
log.Error(err, "Error creating GKE cluster", "name", s.scope.GCPManagedControlPlane.Spec.ClusterName)
return err
}

Expand All @@ -246,7 +247,7 @@ func (s *Service) updateCluster(ctx context.Context, updateClusterRequest *conta

_, err := s.scope.ManagedControlPlaneClient().UpdateCluster(ctx, updateClusterRequest)
if err != nil {
log.Error(err, "Error updating GKE cluster", "name", s.scope.GCPManagedControlPlane.Name)
log.Error(err, "Error updating GKE cluster", "name", s.scope.GCPManagedControlPlane.Spec.ClusterName)
return err
}

Expand All @@ -261,7 +262,7 @@ func (s *Service) deleteCluster(ctx context.Context) error {
}
_, err := s.scope.ManagedControlPlaneClient().DeleteCluster(ctx, deleteClusterRequest)
if err != nil {
log.Error(err, "Error deleting GKE cluster", "name", s.scope.GCPManagedControlPlane.Name)
log.Error(err, "Error deleting GKE cluster", "name", s.scope.GCPManagedControlPlane.Spec.ClusterName)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cloud/services/container/nodepools/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright The Kubernetes Authors.
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit f13e97e

Please sign in to comment.