Skip to content

Commit

Permalink
fix: parse pre-cache-resources arguments bug (#18)
Browse files Browse the repository at this point in the history
* fix: parse `pre-cache-resources` arguments bug

Signed-off-by: Yusan Kurban <[email protected]>
  • Loading branch information
yusank authored Apr 12, 2023
1 parent 57d4310 commit 92b2a98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
23 changes: 17 additions & 6 deletions cmd/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewOptions() *Options {

// AddFlags adds flags to the specified FlagSet.
func (o *Options) AddFlags(flags *pflag.FlagSet) {
o.PreCacheResources = NewPreCacheResources([]string{"Deployment/apps/v1", "ReplicaSet/apps/v1"})
o.PreCacheResources = NewPreCacheResources([]string{})
flags.StringVar(&o.BindAddress, "bind-address", defaultBindAddress,
"The IP address on which to listen for the --secure-port port.")
flags.IntVar(&o.SecurePort, "secure-port", defaultPort,
Expand Down Expand Up @@ -96,6 +96,11 @@ func NewPreCacheResources(slice []string) *ResourceSlice {
func (s *ResourceSlice) String() string {
sb := &strings.Builder{}

// check if the value is nil
if s.value == nil {
return "[]"
}

for i, gvk := range *s.value {
if i != 0 {
sb.WriteString(",")
Expand All @@ -113,12 +118,18 @@ func (s *ResourceSlice) Set(val string) error {
if !s.changed {
*s.value = make([]schema.GroupVersionKind, 0)
}
gvk, err := s.readResource(val)
if err != nil {
return err

// split by comma
vals := strings.Split(val, ",")
for _, v := range vals {
gvk, err := s.readResource(v)
if err != nil {
return err
}
*s.value = append(*s.value, gvk)
s.changed = true
}
*s.value = append(*s.value, gvk)
s.changed = true

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/app/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestResourceSlice_Set(t *testing.T) {
s: NewPreCacheResources([]string{"Pod/v1"}),
},
args: args{
val: "Node/v1",
val: "Node/v1,B/apps/v1",
},
wantErr: false,
},
Expand Down Expand Up @@ -79,6 +79,7 @@ func TestResourceSlice_Set(t *testing.T) {
if err := tt.fields.s.Set(tt.args.val); (err != nil) != tt.wantErr {
t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr)
}
t.Log(tt.fields.s.String())
t.Log(tt.fields.s.Type())
})
}
Expand Down

0 comments on commit 92b2a98

Please sign in to comment.