Hidde Beydals 4 years ago
parent 495abf42ef
commit 8e2316ba62

@ -26,12 +26,13 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"text/template" "text/template"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1" rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime" apiruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/kustomize/api/filesys" "sigs.k8s.io/kustomize/api/filesys"
@ -120,13 +121,16 @@ func (*Utils) execTemplate(obj interface{}, tmpl, filename string) error {
return file.Sync() return file.Sync()
} }
func (*Utils) kubeClient(config string) (client.Client, error) { func (*Utils) kubeClient(kubeConfigPath string) (client.Client, error) {
cfg, err := clientcmd.BuildConfigFromFlags("", config) configFiles := utils.splitKubeConfigPath(kubeConfigPath)
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{Precedence: configFiles},
&clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil { if err != nil {
return nil, fmt.Errorf("Kubernetes client initialization failed: %w", err) return nil, fmt.Errorf("kubernetes client initialization failed: %w", err)
} }
scheme := runtime.NewScheme() scheme := apiruntime.NewScheme()
_ = corev1.AddToScheme(scheme) _ = corev1.AddToScheme(scheme)
_ = rbacv1.AddToScheme(scheme) _ = rbacv1.AddToScheme(scheme)
_ = sourcev1.AddToScheme(scheme) _ = sourcev1.AddToScheme(scheme)
@ -137,12 +141,27 @@ func (*Utils) kubeClient(config string) (client.Client, error) {
Scheme: scheme, Scheme: scheme,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("Kubernetes client initialization failed: %w", err) return nil, fmt.Errorf("kubernetes client initialization failed: %w", err)
} }
return kubeClient, nil return kubeClient, nil
} }
// splitKubeConfigPath splits the given KUBECONFIG path based on the runtime OS
// target.
//
// Ref: https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#the-kubeconfig-environment-variable
func (*Utils) splitKubeConfigPath(path string) []string {
var sep string
switch runtime.GOOS {
case "windows":
sep = ";"
default:
sep = ":"
}
return strings.Split(path, sep)
}
func (*Utils) writeFile(content, filename string) error { func (*Utils) writeFile(content, filename string) error {
file, err := os.Create(filename) file, err := os.Create(filename)
if err != nil { if err != nil {

Loading…
Cancel
Save