|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 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 flags |
| 18 | + |
| 19 | +import ( |
| 20 | + goflag "flag" |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/golang/glog" |
| 25 | + "github.com/spf13/pflag" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + // these are flags from vendored cadvisor |
| 30 | + hiddenVendorFlags = []string{ |
| 31 | + "boot-id-file", |
| 32 | + "container-hints", |
| 33 | + "docker", |
| 34 | + "docker-env-metadata-whitelist", |
| 35 | + "docker-only", |
| 36 | + "docker-root", |
| 37 | + "event-storage-age-limit", |
| 38 | + "event-storage-event-limit", |
| 39 | + "global-housekeeping-interval", |
| 40 | + "housekeeping-interval", |
| 41 | + "log-cadvisor-usage", |
| 42 | + "machine-id-file", |
| 43 | + "stderrthreshold", |
| 44 | + "storage-driver-buffer-duration", |
| 45 | + "storage-driver-db ", |
| 46 | + "storage-driver-host", |
| 47 | + "storage-driver-password", |
| 48 | + "storage-driver-secure", |
| 49 | + "storage-driver-table", |
| 50 | + "storage-driver-user", |
| 51 | + "vmodule", |
| 52 | + } |
| 53 | +) |
| 54 | + |
| 55 | +// WordSepNormalizeFunc changes all flags that contain "_" separators |
| 56 | +func WordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName { |
| 57 | + if strings.Contains(name, "_") { |
| 58 | + return pflag.NormalizedName(strings.Replace(name, "_", "-", -1)) |
| 59 | + } |
| 60 | + return pflag.NormalizedName(name) |
| 61 | +} |
| 62 | + |
| 63 | +// WarnWordSepNormalizeFunc changes and warns for flags that contain "_" separators |
| 64 | +func WarnWordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName { |
| 65 | + if strings.Contains(name, "_") { |
| 66 | + nname := strings.Replace(name, "_", "-", -1) |
| 67 | + glog.Warningf("%s is DEPRECATED and will be removed in a future version. Use %s instead.", name, nname) |
| 68 | + |
| 69 | + return pflag.NormalizedName(nname) |
| 70 | + } |
| 71 | + return pflag.NormalizedName(name) |
| 72 | +} |
| 73 | + |
| 74 | +// InitFlags normalizes and parses the command line flags |
| 75 | +func InitFlags() { |
| 76 | + pflag.CommandLine.SetNormalizeFunc(WordSepNormalizeFunc) |
| 77 | + pflag.CommandLine.AddGoFlagSet(goflag.CommandLine) |
| 78 | + |
| 79 | + for _, hiddenFlag := range hiddenVendorFlags { |
| 80 | + pflag.CommandLine.MarkHidden(hiddenFlag) |
| 81 | + } |
| 82 | + |
| 83 | + pflag.Parse() |
| 84 | + |
| 85 | + path := pflag.Lookup("log-dir").Value.String() |
| 86 | + if _, err := os.Stat(path); os.IsNotExist(err) { |
| 87 | + os.MkdirAll(path, 0755) |
| 88 | + } |
| 89 | +} |
0 commit comments