-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add remote.ClusterClient to access remote workload clusters #1006
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-sigs:master
from
vincepri:add-remote-cluster-client
Jun 11, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2019
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, will fix tomorrow in a follow-up PR!