This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
forked from Azure/draft-classic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
draft.go
177 lines (152 loc) · 4.34 KB
/
draft.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the MIT license.
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/helm/pkg/kube"
"github.com/Azure/draft/pkg/draft"
"github.com/Azure/draft/pkg/draft/draftpath"
"github.com/Azure/draft/pkg/draftd/portforwarder"
)
const (
hostEnvVar = "DRAFT_HOST"
homeEnvVar = "DRAFT_HOME"
)
var (
// flagDebug is a signal that the user wants additional output.
flagDebug bool
kubeContext string
// draftdTunnel is a tunnelled connection used to send requests to Draftd.
// TODO refactor out this global var
draftdTunnel *kube.Tunnel
// draftHome depicts the home directory where all Draft config is stored.
draftHome string
// draftHost depicts where the Draftd server is hosted. This is used when the port forwarding logic by Kubernetes is unavailable.
draftHost string
)
var globalUsage = `The application deployment tool for Kubernetes.
`
func newRootCmd(out io.Writer, in io.Reader) *cobra.Command {
cmd := &cobra.Command{
Use: "draft",
Short: "The application deployment tool for Kubernetes.",
Long: globalUsage,
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if flagDebug {
log.SetLevel(log.DebugLevel)
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
teardown()
},
}
p := cmd.PersistentFlags()
p.StringVar(&draftHome, "home", defaultDraftHome(), "location of your Draft config. Overrides $DRAFT_HOME")
p.BoolVar(&flagDebug, "debug", false, "enable verbose output")
p.StringVar(&kubeContext, "kube-context", "", "name of the kubeconfig context to use")
p.StringVar(&draftHost, "host", defaultDraftHost(), "address of Draftd. Overrides $DRAFT_HOST")
cmd.AddCommand(
newCreateCmd(out),
newHomeCmd(out),
newInitCmd(out, in),
newUpCmd(out),
newVersionCmd(out),
newPluginCmd(out),
)
// Find and add plugins
loadPlugins(cmd, draftpath.Home(homePath()), out, in)
return cmd
}
func setupConnection(c *cobra.Command, args []string) error {
if draftHost == "" {
clientset, config, err := getKubeClient(kubeContext)
if err != nil {
return err
}
clientConfig, err := config.ClientConfig()
if err != nil {
return err
}
tunnel, err := portforwarder.New(clientset, clientConfig)
if err != nil {
return err
}
draftHost = fmt.Sprintf("localhost:%d", tunnel.Local)
log.Debugf("Created tunnel using local port: '%d'", tunnel.Local)
}
log.Debugf("SERVER: %q", draftHost)
return nil
}
func teardown() {
if draftdTunnel != nil {
draftdTunnel.Close()
}
}
func ensureDraftClient(client *draft.Client) *draft.Client {
if client == nil {
return draft.NewClient(&draft.ClientConfig{
ServerAddr: draftHost,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
}
return client
}
func defaultDraftHost() string {
return os.Getenv(hostEnvVar)
}
func defaultDraftHome() string {
if home := os.Getenv(homeEnvVar); home != "" {
return home
}
homeEnvPath := os.Getenv("HOME")
if homeEnvPath == "" && runtime.GOOS == "windows" {
homeEnvPath = os.Getenv("USERPROFILE")
}
return filepath.Join(homeEnvPath, ".draft")
}
func homePath() string {
return os.ExpandEnv(draftHome)
}
// getKubeClient is a convenience method for creating kubernetes config and client
// for a given kubeconfig context
func getKubeClient(context string) (*kubernetes.Clientset, clientcmd.ClientConfig, error) {
config := kube.GetConfig(context)
clientConfig, err := config.ClientConfig()
if err != nil {
return nil, nil, fmt.Errorf("could not get kubernetes config for context '%s': %s", context, err)
}
client, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, nil, fmt.Errorf("could not get kubernetes client: %s", err)
}
return client, config, nil
}
func main() {
cmd := newRootCmd(os.Stdout, os.Stdin)
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
func debug(format string, args ...interface{}) {
if flagDebug {
format = fmt.Sprintf("[debug] %s\n", format)
fmt.Printf(format, args...)
}
}
func validateArgs(args, expectedArgs []string) error {
if len(args) != len(expectedArgs) {
return fmt.Errorf("This command needs %v argument(s): %v", len(expectedArgs), expectedArgs)
}
return nil
}