Skip to content

Commit

Permalink
Add remote/util.go helpers to work with KubeConfig Secrets
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Jun 10, 2019
1 parent f88835a commit 65975e0
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/controller/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go_library(
"add_machinedeployment.go",
"add_machineset.go",
"add_node.go",
"noderef_controller.go",
"controller.go",
],
importpath = "sigs.k8s.io/cluster-api/pkg/controller",
visibility = ["//visibility:public"],
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/cluster/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//pkg/controller/error:go_default_library",
"//pkg/util:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library",
Expand Down
24 changes: 24 additions & 0 deletions pkg/controller/remote/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["util.go"],
importpath = "sigs.k8s.io/cluster-api/pkg/controller/remote",
visibility = ["//visibility:public"],
deps = [
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["util_test.go"],
embed = [":go_default_library"],
deps = [
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client/fake:go_default_library",
],
)
68 changes: 68 additions & 0 deletions pkg/controller/remote/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2019 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 remote

import (
"context"
"encoding/base64"
"fmt"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
kubeconfigSecretKey = "value"
)

// KubeConfigSecretName generates the expected name for the Kubeconfig secret
// to access a remote cluster given the cluster's name.
func KubeConfigSecretName(cluster string) string {
return fmt.Sprintf("%s-kubeconfig", cluster)
}

// GetKubeConfigSecret retrieves the KubeConfig Secret (if any)
// from the given cluster name and namespace.
func GetKubeConfigSecret(c client.Client, cluster, namespace string) (*corev1.Secret, error) {
secret := &corev1.Secret{}
secretKey := client.ObjectKey{
Namespace: namespace,
Name: KubeConfigSecretName(cluster),
}

if err := c.Get(context.TODO(), secretKey, secret); err != nil {
return nil, err
}

return secret, nil
}

// DecodeKubeConfigSecret uses the Secret to retrieve and decode the data.
func DecodeKubeConfigSecret(secret *corev1.Secret) ([]byte, error) {
encodedKubeconfig, ok := secret.Data[kubeconfigSecretKey]
if !ok {
return nil, errors.Errorf("missing value in secret %s/%s", secret.Namespace, secret.Name)
}

kubeconfig, err := base64.StdEncoding.DecodeString(string(encodedKubeconfig))
if err != nil {
return nil, errors.Wrapf(err, "cannot decode kubeconfig secret %s/%s", secret.Namespace, secret.Name)
}

return kubeconfig, nil
}
102 changes: 102 additions & 0 deletions pkg/controller/remote/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2019 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 remote

import (
"encoding/base64"
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var (
validKubeConfig = `
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRFNU1ERXhNREU0TURBME1Gb1hEVEk1TURFd056RTRNREEwTUZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBT1EvCmVndmViNk1qMHkzM3hSbGFjczd6OXE4QTNDajcrdnRrZ0pUSjRUSVB4TldRTEd0Q0dmL0xadzlHMW9zNmRKUHgKZFhDNmkwaHA5czJuT0Y2VjQvREUrYUNTTU45VDYzckdWb2s0TkcwSlJPYmlRWEtNY1VQakpiYm9PTXF2R2lLaAoyMGlFY0h5K3B4WkZOb3FzdnlaRGM5L2dRSHJVR1FPNXp6TDNHZGhFL0k1Nkczek9JaWhhbFRHSTNaakRRS05CCmVFV3FONTVDcHZzT3I1b0ZnTmZZTXk2YzZ4WXlUTlhWSUkwNFN0Z2xBbUk4bzZWaTNUVEJhZ1BWaldIVnRha1EKU2w3VGZtVUlIdndKZUo3b2hxbXArVThvaGVTQUIraHZSbDIrVHE5NURTemtKcmRjNmphcyswd2FWaEJydEh1agpWMU15NlNvV2VVUlkrdW5VVFgwQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFIT2thSXNsd0pCOE5PaENUZkF4UWlnaUc1bEMKQlo0LytGeHZ3Y1pnWGhlL0IyUWo1UURMNWlRUU1VL2NqQ0tyYUxkTFFqM1o1aHA1dzY0K2NWRUg3Vm9PSTFCaQowMm13YTc4eWo4aDNzQ2lLQXJiU21kKzNld1QrdlNpWFMzWk9EYWRHVVRRa1BnUHB0THlaMlRGakF0SW43WjcyCmpnYlVnT2FXaklKbnlwRVJ5UmlSKzBvWlk4SUlmWWFsTHUwVXlXcmkwaVhNRmZqQUQ1UVNURy8zRGN5djhEN1UKZHBxU2l5ekJkZVRjSExyenpEbktBeXhQWWgvcWpKZ0tRdEdIakhjY0FCSE1URlFtRy9Ea1pNTnZWL2FZSnMrKwp0aVJCbHExSFhlQ0d4aExFcGdQcGxVb3IrWmVYTGF2WUo0Z2dMVmIweGl2QTF2RUtyaUUwak1Wd2lQaz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
server: https://test-cluster-api:6443
name: test-cluster-api
contexts:
- context:
cluster: test-cluster-api
user: kubernetes-admin
name: kubernetes-admin@test-cluster-api
current-context: kubernetes-admin@test-cluster-api
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJUTJFS3c0cU0wbFV3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4T1RBeE1UQXhPREF3TkRCYUZ3MHlNREF4TVRBeE9EQXdOREphTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJk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091QkJFRE9oeXdiRnN5NC9ubVduTm4rQXRYQklDaGkKR2J6LzFuWkZTOGc2Nlh2cGFTWEQ5blVpS1BOUW5ORzUralJmclluTlI4WERCL3ZiNk9TMVFHbXBvWWxJZ2Q3UgpjTVpSRm1sbTUvbkJMWkdoVVpjOXZVN1pRVis4RXdLK2lHaGNrTFduVGhHNURZTkRWaksxcFE9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
`

validSecret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test1-kubeconfig",
Namespace: "test",
},
Data: map[string][]byte{
kubeconfigSecretKey: []byte(base64.StdEncoding.EncodeToString([]byte(validKubeConfig))),
},
}

invalidSecret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test2-kubeconfig",
Namespace: "test",
},
Data: map[string][]byte{
kubeconfigSecretKey: []byte("Not valid!!1"),
},
}
)

func TestGetKubeConfigSecret(t *testing.T) {
client := fake.NewFakeClient(validSecret)
found, err := GetKubeConfigSecret(client, "test1", "test")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

if !reflect.DeepEqual(validSecret, found) {
t.Fatalf("Expected found secret to be equal to input")
}
}

func TestDecodeKubeConfigSecret(t *testing.T) {
t.Run("with valid secret", func(t *testing.T) {
out, err := DecodeKubeConfigSecret(validSecret)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

if string(out) != validKubeConfig {
t.Fatalf("Expected decoded KubeConfig to match input")
}
})

t.Run("with invalid secret", func(t *testing.T) {
_, err := DecodeKubeConfigSecret(invalidSecret)
if err == nil {
t.Fatalf("Expected error, got nil")
}
})
}

0 comments on commit 65975e0

Please sign in to comment.