mirror of https://github.com/k3s-io/k3s
add secret flag to options
parent
db65bed243
commit
131802a5cd
|
@ -183,7 +183,7 @@ func (o AnnotateOptions) RunAnnotate(f cmdutil.Factory, cmd *cobra.Command) erro
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
changeCause := f.Command()
|
changeCause := f.Command(cmd, false)
|
||||||
|
|
||||||
mapper, typer, err := f.UnstructuredObject()
|
mapper, typer, err := f.UnstructuredObject()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -258,7 +258,7 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmdutil.ShouldRecord(cmd, info) {
|
if cmdutil.ShouldRecord(cmd, info) {
|
||||||
if err := cmdutil.RecordChangeCause(info.Object, f.Command()); err != nil {
|
if err := cmdutil.RecordChangeCause(info.Object, f.Command(cmd, false)); err != nil {
|
||||||
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -313,7 +313,7 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmdutil.ShouldRecord(cmd, info) {
|
if cmdutil.ShouldRecord(cmd, info) {
|
||||||
if patch, patchType, err := cmdutil.ChangeResourcePatch(info, f.Command()); err == nil {
|
if patch, patchType, err := cmdutil.ChangeResourcePatch(info, f.Command(cmd, true)); err == nil {
|
||||||
if _, err = helper.Patch(info.Namespace, info.Name, patchType, patch); err != nil {
|
if _, err = helper.Patch(info.Namespace, info.Name, patchType, patch); err != nil {
|
||||||
glog.V(4).Infof("error recording reason: %v", err)
|
glog.V(4).Infof("error recording reason: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,7 +156,7 @@ func RunAutoscale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if cmdutil.ShouldRecord(cmd, hpa) {
|
if cmdutil.ShouldRecord(cmd, hpa) {
|
||||||
if err := cmdutil.RecordChangeCause(hpa.Object, f.Command()); err != nil {
|
if err := cmdutil.RecordChangeCause(hpa.Object, f.Command(cmd, false)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
object = hpa.Object
|
object = hpa.Object
|
||||||
|
|
|
@ -154,7 +154,7 @@ func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opt
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmdutil.ShouldRecord(cmd, info) {
|
if cmdutil.ShouldRecord(cmd, info) {
|
||||||
if err := cmdutil.RecordChangeCause(info.Object, f.Command()); err != nil {
|
if err := cmdutil.RecordChangeCause(info.Object, f.Command(cmd, false)); err != nil {
|
||||||
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -536,7 +536,7 @@ func visitAnnotation(cmd *cobra.Command, f cmdutil.Factory, annotationVisitor re
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if cmdutil.ShouldRecord(cmd, info) {
|
if cmdutil.ShouldRecord(cmd, info) {
|
||||||
if err := cmdutil.RecordChangeCause(info.Object, f.Command()); err != nil {
|
if err := cmdutil.RecordChangeCause(info.Object, f.Command(cmd, false)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -248,7 +248,7 @@ func RunExpose(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if cmdutil.ShouldRecord(cmd, info) {
|
if cmdutil.ShouldRecord(cmd, info) {
|
||||||
if err := cmdutil.RecordChangeCause(object, f.Command()); err != nil {
|
if err := cmdutil.RecordChangeCause(object, f.Command(cmd, false)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -178,7 +178,7 @@ func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
changeCause := f.Command()
|
changeCause := f.Command(cmd, false)
|
||||||
|
|
||||||
mapper, typer, err := f.UnstructuredObject()
|
mapper, typer, err := f.UnstructuredObject()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -189,7 +189,7 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
|
||||||
infoCopy := *info
|
infoCopy := *info
|
||||||
infoCopy.Object = patchedObj
|
infoCopy.Object = patchedObj
|
||||||
infoCopy.VersionedObject = patchedObj
|
infoCopy.VersionedObject = patchedObj
|
||||||
if patch, patchType, err := cmdutil.ChangeResourcePatch(&infoCopy, f.Command()); err == nil {
|
if patch, patchType, err := cmdutil.ChangeResourcePatch(&infoCopy, f.Command(cmd, true)); err == nil {
|
||||||
if _, err = helper.Patch(info.Namespace, info.Name, patchType, patch); err != nil {
|
if _, err = helper.Patch(info.Namespace, info.Name, patchType, patch); err != nil {
|
||||||
glog.V(4).Infof("error recording reason: %v", err)
|
glog.V(4).Infof("error recording reason: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,7 +151,7 @@ func RunReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmdutil.ShouldRecord(cmd, info) {
|
if cmdutil.ShouldRecord(cmd, info) {
|
||||||
if err := cmdutil.RecordChangeCause(info.Object, f.Command()); err != nil {
|
if err := cmdutil.RecordChangeCause(info.Object, f.Command(cmd, false)); err != nil {
|
||||||
return cmdutil.AddSourceToErr("replacing", info.Source, err)
|
return cmdutil.AddSourceToErr("replacing", info.Source, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,7 +272,7 @@ func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmdutil.ShouldRecord(cmd, info) {
|
if cmdutil.ShouldRecord(cmd, info) {
|
||||||
if err := cmdutil.RecordChangeCause(info.Object, f.Command()); err != nil {
|
if err := cmdutil.RecordChangeCause(info.Object, f.Command(cmd, false)); err != nil {
|
||||||
return cmdutil.AddSourceToErr("replacing", info.Source, err)
|
return cmdutil.AddSourceToErr("replacing", info.Source, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -585,7 +585,7 @@ func createGeneratedObject(f cmdutil.Factory, cmd *cobra.Command, generator kube
|
||||||
return nil, "", nil, nil, err
|
return nil, "", nil, nil, err
|
||||||
}
|
}
|
||||||
if cmdutil.GetRecordFlag(cmd) || len(annotations[kubectl.ChangeCauseAnnotation]) > 0 {
|
if cmdutil.GetRecordFlag(cmd) || len(annotations[kubectl.ChangeCauseAnnotation]) > 0 {
|
||||||
if err := cmdutil.RecordChangeCause(obj, f.Command()); err != nil {
|
if err := cmdutil.RecordChangeCause(obj, f.Command(cmd, false)); err != nil {
|
||||||
return nil, "", nil, nil, err
|
return nil, "", nil, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -164,7 +164,7 @@ func RunScale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if cmdutil.ShouldRecord(cmd, info) {
|
if cmdutil.ShouldRecord(cmd, info) {
|
||||||
patchBytes, patchType, err := cmdutil.ChangeResourcePatch(info, f.Command())
|
patchBytes, patchType, err := cmdutil.ChangeResourcePatch(info, f.Command(cmd, true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,7 @@ func (o *ImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
|
||||||
o.Encoder = f.JSONEncoder()
|
o.Encoder = f.JSONEncoder()
|
||||||
o.ShortOutput = cmdutil.GetFlagString(cmd, "output") == "name"
|
o.ShortOutput = cmdutil.GetFlagString(cmd, "output") == "name"
|
||||||
o.Record = cmdutil.GetRecordFlag(cmd)
|
o.Record = cmdutil.GetRecordFlag(cmd)
|
||||||
o.ChangeCause = f.Command()
|
o.ChangeCause = f.Command(cmd, false)
|
||||||
o.PrintObject = f.PrintObject
|
o.PrintObject = f.PrintObject
|
||||||
o.DryRun = cmdutil.GetDryRunFlag(cmd)
|
o.DryRun = cmdutil.GetDryRunFlag(cmd)
|
||||||
o.Output = cmdutil.GetFlagString(cmd, "output")
|
o.Output = cmdutil.GetFlagString(cmd, "output")
|
||||||
|
|
|
@ -131,7 +131,7 @@ func (o *ResourcesOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args
|
||||||
o.Encoder = f.JSONEncoder()
|
o.Encoder = f.JSONEncoder()
|
||||||
o.ShortOutput = cmdutil.GetFlagString(cmd, "output") == "name"
|
o.ShortOutput = cmdutil.GetFlagString(cmd, "output") == "name"
|
||||||
o.Record = cmdutil.GetRecordFlag(cmd)
|
o.Record = cmdutil.GetRecordFlag(cmd)
|
||||||
o.ChangeCause = f.Command()
|
o.ChangeCause = f.Command(cmd, false)
|
||||||
o.PrintObject = f.PrintObject
|
o.PrintObject = f.PrintObject
|
||||||
o.Cmd = cmd
|
o.Cmd = cmd
|
||||||
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ func (o *SelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
o.changeCause = f.Command()
|
o.changeCause = f.Command(cmd, false)
|
||||||
mapper, _ := f.Object()
|
mapper, _ := f.Object()
|
||||||
o.mapper = mapper
|
o.mapper = mapper
|
||||||
o.encoder = f.JSONEncoder()
|
o.encoder = f.JSONEncoder()
|
||||||
|
|
|
@ -437,7 +437,7 @@ func (f *FakeFactory) EditorEnvs() []string {
|
||||||
func (f *FakeFactory) PrintObjectSpecificMessage(obj runtime.Object, out io.Writer) {
|
func (f *FakeFactory) PrintObjectSpecificMessage(obj runtime.Object, out io.Writer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeFactory) Command() string {
|
func (f *FakeFactory) Command(*cobra.Command, bool) string {
|
||||||
return f.tf.Command
|
return f.tf.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -638,7 +638,7 @@ func (f *fakeAPIFactory) DefaultNamespace() (string, bool, error) {
|
||||||
return f.tf.Namespace, false, f.tf.Err
|
return f.tf.Namespace, false, f.tf.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeAPIFactory) Command() string {
|
func (f *fakeAPIFactory) Command(*cobra.Command, bool) string {
|
||||||
return f.tf.Command
|
return f.tf.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -130,7 +130,7 @@ type ClientAccessFactory interface {
|
||||||
FlagSet() *pflag.FlagSet
|
FlagSet() *pflag.FlagSet
|
||||||
// Command will stringify and return all environment arguments ie. a command run by a client
|
// Command will stringify and return all environment arguments ie. a command run by a client
|
||||||
// using the factory.
|
// using the factory.
|
||||||
Command() string
|
Command(cmd *cobra.Command, showSecrets bool) string
|
||||||
// BindFlags adds any flags that are common to all kubectl sub commands.
|
// BindFlags adds any flags that are common to all kubectl sub commands.
|
||||||
BindFlags(flags *pflag.FlagSet)
|
BindFlags(flags *pflag.FlagSet)
|
||||||
// BindExternalFlags adds any flags defined by external projects (not part of pflags)
|
// BindExternalFlags adds any flags defined by external projects (not part of pflags)
|
||||||
|
|
|
@ -330,14 +330,35 @@ func (f *ring0Factory) FlagSet() *pflag.FlagSet {
|
||||||
return f.flags
|
return f.flags
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: We need to filter out stuff like secrets.
|
// Set showSecrets false to filter out stuff like secrets.
|
||||||
func (f *ring0Factory) Command() string {
|
func (f *ring0Factory) Command(cmd *cobra.Command, showSecrets bool) string {
|
||||||
if len(os.Args) == 0 {
|
if len(os.Args) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flags := ""
|
||||||
|
parseFunc := func(flag *pflag.Flag, value string) error {
|
||||||
|
flags = flags + " --" + flag.Name
|
||||||
|
if set, ok := flag.Annotations["classified"]; showSecrets || !ok || len(set) == 0 {
|
||||||
|
flags = flags + "=" + value
|
||||||
|
} else {
|
||||||
|
flags = flags + "=CLASSIFIED"
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
err = cmd.Flags().ParseAll(os.Args[1:], parseFunc)
|
||||||
|
if err != nil || !cmd.Flags().Parsed() {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
args := ""
|
||||||
|
if arguments := cmd.Flags().Args(); len(arguments) > 0 {
|
||||||
|
args = " " + strings.Join(arguments, " ")
|
||||||
|
}
|
||||||
|
|
||||||
base := filepath.Base(os.Args[0])
|
base := filepath.Base(os.Args[0])
|
||||||
args := append([]string{base}, os.Args[1:]...)
|
return base + args + flags
|
||||||
return strings.Join(args, " ")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *ring0Factory) BindFlags(flags *pflag.FlagSet) {
|
func (f *ring0Factory) BindFlags(flags *pflag.FlagSet) {
|
||||||
|
|
|
@ -85,16 +85,23 @@ type FlagInfo struct {
|
||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddSecretAnnotation add secret flag to Annotation.
|
||||||
|
func (f FlagInfo) AddSecretAnnotation(flags *pflag.FlagSet) FlagInfo {
|
||||||
|
flags.SetAnnotation(f.LongName, "classified", []string{"true"})
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
// BindStringFlag binds the flag based on the provided info. If LongName == "", nothing is registered
|
// BindStringFlag binds the flag based on the provided info. If LongName == "", nothing is registered
|
||||||
func (f FlagInfo) BindStringFlag(flags *pflag.FlagSet, target *string) {
|
func (f FlagInfo) BindStringFlag(flags *pflag.FlagSet, target *string) FlagInfo {
|
||||||
// you can't register a flag without a long name
|
// you can't register a flag without a long name
|
||||||
if len(f.LongName) > 0 {
|
if len(f.LongName) > 0 {
|
||||||
flags.StringVarP(target, f.LongName, f.ShortName, f.Default, f.Description)
|
flags.StringVarP(target, f.LongName, f.ShortName, f.Default, f.Description)
|
||||||
}
|
}
|
||||||
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
// BindBoolFlag binds the flag based on the provided info. If LongName == "", nothing is registered
|
// BindBoolFlag binds the flag based on the provided info. If LongName == "", nothing is registered
|
||||||
func (f FlagInfo) BindBoolFlag(flags *pflag.FlagSet, target *bool) {
|
func (f FlagInfo) BindBoolFlag(flags *pflag.FlagSet, target *bool) FlagInfo {
|
||||||
// you can't register a flag without a long name
|
// you can't register a flag without a long name
|
||||||
if len(f.LongName) > 0 {
|
if len(f.LongName) > 0 {
|
||||||
// try to parse Default as a bool. If it fails, assume false
|
// try to parse Default as a bool. If it fails, assume false
|
||||||
|
@ -105,6 +112,7 @@ func (f FlagInfo) BindBoolFlag(flags *pflag.FlagSet, target *bool) {
|
||||||
|
|
||||||
flags.BoolVarP(target, f.LongName, f.ShortName, boolVal, f.Description)
|
flags.BoolVarP(target, f.LongName, f.ShortName, boolVal, f.Description)
|
||||||
}
|
}
|
||||||
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -180,12 +188,12 @@ func BindOverrideFlags(overrides *ConfigOverrides, flags *pflag.FlagSet, flagNam
|
||||||
|
|
||||||
// BindAuthInfoFlags is a convenience method to bind the specified flags to their associated variables
|
// BindAuthInfoFlags is a convenience method to bind the specified flags to their associated variables
|
||||||
func BindAuthInfoFlags(authInfo *clientcmdapi.AuthInfo, flags *pflag.FlagSet, flagNames AuthOverrideFlags) {
|
func BindAuthInfoFlags(authInfo *clientcmdapi.AuthInfo, flags *pflag.FlagSet, flagNames AuthOverrideFlags) {
|
||||||
flagNames.ClientCertificate.BindStringFlag(flags, &authInfo.ClientCertificate)
|
flagNames.ClientCertificate.BindStringFlag(flags, &authInfo.ClientCertificate).AddSecretAnnotation(flags)
|
||||||
flagNames.ClientKey.BindStringFlag(flags, &authInfo.ClientKey)
|
flagNames.ClientKey.BindStringFlag(flags, &authInfo.ClientKey).AddSecretAnnotation(flags)
|
||||||
flagNames.Token.BindStringFlag(flags, &authInfo.Token)
|
flagNames.Token.BindStringFlag(flags, &authInfo.Token).AddSecretAnnotation(flags)
|
||||||
flagNames.Impersonate.BindStringFlag(flags, &authInfo.Impersonate)
|
flagNames.Impersonate.BindStringFlag(flags, &authInfo.Impersonate).AddSecretAnnotation(flags)
|
||||||
flagNames.Username.BindStringFlag(flags, &authInfo.Username)
|
flagNames.Username.BindStringFlag(flags, &authInfo.Username).AddSecretAnnotation(flags)
|
||||||
flagNames.Password.BindStringFlag(flags, &authInfo.Password)
|
flagNames.Password.BindStringFlag(flags, &authInfo.Password).AddSecretAnnotation(flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BindClusterFlags is a convenience method to bind the specified flags to their associated variables
|
// BindClusterFlags is a convenience method to bind the specified flags to their associated variables
|
||||||
|
|
Loading…
Reference in New Issue