mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: crenshaw-dev <[email protected]>
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package util
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"os/user"
|
|
"path"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
appclientset "github.com/argoproj/argo-cd/v3/pkg/client/clientset/versioned"
|
|
)
|
|
|
|
type Kube struct {
|
|
Namespace string
|
|
Context string
|
|
}
|
|
|
|
func getDefaultKubeConfigPath(homeDir string) string {
|
|
return path.Join(homeDir, ".kube", "config")
|
|
}
|
|
|
|
func getKubeConfigPath() string {
|
|
var kubeConfigPath string
|
|
currentUser, _ := user.Current()
|
|
if currentUser != nil {
|
|
kubeConfigPath = os.Getenv("KUBECONFIG")
|
|
if kubeConfigPath == "" {
|
|
kubeConfigPath = getDefaultKubeConfigPath(currentUser.HomeDir)
|
|
}
|
|
}
|
|
return kubeConfigPath
|
|
}
|
|
|
|
func ConnectToK8sArgoClientSet() *appclientset.Clientset {
|
|
config, err := clientcmd.BuildConfigFromFlags("", getKubeConfigPath())
|
|
if err != nil {
|
|
log.Panicln("failed to create Argocd K8s config")
|
|
}
|
|
return appclientset.NewForConfigOrDie(config)
|
|
}
|
|
|
|
func ConnectToK8sConfig() *rest.Config {
|
|
config, err := clientcmd.BuildConfigFromFlags("", getKubeConfigPath())
|
|
if err != nil {
|
|
log.Panicln("failed to create K8s config")
|
|
}
|
|
return config
|
|
}
|
|
|
|
func ConnectToK8sClientSet() *kubernetes.Clientset {
|
|
config, err := clientcmd.BuildConfigFromFlags("", getKubeConfigPath())
|
|
if err != nil {
|
|
log.Panicln("failed to create K8s config")
|
|
}
|
|
return kubernetes.NewForConfigOrDie(config)
|
|
}
|