Skip to content

Commit

Permalink
Add GenerateKubeConfiguration helper (#541)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
tamalsaha authored Jul 25, 2023
1 parent a78b13d commit 9576747
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tools/clientcmd/rest_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright AppsCode Inc. and Contributors
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 clientcmd

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
)

// https://github.com/kubernetes/client-go/issues/711#issuecomment-730112049
func GenerateKubeConfiguration(cfg *rest.Config, namespace string) ([]byte, error) {
if err := rest.LoadTLSFiles(cfg); err != nil {
return nil, err
}

clusters := make(map[string]*clientcmdapi.Cluster)
clusters["default-cluster"] = &clientcmdapi.Cluster{
Server: cfg.Host,
CertificateAuthorityData: cfg.CAData,
}

contexts := make(map[string]*clientcmdapi.Context)
contexts["default-context"] = &clientcmdapi.Context{
Cluster: "default-cluster",
Namespace: namespace,
AuthInfo: "default-user",
}

authinfos := make(map[string]*clientcmdapi.AuthInfo)
authinfos["default-user"] = &clientcmdapi.AuthInfo{
LocationOfOrigin: "",
ClientCertificate: "",
ClientCertificateData: cfg.CertData,
ClientKey: "",
ClientKeyData: cfg.KeyData,
Token: cfg.BearerToken,
TokenFile: "",
Impersonate: cfg.Impersonate.UserName,
ImpersonateUID: cfg.Impersonate.UID,
ImpersonateGroups: cfg.Impersonate.Groups,
ImpersonateUserExtra: cfg.Impersonate.Extra,
Username: cfg.Username,
Password: cfg.Password,
AuthProvider: cfg.AuthProvider,
Exec: cfg.ExecProvider,
Extensions: nil,
}

clientConfig := clientcmdapi.Config{
Kind: "Config",
APIVersion: "v1",
Clusters: clusters,
Contexts: contexts,
CurrentContext: "default-context",
AuthInfos: authinfos,
}
return runtime.Encode(clientcmdlatest.Codec, &clientConfig)
}

0 comments on commit 9576747

Please sign in to comment.