Merge pull request #31795 from mmorel-35/modernize-internal-1-bbfa1fe

chore(internal): fix modernize linter
pull/31804/head
Terry Howe 7 months ago committed by GitHub
commit 9b22aa4e05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -69,15 +69,15 @@ func Chartfile(linter *support.Linter) {
linter.RunLinterRule(support.ErrorSev, chartFileName, validateChartDependencies(chartFile)) linter.RunLinterRule(support.ErrorSev, chartFileName, validateChartDependencies(chartFile))
} }
func validateChartVersionType(data map[string]interface{}) error { func validateChartVersionType(data map[string]any) error {
return isStringValue(data, "version") return isStringValue(data, "version")
} }
func validateChartAppVersionType(data map[string]interface{}) error { func validateChartAppVersionType(data map[string]any) error {
return isStringValue(data, "appVersion") return isStringValue(data, "appVersion")
} }
func isStringValue(data map[string]interface{}, key string) error { func isStringValue(data map[string]any, key string) error {
value, ok := data[key] value, ok := data[key]
if !ok { if !ok {
return nil return nil
@ -214,12 +214,12 @@ func validateChartType(cf *chart.Metadata) error {
// loadChartFileForTypeCheck loads the Chart.yaml // loadChartFileForTypeCheck loads the Chart.yaml
// in a generic form of a map[string]interface{}, so that the type // in a generic form of a map[string]interface{}, so that the type
// of the values can be checked // of the values can be checked
func loadChartFileForTypeCheck(filename string) (map[string]interface{}, error) { func loadChartFileForTypeCheck(filename string) (map[string]any, error) {
b, err := os.ReadFile(filename) b, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
y := make(map[string]interface{}) y := make(map[string]any)
err = yaml.Unmarshal(b, &y) err = yaml.Unmarshal(b, &y)
return y, err return y, err
} }

@ -42,17 +42,17 @@ import (
) )
// Templates lints the templates in the Linter. // Templates lints the templates in the Linter.
func Templates(linter *support.Linter, values map[string]interface{}, namespace string, _ bool) { func Templates(linter *support.Linter, values map[string]any, namespace string, _ bool) {
TemplatesWithKubeVersion(linter, values, namespace, nil) TemplatesWithKubeVersion(linter, values, namespace, nil)
} }
// TemplatesWithKubeVersion lints the templates in the Linter, allowing to specify the kubernetes version. // TemplatesWithKubeVersion lints the templates in the Linter, allowing to specify the kubernetes version.
func TemplatesWithKubeVersion(linter *support.Linter, values map[string]interface{}, namespace string, kubeVersion *common.KubeVersion) { func TemplatesWithKubeVersion(linter *support.Linter, values map[string]any, namespace string, kubeVersion *common.KubeVersion) {
TemplatesWithSkipSchemaValidation(linter, values, namespace, kubeVersion, false) TemplatesWithSkipSchemaValidation(linter, values, namespace, kubeVersion, false)
} }
// TemplatesWithSkipSchemaValidation lints the templates in the Linter, allowing to specify the kubernetes version and if schema validation is enabled or not. // TemplatesWithSkipSchemaValidation lints the templates in the Linter, allowing to specify the kubernetes version and if schema validation is enabled or not.
func TemplatesWithSkipSchemaValidation(linter *support.Linter, values map[string]interface{}, namespace string, kubeVersion *common.KubeVersion, skipSchemaValidation bool) { func TemplatesWithSkipSchemaValidation(linter *support.Linter, values map[string]any, namespace string, kubeVersion *common.KubeVersion, skipSchemaValidation bool) {
fpath := "templates/" fpath := "templates/"
templatesPath := filepath.Join(linter.ChartDir, fpath) templatesPath := filepath.Join(linter.ChartDir, fpath)

@ -32,7 +32,7 @@ import (
// they are only tested for well-formedness. // they are only tested for well-formedness.
// //
// If additional values are supplied, they are coalesced into the values in values.yaml. // If additional values are supplied, they are coalesced into the values in values.yaml.
func ValuesWithOverrides(linter *support.Linter, valueOverrides map[string]interface{}, skipSchemaValidation bool) { func ValuesWithOverrides(linter *support.Linter, valueOverrides map[string]any, skipSchemaValidation bool) {
file := "values.yaml" file := "values.yaml"
vf := filepath.Join(linter.ChartDir, file) vf := filepath.Join(linter.ChartDir, file)
fileExists := linter.RunLinterRule(support.InfoSev, file, validateValuesFileExistence(vf)) fileExists := linter.RunLinterRule(support.InfoSev, file, validateValuesFileExistence(vf))
@ -52,7 +52,7 @@ func validateValuesFileExistence(valuesPath string) error {
return nil return nil
} }
func validateValuesFile(valuesPath string, overrides map[string]interface{}, skipSchemaValidation bool) error { func validateValuesFile(valuesPath string, overrides map[string]any, skipSchemaValidation bool) error {
values, err := common.ReadValuesFile(valuesPath) values, err := common.ReadValuesFile(valuesPath)
if err != nil { if err != nil {
return fmt.Errorf("unable to parse YAML: %w", err) return fmt.Errorf("unable to parse YAML: %w", err)
@ -63,7 +63,7 @@ func validateValuesFile(valuesPath string, overrides map[string]interface{}, ski
// We could change that. For now, though, we retain that strategy, and thus can // We could change that. For now, though, we retain that strategy, and thus can
// coalesce tables (like reuse-values does) instead of doing the full chart // coalesce tables (like reuse-values does) instead of doing the full chart
// CoalesceValues // CoalesceValues
coalescedValues := util.CoalesceTables(make(map[string]interface{}, len(overrides)), overrides) coalescedValues := util.CoalesceTables(make(map[string]any, len(overrides)), overrides)
coalescedValues = util.CoalesceTables(coalescedValues, values) coalescedValues = util.CoalesceTables(coalescedValues, values)
ext := filepath.Ext(valuesPath) ext := filepath.Ext(valuesPath)

@ -67,7 +67,7 @@ func TestValidateValuesFileWellFormed(t *testing.T) {
` `
tmpdir := ensure.TempFile(t, "values.yaml", []byte(badYaml)) tmpdir := ensure.TempFile(t, "values.yaml", []byte(badYaml))
valfile := filepath.Join(tmpdir, "values.yaml") valfile := filepath.Join(tmpdir, "values.yaml")
if err := validateValuesFile(valfile, map[string]interface{}{}, false); err == nil { if err := validateValuesFile(valfile, map[string]any{}, false); err == nil {
t.Fatal("expected values file to fail parsing") t.Fatal("expected values file to fail parsing")
} }
} }
@ -78,7 +78,7 @@ func TestValidateValuesFileSchema(t *testing.T) {
createTestingSchema(t, tmpdir) createTestingSchema(t, tmpdir)
valfile := filepath.Join(tmpdir, "values.yaml") valfile := filepath.Join(tmpdir, "values.yaml")
if err := validateValuesFile(valfile, map[string]interface{}{}, false); err != nil { if err := validateValuesFile(valfile, map[string]any{}, false); err != nil {
t.Fatalf("Failed validation with %s", err) t.Fatalf("Failed validation with %s", err)
} }
} }
@ -91,7 +91,7 @@ func TestValidateValuesFileSchemaFailure(t *testing.T) {
valfile := filepath.Join(tmpdir, "values.yaml") valfile := filepath.Join(tmpdir, "values.yaml")
err := validateValuesFile(valfile, map[string]interface{}{}, false) err := validateValuesFile(valfile, map[string]any{}, false)
if err == nil { if err == nil {
t.Fatal("expected values file to fail parsing") t.Fatal("expected values file to fail parsing")
} }
@ -107,7 +107,7 @@ func TestValidateValuesFileSchemaFailureButWithSkipSchemaValidation(t *testing.T
valfile := filepath.Join(tmpdir, "values.yaml") valfile := filepath.Join(tmpdir, "values.yaml")
err := validateValuesFile(valfile, map[string]interface{}{}, true) err := validateValuesFile(valfile, map[string]any{}, true)
if err != nil { if err != nil {
t.Fatal("expected values file to pass parsing because of skipSchemaValidation") t.Fatal("expected values file to pass parsing because of skipSchemaValidation")
} }
@ -115,7 +115,7 @@ func TestValidateValuesFileSchemaFailureButWithSkipSchemaValidation(t *testing.T
func TestValidateValuesFileSchemaOverrides(t *testing.T) { func TestValidateValuesFileSchemaOverrides(t *testing.T) {
yaml := "username: admin" yaml := "username: admin"
overrides := map[string]interface{}{ overrides := map[string]any{
"password": "swordfish", "password": "swordfish",
} }
tmpdir := ensure.TempFile(t, "values.yaml", []byte(yaml)) tmpdir := ensure.TempFile(t, "values.yaml", []byte(yaml))
@ -131,24 +131,24 @@ func TestValidateValuesFile(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
yaml string yaml string
overrides map[string]interface{} overrides map[string]any
errorMessage string errorMessage string
}{ }{
{ {
name: "value added", name: "value added",
yaml: "username: admin", yaml: "username: admin",
overrides: map[string]interface{}{"password": "swordfish"}, overrides: map[string]any{"password": "swordfish"},
}, },
{ {
name: "value not overridden", name: "value not overridden",
yaml: "username: admin\npassword:", yaml: "username: admin\npassword:",
overrides: map[string]interface{}{"username": "anotherUser"}, overrides: map[string]any{"username": "anotherUser"},
errorMessage: "- at '/password': got null, want string", errorMessage: "- at '/password': got null, want string",
}, },
{ {
name: "value overridden", name: "value overridden",
yaml: "username: admin\npassword:", yaml: "username: admin\npassword:",
overrides: map[string]interface{}{"username": "anotherUser", "password": "swordfish"}, overrides: map[string]any{"username": "anotherUser", "password": "swordfish"},
}, },
} }

@ -182,11 +182,11 @@ func LoadFiles(files []*archive.BufferedFile) (*chart.Chart, error) {
// //
// The reader is expected to contain one or more YAML documents, the values of which are merged. // The reader is expected to contain one or more YAML documents, the values of which are merged.
// And the values can be either a chart's default values or user-supplied values. // And the values can be either a chart's default values or user-supplied values.
func LoadValues(data io.Reader) (map[string]interface{}, error) { func LoadValues(data io.Reader) (map[string]any, error) {
values := map[string]interface{}{} values := map[string]any{}
reader := utilyaml.NewYAMLReader(bufio.NewReader(data)) reader := utilyaml.NewYAMLReader(bufio.NewReader(data))
for { for {
currentMap := map[string]interface{}{} currentMap := map[string]any{}
raw, err := reader.Read() raw, err := reader.Read()
if err != nil { if err != nil {
if errors.Is(err, io.EOF) { if errors.Is(err, io.EOF) {
@ -204,13 +204,13 @@ func LoadValues(data io.Reader) (map[string]interface{}, error) {
// MergeMaps merges two maps. If a key exists in both maps, the value from b will be used. // MergeMaps merges two maps. If a key exists in both maps, the value from b will be used.
// If the value is a map, the maps will be merged recursively. // If the value is a map, the maps will be merged recursively.
func MergeMaps(a, b map[string]interface{}) map[string]interface{} { func MergeMaps(a, b map[string]any) map[string]any {
out := make(map[string]interface{}, len(a)) out := make(map[string]any, len(a))
maps.Copy(out, a) maps.Copy(out, a)
for k, v := range b { for k, v := range b {
if v, ok := v.(map[string]interface{}); ok { if v, ok := v.(map[string]any); ok {
if bv, ok := out[k]; ok { if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok { if bv, ok := bv.(map[string]any); ok {
out[k] = MergeMaps(bv, v) out[k] = MergeMaps(bv, v)
continue continue
} }

@ -455,7 +455,7 @@ func TestLoadInvalidArchive(t *testing.T) {
func TestLoadValues(t *testing.T) { func TestLoadValues(t *testing.T) {
testCases := map[string]struct { testCases := map[string]struct {
data []byte data []byte
expctedValues map[string]interface{} expctedValues map[string]any
}{ }{
"It should load values correctly": { "It should load values correctly": {
data: []byte(` data: []byte(`
@ -464,11 +464,11 @@ foo:
bar: bar:
version: v2 version: v2
`), `),
expctedValues: map[string]interface{}{ expctedValues: map[string]any{
"foo": map[string]interface{}{ "foo": map[string]any{
"image": "foo:v1", "image": "foo:v1",
}, },
"bar": map[string]interface{}{ "bar": map[string]any{
"version": "v2", "version": "v2",
}, },
}, },
@ -483,11 +483,11 @@ bar:
foo: foo:
image: foo:v2 image: foo:v2
`), `),
expctedValues: map[string]interface{}{ expctedValues: map[string]any{
"foo": map[string]interface{}{ "foo": map[string]any{
"image": "foo:v2", "image": "foo:v2",
}, },
"bar": map[string]interface{}{ "bar": map[string]any{
"version": "v2", "version": "v2",
}, },
}, },
@ -507,24 +507,24 @@ foo:
} }
func TestMergeValuesV3(t *testing.T) { func TestMergeValuesV3(t *testing.T) {
nestedMap := map[string]interface{}{ nestedMap := map[string]any{
"foo": "bar", "foo": "bar",
"baz": map[string]string{ "baz": map[string]string{
"cool": "stuff", "cool": "stuff",
}, },
} }
anotherNestedMap := map[string]interface{}{ anotherNestedMap := map[string]any{
"foo": "bar", "foo": "bar",
"baz": map[string]string{ "baz": map[string]string{
"cool": "things", "cool": "things",
"awesome": "stuff", "awesome": "stuff",
}, },
} }
flatMap := map[string]interface{}{ flatMap := map[string]any{
"foo": "bar", "foo": "bar",
"baz": "stuff", "baz": "stuff",
} }
anotherFlatMap := map[string]interface{}{ anotherFlatMap := map[string]any{
"testing": "fun", "testing": "fun",
} }
@ -547,7 +547,7 @@ func TestMergeValuesV3(t *testing.T) {
} }
testMap = MergeMaps(anotherFlatMap, anotherNestedMap) testMap = MergeMaps(anotherFlatMap, anotherNestedMap)
expectedMap := map[string]interface{}{ expectedMap := map[string]any{
"testing": "fun", "testing": "fun",
"foo": "bar", "foo": "bar",
"baz": map[string]string{ "baz": map[string]string{

@ -140,7 +140,7 @@ func copyMetadata(metadata *chart.Metadata) *chart.Metadata {
} }
// processDependencyEnabled removes disabled charts from dependencies // processDependencyEnabled removes disabled charts from dependencies
func processDependencyEnabled(c *chart.Chart, v map[string]interface{}, path string) error { func processDependencyEnabled(c *chart.Chart, v map[string]any, path string) error {
if c.Metadata.Dependencies == nil { if c.Metadata.Dependencies == nil {
return nil return nil
} }
@ -226,7 +226,7 @@ Loop:
} }
// pathToMap creates a nested map given a YAML path in dot notation. // pathToMap creates a nested map given a YAML path in dot notation.
func pathToMap(path string, data map[string]interface{}) map[string]interface{} { func pathToMap(path string, data map[string]any) map[string]any {
if path == "." { if path == "." {
return data return data
} }
@ -235,13 +235,13 @@ func pathToMap(path string, data map[string]interface{}) map[string]interface{}
func parsePath(key string) []string { return strings.Split(key, ".") } func parsePath(key string) []string { return strings.Split(key, ".") }
func set(path []string, data map[string]interface{}) map[string]interface{} { func set(path []string, data map[string]any) map[string]any {
if len(path) == 0 { if len(path) == 0 {
return nil return nil
} }
cur := data cur := data
for i := len(path) - 1; i >= 0; i-- { for i := len(path) - 1; i >= 0; i-- {
cur = map[string]interface{}{path[i]: cur} cur = map[string]any{path[i]: cur}
} }
return cur return cur
} }
@ -262,13 +262,13 @@ func processImportValues(c *chart.Chart, merge bool) error {
if err != nil { if err != nil {
return err return err
} }
b := make(map[string]interface{}) b := make(map[string]any)
// import values from each dependency if specified in import-values // import values from each dependency if specified in import-values
for _, r := range c.Metadata.Dependencies { for _, r := range c.Metadata.Dependencies {
var outiv []interface{} var outiv []any
for _, riv := range r.ImportValues { for _, riv := range r.ImportValues {
switch iv := riv.(type) { switch iv := riv.(type) {
case map[string]interface{}: case map[string]any:
child := fmt.Sprintf("%v", iv["child"]) child := fmt.Sprintf("%v", iv["child"])
parent := fmt.Sprintf("%v", iv["parent"]) parent := fmt.Sprintf("%v", iv["parent"])
@ -336,27 +336,27 @@ func processImportValues(c *chart.Chart, merge bool) error {
return nil return nil
} }
func deepCopyMap(vals map[string]interface{}) map[string]interface{} { func deepCopyMap(vals map[string]any) map[string]any {
valsCopy, err := copystructure.Copy(vals) valsCopy, err := copystructure.Copy(vals)
if err != nil { if err != nil {
return vals return vals
} }
return valsCopy.(map[string]interface{}) return valsCopy.(map[string]any)
} }
func trimNilValues(vals map[string]interface{}) map[string]interface{} { func trimNilValues(vals map[string]any) map[string]any {
valsCopy, err := copystructure.Copy(vals) valsCopy, err := copystructure.Copy(vals)
if err != nil { if err != nil {
return vals return vals
} }
valsCopyMap := valsCopy.(map[string]interface{}) valsCopyMap := valsCopy.(map[string]any)
for key, val := range valsCopyMap { for key, val := range valsCopyMap {
if val == nil { if val == nil {
// Iterate over the values and remove nil keys // Iterate over the values and remove nil keys
delete(valsCopyMap, key) delete(valsCopyMap, key)
} else if istable(val) { } else if istable(val) {
// Recursively call into ourselves to remove keys from inner tables // Recursively call into ourselves to remove keys from inner tables
valsCopyMap[key] = trimNilValues(val.(map[string]interface{})) valsCopyMap[key] = trimNilValues(val.(map[string]any))
} }
} }
@ -364,8 +364,8 @@ func trimNilValues(vals map[string]interface{}) map[string]interface{} {
} }
// istable is a special-purpose function to see if the present thing matches the definition of a YAML table. // istable is a special-purpose function to see if the present thing matches the definition of a YAML table.
func istable(v interface{}) bool { func istable(v any) bool {
_, ok := v.(map[string]interface{}) _, ok := v.(map[string]any)
return ok return ok
} }

Loading…
Cancel
Save