-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API to get the Kubeconfig for arbitrary UCP resource
- Add API to get the kubeconfig for arbitrary UCP resource given the UCP context Signed-off-by: Prem Kumar Kalle <[email protected]>
- Loading branch information
Showing
6 changed files
with
644 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
apiVersion: v1 | ||
kind: Config | ||
current-context: foo-context | ||
preferences: | ||
colors: true | ||
clusters: | ||
- cluster: | ||
insecure-skip-tls-verify: true | ||
server: https://bar.org:4443 | ||
name: bar-cluster | ||
- cluster: | ||
insecure-skip-tls-verify: true | ||
server: https://foo.org:443 | ||
name: foo-cluster | ||
- cluster: | ||
insecure-skip-tls-verify: true | ||
server: https://api.tanzu.cloud.vmware.com:443/org/fake-org-id | ||
name: tanzu-cli-myucp/current | ||
contexts: | ||
- context: | ||
cluster: bar-cluster | ||
namespace: chisel-ns | ||
user: green-user | ||
name: bar-context | ||
- context: | ||
cluster: foo-cluster | ||
namespace: saw-ns | ||
user: blue-user | ||
name: foo-context | ||
- context: | ||
cluster: tanzu-cli-myucp/current | ||
user: tanzu-cli-myucp-user | ||
name: tanzu-cli-myucp | ||
users: | ||
- name: blue-user | ||
user: | ||
token: blue-token | ||
- name: green-user | ||
user: | ||
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJQ24vUDZ1S1pWWTR3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TURBeE1qVXdPRFV4TVRCYUZ3MHlNVEF4TWpRd09EVXhNVEphTURReApGekFWQmdOVkJBb1REbk41YzNSbG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b0dBUDZ1NGxrSjg0amRjTVROVTdYeW16ZkMwSkd5aFRNcWQ2YnV4NG9FQlViSFhoOW1DaVRMSmFaV1YKN0F5NkxTV21lRGIrWEFrS0tQL2UwQVMrWElKV2p6MDI5SFdDaXM0T3NZV3VqeksrVjVaVUVkQlJNUEN0QUo4ZApCaDBvRTliSUdIOVB0RTFIdTh6NGpGaG5WUmVqdmdhMzRQdG9QV3JNL01sZnQzLy9Lazg9Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg== | ||
- name: tanzu-cli-myucp-user | ||
user: | ||
exec: | ||
apiVersion: client.authentication.k8s.io/v1beta1 | ||
args: | ||
- context | ||
- get-token | ||
- myucp | ||
command: tanzu | ||
env: [] |
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 2023 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package kubeconfig provides the kubeconfig file access functionality | ||
package kubeconfig | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// ReadKubeConfig reads the kubeconfig file and returns the Config | ||
func ReadKubeConfig(path string) (*Config, error) { | ||
kubeconfig, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var config Config | ||
err = yaml.Unmarshal(kubeconfig, &config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &config, nil | ||
} | ||
|
||
// MinifyKubeConfig removes all information not used by the provided kubecontext name | ||
func MinifyKubeConfig(kubeconfig *Config, kubeContextName string) (*Config, error) { | ||
context := GetContext(kubeconfig, kubeContextName) | ||
if context == nil { | ||
return nil, errors.Errorf("context %q missing in the kubeconfig", kubeContextName) | ||
} | ||
|
||
clusterName := context.Context.Cluster | ||
userName := context.Context.AuthInfo | ||
|
||
cluster := GetCluster(kubeconfig, clusterName) | ||
if cluster == nil { | ||
return nil, errors.Errorf("cluster %q missing in the kubeconfig", clusterName) | ||
} | ||
user := GetAuthInfo(kubeconfig, userName) | ||
if user == nil { | ||
return nil, errors.Errorf("user %q missing in the kubeconfig", userName) | ||
} | ||
|
||
return &Config{ | ||
Kind: "Config", | ||
APIVersion: "v1", | ||
Clusters: []*Cluster{{Name: clusterName, Cluster: cluster.Cluster}}, | ||
AuthInfos: []*AuthInfo{{Name: userName, AuthInfo: user.AuthInfo}}, | ||
Contexts: []*Context{{Name: kubeContextName, Context: context.Context}}, | ||
CurrentContext: kubeContextName, | ||
}, nil | ||
} | ||
|
||
// GetContext returns the Context information from the provided config | ||
func GetContext(kubeconfig *Config, kubeContextName string) *Context { | ||
for idx := range kubeconfig.Contexts { | ||
if kubeconfig.Contexts[idx].Name == kubeContextName { | ||
return kubeconfig.Contexts[idx] | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// GetCluster returns the Cluster information from the provided config | ||
func GetCluster(kubeconfig *Config, clusterName string) *Cluster { | ||
for idx := range kubeconfig.Clusters { | ||
if kubeconfig.Clusters[idx].Name == clusterName { | ||
return kubeconfig.Clusters[idx] | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// GetAuthInfo returns the AuthInfo information from the provided config | ||
func GetAuthInfo(kubeconfig *Config, userName string) *AuthInfo { | ||
for idx := range kubeconfig.AuthInfos { | ||
if kubeconfig.AuthInfos[idx].Name == userName { | ||
return kubeconfig.AuthInfos[idx] | ||
} | ||
} | ||
return nil | ||
} |
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,106 @@ | ||
// Copyright 2023 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package kubeconfig | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const ConfigFilePermissions = 0o600 | ||
|
||
func copyFile(t *testing.T, sourceFile, destFile string) { | ||
input, err := os.ReadFile(sourceFile) | ||
assert.NoError(t, err) | ||
err = os.WriteFile(destFile, input, ConfigFilePermissions) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestReadAndMinifyKubeConfig(t *testing.T) { | ||
expectedMyucpKubeconfig := ` | ||
kind: Config | ||
apiVersion: v1 | ||
preferences: {} | ||
clusters: | ||
- name: tanzu-cli-myucp/current | ||
cluster: | ||
server: https://api.tanzu.cloud.vmware.com:443/org/fake-org-id | ||
insecure-skip-tls-verify: true | ||
users: | ||
- name: tanzu-cli-myucp-user | ||
user: | ||
exec: | ||
command: tanzu | ||
args: | ||
- context | ||
- get-token | ||
- myucp | ||
env: [] | ||
apiVersion: client.authentication.k8s.io/v1beta1 | ||
provideClusterInfo: false | ||
contexts: | ||
- name: tanzu-cli-myucp | ||
context: | ||
cluster: tanzu-cli-myucp/current | ||
user: tanzu-cli-myucp-user | ||
current-context: tanzu-cli-myucp | ||
` | ||
|
||
expectedFooContextKubeconfig := ` | ||
kind: Config | ||
apiVersion: v1 | ||
preferences: {} | ||
clusters: | ||
- name: foo-cluster | ||
cluster: | ||
server: https://foo.org:443 | ||
insecure-skip-tls-verify: true | ||
users: | ||
- name: blue-user | ||
user: | ||
token: blue-token | ||
contexts: | ||
- name: foo-context | ||
context: | ||
cluster: foo-cluster | ||
user: blue-user | ||
namespace: saw-ns | ||
current-context: foo-context | ||
` | ||
|
||
testKubeconfiFilePath := "../../../fakes/config/kubeconfig-1.yaml" | ||
kubeconfigFilePath, err := os.CreateTemp("", "config") | ||
assert.NoError(t, err) | ||
copyFile(t, testKubeconfiFilePath, kubeconfigFilePath.Name()) | ||
|
||
defer func() { | ||
_ = os.RemoveAll(kubeconfigFilePath.Name()) | ||
}() | ||
|
||
config, err := ReadKubeConfig(kubeconfigFilePath.Name()) | ||
assert.NoError(t, err) | ||
|
||
// Test with non-existing context | ||
_, err = MinifyKubeConfig(config, "non-existing-context") | ||
assert.Equal(t, err.Error(), `context "non-existing-context" missing in the kubeconfig`) | ||
|
||
// Test reading and minifying the kubeconfig using Token as user(AuthInfo) | ||
minifiedKubeconfig, err := MinifyKubeConfig(config, "foo-context") | ||
assert.NoError(t, err) | ||
wantKubeConfig := &Config{} | ||
err = yaml.Unmarshal([]byte(expectedFooContextKubeconfig), wantKubeConfig) | ||
assert.NoError(t, err) | ||
assert.Equal(t, minifiedKubeconfig, wantKubeConfig) | ||
|
||
// Test reading and minifying the kubeconfig having ExecConfig as user(AuthInfo) | ||
minifiedKubeconfig, err = MinifyKubeConfig(config, "tanzu-cli-myucp") | ||
assert.NoError(t, err) | ||
wantKubeConfig = &Config{} | ||
err = yaml.Unmarshal([]byte(expectedMyucpKubeconfig), wantKubeConfig) | ||
assert.NoError(t, err) | ||
assert.Equal(t, minifiedKubeconfig, wantKubeConfig) | ||
} |
Oops, something went wrong.