-
Notifications
You must be signed in to change notification settings - Fork 22
/
update.go
89 lines (72 loc) · 1.78 KB
/
update.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
package cmd
import (
"encoding/json"
"fmt"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/errors"
"k8s.io/client-go/pkg/runtime"
"k8s.io/client-go/pkg/util/diff"
"github.com/ksonnet/kubecfg/utils"
)
const (
flagCreate = "create"
)
func init() {
RootCmd.AddCommand(updateCmd)
updateCmd.PersistentFlags().Bool(flagCreate, true, "Create missing resources")
}
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update Kubernetes resources with local config",
RunE: func(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
create, err := flags.GetBool(flagCreate)
if err != nil {
return err
}
objs, err := readObjs(cmd, args)
if err != nil {
return err
}
clientpool, disco, err := restClientPool(cmd)
if err != nil {
return err
}
defaultNs, _, err := clientConfig.Namespace()
if err != nil {
return err
}
utils.SortDepFirst(objs)
for _, obj := range objs {
desc := fmt.Sprintf("%s/%s", obj.GetKind(), fqName(obj))
glog.Info("Updating ", desc)
c, err := clientForResource(clientpool, disco, obj, defaultNs)
if err != nil {
return err
}
asPatch, err := json.Marshal(obj)
if err != nil {
return err
}
newobj, err := c.Patch(obj.GetName(), api.MergePatchType, asPatch)
if create && errors.IsNotFound(err) {
glog.Info(" Creating non-existent ", desc)
newobj, err = c.Create(obj)
}
if err != nil {
// TODO: retry
return fmt.Errorf("Error updating %s: %s", desc, err)
}
glog.V(2).Info("Updated object: ", diff.ObjectDiff(obj, newobj))
}
return nil
},
}
func fqName(o *runtime.Unstructured) string {
if o.GetNamespace() == "" {
return o.GetName()
}
return fmt.Sprintf("%s.%s", o.GetNamespace(), o.GetName())
}