-
Notifications
You must be signed in to change notification settings - Fork 82
/
getscript.go
139 lines (112 loc) · 3.02 KB
/
getscript.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Authors of KubeArmor
package vm
import (
"context"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"github.com/kubearmor/kubearmor-client/k8s"
pb "github.com/kubearmor/kubearmor-client/vm/protobuf"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ScriptOptions for karmor vm getscript
type ScriptOptions struct {
Port string
VMName string
File string
}
var (
serviceAccountName = "kvmservice"
pbClient pb.HandleCliClient
namespace string
)
func initGRPCClient(ip string, port string) error {
grpcClientConn, err := grpc.DialContext(context.Background(), net.JoinHostPort(ip, port), grpc.WithInsecure())
if err != nil {
return err
}
pbClient = pb.NewHandleCliClient(grpcClientConn)
if pbClient == nil {
return errors.New("invalid grpc client handle")
}
return nil
}
func writeScriptDataToFile(options ScriptOptions, scriptData string) error {
filename := ""
if options.File == "none" {
filename = options.VMName + ".sh"
} else {
filename = options.File
}
file, err := os.Create(filepath.Clean(filename))
if err != nil {
return err
}
_, err = file.WriteString(scriptData)
if err != nil {
return err
}
fmt.Printf("VM installation script copied to %s\n", filename)
return nil
}
func getClusterIP(c *k8s.Client, options ScriptOptions) (string, error) {
externalIP := ""
svcInfo, err := c.K8sClientset.CoreV1().Services(namespace).Get(context.Background(), serviceAccountName, metav1.GetOptions{})
if err != nil {
return "", err
}
for _, lbIngress := range svcInfo.Status.LoadBalancer.Ingress {
externalIP = lbIngress.IP
break
}
return externalIP, err
}
// GetScript - Function to handle script download for vm option
func GetScript(c *k8s.Client, options ScriptOptions, httpIP string, isNonK8sEnv bool) error {
var (
clusterIP string
err error
)
if isNonK8sEnv {
// Consider as kubectl is not configured
clusterIP = httpIP
} else {
// Get the list of namespaces in kubernetes context
namespaces, err := c.K8sClientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
return err
}
for _, ns := range namespaces.Items {
// Fetch the namespace of kvmservice
if _, err := c.K8sClientset.CoreV1().ServiceAccounts(ns.Name).Get(context.Background(), serviceAccountName, metav1.GetOptions{}); err != nil {
continue
}
namespace = ns.Name
break
}
clusterIP, err := getClusterIP(c, options)
if err != nil || clusterIP == "" {
return err
}
}
err = initGRPCClient(clusterIP, options.Port)
if err != nil {
log.Error().Msgf("unable to connect to grpc server: %s", err.Error())
return err
}
response, err := pbClient.HandleCliRequest(context.Background(), &pb.CliRequest{KvmName: options.VMName})
if err != nil {
return err
}
if response.Status == 0 {
err = writeScriptDataToFile(options, response.ScriptData)
} else {
return errors.New(response.StatusMsg)
}
return err
}