Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 1 | // Copyright 2017 The kubecfg authors |
| 2 | // |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | package cmd |
| 17 | |
| 18 | import ( |
| 19 | "github.com/spf13/cobra" |
| 20 | |
| 21 | "code.hackerspace.pl/hscloud/cluster/tools/kartongips/pkg/kubecfg" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | flagCreate = "create" |
| 26 | flagSkipGc = "skip-gc" |
| 27 | flagGcTag = "gc-tag" |
| 28 | flagDryRun = "dry-run" |
| 29 | flagValidate = "validate" |
| 30 | ) |
| 31 | |
| 32 | func init() { |
| 33 | RootCmd.AddCommand(updateCmd) |
| 34 | updateCmd.PersistentFlags().Bool(flagCreate, true, "Create missing resources") |
| 35 | updateCmd.PersistentFlags().Bool(flagSkipGc, false, "Don't perform garbage collection, even with --"+flagGcTag) |
| 36 | updateCmd.PersistentFlags().String(flagGcTag, "", "Add this tag to updated objects, and garbage collect existing objects with this tag and not in config") |
| 37 | updateCmd.PersistentFlags().Bool(flagDryRun, false, "Perform only read-only operations") |
| 38 | updateCmd.PersistentFlags().Bool(flagValidate, true, "Validate input against server schema") |
| 39 | updateCmd.PersistentFlags().Bool(flagIgnoreUnknown, false, "Don't fail validation if the schema for a given resource type is not found") |
| 40 | } |
| 41 | |
| 42 | var updateCmd = &cobra.Command{ |
| 43 | Use: "update", |
| 44 | Short: "Update Kubernetes resources with local config", |
| 45 | Args: cobra.ArbitraryArgs, |
| 46 | RunE: func(cmd *cobra.Command, args []string) error { |
| 47 | flags := cmd.Flags() |
| 48 | var err error |
| 49 | c := kubecfg.UpdateCmd{} |
| 50 | |
| 51 | validate, err := flags.GetBool(flagValidate) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | |
| 56 | c.Create, err = flags.GetBool(flagCreate) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | c.GcTag, err = flags.GetString(flagGcTag) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | c.SkipGc, err = flags.GetBool(flagSkipGc) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | c.DryRun, err = flags.GetBool(flagDryRun) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | |
| 76 | c.Client, c.Mapper, c.Discovery, err = getDynamicClients(cmd) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | c.DefaultNamespace, err = defaultNamespace(clientConfig) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | objs, err := readObjs(cmd, args) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | if validate { |
| 92 | v := kubecfg.ValidateCmd{ |
| 93 | Mapper: c.Mapper, |
| 94 | Discovery: c.Discovery, |
| 95 | } |
| 96 | |
| 97 | v.IgnoreUnknown, err = flags.GetBool(flagIgnoreUnknown) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | if err := v.Run(objs, cmd.OutOrStdout()); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return c.Run(cmd.Context(), objs) |
| 108 | }, |
| 109 | } |