Skip to content

Commit

Permalink
feat: added GCPManagedCluster controller implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Case <[email protected]>
  • Loading branch information
richardcase committed Jan 11, 2023
1 parent 6483cf8 commit 81f5ca3
Show file tree
Hide file tree
Showing 10 changed files with 630 additions and 79 deletions.
38 changes: 1 addition & 37 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ import (

"github.com/pkg/errors"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-gcp/cloud"
Expand Down Expand Up @@ -54,7 +51,7 @@ func NewClusterScope(ctx context.Context, params ClusterScopeParams) (*ClusterSc
}

if params.GCPServices.Compute == nil {
computeSvc, err := createComputeService(ctx, params)
computeSvc, err := createComputeService(ctx, params.GCPCluster.Spec.CredentialsRef, params.Client)
if err != nil {
return nil, errors.Errorf("failed to create gcp compute client: %v", err)
}
Expand All @@ -76,39 +73,6 @@ func NewClusterScope(ctx context.Context, params ClusterScopeParams) (*ClusterSc
}, nil
}

func createComputeService(ctx context.Context, params ClusterScopeParams) (*compute.Service, error) {
if params.GCPCluster.Spec.CredentialsRef == nil {
computeSvc, err := compute.NewService(ctx)
if err != nil {
return nil, errors.Errorf("failed to create gcp compute client: %v", err)
}

return computeSvc, nil
}

secretRefName := types.NamespacedName{
Name: params.GCPCluster.Spec.CredentialsRef.Name,
Namespace: params.GCPCluster.Spec.CredentialsRef.Namespace,
}

credSecret := &corev1.Secret{}
if err := params.Client.Get(ctx, secretRefName, credSecret); err != nil {
return nil, fmt.Errorf("getting credentials secret %s\\%s: %w", secretRefName.Namespace, secretRefName.Name, err)
}

rawData, ok := credSecret.Data["credentials"]
if !ok {
return nil, errors.New("no credentials key in secret")
}

computeSvc, err := compute.NewService(ctx, option.WithCredentialsJSON(rawData))
if err != nil {
return nil, errors.Errorf("failed to create gcp compute client with credentials secret: %v", err)
}

return computeSvc, nil
}

// ClusterScope defines the basic context for an actuator to operate upon.
type ClusterScope struct {
client client.Client
Expand Down
63 changes: 63 additions & 0 deletions cloud/scope/credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2022 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scope

import (
"context"
"fmt"

"github.com/pkg/errors"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func createComputeService(ctx context.Context, credentialsRef *infrav1.ObjectReference, crClient client.Client) (*compute.Service, error) {
if credentialsRef == nil {
computeSvc, err := compute.NewService(ctx)
if err != nil {
return nil, errors.Errorf("failed to create gcp compute client: %v", err)
}

return computeSvc, nil
}

secretRefName := types.NamespacedName{
Name: credentialsRef.Name,
Namespace: credentialsRef.Namespace,
}

credSecret := &corev1.Secret{}
if err := crClient.Get(ctx, secretRefName, credSecret); err != nil {
return nil, fmt.Errorf("getting credentials secret %s\\%s: %w", secretRefName.Namespace, secretRefName.Name, err)
}

rawData, ok := credSecret.Data["credentials"]
if !ok {
return nil, errors.New("no credentials key in secret")
}

computeSvc, err := compute.NewService(ctx, option.WithCredentialsJSON(rawData))
if err != nil {
return nil, errors.Errorf("failed to create gcp compute client with credentials secret: %v", err)
}

return computeSvc, nil
}
Loading

0 comments on commit 81f5ca3

Please sign in to comment.