Skip to content
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 namespace to cluster resource. #1255

Merged
merged 3 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/kubeconfigwriter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func createKubeconfigFile(resource *v1alpha1.ClusterResource, logger *zap.Sugare
context := &clientcmdapi.Context{
Cluster: resource.Name,
AuthInfo: resource.Username,
// Namespace isn't written to kubeconfig if this is empty
Namespace: resource.Namespace,
This conversation was marked as resolved.
Show resolved Hide resolved
}
c := clientcmdapi.NewConfig()
c.Clusters[resource.Name] = cluster
Expand Down
1 change: 1 addition & 0 deletions docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ The Cluster resource has the following parameters:
- `url` (required): Host url of the master node
- `username` (required): the user with access to the cluster
- `password`: to be used for clusters with basic auth
- `namespace`: The namespace to target in the cluster
- `token`: to be used for authentication, if present will be used ahead of the
password
- `insecure`: to indicate server should be accessed without verifying the TLS
Expand Down
26 changes: 15 additions & 11 deletions pkg/apis/pipeline/v1alpha1/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ type ClusterResource struct {
URL string `json:"url"`
Revision string `json:"revision"`
// Server requires Basic authentication
Username string `json:"username"`
Password string `json:"password"`
Username string `json:"username"`
Password string `json:"password"`
Namespace string `json:"namespace"`
// Server requires Bearer authentication. This client will not attempt to use
// refresh tokens for an OAuth2 flow.
// Token overrides userame and password
Expand Down Expand Up @@ -74,6 +75,8 @@ func NewClusterResource(r *PipelineResource) (*ClusterResource, error) {
clusterResource.Revision = param.Value
case strings.EqualFold(param.Name, "Username"):
clusterResource.Username = param.Value
case strings.EqualFold(param.Name, "Namespace"):
clusterResource.Namespace = param.Value
case strings.EqualFold(param.Name, "Password"):
clusterResource.Password = param.Value
case strings.EqualFold(param.Name, "Token"):
Expand Down Expand Up @@ -121,15 +124,16 @@ func (s *ClusterResource) GetURL() string {
// Replacements is used for template replacement on a ClusterResource inside of a Taskrun.
func (s *ClusterResource) Replacements() map[string]string {
return map[string]string{
"name": s.Name,
"type": string(s.Type),
"url": s.URL,
"revision": s.Revision,
"username": s.Username,
"password": s.Password,
"token": s.Token,
"insecure": strconv.FormatBool(s.Insecure),
"cadata": string(s.CAData),
"name": s.Name,
"type": string(s.Type),
"url": s.URL,
"revision": s.Revision,
"username": s.Username,
"password": s.Password,
"namespace": s.Namespace,
"token": s.Token,
"insecure": strconv.FormatBool(s.Insecure),
"cadata": string(s.CAData),
}
}

Expand Down
20 changes: 19 additions & 1 deletion pkg/apis/pipeline/v1alpha1/cluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ func TestNewClusterResource(t *testing.T) {
Token: "my-token",
Insecure: true,
},
}, {
desc: "basic cluster resource with namespace",
resource: tb.PipelineResource("test-cluster-resource", "default", tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeCluster,
tb.PipelineResourceSpecParam("name", "test_cluster_resource"),
tb.PipelineResourceSpecParam("url", "http://10.10.10.10"),
tb.PipelineResourceSpecParam("cadata", "bXktY2x1c3Rlci1jZXJ0Cg"),
tb.PipelineResourceSpecParam("token", "my-token"),
tb.PipelineResourceSpecParam("namespace", "my-namespace"),
)),
want: &v1alpha1.ClusterResource{
Name: "test_cluster_resource",
Type: v1alpha1.PipelineResourceTypeCluster,
URL: "http://10.10.10.10",
CAData: []byte("my-cluster-cert"),
Token: "my-token",
Namespace: "my-namespace",
},
}, {
desc: "basic resource with secrets",
resource: tb.PipelineResource("test-cluster-resource", "default", tb.PipelineResourceSpec(
Expand Down Expand Up @@ -132,7 +150,7 @@ func Test_ClusterResource_GetDownloadSteps(t *testing.T) {
Name: "kubeconfig-9l9zj",
Image: "override-with-kubeconfig-writer:latest",
Command: []string{"/ko-app/kubeconfigwriter"},
Args: []string{"-clusterConfig", `{"name":"test-cluster-resource","type":"cluster","url":"http://10.10.10.10","revision":"","username":"","password":"","token":"","Insecure":false,"cadata":null,"secrets":[{"fieldName":"cadata","secretKey":"cadatakey","secretName":"secret1"}]}`},
Args: []string{"-clusterConfig", `{"name":"test-cluster-resource","type":"cluster","url":"http://10.10.10.10","revision":"","username":"","password":"","namespace":"","token":"","Insecure":false,"cadata":null,"secrets":[{"fieldName":"cadata","secretKey":"cadatakey","secretName":"secret1"}]}`},
Env: []corev1.EnvVar{{
Name: "CADATA",
ValueFrom: &corev1.EnvVarSource{
Expand Down
7 changes: 5 additions & 2 deletions pkg/reconciler/taskrun/resources/input_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func setUp(t *testing.T) {
}, {
Name: "Url",
Value: "http://10.10.10.10",
}, {
Name: "Namespace",
Value: "namespace1",
}, {
Name: "CAdata",
// echo "my-ca-cert" | base64
Expand Down Expand Up @@ -639,7 +642,7 @@ func TestAddResourceToTask(t *testing.T) {
Image: "override-with-kubeconfig-writer:latest",
Command: []string{"/ko-app/kubeconfigwriter"},
Args: []string{
"-clusterConfig", `{"name":"cluster3","type":"cluster","url":"http://10.10.10.10","revision":"","username":"","password":"","token":"","Insecure":false,"cadata":"bXktY2EtY2VydAo=","secrets":null}`,
"-clusterConfig", `{"name":"cluster3","type":"cluster","url":"http://10.10.10.10","revision":"","username":"","password":"","namespace":"namespace1","token":"","Insecure":false,"cadata":"bXktY2EtY2VydAo=","secrets":null}`,
},
}}},
},
Expand Down Expand Up @@ -681,7 +684,7 @@ func TestAddResourceToTask(t *testing.T) {
Image: "override-with-kubeconfig-writer:latest",
Command: []string{"/ko-app/kubeconfigwriter"},
Args: []string{
"-clusterConfig", `{"name":"cluster2","type":"cluster","url":"http://10.10.10.10","revision":"","username":"","password":"","token":"","Insecure":false,"cadata":null,"secrets":[{"fieldName":"cadata","secretKey":"cadatakey","secretName":"secret1"}]}`,
"-clusterConfig", `{"name":"cluster2","type":"cluster","url":"http://10.10.10.10","revision":"","username":"","password":"","namespace":"","token":"","Insecure":false,"cadata":null,"secrets":[{"fieldName":"cadata","secretKey":"cadatakey","secretName":"secret1"}]}`,
},
Env: []corev1.EnvVar{{
ValueFrom: &corev1.EnvVarSource{
Expand Down