forked from bmatcuk/helm-take-ownership
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (186 loc) · 7.27 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"errors"
"fmt"
"log"
"os"
"github.com/bmatcuk/helm-take-ownership/pkg"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/helm/pkg/storage"
"k8s.io/helm/pkg/storage/driver"
"k8s.io/helm/pkg/tiller/environment"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)
var (
version = "master"
date = "1970-01-01_00:00:00"
)
func main() {
var releaseName string
var printVersion, debug, dryRun bool
var kubeConfig clientcmd.ClientConfig
kube := pkg.Kube{}
helmPluginName := os.Getenv("HELM_PLUGIN_NAME")
if helmPluginName == "" {
helmPluginName = "own"
}
rootCmd := &cobra.Command{
Use: fmt.Sprintf("helm %v RELEASE_NAME", helmPluginName),
Short: "Transfer ownership of a k8s release to helm",
Long: `The helm-take-ownership plugin transfers ownership from a non-helm-deployed release to helm's management.`,
Example: fmt.Sprintf("helm %v --deploy my-deployment --svc my-service -n stg my-release", helmPluginName),
Args: func(c *cobra.Command, args []string) error {
if !printVersion {
if len(args) < 1 {
return errors.New("You must specify the release name.")
}
if len(args) > 1 {
return errors.New("Too many arguments.")
}
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
if printVersion {
fmt.Printf("helm-take-ownership v%v (%.10s)", version, date)
return nil
}
releaseName = args[0]
if kube.ChartName == "RELEASE_NAME" {
kube.ChartName = releaseName
}
var valid bool
var err error
kube.Namespace, valid, err = kubeConfig.Namespace()
if !valid {
if err != nil {
return err
} else {
return errors.New("You must specify a namespace")
}
}
// create kubernetes factory
factory := cmdutil.NewFactory(kubeConfig)
kubernetesClientSet, err := factory.KubernetesClientSet()
if err != nil {
return err
}
clientset, err := factory.ClientSet()
if err != nil {
return err
}
// download the kube objects
log.Print("Downloading kubernetes objects...")
if err := kube.GetKubeObjects(kubernetesClientSet); err != nil {
return err
}
// create a release
log.Print("Constructing Helm Release...")
release := kube.BuildRelease(releaseName)
// print chart
if debug {
data, err := yaml.Marshal(release)
if err != nil {
return err
}
log.Print("Helm Release:")
log.Print(string(data))
}
// install the release
if dryRun {
log.Print("Skipping install because this is a dry run.")
if debug {
kubeClientConfig, err := kubeConfig.RawConfig()
if err != nil {
return err
}
data, err := yaml.Marshal(kubeClientConfig)
if err != nil {
return err
}
log.Print("Kube config:")
log.Print(string(data))
}
} else {
log.Print("Installing Helm Chart...")
cfgmaps := driver.NewConfigMaps(clientset.Core().ConfigMaps(kube.Namespace))
cfgmaps.Log = helmPrinter
releases := storage.Init(cfgmaps)
releases.Log = helmPrinter
if err := releases.Create(release); err != nil {
return err
}
}
return nil
},
}
flags := rootCmd.Flags()
flags.StringVar(&kube.ChartName, "chartname", "RELEASE_NAME", "Name of the helm Chart")
flags.StringVar(&kube.ChartVersion, "chartversion", "0.1.0", "Version of the helm Chart")
flags.BoolVarP(&printVersion, "version", "v", false, "Print version and exit")
flags.BoolVarP(&debug, "debug", "d", debug, "Debug mode")
flags.BoolVar(&dryRun, "dry-run", dryRun, "Dry-run")
flags.StringSliceVar(&kube.ConfigMapNames, "configmaps", kube.ConfigMapNames, "(aka 'cm') Comma-separated names of ConfigMaps to include")
flags.StringSliceVar(&kube.DaemonSetNames, "daemonsets", kube.DaemonSetNames, "(aka 'ds') Comma-separated names of DaemonSets to include")
flags.StringSliceVar(&kube.DeploymentNames, "deployments", kube.DeploymentNames, "(aka 'deploy') Comma-separated names of Deployments to include")
flags.StringSliceVar(&kube.HorizontalPodAutoscalerNames, "horizontalpodautoscalers", kube.HorizontalPodAutoscalers, "(aka 'hpa') Comma-separated names of Horizontal Pod Autoscalers to include")
flags.StringSliceVar(&kube.IngressNames, "ingresses", kube.IngressNames, "(aka 'ing') Comma-separated names of Ingresses to include")
flags.StringSliceVar(&kube.JobNames, "jobs", kube.JobNames, "Comma-separated names of Jobs to include")
flags.StringSliceVar(&kube.PersistentVolumeClaimNames, "persistentvolumeclaims", kube.PersistentVolumeClaimNames, "(aka 'pvc') Comma-separated names of PersistentVolumeClaims to include")
flags.StringSliceVar(&kube.PersistentVolumeNames, "persistentvolumes", kube.PersistentVolumeNames, "(aka 'pv') Comma-separated names of PersistentVolumes to include")
flags.StringSliceVar(&kube.PodNames, "pods", kube.PodNames, "(aka po') Comma-separated names of Pods to include")
flags.StringSliceVar(&kube.ReplicaSetNames, "replicasets", kube.ReplicaSetNames, "(aka 'rs') Comma-separated names of ReplicaSets to include")
flags.StringSliceVar(&kube.ReplicationControllerNames, "replicationcontrollers", kube.ReplicationControllerNames, "(aka 'rc') Comma-separated names of ReplicationControllers to include")
flags.StringSliceVar(&kube.SecretNames, "secrets", kube.SecretNames, "Comma-separated names of Secrets to include")
flags.StringSliceVar(&kube.ServiceNames, "services", kube.ServiceNames, "(aka 'svc') Comma-separated names of Services to include")
flags.StringSliceVar(&kube.StatefulSetNames, "statefulsets", kube.StatefulSetNames, "Comma-separated names of StatefulSets to include")
flags.StringSliceVar(&kube.StorageClassNames, "storageclasses", kube.StorageClassNames, "Comma-separated names of StorageClasses to include")
// Add generic kubectl flags. Taken from:
// https://github.com/kubernetes/kubernetes/blob/v1.8.0/pkg/kubectl/cmd/util/factory_client_access.go#L156
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
flags.StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
kubeOverrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
flagNames := clientcmd.RecommendedConfigOverrideFlags("k8s-")
flagNames.ClusterOverrideFlags.APIServer.ShortName = "s"
clientcmd.BindOverrideFlags(kubeOverrides, flags, flagNames)
kubeConfig = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, kubeOverrides)
// support for 2-3 letter shorthands similar to kubectl
flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
switch name {
case "cm":
name = "configmaps"
case "ds":
name = "daemonsets"
case "deploy":
name = "deployments"
case "hpa":
name = "horizontalpodautoscalers"
case "ing":
name = "ingresses"
case "pvc":
name = "persistentvolumeclaims"
case "pv":
name = "persistentvolumes"
case "po":
name = "pods"
case "rs":
name = "replicasets"
case "rc":
name = "replicationcontrollers"
case "svc":
name = "services"
}
return pflag.NormalizedName(name)
})
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
func helmPrinter(s string, params ...interface{}) {
params = append([]interface{}{s}, params...)
log.Print(params...)
}