Merge pull request #5671 from emirot/refactor/string_inSlice

refactor: string in slice is now part of stdlib
This commit is contained in:
Kubernetes Prow Robot
2024-04-23 04:36:33 -07:00
committed by GitHub
16 changed files with 33 additions and 28 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -113,7 +114,7 @@ func runCreate(opts createFlags, fSys filesys.FileSystem, rf *resource.Factory)
return err return err
} }
for _, resource := range detected { for _, resource := range detected {
if kustfile.StringInSlice(resource, resources) { if slices.Contains(resources, resource) {
continue continue
} }
resources = append(resources, resource) resources = append(resources, resource)

View File

@@ -8,6 +8,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"slices"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
"sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/filesys"
@@ -64,7 +66,7 @@ func (o *addBaseOptions) RunAddBase(fSys filesys.FileSystem) error {
if !fSys.Exists(path) { if !fSys.Exists(path) {
return errors.New(path + " does not exist") return errors.New(path + " does not exist")
} }
if kustfile.StringInSlice(path, m.Resources) { if slices.Contains(m.Resources, path) {
return fmt.Errorf("base %s already in kustomization file", path) return fmt.Errorf("base %s already in kustomization file", path)
} }
m.Resources = append(m.Resources, path) m.Resources = append(m.Resources, path)

View File

@@ -4,12 +4,12 @@
package add package add
import ( import (
"slices"
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils" testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
"sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/filesys"
) )
@@ -57,7 +57,7 @@ func TestAddBaseAlreadyThere(t *testing.T) {
for _, base := range bases { for _, base := range bases {
msg := "base " + base + " already in kustomization file" msg := "base " + base + " already in kustomization file"
expectedErrors = append(expectedErrors, msg) expectedErrors = append(expectedErrors, msg)
assert.True(t, kustfile.StringInSlice(msg, expectedErrors)) assert.True(t, slices.Contains(expectedErrors, msg))
} }
} }

View File

@@ -5,6 +5,7 @@ package add
import ( import (
"fmt" "fmt"
"slices"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
@@ -60,7 +61,7 @@ func (o *addBuildMetadataOptions) RunAddBuildMetadata(fSys filesys.FileSystem) e
return err return err
} }
for _, opt := range o.buildMetadataOptions { for _, opt := range o.buildMetadataOptions {
if kustfile.StringInSlice(opt, m.BuildMetadata) { if slices.Contains(m.BuildMetadata, opt) {
return fmt.Errorf("buildMetadata option %s already in kustomization file", opt) return fmt.Errorf("buildMetadata option %s already in kustomization file", opt)
} }
m.BuildMetadata = append(m.BuildMetadata, opt) m.BuildMetadata = append(m.BuildMetadata, opt)

View File

@@ -6,6 +6,7 @@ package add
import ( import (
"errors" "errors"
"log" "log"
"slices"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/pkg/loader" "sigs.k8s.io/kustomize/api/pkg/loader"
@@ -69,7 +70,7 @@ func (o *addComponentOptions) RunAddComponent(fSys filesys.FileSystem) error {
for _, component := range components { for _, component := range components {
if mf.GetPath() != component { if mf.GetPath() != component {
if kustfile.StringInSlice(component, m.Components) { if slices.Contains(m.Components, component) {
log.Printf("component %s already in kustomization file", component) log.Printf("component %s already in kustomization file", component)
continue continue
} }

View File

@@ -6,6 +6,7 @@ package add
import ( import (
"errors" "errors"
"log" "log"
"slices"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
@@ -62,7 +63,7 @@ func (o *addGeneratorOptions) RunAddGenerator(fSys filesys.FileSystem) error {
return err return err
} }
for _, t := range o.generatorFilePaths { for _, t := range o.generatorFilePaths {
if kustfile.StringInSlice(t, m.Generators) { if slices.Contains(m.Generators, t) {
log.Printf("generator %s already in kustomization file", t) log.Printf("generator %s already in kustomization file", t)
continue continue
} }

View File

@@ -6,6 +6,7 @@ package add
import ( import (
"errors" "errors"
"log" "log"
"slices"
"github.com/spf13/cobra" "github.com/spf13/cobra"
ldrhelper "sigs.k8s.io/kustomize/api/pkg/loader" ldrhelper "sigs.k8s.io/kustomize/api/pkg/loader"
@@ -73,7 +74,7 @@ func (o *addResourceOptions) RunAddResource(fSys filesys.FileSystem) error {
for _, resource := range resources { for _, resource := range resources {
if mf.GetPath() != resource { if mf.GetPath() != resource {
if kustfile.StringInSlice(resource, m.Resources) { if slices.Contains(m.Resources, resource) {
log.Printf("resource %s already in kustomization file", resource) log.Printf("resource %s already in kustomization file", resource)
continue continue
} }

View File

@@ -6,6 +6,7 @@ package add
import ( import (
"errors" "errors"
"log" "log"
"slices"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
@@ -62,7 +63,7 @@ func (o *addTransformerOptions) RunAddTransformer(fSys filesys.FileSystem) error
return err return err
} }
for _, t := range o.transformerFilePaths { for _, t := range o.transformerFilePaths {
if kustfile.StringInSlice(t, m.Transformers) { if slices.Contains(m.Transformers, t) {
log.Printf("transformer %s already in kustomization file", t) log.Printf("transformer %s already in kustomization file", t)
continue continue
} }

View File

@@ -7,6 +7,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"path" "path"
"slices"
"strconv" "strconv"
"strings" "strings"
@@ -55,11 +56,11 @@ func filesTouchedByKustomize(k *types.Kustomization, filepath string, fSys files
files, err := fSys.ReadDir(r) files, err := fSys.ReadDir(r)
if err == nil && len(files) > 0 { if err == nil && len(files) > 0 {
for _, file := range files { for _, file := range files {
if !stringInSlice(file, []string{ if !slices.Contains([]string{
"kustomization.yaml", "kustomization.yaml",
"kustomization.yml", "kustomization.yml",
"Kustomization", "Kustomization",
}) { }, file) {
continue continue
} }

View File

@@ -4,6 +4,8 @@
package remove package remove
import ( import (
"slices"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util"
@@ -59,7 +61,7 @@ func (o *removeBuildMetadataOptions) RunRemoveBuildMetadata(fSys filesys.FileSys
} }
var newOptions []string var newOptions []string
for _, opt := range m.BuildMetadata { for _, opt := range m.BuildMetadata {
if !kustfile.StringInSlice(opt, o.buildMetadataOptions) { if !slices.Contains(o.buildMetadataOptions, opt) {
newOptions = append(newOptions, opt) newOptions = append(newOptions, opt)
} }
} }

View File

@@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"slices"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -88,7 +89,7 @@ func (o *removeConfigMapOptions) RunRemoveConfigMap(fSys filesys.FileSystem) err
remainingConfigMaps := make([]types.ConfigMapArgs, 0, len(m.ConfigMapGenerator)) remainingConfigMaps := make([]types.ConfigMapArgs, 0, len(m.ConfigMapGenerator))
for _, currentConfigMap := range m.ConfigMapGenerator { for _, currentConfigMap := range m.ConfigMapGenerator {
if kustfile.StringInSlice(currentConfigMap.Name, o.configMapNamesToRemove) && if slices.Contains(o.configMapNamesToRemove, currentConfigMap.Name) &&
util.NamespaceEqual(currentConfigMap.Namespace, o.namespace) { util.NamespaceEqual(currentConfigMap.Namespace, o.namespace) {
foundConfigMaps[currentConfigMap.Name] = struct{}{} foundConfigMaps[currentConfigMap.Name] = struct{}{}
continue continue

View File

@@ -6,6 +6,7 @@ package remove
import ( import (
"errors" "errors"
"path/filepath" "path/filepath"
"slices"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig" "sigs.k8s.io/kustomize/api/konfig"
@@ -73,7 +74,7 @@ func (o *removeResourceOptions) RunRemoveResource(fSys filesys.FileSystem) error
newResources := make([]string, 0, len(m.Resources)) newResources := make([]string, 0, len(m.Resources))
for _, resource := range m.Resources { for _, resource := range m.Resources {
if kustfile.StringInSlice(resource, resources) { if slices.Contains(resources, resource) {
continue continue
} }
newResources = append(newResources, resource) newResources = append(newResources, resource)

View File

@@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"slices"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -89,7 +90,7 @@ func (o *removeSecretOptions) RunRemoveSecret(fSys filesys.FileSystem) error {
remainingSecrets := make([]types.SecretArgs, 0, len(m.SecretGenerator)) remainingSecrets := make([]types.SecretArgs, 0, len(m.SecretGenerator))
for _, currentSecret := range m.SecretGenerator { for _, currentSecret := range m.SecretGenerator {
if kustfile.StringInSlice(currentSecret.Name, o.secretNamesToRemove) && if slices.Contains(o.secretNamesToRemove, currentSecret.Name) &&
util.NamespaceEqual(currentSecret.Namespace, o.namespace) { util.NamespaceEqual(currentSecret.Namespace, o.namespace) {
foundSecrets[currentSecret.Name] = struct{}{} foundSecrets[currentSecret.Name] = struct{}{}
continue continue

View File

@@ -5,6 +5,7 @@ package remove
import ( import (
"errors" "errors"
"slices"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig" "sigs.k8s.io/kustomize/api/konfig"
@@ -72,7 +73,7 @@ func (o *removeTransformerOptions) RunRemoveTransformer(fSys filesys.FileSystem)
newTransformers := make([]string, 0, len(m.Transformers)) newTransformers := make([]string, 0, len(m.Transformers))
for _, transformer := range m.Transformers { for _, transformer := range m.Transformers {
if kustfile.StringInSlice(transformer, transformers) { if slices.Contains(transformers, transformer) {
continue continue
} }
newTransformers = append(newTransformers, transformer) newTransformers = append(newTransformers, transformer)

View File

@@ -189,16 +189,6 @@ func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error {
return mf.fSys.WriteFile(mf.path, data) return mf.fSys.WriteFile(mf.path, data)
} }
// StringInSlice returns true if the string is in the slice.
func StringInSlice(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}
func (mf *kustomizationFile) parseCommentedFields(content []byte) error { func (mf *kustomizationFile) parseCommentedFields(content []byte) error {
buffer := bytes.NewBuffer(content) buffer := bytes.NewBuffer(content)
var comments [][]byte var comments [][]byte

View File

@@ -6,10 +6,10 @@ package util
import ( import (
"errors" "errors"
"fmt" "fmt"
"slices"
"strings" "strings"
"sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
) )
type BuildMetadataValidator struct{} type BuildMetadataValidator struct{}
@@ -23,7 +23,7 @@ func (b *BuildMetadataValidator) Validate(args []string) ([]string, error) {
} }
opts := strings.Split(args[0], ",") opts := strings.Split(args[0], ",")
for _, opt := range opts { for _, opt := range opts {
if !kustfile.StringInSlice(opt, types.BuildMetadataOptions) { if !slices.Contains(types.BuildMetadataOptions, opt) {
return nil, fmt.Errorf("invalid buildMetadata option: %s", opt) return nil, fmt.Errorf("invalid buildMetadata option: %s", opt)
} }
} }