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

Implement GCPManagedCluster reconciliation (GKE Part 2) #787

Merged
merged 2 commits into from
Jan 16, 2023
Merged
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
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 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.
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