Skip to content

Commit

Permalink
Merge pull request #586 from wzshiming/automated-cherry-pick-of-#583-…
Browse files Browse the repository at this point in the history
…upstream-release-0.2

Automated cherry pick of #583: Fix duplicate loading of default configuration
  • Loading branch information
wzshiming authored May 10, 2023
2 parents 9716a8d + 756630b commit d5fd978
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 20 deletions.
55 changes: 35 additions & 20 deletions pkg/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"sigs.k8s.io/kwok/pkg/consts"
"sigs.k8s.io/kwok/pkg/log"
"sigs.k8s.io/kwok/pkg/utils/file"
"sigs.k8s.io/kwok/pkg/utils/path"
"sigs.k8s.io/kwok/pkg/utils/slices"
)
Expand All @@ -34,45 +35,59 @@ func InitFlags(ctx context.Context, flags *pflag.FlagSet) (context.Context, erro
config := flags.StringArrayP("config", "c", []string{defaultConfigPath}, "config path")
_ = flags.Parse(os.Args[1:])

existDefaultConfig := false
configPath, err := path.Expand(defaultConfigPath)
if err == nil {
_, err = os.Stat(configPath)
if err == nil {
existDefaultConfig = true
}
// Expand the all config paths.
defaultConfigPath, err := path.Expand(defaultConfigPath)
if err != nil {
return nil, err
}

if !slices.Contains(*config, defaultConfigPath) {
if existDefaultConfig {
*config = append([]string{configPath}, *config...)
}
} else {
if !existDefaultConfig {
*config = slices.Filter(*config, func(s string) bool {
return s != defaultConfigPath
})
configPaths := make([]string, 0, len(*config))
for _, c := range *config {
configPath, err := path.Expand(c)
if err != nil {
return nil, err
}
configPaths = append(configPaths, configPath)
}

configPaths = loadConfig(configPaths, defaultConfigPath, file.Exists(defaultConfigPath))

logger := log.FromContext(ctx)
objs, err := Load(ctx, *config...)
objs, err := Load(ctx, configPaths...)
if err != nil {
return nil, err
}

if len(objs) == 0 {
logger.Debug("Load config",
"path", *config,
"path", configPaths,
"err", "empty config",
)
} else {
logger.Debug("Load config",
"path", *config,
"path", configPaths,
"count", len(objs),
"content", objs,
)
}

return setupContext(ctx, objs), nil
}

// loadConfig loads the config paths.
// ~/.kwok/kwok.yaml will be loaded first if it exists.
func loadConfig(configPaths []string, defaultConfigPath string, existDefaultConfig bool) []string {
if !slices.Contains(configPaths, defaultConfigPath) {
if existDefaultConfig {
// If the defaultConfigPath is not specified and the default config exists, it will be loaded first.
return append([]string{defaultConfigPath}, configPaths...)
}
} else {
if !existDefaultConfig {
// If the defaultConfigPath is specified and the default config does not exist, it will be removed.
return slices.Filter(configPaths, func(s string) bool {
return s != defaultConfigPath
})
}
}
return configPaths
}
88 changes: 88 additions & 0 deletions pkg/config/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2023 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 config

import (
"reflect"
"testing"
)

func Test_loadConfig(t *testing.T) {
type args struct {
configPaths []string
defaultConfigPath string
existDefaultConfig bool
}
tests := []struct {
name string
args args
want []string
}{
{
name: "default config",
args: args{
configPaths: []string{},
defaultConfigPath: "default",
existDefaultConfig: true,
},
want: []string{"default"},
},
{
name: "add default config",
args: args{
configPaths: []string{"config"},
defaultConfigPath: "default",
existDefaultConfig: true,
},
want: []string{"default", "config"},
},
{
name: "no default config",
args: args{
configPaths: []string{"config"},
defaultConfigPath: "default",
existDefaultConfig: false,
},
want: []string{"config"},
},
{
name: "no config",
args: args{
configPaths: []string{},
defaultConfigPath: "default",
existDefaultConfig: false,
},
want: []string{},
},
{
name: "remove default config",
args: args{
configPaths: []string{"default", "config"},
defaultConfigPath: "default",
existDefaultConfig: false,
},
want: []string{"config"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := loadConfig(tt.args.configPaths, tt.args.defaultConfigPath, tt.args.existDefaultConfig); !reflect.DeepEqual(got, tt.want) {
t.Errorf("loadConfig() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit d5fd978

Please sign in to comment.