forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flagz implementation and enablement in apiserver
- Loading branch information
1 parent
60651eb
commit da8dc43
Showing
16 changed files
with
445 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
staging/src/k8s.io/component-base/zpages/flagz/flagreader.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright 2024 The Kubernetes 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 flagz | ||
|
||
import ( | ||
"github.com/spf13/pflag" | ||
cliflag "k8s.io/component-base/cli/flag" | ||
) | ||
|
||
type Reader interface { | ||
GetFlagz() map[string]string | ||
} | ||
|
||
// NamedFlagSetsGetter implements Reader for cliflag.NamedFlagSets | ||
type NamedFlagSetsReader struct { | ||
FlagSets cliflag.NamedFlagSets | ||
} | ||
|
||
func (n NamedFlagSetsReader) GetFlagz() map[string]string { | ||
return convertNamedFlagSetToFlags(&n.FlagSets) | ||
} | ||
|
||
func convertNamedFlagSetToFlags(flagSets *cliflag.NamedFlagSets) map[string]string { | ||
flags := make(map[string]string) | ||
for _, fs := range flagSets.FlagSets { | ||
fs.VisitAll(func(flag *pflag.Flag) { | ||
if flag.Value != nil { | ||
value := flag.Value.String() | ||
if set, ok := flag.Annotations["classified"]; ok && len(set) > 0 { | ||
value = "CLASSIFIED" | ||
} | ||
flags[flag.Name] = value | ||
} | ||
}) | ||
} | ||
|
||
return flags | ||
} |
95 changes: 95 additions & 0 deletions
95
staging/src/k8s.io/component-base/zpages/flagz/flagreader_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright 2024 The Kubernetes 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 flagz | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"k8s.io/component-base/cli/flag" | ||
) | ||
|
||
func TestConvertNamedFlagSetToFlags(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
flagSets *flag.NamedFlagSets | ||
want map[string]string | ||
}{ | ||
{ | ||
name: "basic flags", | ||
flagSets: &flag.NamedFlagSets{ | ||
FlagSets: map[string]*pflag.FlagSet{ | ||
"test": flagSet(t, map[string]flagValue{ | ||
"flag1": {value: "value1", sensitive: false}, | ||
"flag2": {value: "value2", sensitive: false}, | ||
}), | ||
}, | ||
}, | ||
want: map[string]string{ | ||
"flag1": "value1", | ||
"flag2": "value2", | ||
}, | ||
}, | ||
{ | ||
name: "classified flags", | ||
flagSets: &flag.NamedFlagSets{ | ||
FlagSets: map[string]*pflag.FlagSet{ | ||
"test": flagSet(t, map[string]flagValue{ | ||
"secret1": {value: "value1", sensitive: true}, | ||
"flag2": {value: "value2", sensitive: false}, | ||
}), | ||
}, | ||
}, | ||
want: map[string]string{ | ||
"flag2": "value2", | ||
"secret1": "CLASSIFIED", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := convertNamedFlagSetToFlags(tt.flagSets) | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ConvertNamedFlagSetToFlags() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type flagValue struct { | ||
value string | ||
sensitive bool | ||
} | ||
|
||
func flagSet(t *testing.T, flags map[string]flagValue) *pflag.FlagSet { | ||
fs := pflag.NewFlagSet("test-set", pflag.ContinueOnError) | ||
for flagName, flagVal := range flags { | ||
flagValue := "" | ||
fs.StringVar(&flagValue, flagName, flagVal.value, "test-usage") | ||
if flagVal.sensitive { | ||
err := fs.SetAnnotation(flagName, "classified", []string{"true"}) | ||
if err != nil { | ||
t.Fatalf("unexpected error when setting flag annotation: %v", err) | ||
} | ||
} | ||
} | ||
|
||
return fs | ||
} |
Oops, something went wrong.