This repository has been archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
startup.go
110 lines (100 loc) · 2.73 KB
/
startup.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
package cmds
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
proto "github.com/appscode/api/kubernetes/v1beta1"
"github.com/appscode/krank/cloud"
"github.com/appscode/log"
"github.com/appscode/pharmer/api"
"github.com/golang/protobuf/jsonpb"
"github.com/spf13/cobra"
)
func NewCmdStartup() *cobra.Command {
var resp proto.ClusterStartupConfigResponse
cmd := &cobra.Command{
Use: "krank",
Short: "Bootstrap as a Kubernetes master or node",
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
reader := bufio.NewReader(os.Stdin)
conf, err := ioutil.ReadAll(reader)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
log.Infof("conf: %v", string(conf))
err = jsonpb.UnmarshalString(string(conf), &resp)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var req api.ClusterStartupConfig
err = json.Unmarshal([]byte(resp.Configuration), &req)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
osFamily := OSFamily(req.OS)
fmt.Printf("Using Provider=%v and OSFamily=%v\n", req.Provider, osFamily)
if req.Provider == "" {
fmt.Fprintln(os.Stderr, "Select a cloud.")
os.Exit(1)
}
if osFamily == "" {
fmt.Fprintln(os.Stderr, "Select an os.")
os.Exit(1)
}
if req.Role == "" {
fmt.Fprintln(os.Stderr, "Select a Kubernetes role for this node.")
os.Exit(1)
}
labels, err := api.ParseNodeLabels(req.NodeLabels)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to parse node labels.")
os.Exit(1)
}
roleLabel := "node"
if req.KubernetesMaster {
roleLabel = "master"
}
req.NodeLabels = labels.
WithInt64(api.NodeLabelKey_ContextVersion, 1 /*req.ContextVersion*/).
WithString(api.NodeLabelKey_Role, roleLabel).
WithString(api.NodeLabelKey_SKU, resp.Sku).
String()
selector := req.Provider + "-" + osFamily
for k := range cloud.CloudProviders() {
log.Infof("Found kube starters for %v", k)
}
script, err := cloud.GetCloudProvider(selector, nil)
if err != nil {
req.Provider = "generic"
log.Infof("Using selector: %v-%v", req.Provider, osFamily)
script, err = cloud.GetCloudProvider(req.Provider+"-"+osFamily, nil)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
err = script.Run(&req)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
}
cmd.Flags().AddGoFlagSet(flag.CommandLine)
cmd.Flags().StringVar(&resp.Sku, "sku", "", "SKU of this instance")
return cmd
}
func OSFamily(os string) string {
os = strings.ToLower(os)
os = strings.Replace(os, "_", " ", -1)
os = strings.Replace(os, "-", " ", -1)
return strings.Split(os, " ")[0]
}