K8s helm plugin code
[ealt-edge.git] / mecm / mepm / applcm / k8shelm / pkg / plugin / helmclient.go
1 package plugin
2
3 import (
4         "bytes"
5         "fmt"
6         "helm.sh/helm/v3/pkg/action"
7         "helm.sh/helm/v3/pkg/chart/loader"
8         "helm.sh/helm/v3/pkg/kube"
9         "log"
10         "os"
11 )
12
13 const releaseNamespace  = "default"
14 const chartPath  = "/go/release/charts/"
15 const kubeconfigPath  = "/go/release/kubeconfig/"
16
17 //const chartPath  = "/home/root1/code/mecm/mepm/applcm/k8shelm/pkg/plugin/"
18 //const kubeconfigPath  = "/home/root1/"
19
20 func installChart(helmPkg bytes.Buffer, hostIP string) string {
21         logger := log.New(os.Stdout, "helmplugin ", log.LstdFlags|log.Lshortfile)
22         logger.Println("Inside helm client")
23
24         file, err := os.Create(chartPath + "temp.tar.gz")
25         if err != nil {
26                 logger.Printf("unable to create file")
27         }
28
29         _, err = helmPkg.WriteTo(file)
30         if err != nil {
31                 logger.Printf("uanble to write to file")
32         }
33
34         chart, err := loader.Load(chartPath + "temp.tar.gz")
35         if err != nil {
36
37                 panic(err)
38         }
39
40         releaseName := chart.Metadata.Name
41
42         kubeconfig := kubeconfigPath + hostIP
43
44         actionConfig := new(action.Configuration)
45         if err := actionConfig.Init(kube.GetConfig(kubeconfig, "", releaseNamespace), releaseNamespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
46                 fmt.Sprintf(format, v)
47         }); err != nil {
48                 panic(err)
49         }
50
51         iCli := action.NewInstall(actionConfig)
52         iCli.Namespace = releaseNamespace
53         iCli.ReleaseName = releaseName
54         rel, err := iCli.Run(chart, nil)
55         if err != nil {
56                 panic(err)
57         }
58         fmt.Println("Successfully installed release: ", rel.Name)
59         return rel.Name
60 }
61
62 func uninstallChart(relName string, hostIP string) {
63         kubeconfig := kubeconfigPath + hostIP
64         actionConfig := new(action.Configuration)
65         if err := actionConfig.Init(kube.GetConfig(kubeconfig, "", releaseNamespace), releaseNamespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
66                 fmt.Sprintf(format, v)
67         }); err != nil {
68                 panic(err)
69         }
70         iCli := action.NewUninstall(actionConfig)
71         res, err := iCli.Run(relName);
72         if err != nil {
73                 panic(err)
74         }
75         fmt.Println("Successfully uninstalled release: ", res.Info)
76 }
77
78 func queryChart(relName string, hostIP string) string  {
79         kubeconfig := kubeconfigPath + hostIP
80         actionConfig := new(action.Configuration)
81         if err := actionConfig.Init(kube.GetConfig(kubeconfig, "", releaseNamespace), releaseNamespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
82                 fmt.Sprintf(format, v)
83         }); err != nil {
84                 panic(err)
85         }
86         iCli := action.NewStatus(actionConfig)
87         res, err := iCli.Run(relName)
88         if err != nil {
89                 panic(err)
90         }
91         return res.Info.Status.String()
92 }
93