Skip to content

Commit 2e39d03

Browse files
committed
Add remote/util.go helpers to work with KubeConfig Secrets
Signed-off-by: Vince Prignano <[email protected]>
1 parent f88835a commit 2e39d03

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

pkg/controller/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ go_library(
66
"add_machinedeployment.go",
77
"add_machineset.go",
88
"add_node.go",
9-
"noderef_controller.go",
9+
"controller.go",
1010
],
1111
importpath = "sigs.k8s.io/cluster-api/pkg/controller",
1212
visibility = ["//visibility:public"],

pkg/controller/cluster/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
"//pkg/controller/error:go_default_library",
1515
"//pkg/util:go_default_library",
1616
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
17+
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
1718
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
1819
"//vendor/k8s.io/klog:go_default_library",
1920
"//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library",

pkg/controller/remote/BUILD.bazel

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["util.go"],
6+
importpath = "sigs.k8s.io/cluster-api/pkg/controller/remote",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//vendor/github.com/pkg/errors:go_default_library",
10+
"//vendor/k8s.io/api/core/v1:go_default_library",
11+
"//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library",
12+
],
13+
)

pkg/controller/remote/util.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package remote
18+
19+
import (
20+
"context"
21+
"encoding/base64"
22+
"fmt"
23+
24+
"github.com/pkg/errors"
25+
corev1 "k8s.io/api/core/v1"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
)
28+
29+
const (
30+
kubeconfigSecretKey = "value"
31+
)
32+
33+
// KubeConfigSecretName generates the expected name for the Kubeconfig secret
34+
// to access a remote cluster given the cluster's name.
35+
func KubeConfigSecretName(cluster string) string {
36+
return fmt.Sprintf("%s-kubeconfig", cluster)
37+
}
38+
39+
// GetKubeConfigSecret retrieves the KubeConfig Secret (if any)
40+
// from the given cluster name and namespace.
41+
func GetKubeConfigSecret(c client.Client, cluster, namespace string) (*corev1.Secret, error) {
42+
secret := &corev1.Secret{}
43+
secretKey := client.ObjectKey{
44+
Namespace: namespace,
45+
Name: KubeConfigSecretName(cluster),
46+
}
47+
48+
if err := c.Get(context.TODO(), secretKey, secret); err != nil {
49+
return nil, err
50+
}
51+
52+
return secret, nil
53+
}
54+
55+
// DecodeKubeConfigSecret uses the Secret to retrieve and decode the data.
56+
func DecodeKubeConfigSecret(secret *corev1.Secret) ([]byte, error) {
57+
encodedKubeconfig, ok := secret.Data[kubeconfigSecretKey]
58+
if !ok {
59+
return nil, errors.Errorf("missing value in secret %s/%s", secret.Namespace, secret.Name)
60+
}
61+
62+
kubeconfig, err := base64.StdEncoding.DecodeString(string(encodedKubeconfig))
63+
if err != nil {
64+
return nil, errors.Wrapf(err, "cannot decode kubeconfig secret %s/%s", secret.Namespace, secret.Name)
65+
}
66+
67+
return kubeconfig, nil
68+
}

0 commit comments

Comments
 (0)