-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add remote.ClusterClient to access remote workload clusters (#1006)
Signed-off-by: Vince Prignano <[email protected]>
- Loading branch information
1 parent
f96ac27
commit 0b25546
Showing
5 changed files
with
181 additions
and
5 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,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()) | ||
} |
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,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) | ||
} | ||
}) | ||
|
||
} |
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