-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added GCPManagedCluster controller
Signed-off-by: Richard Case <[email protected]>
- Loading branch information
1 parent
6483cf8
commit c35cc84
Showing
7 changed files
with
542 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.