-
Notifications
You must be signed in to change notification settings - Fork 20
/
kube.go
93 lines (73 loc) · 2.44 KB
/
kube.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var (
// kubeSecurityGroup represents the firewall security group to add k8s VM instances into
kubeSecurityGroup = "exokube"
// kubeTagKubernetes represents the name of the tag used to store the version of Kubernetes
kubeTagKubernetes = "exokube:kubernetes"
// kubeTagCalico represents the name of the tag used to store the version of Calico
kubeTagCalico = "exokube:calico"
// kubeTagDocker represents the name of the tag used to store the version of Docker
kubeTagDocker = "exokube:docker"
)
// kubeCmd represents the kube command
var kubeCmd = &cobra.Command{
Use: "kube",
Short: "Standalone Kubernetes cluster management",
Long: `These commands allow you to bootstrap standalone Kubernetes cluster
instances in a similar fashion as Minikube. It runs a single-node Kubernetes
cluster inside an Exoscale VM for users looking to try out Kubernetes or develop
with it day-to-day.`,
}
func getKubeInstanceVersion(vm *egoscale.VirtualMachine, tagName string) string {
for _, tag := range vm.Tags {
if tag.Key == tagName {
return tag.Value
}
}
return ""
}
func getKubeconfigPath(clusterName string) string {
return path.Join(gConfigFolder, "kube", clusterName)
}
func saveKubeData(clusterName, key string, data []byte) error {
if _, err := os.Stat(getKubeconfigPath(clusterName)); os.IsNotExist(err) {
if err := os.MkdirAll(getKubeconfigPath(clusterName), os.ModePerm); err != nil {
return fmt.Errorf("unable to create directory: %s", err)
}
}
if err := ioutil.WriteFile(path.Join(getKubeconfigPath(clusterName), key), data, 0600); err != nil {
return fmt.Errorf("unable to write file: %s", err)
}
return nil
}
func getKubeVM(clusterName string) (*egoscale.VirtualMachine, error) {
filename := path.Join(getKubeconfigPath(clusterName), "instance")
content, err := ioutil.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("%q: no such cluster", clusterName)
}
return nil, err
}
return getVirtualMachineByNameOrID(string(string(content)))
}
func deleteKubeData(clusterName string) error {
folder := getKubeconfigPath(clusterName)
if _, err := os.Stat(folder); !os.IsNotExist(err) {
if err := os.RemoveAll(folder); err != nil {
return fmt.Errorf("the Kubernetes cluster configuration could not be deleted: %s", err)
}
}
return nil
}
func init() {
labCmd.AddCommand(kubeCmd)
}