Skip to content

Commit 34bfa2b

Browse files
committed
deprecated flag handling
Several sidecars (for example, external-provisioner) deprecate command line flags. When that happens, the flag must still be treated the same way as before (i.e. optional values for boolean, required values for non-boolean), but trying to use the flag needs to trigger a deprecation warning. An alternative to this code is to let the flag set a variable and then check the variable. But that only catches cases where the variable is set to a non-default value and leaves the variable in the code, which can lead to mistakes (see kubernetes-csi/external-provisioner#239).
1 parent 8053f37 commit 34bfa2b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

deprecatedflags/deprecatedflags.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2019 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 deprecatedflags can be used to declare a flag that is no
18+
// longer supported.
19+
package deprecatedflags
20+
21+
import (
22+
"flag"
23+
24+
"k8s.io/klog"
25+
)
26+
27+
// Add defines a deprecated option which used to take some kind of
28+
// value. The type of the argument no longer matters, it gets ignored
29+
// and instead using the option triggers a deprecation warning. The
30+
// return code can be safely ignored and is only provided to support
31+
// replacing functions like flag.Int in a global variable section.
32+
func Add(name string) bool {
33+
flag.Var(deprecated{name: name}, name, "This option is deprecated.")
34+
return true
35+
}
36+
37+
// AddBool defines a deprecated boolean option. Otherwise it behaves
38+
// like Add.
39+
func AddBool(name string) bool {
40+
flag.Var(deprecated{name: name, isBool: true}, name, "This option is deprecated.")
41+
return true
42+
}
43+
44+
type deprecated struct {
45+
name string
46+
isBool bool
47+
}
48+
49+
var _ flag.Value = deprecated{}
50+
51+
func (d deprecated) String() string { return "" }
52+
func (d deprecated) Set(value string) error {
53+
klog.Warningf("Warning: option %s=%q is deprecated and has no effect", d.name, value)
54+
return nil
55+
}
56+
func (d deprecated) Type() string { return "" }
57+
func (d deprecated) IsBoolFlag() bool { return d.isBool }

0 commit comments

Comments
 (0)