diff options
| author | zhangjie <iamkadisi@163.com> | 2020-01-15 11:19:54 +0800 |
|---|---|---|
| committer | zhangjie <iamkadisi@163.com> | 2020-01-15 20:03:32 +0800 |
| commit | 703fb0b239dc38022f05bcff76f4608b6a3e83b6 (patch) | |
| tree | 6b29969e4ca0dc9393f3e41e285b586f42b15215 /edgesite | |
| parent | Merge pull request #1397 from fisherxu/upgrade-kind (diff) | |
| download | kubeedge-703fb0b239dc38022f05bcff76f4608b6a3e83b6.tar.gz | |
edgesite support --minconfig subcommand
Signed-off-by: zhangjie <iamkadisi@163.com>
Diffstat (limited to 'edgesite')
| -rw-r--r-- | edgesite/cmd/app/flags/flags.go | 105 | ||||
| -rw-r--r-- | edgesite/cmd/app/server.go | 3 |
2 files changed, 108 insertions, 0 deletions
diff --git a/edgesite/cmd/app/flags/flags.go b/edgesite/cmd/app/flags/flags.go new file mode 100644 index 000000000..cac238e93 --- /dev/null +++ b/edgesite/cmd/app/flags/flags.go @@ -0,0 +1,105 @@ +/* +Copyright 2020 The KubeEdge 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. +*/ +package flags + +import ( + "fmt" + "os" + "strconv" + + flag "github.com/spf13/pflag" + "sigs.k8s.io/yaml" + + "github.com/kubeedge/kubeedge/pkg/apis/edgesite/v1alpha1" +) + +type minConfigValue int + +const ( + MinConfigFalse minConfigValue = 0 + MinConfigTrue minConfigValue = 1 + // TODO support json output (default yaml ) @kadisi +) + +func (m *minConfigValue) IsBoolFlag() bool { + return true +} + +func (m *minConfigValue) Get() interface{} { + return minConfigValue(*m) +} + +func (m *minConfigValue) Set(s string) error { + boolVal, err := strconv.ParseBool(s) + if boolVal { + *m = MinConfigTrue + } else { + *m = MinConfigFalse + } + return err +} + +func (m *minConfigValue) String() string { + return fmt.Sprintf("%v", bool(*m == MinConfigTrue)) +} + +// The type of the flag as required by the pflag.Value interface +func (m *minConfigValue) Type() string { + return "minconfig" +} + +func MinConfigVar(p *minConfigValue, name string, value minConfigValue, usage string) { + *p = value + flag.Var(p, name, usage) + // "--minconfig" will be treated as "--minconfig=true" + flag.Lookup(name).NoOptDefVal = "true" +} + +func minConfig(name string, value minConfigValue, usage string) *minConfigValue { + p := new(minConfigValue) + MinConfigVar(p, name, value, usage) + return p +} + +const minConfigFlagName = "minconfig" + +var ( + minConfigFlag = minConfig(minConfigFlagName, MinConfigFalse, "Print min configuration for reference, users can refer to it to create their own configuration files") +) + +// AddFlags registers this package's flags on arbitrary FlagSets, such that they point to the +// same value as the global flags. +func AddFlags(fs *flag.FlagSet) { + fs.AddFlag(flag.Lookup(minConfigFlagName)) +} + +// PrintMinConfigAndExitIfRequested will check if the -minconfig flag was passed +// and, if so, print the min config and exit. +func PrintMinConfigAndExitIfRequested() { + if *minConfigFlag == MinConfigTrue { + config := v1alpha1.NewMinEdgeSiteConfig() + data, err := yaml.Marshal(config) + if err != nil { + fmt.Printf("Marshal edgesite min config to yaml error %v\n", err) + os.Exit(1) + } + fmt.Println("# With --minconfig , you can easily used this configurations as reference.") + fmt.Println("# It's useful to users who are new to KubeEdge, and you can modify/create your own configs accordingly. ") + fmt.Println("# This configuration is suitable for beginners.") + fmt.Printf("\n%v\n\n", string(data)) + os.Exit(0) + } +} diff --git a/edgesite/cmd/app/server.go b/edgesite/cmd/app/server.go index cba70d91e..25832273d 100644 --- a/edgesite/cmd/app/server.go +++ b/edgesite/cmd/app/server.go @@ -14,6 +14,7 @@ import ( "github.com/kubeedge/kubeedge/edge/pkg/common/dbm" "github.com/kubeedge/kubeedge/edge/pkg/edged" "github.com/kubeedge/kubeedge/edge/pkg/metamanager" + "github.com/kubeedge/kubeedge/edgesite/cmd/app/flags" "github.com/kubeedge/kubeedge/edgesite/cmd/app/options" "github.com/kubeedge/kubeedge/pkg/util/flag" "github.com/kubeedge/kubeedge/pkg/version" @@ -31,6 +32,7 @@ It is also responsible for storing/retrieving metadata to/from a lightweight dat runs on edge nodes and manages containerized applications.`, Run: func(cmd *cobra.Command, args []string) { verflag.PrintAndExitIfRequested() + flags.PrintMinConfigAndExitIfRequested() flag.PrintFlags(cmd.Flags()) // To help debugging, immediately log version @@ -44,6 +46,7 @@ runs on edge nodes and manages containerized applications.`, fs := cmd.Flags() namedFs := opts.Flags() verflag.AddFlags(namedFs.FlagSet("global")) + flags.AddFlags(namedFs.FlagSet("global")) globalflag.AddGlobalFlags(namedFs.FlagSet("global"), cmd.Name()) for _, f := range namedFs.FlagSets { fs.AddFlagSet(f) |
