forked from opskumu/helm-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm.go
65 lines (58 loc) · 1.78 KB
/
helm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"github.com/gin-gonic/gin"
"os"
"github.com/golang/glog"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/kube"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
const (
XKubeToken = "X-KubeToken"
XKubeApiServer = "X-KubeApiServer"
)
type KubeInformation struct {
AimNamespace string
AimContext string
AimConfig string
AimKubeToken string
AimKubeAPIServer string
}
func InitKubeInformation(namespace, context, config string, c *gin.Context) *KubeInformation {
return &KubeInformation{
AimNamespace: namespace,
AimContext: context,
AimConfig: config,
AimKubeToken: c.GetHeader(XKubeToken),
AimKubeAPIServer: c.GetHeader(XKubeApiServer),
}
}
func actionConfigInit(kubeInfo *KubeInformation) (*action.Configuration, error) {
actionConfig := new(action.Configuration)
if kubeInfo.AimContext == "" {
kubeInfo.AimContext = settings.KubeContext
}
clientConfig := new(genericclioptions.ConfigFlags)
if kubeInfo.AimConfig == "" {
clientConfig = kube.GetConfig(settings.KubeConfig, kubeInfo.AimContext, kubeInfo.AimNamespace)
} else {
clientConfig = kube.GetConfig(kubeInfo.AimConfig, kubeInfo.AimContext, kubeInfo.AimNamespace)
}
if kubeInfo.AimKubeToken != "" {
clientConfig.BearerToken = &kubeInfo.AimKubeToken
*clientConfig.Insecure = true
} else if settings.KubeToken != "" {
clientConfig.BearerToken = &settings.KubeToken
}
if kubeInfo.AimKubeAPIServer != "" {
clientConfig.APIServer = &kubeInfo.AimKubeAPIServer
} else if settings.KubeAPIServer != "" {
clientConfig.APIServer = &settings.KubeAPIServer
}
err := actionConfig.Init(clientConfig, kubeInfo.AimNamespace, os.Getenv("HELM_DRIVER"), glog.Infof)
if err != nil {
glog.Errorf("%+v", err)
return nil, err
}
return actionConfig, nil
}