-
Notifications
You must be signed in to change notification settings - Fork 6
/
kubectl.go
360 lines (302 loc) · 10.9 KB
/
kubectl.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package util
import (
"bytes"
"context"
"errors"
"fmt"
opConfig "github.com/onepanelio/cli/config"
"github.com/spf13/cobra"
"io/ioutil"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8error "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/kubectl/pkg/cmd/apply"
k8delete "k8s.io/kubectl/pkg/cmd/delete"
"k8s.io/kubectl/pkg/cmd/get"
"k8s.io/kubectl/pkg/cmd/patch"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"os"
"runtime"
"strconv"
"strings"
"time"
)
func KubectlGet(resource string, resourceName string, namespace string, extraArgs []string, flags map[string]interface{}) (stdout string, stderr string, err error) {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.Namespace = &namespace
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
out := &bytes.Buffer{}
errOut := &bytes.Buffer{}
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
ioStreams := genericclioptions.IOStreams{
In: os.Stdin,
Out: out,
ErrOut: errOut,
}
cmd := get.NewCmdGet("kubectl", f, ioStreams)
getOptions := get.NewGetOptions("kubectl", ioStreams)
for flagName, flagVal := range flags {
boolVal, okBool := flagVal.(bool)
if okBool {
if err = cmd.Flags().Set(flagName, strconv.FormatBool(boolVal)); err != nil {
return "", "", err
}
continue
}
stringVal, okStr := flagVal.(string)
if okStr {
if flagName == "output" {
getOptions.PrintFlags.OutputFormat = &stringVal
}
if err = cmd.Flags().Set(flagName, stringVal); err != nil {
return "", "", err
}
continue
}
return "", "", errors.New(flagName + ", unexpected flag value type")
}
var args []string
if resource != "" {
args = append(args, resource)
}
if resourceName != "" {
args = append(args, resourceName)
}
args = append(args, extraArgs...)
if err = getOptions.Complete(f, cmd, args); err != nil {
return "", "", err
}
if err = getOptions.Validate(cmd); err != nil {
return "", "", err
}
if err = getOptions.Run(f, cmd, args); err != nil {
return "", "", err
}
stdout = out.String()
stderr = errOut.String()
return
}
// KubectlApply applies the yaml at the given filePath
func KubectlApply(filePath string) (err error) {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
ioStreams := genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
cmd := ApplyCmdWithError(f, ioStreams)
if err := cmd.Flags().Set("filename", filePath); err != nil {
return err
}
return cmd.RunE(cmd, []string{})
}
// KubectlPatch patches a resource.
// resource example: serviceaccount/default
func KubectlPatch(namespace string, resource string, filePath string) (err error) {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.Namespace = &namespace
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
ioStreams := genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
content, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
cmd := patch.NewCmdPatch(f, ioStreams)
if err := cmd.Flags().Set("patch", string(content)); err != nil {
return err
}
cmd.Run(cmd, []string{resource})
return
}
// KubectlDelete run's kubectl delete using the input filePath
func KubectlDelete(filePath string) (err error) {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
ioStreams := genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
deleteFlags := k8delete.NewDeleteCommandFlags("containing the resource to delete.")
cmd := &cobra.Command{}
deleteFlags.AddFlags(cmd)
cmdutil.AddDryRunFlag(cmd)
deleteOptions, err := deleteFlags.ToOptions(nil, ioStreams)
if err != nil {
return err
}
deleteOptions.Filenames = []string{filePath}
if err := deleteOptions.Complete(f, []string{}, cmd); err != nil {
return err
}
if err := deleteOptions.Validate(); err != nil {
return err
}
if err := deleteOptions.RunDelete(f); err != nil {
errorAggregate, ok := err.(k8error.Aggregate)
if ok {
finalErrors := make([]error, 0)
// Skip any errors that mean "not found"
for _, errItem := range errorAggregate.Errors() {
if strings.Contains(errItem.Error(), "not found") {
continue
}
if strings.Contains(errItem.Error(), "no matches for kind") {
continue
}
if strings.Contains(errItem.Error(), "the server could not find the requested resource") {
continue
}
finalErrors = append(finalErrors, errItem)
}
if len(finalErrors) == 0 {
return nil
}
return k8error.NewAggregate(finalErrors)
}
return err
}
return
}
func validateArgs(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args)
}
return nil
}
func validatePruneAll(prune, all bool, selector string) error {
if all && len(selector) > 0 {
return fmt.Errorf("cannot set --all and --selector at the same time")
}
if prune && !all && selector == "" {
return fmt.Errorf("all resources selected for prune without explicitly passing --all. To prune all resources, pass the --all flag. If you did not mean to prune all resources, specify a label selector")
}
return nil
}
// getDeployedIP attempts to get the ip address of the load balancer
// ("pending", nil) is returned if there is no ip yet
func getDeployedIP(c *kubernetes.Clientset) (string, error) {
svc, err := c.CoreV1().Services("istio-system").Get(context.Background(), "istio-ingressgateway", v1.GetOptions{})
if err != nil {
return "", err
}
ingress := svc.Status.LoadBalancer.Ingress
if ingress == nil {
return "pending", nil
}
if len(ingress) == 1 {
if ingress[0].IP == "" && ingress[0].Hostname != "" {
return ingress[0].Hostname, nil
}
return ingress[0].IP, nil
}
return "", fmt.Errorf("unable to get load balancer ip")
}
// getDeployedIPRetry calls getDeployedIP retries times, with a delay in between each call while the ip is pending
func getDeployedIPRetry(c *kubernetes.Clientset, retries int, delay time.Duration) (string, error) {
for tries := 0; tries < retries; tries++ {
ip, err := getDeployedIP(c)
if err != nil {
return ip, err
}
if ip != "pending" {
return ip, err
}
time.Sleep(delay)
}
return "", fmt.Errorf("unable to get deployed ip from LoadBalancer")
}
// PrintClusterNetworkInformation prints the ip address of the cluster and network DNS configuration required
func PrintClusterNetworkInformation(c *kubernetes.Clientset, url string) {
clusterIP, err := getDeployedIPRetry(c, 20, 6*time.Second)
if err != nil {
fmt.Printf("error: %v", err)
}
configFilePath := "config.yaml"
config, err := opConfig.FromFile(configFilePath)
if err != nil {
fmt.Printf("Unable to read configuration file: %v", err.Error())
return
}
yamlFile, err := LoadDynamicYamlFromFile(config.Spec.Params)
if err != nil {
fmt.Printf("Unable to load yaml file: %v", err.Error())
return
}
var dnsRecordMessage string
if yamlFile.HasKey("application.provider") {
provider := yamlFile.GetValue("application.provider").Value
if provider == "minikube" || provider == "microk8s" {
domain := yamlFile.GetValue("application.domain").Value
fqdn := yamlFile.GetValue("application.fqdn").Value
hostsPath := "/etc/hosts"
if runtime.GOOS == "windows" {
hostsPath = "C:\\Windows\\System32\\Drivers\\etc\\hosts"
}
dnsRecordMessage = "local"
fmt.Printf("\nIn your %v file, add\n", hostsPath)
fmt.Printf(" %v %v\n", clusterIP, fqdn)
defaultNamespace := yamlFile.GetValue("application.defaultNamespace").Value
sysStorageURL := fmt.Sprintf("sys-storage-%v.%v", defaultNamespace, domain)
fmt.Printf(" %v %v\n", clusterIP, sysStorageURL)
if config.Spec.HasLikeComponent("kfserving") {
modelServingURL := fmt.Sprintf("serving.%v", domain)
fmt.Printf(" %v %v\n", clusterIP, modelServingURL)
}
fmt.Println()
} else {
dnsRecordMessage = "an A"
if !IsIpv4(clusterIP) {
dnsRecordMessage = "a CNAME"
}
fmt.Printf("\nIn your DNS, add %v record for %v and point it to %v\n", dnsRecordMessage, GetWildCardDNS(url), clusterIP)
}
}
//If yaml key is missing due to older params.yaml file, use this default.
if dnsRecordMessage == "" {
dnsRecordMessage = "an A"
if !IsIpv4(clusterIP) {
dnsRecordMessage = "a CNAME"
}
fmt.Printf("\nIn your DNS, add %v record for %v and point it to %v\n", dnsRecordMessage, GetWildCardDNS(url), clusterIP)
}
fmt.Printf("Once complete, your application will be running at %v\n\n", url)
}
// ApplyCmdWithError runs the kubectl apply command and returns an error, if any.
func ApplyCmdWithError(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
o := apply.NewApplyOptions(ioStreams)
cmd := &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
cmdutil.CheckErr(o.Complete(f, cmd))
cmdutil.CheckErr(validateArgs(cmd, args))
cmdutil.CheckErr(validatePruneAll(o.Prune, o.All, o.Selector))
return o.Run()
},
}
// bind flag structs
o.DeleteFlags.AddFlags(cmd)
o.RecordFlags.AddFlags(cmd)
o.PrintFlags.AddFlags(cmd)
cmd.Flags().BoolVar(&o.Overwrite, "overwrite", o.Overwrite, "Automatically resolve conflicts between the modified and live configuration by using values from the modified configuration")
cmd.Flags().BoolVar(&o.Prune, "prune", o.Prune, "Automatically delete resource objects, including the uninitialized ones, that do not appear in the configs and are created by either apply or create --save-config. Should be used with either -l or --all.")
cmdutil.AddValidateFlags(cmd)
cmd.Flags().StringVarP(&o.Selector, "selector", "l", o.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
cmd.Flags().BoolVar(&o.All, "all", o.All, "Select all resources in the namespace of the specified resource types.")
cmd.Flags().StringArrayVar(&o.PruneWhitelist, "prune-whitelist", o.PruneWhitelist, "Overwrite the default whitelist with <group/version/kind> for --prune")
cmd.Flags().BoolVar(&o.OpenAPIPatch, "openapi-patch", o.OpenAPIPatch, "If true, use openapi to calculate diff when the openapi presents and the resource can be found in the openapi spec. Otherwise, fall back to use baked-in types.")
cmdutil.AddDryRunFlag(cmd)
cmdutil.AddServerSideApplyFlags(cmd)
cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, apply.FieldManagerClientSideApply)
return cmd
}