I am trying to run a tool(Tekton cli) in a container.
It could be any tool using kubeconfig, as long I can give it a parameter of kubeconfig.
In code I can use InClusterConfig
, but I am using a tool that do not use InClusterConfig
, so I was thinking maybe I can write out the kubeconfig file from client-go InClusterConfig
and use it as a parameter to the tool(Tekton cli)
I found some usage of clientcmd.WriteToFile but I can not figure out how to convert rest.Config to api.Config - and why are there so many Config types.
Anyways do you know how to write out restclient.InClusterConfig
to a file, so it can be used as kubeconfig file?
if clusterConfig, err := restclient.InClusterConfig(); err == nil {
if tempFile, err := ioutil.TempFile(os.TempDir(), "kubeconfig-"); err == nil {
// problem >> kubeConfig := createKubeConfig(clusterConfig)
clientcmd.WriteToFile(*kubeConfig, tempFile.Name())
}
}
A ClientConfig
may be instantiated based on a KubeConfig
. The ClientConfig
has a method ClientConfig()
which returns *rest.Config
; it also has a method RawConfig()
which may be used to get an api.Config
.
The KubeConfig
may then be written back to a file using the clientCmd.WriteToFile()
method and passing the api.Config
returned from RawConfig()
.
package main
import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"log"
)
var clientCfg clientcmd.ClientConfig
clientCfg, err = clientcmd.NewClientConfigFromBytes(kubeConfig)
if err != nil {
log.Fatal(err)
}
var restCfg *rest.Config
restCfg, err = clientCfg.ClientConfig()
if err != nil {
log.Fatal(err)
}
var apiCfg api.Config
apiCfg, err = clientCfg.RawConfig()
if err != nil {
log.Fatal(err)
}
err = clientcmd.WriteToFile(apiCfg, filename)
if err != nil {
log.Fatal(err)
}