Skip to content

Commit

Permalink
Add remote.ClusterClient to access remote workload clusters (#1006)
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri authored and k8s-ci-robot committed Jun 11, 2019
1 parent f96ac27 commit 0b25546
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 5 deletions.
17 changes: 15 additions & 2 deletions pkg/controller/remote/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

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

go_test(
name = "go_default_test",
srcs = ["util_test.go"],
srcs = [
"cluster_test.go",
"util_test.go",
],
embed = [":go_default_library"],
deps = [
"//pkg/apis/cluster/v1alpha1:go_default_library",
"//vendor/k8s.io/api/core/v1: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/sigs.k8s.io/controller-runtime/pkg/client/fake:go_default_library",
],
Expand Down
68 changes: 68 additions & 0 deletions pkg/controller/remote/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2018 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 (
"github.com/pkg/errors"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ClusterClient is a helper struct to connect to remote workload clusters.
type ClusterClient struct {
restConfig *restclient.Config
cluster *v1alpha1.Cluster
}

// NewClusterClient creates a new ClusterClient instance.
func NewClusterClient(c client.Client, cluster *v1alpha1.Cluster) (*ClusterClient, error) {
secret, err := GetKubeConfigSecret(c, cluster.Name, cluster.Namespace)
if err != nil {
return nil, errors.Wrapf(err, "failed to retrieve kubeconfig secret for Cluster %q in namespace %q",
cluster.Name, cluster.Namespace)
}

kubeconfig, err := DecodeKubeConfigSecret(secret)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode kubeconfig secret for Cluster %q in namespace %q",
cluster.Name, cluster.Namespace)
}

restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeconfig)
if err != nil {
return nil, errors.Wrapf(err, "failed to create client configuration for Cluster %q in namespace %q",
cluster.Name, cluster.Namespace)
}

return &ClusterClient{
restConfig: restConfig,
cluster: cluster,
}, nil
}

// RESTConfig returns a configuration instance to be used with a Kubernetes client.
func (c *ClusterClient) RESTConfig() *restclient.Config {
return c.restConfig
}

// CoreV1 returns a new Kubernetes CoreV1 client.
func (c *ClusterClient) CoreV1() (corev1.CoreV1Interface, error) {
return corev1.NewForConfig(c.RESTConfig())
}
86 changes: 86 additions & 0 deletions pkg/controller/remote/cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
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 (
"strings"
"testing"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var (
clusterWithValidKubeConfig = &v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test1",
Namespace: "test",
},
}

clusterWithInvalidKubeConfig = &v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test2",
Namespace: "test",
},
}

clusterWithNoKubeConfig = &v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test3",
Namespace: "test",
},
}
)

func TestNewClusterClient(t *testing.T) {
t.Run("cluster with valid kubeconfig", func(t *testing.T) {
client := fake.NewFakeClient(validSecret)
c, err := NewClusterClient(client, clusterWithValidKubeConfig)
if err != nil {
t.Fatalf("Expected no errors, got %v", err)
}

if c == nil {
t.Fatal("Expected actual client, got nil")
}

restConfig := c.RESTConfig()
if restConfig.Host != "https://test-cluster-api:6443" {
t.Fatalf("Unexpected Host value in RESTConfig: %q", restConfig.Host)
}
})

t.Run("cluster with no kubeconfig", func(t *testing.T) {
client := fake.NewFakeClient()
_, err := NewClusterClient(client, clusterWithNoKubeConfig)
if !strings.Contains(err.Error(), "secret not found") {
t.Fatalf("Expected not found error, got %v", err)
}
})

t.Run("cluster with invalid kubeconfig", func(t *testing.T) {
client := fake.NewFakeClient(invalidSecret)
_, err := NewClusterClient(client, clusterWithInvalidKubeConfig)
if err == nil || apierrors.IsNotFound(err) {
t.Fatalf("Expected error, got %v", err)
}
})

}
13 changes: 11 additions & 2 deletions pkg/controller/remote/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ import (

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

const (
kubeconfigSecretKey = "value"
)

var (
ErrSecretNotFound = errors.New("secret not found")
ErrSecretMissingValue = errors.New("missing value in secret")
)

// KubeConfigSecretName generates the expected name for the Kubeconfig secret
// to access a remote cluster given the cluster's name.
func KubeConfigSecretName(cluster string) string {
Expand All @@ -46,6 +52,9 @@ func GetKubeConfigSecret(c client.Client, cluster, namespace string) (*corev1.Se
}

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

Expand All @@ -56,12 +65,12 @@ func GetKubeConfigSecret(c client.Client, cluster, namespace string) (*corev1.Se
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)
return nil, ErrSecretMissingValue
}

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 nil, err
}

return kubeconfig, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/remote/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ users:
- name: kubernetes-admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJUTJFS3c0cU0wbFV3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4T1RBeE1UQXhPREF3TkRCYUZ3MHlNREF4TVRBeE9EQXdOREphTURReApGekFWQmdOVkJBb1REbk41YzNS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