update.go 2.41 KB
Newer Older
Angus Lees's avatar
Angus Lees committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2017 The kubecfg authors
//
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

Angus Lees's avatar
Angus Lees committed
16 17 18 19 20
package cmd

import (
	"encoding/json"
	"fmt"
Angus Lees's avatar
Angus Lees committed
21
	"sort"
Angus Lees's avatar
Angus Lees committed
22 23 24 25 26 27 28

	"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"
29 30

	"github.com/ksonnet/kubecfg/utils"
Angus Lees's avatar
Angus Lees committed
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
)

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
		}

Angus Lees's avatar
Angus Lees committed
68
		sort.Sort(utils.DependencyOrder(objs))
69

Angus Lees's avatar
Angus Lees committed
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
		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())
}