|
| 1 | +// |
| 2 | +// Copyright 2020 IBM Corporation |
| 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 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/IBM/k8s-volume-utils/pkg/kubeutils" |
| 25 | + "github.com/spf13/cobra" |
| 26 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 27 | + "k8s.io/klog" |
| 28 | + cmdutil "k8s.io/kubectl/pkg/cmd/util" |
| 29 | +) |
| 30 | + |
| 31 | +// rootCmd represents the base command when called without any subcommands |
| 32 | +var rootCmd = &cobra.Command{ |
| 33 | + Use: "k8s-volume-utils", |
| 34 | + Short: "it is used to rename PVC object or help to reuse pv", |
| 35 | + Long: ` |
| 36 | +k8s-volume-utils pvc |
| 37 | + It is used to rename a PVC object in kubernetes cluster. |
| 38 | + It will delete original PVC and create new PVC referring to same PV. |
| 39 | + New PVC can be in another namespace. |
| 40 | +k8s-volume-utils pv |
| 41 | + It can help to reuse a pv |
| 42 | +`, |
| 43 | + // Uncomment the following line if your bare application |
| 44 | + // has an action associated with it: |
| 45 | + // Run: func(cmd *cobra.Command, args []string) { }, |
| 46 | +} |
| 47 | + |
| 48 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 49 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 50 | +func Execute() { |
| 51 | + klog.InitFlags(nil) |
| 52 | + if err := rootCmd.Execute(); err != nil { |
| 53 | + fmt.Println(err) |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | +} |
| 57 | +func init() { |
| 58 | + flags := rootCmd.PersistentFlags() |
| 59 | + |
| 60 | + kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag() |
| 61 | + kubeConfigFlags.AddFlags(flags) |
| 62 | + matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags) |
| 63 | + matchVersionKubeConfigFlags.AddFlags(rootCmd.PersistentFlags()) |
| 64 | + |
| 65 | + rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) |
| 66 | + |
| 67 | + f := cmdutil.NewFactory(matchVersionKubeConfigFlags) |
| 68 | + var namespace string |
| 69 | + var err error |
| 70 | + namespace, _, err = f.ToRawKubeConfigLoader().Namespace() |
| 71 | + if err != nil { |
| 72 | + klog.Errorf("fail to get default namespace: %s", err.Error()) |
| 73 | + } |
| 74 | + |
| 75 | + client, e := f.KubernetesClientSet() |
| 76 | + if e != nil { |
| 77 | + klog.Errorf("fail to get kubeclient: %s", e.Error()) |
| 78 | + } |
| 79 | + kubeutils.InitKube(client, namespace) |
| 80 | + klog.Infof("kubeclient initialized. default namespace: %s", namespace) |
| 81 | +} |
0 commit comments