Skip to content

Instantly share code, notes, and snippets.

@abohmeed
Created June 1, 2020 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abohmeed/03db4cdc7a1866dbfcc0d68641397963 to your computer and use it in GitHub Desktop.
Save abohmeed/03db4cdc7a1866dbfcc0d68641397963 to your computer and use it in GitHub Desktop.
kubeConfig, err := clientcmd.LoadFromFile(kubeconfig)
check("The following error occured while loading the KubeConfig file", err)
if _, v := kubeConfig.Clusters[*clusterPtr]; !v {
log.Fatal(fmt.Sprintf("Cluster \"%s\" was not found in the current Kube Config file", *clusterPtr))
}
kc := &KubeConfig{
APIVersion: "v1",
Clusters: Clusters{
0: {
Cluster{
base64.StdEncoding.EncodeToString([]byte(kubeConfig.Clusters[*clusterPtr].CertificateAuthorityData)),
kubeConfig.Clusters[*clusterPtr].Server,
},
*clusterPtr,
},
},
Contexts: Contexts{
0: {
Context{
Cluster: *clusterPtr,
User: *usernamePtr,
},
*clusterPtr,
},
},
CurrentContext: *clusterPtr,
Kind: "Config",
Users: Users{
0: {
User{
ClientCertificateData: base64.StdEncoding.EncodeToString(csr.Status.Certificate),
ClientKeyData: base64.StdEncoding.EncodeToString(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: keyDer})),
},
*usernamePtr,
},
},
}
dir, err := os.Getwd()
check("The following error occured while getting the current working directory %s", err)
_, err = os.Create(filepath.Join(dir, *usernamePtr))
check("The following error occured while creating the target file %s", err)
file, err := os.OpenFile(*usernamePtr, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
check("The following error occured while creating the target Kube Config file", err)
defer file.Close()
e := yaml.NewEncoder(file)
err = e.Encode(kc)
check("The following error occured while writing YAML to the target Kube Config file", err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment