This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 396
/
draft.go
182 lines (156 loc) · 5.16 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
178
179
180
181
182
// 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/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/helm/pkg/helm"
hpf "k8s.io/helm/pkg/helm/portforwarder"
"k8s.io/helm/pkg/kube"
"k8s.io/helm/pkg/tiller/environment"
"github.com/Azure/draft/pkg/draft/draftpath"
)
const (
homeEnvVar = "DRAFT_HOME"
hostEnvVar = "HELM_HOST"
namespaceEnvVar = "TILLER_NAMESPACE"
)
var (
// flagDebug is a signal that the user wants additional output.
flagDebug bool
kubeContext string
// draftHome depicts the home directory where all Draft config is stored.
draftHome string
// tillerHost depicts where Tiller is hosted. This is used when the port forwarding logic by Kubernetes is unavailable.
tillerHost string
// tillerNamespace depicts which namespace Tiller is running in. This is used when Tiller was installed in a different namespace than kube-system.
tillerNamespace string
//rootCmd is the root command handling `draft`. It's used in other parts of package cmd to add/search the command tree.
rootCmd *cobra.Command
// globalConfig is the configuration stored in $DRAFT_HOME/config.toml
globalConfig DraftConfig
)
var globalUsage = `The application deployment tool for Kubernetes.
`
func init() {
rootCmd = newRootCmd(os.Stdout, os.Stdin)
}
func newRootCmd(out io.Writer, in io.Reader) *cobra.Command {
cmd := &cobra.Command{
Use: "draft",
Short: globalUsage,
Long: globalUsage,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
if flagDebug {
log.SetLevel(log.DebugLevel)
}
os.Setenv(homeEnvVar, draftHome)
globalConfig, err = ReadConfig()
return
},
}
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 when talking to Tiller")
p.StringVar(&tillerNamespace, "tiller-namespace", defaultTillerNamespace(), "namespace where Tiller is running. This is used when Tiller was installed in a different namespace than kube-system. Overrides $TILLER_NAMESPACE")
cmd.AddCommand(
newConfigCmd(out),
newCreateCmd(out),
newHomeCmd(out),
newInitCmd(out, in),
newUpCmd(out),
newVersionCmd(out),
newPluginCmd(out),
newConnectCmd(out),
newDeleteCmd(out),
newLogsCmd(out),
newHistoryCmd(out),
newPackCmd(out),
)
// Find and add plugins
loadPlugins(cmd, draftpath.Home(homePath()), out, in)
return cmd
}
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 defaultTillerNamespace() string {
if namespace := os.Getenv(namespaceEnvVar); namespace != "" {
return namespace
}
return environment.DefaultTillerNamespace
}
func homePath() string {
return os.ExpandEnv(draftHome)
}
// configForContext creates a Kubernetes REST client configuration for a given kubeconfig context.
func configForContext(context string) (clientcmd.ClientConfig, *rest.Config, error) {
clientConfig := kube.GetConfig(context)
config, err := clientConfig.ClientConfig()
if err != nil {
return nil, nil, fmt.Errorf("could not get Kubernetes config for context %q: %s", context, err)
}
return clientConfig, config, nil
}
// getKubeClient creates a Kubernetes config and client for a given kubeconfig context.
func getKubeClient(context string) (kubernetes.Interface, *rest.Config, error) {
_, config, err := configForContext(context)
if err != nil {
return nil, nil, err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, fmt.Errorf("could not get Kubernetes client: %s", err)
}
return client, config, nil
}
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
}
func setupHelm(kubeClient kubernetes.Interface, config *rest.Config, namespace string) (helm.Interface, error) {
tunnel, err := setupTillerConnection(kubeClient, config, namespace)
if err != nil {
return nil, err
}
return helm.NewClient(helm.Host(fmt.Sprintf("127.0.0.1:%d", tunnel.Local))), nil
}
func setupTillerConnection(client kubernetes.Interface, config *rest.Config, namespace string) (*kube.Tunnel, error) {
tunnel, err := hpf.New(namespace, client, config)
if err != nil {
return nil, fmt.Errorf("Could not get a connection to tiller: %s\nPlease ensure you have run `helm init`", err)
}
return tunnel, err
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}