forked from Azure/draft-classic
/
draft.go
158 lines (136 loc) · 4 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
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the MIT license.
package main
import (
"fmt"
"io"
"os"
"path/filepath"
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/kube"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/restclient"
"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"
namespaceEnvVar = "DRAFT_NAMESPACE"
)
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.
draftHost string
// draftNamespace depicts which kubernetes namespace the Draftd server is hosted.
draftNamespace string
)
var globalUsage = `The application deployment tool for Kubernetes.
`
func newRootCmd(out io.Writer) *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")
p.StringVar(&draftNamespace, "namespace", defaultDraftNamespace(), "namespace of Draftd. Overrides $DRAFT_NAMESPACE")
cmd.AddCommand(
newCreateCmd(out),
newHomeCmd(out),
newInitCmd(out),
newUpCmd(out),
newVersionCmd(out),
)
// Find and add plugins
loadPlugins(cmd, draftpath.Home(homePath()), out)
return cmd
}
func setupConnection(c *cobra.Command, args []string) error {
if draftHost == "" {
clientset, config, err := getKubeClient(kubeContext)
if err != nil {
return err
}
tunnel, err := portforwarder.New(draftNamespace, clientset, config)
if err != nil {
return err
}
draftHost = fmt.Sprintf("http://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(p *draft.Client) *draft.Client {
if p != nil {
return p
}
client, err := draft.NewFromString(draftHost, nil)
if err != nil {
panic(err)
}
return client
}
func defaultDraftHost() string {
return os.Getenv(hostEnvVar)
}
func defaultDraftNamespace() string {
return os.Getenv(namespaceEnvVar)
}
func defaultDraftHome() string {
if home := os.Getenv(homeEnvVar); home != "" {
return home
}
return filepath.Join(os.Getenv("HOME"), ".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) (*internalclientset.Clientset, *restclient.Config, error) {
config, err := kube.GetConfig(context).ClientConfig()
if err != nil {
return nil, nil, fmt.Errorf("could not get kubernetes config for context '%s': %s", context, err)
}
client, err := internalclientset.NewForConfig(config)
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)
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}