mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Add glob support in subcommands add patch and add configmap
This commit is contained in:
@@ -18,7 +18,7 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"log"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type addPatchOptions struct {
|
type addPatchOptions struct {
|
||||||
patchFilePath string
|
patchFilePaths []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// newCmdAddPatch adds the name of a file containing a patch to the kustomization file.
|
// newCmdAddPatch adds the name of a file containing a patch to the kustomization file.
|
||||||
@@ -56,10 +56,10 @@ func newCmdAddPatch(fsys fs.FileSystem) *cobra.Command {
|
|||||||
|
|
||||||
// Validate validates addPatch command.
|
// Validate validates addPatch command.
|
||||||
func (o *addPatchOptions) Validate(args []string) error {
|
func (o *addPatchOptions) Validate(args []string) error {
|
||||||
if len(args) != 1 {
|
if len(args) == 0 {
|
||||||
return errors.New("must specify a patch file")
|
return errors.New("must specify a patch file")
|
||||||
}
|
}
|
||||||
o.patchFilePath = args[0]
|
o.patchFilePaths = args
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,8 +70,12 @@ func (o *addPatchOptions) Complete(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// RunAddPatch runs addPatch command (do real work).
|
// RunAddPatch runs addPatch command (do real work).
|
||||||
func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error {
|
func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error {
|
||||||
if !fsys.Exists(o.patchFilePath) {
|
patches, err := globPatterns(fsys, o.patchFilePaths)
|
||||||
return errors.New(o.patchFilePath + " doesn't exist")
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(patches) == 0 {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
|
||||||
@@ -84,11 +88,13 @@ func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if stringInSlice(o.patchFilePath, m.Patches) {
|
for _, patch := range patches {
|
||||||
return fmt.Errorf("patch %s already in kustomization file", o.patchFilePath)
|
if stringInSlice(patch, m.Patches) {
|
||||||
|
log.Printf("patch %s already in kustomization file", patch)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
m.Patches = append(m.Patches, patch)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Patches = append(m.Patches, o.patchFilePath)
|
|
||||||
|
|
||||||
return mf.write(m)
|
return mf.write(m)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,10 +36,11 @@ sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
|||||||
func TestAddPatchHappyPath(t *testing.T) {
|
func TestAddPatchHappyPath(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
|
fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
|
||||||
|
fakeFS.WriteFile(patchFileName+"another", []byte(patchFileContent))
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
||||||
|
|
||||||
cmd := newCmdAddPatch(fakeFS)
|
cmd := newCmdAddPatch(fakeFS)
|
||||||
args := []string{patchFileName}
|
args := []string{patchFileName + "*"}
|
||||||
err := cmd.RunE(cmd, args)
|
err := cmd.RunE(cmd, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected cmd error: %v", err)
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
@@ -51,6 +52,9 @@ func TestAddPatchHappyPath(t *testing.T) {
|
|||||||
if !strings.Contains(string(content), patchFileName) {
|
if !strings.Contains(string(content), patchFileName) {
|
||||||
t.Errorf("expected patch name in kustomization")
|
t.Errorf("expected patch name in kustomization")
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(string(content), patchFileName+"another") {
|
||||||
|
t.Errorf("expected patch name in kustomization")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddPatchAlreadyThere(t *testing.T) {
|
func TestAddPatchAlreadyThere(t *testing.T) {
|
||||||
@@ -65,13 +69,10 @@ func TestAddPatchAlreadyThere(t *testing.T) {
|
|||||||
t.Fatalf("unexpected cmd error: %v", err)
|
t.Fatalf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// adding an existing patch should return an error
|
// adding an existing patch shouldn't return an error
|
||||||
err = cmd.RunE(cmd, args)
|
err = cmd.RunE(cmd, args)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
t.Errorf("expected already there problem")
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
}
|
|
||||||
if err.Error() != "patch "+patchFileName+" already in kustomization file" {
|
|
||||||
t.Errorf("unexpected error %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,20 +70,10 @@ func (o *addResourceOptions) Complete(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// RunAddResource runs addResource command (do real work).
|
// RunAddResource runs addResource command (do real work).
|
||||||
func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
|
func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
|
||||||
var resources []string
|
resources, err := globPatterns(fsys, o.resourceFilePaths)
|
||||||
|
if err != nil {
|
||||||
for _, pattern := range o.resourceFilePaths {
|
return err
|
||||||
files, err := fsys.Glob(pattern)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(files) == 0 {
|
|
||||||
log.Printf("%s has no match", pattern)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
resources = append(resources, files...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resources) == 0 {
|
if len(resources) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cMapFlagsAndArgs encapsulates the options for add configmap commands.
|
// cMapFlagsAndArgs encapsulates the options for add configmap commands.
|
||||||
@@ -48,3 +50,12 @@ func (a *cMapFlagsAndArgs) Validate(args []string) error {
|
|||||||
// TODO: Should we check if the path exists? if it's valid, if it's within the same (sub-)directory?
|
// TODO: Should we check if the path exists? if it's valid, if it's within the same (sub-)directory?
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *cMapFlagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error {
|
||||||
|
result, err := globPatterns(fSys, a.FileSources)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
a.FileSources = result
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,7 +17,10 @@ limitations under the License.
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDataConfigValidation_NoName(t *testing.T) {
|
func TestDataConfigValidation_NoName(t *testing.T) {
|
||||||
@@ -81,3 +84,21 @@ func TestDataConfigValidation_Flags(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExpandFileSource(t *testing.T) {
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
fakeFS.Create("dir/config1")
|
||||||
|
fakeFS.Create("dir/config2")
|
||||||
|
fakeFS.Create("dir/reademe")
|
||||||
|
config := cMapFlagsAndArgs{
|
||||||
|
FileSources: []string{"dir/config*"},
|
||||||
|
}
|
||||||
|
config.ExpandFileSource(fakeFS)
|
||||||
|
expected := []string{
|
||||||
|
"dir/config1",
|
||||||
|
"dir/config2",
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(config.FileSources, expected) {
|
||||||
|
t.Fatalf("FileSources is not correctly expanded: %v", config.FileSources)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -50,6 +50,11 @@ func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = flagsAndArgs.ExpandFileSource(fSys)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Load the kustomization file.
|
// Load the kustomization file.
|
||||||
mf, err := newKustomizationFile(constants.KustomizationFileName, fSys)
|
mf, err := newKustomizationFile(constants.KustomizationFileName, fSys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
39
pkg/commands/util.go
Normal file
39
pkg/commands/util.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2018 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 commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func globPatterns(fsys fs.FileSystem, patterns []string) ([]string, error) {
|
||||||
|
var result []string
|
||||||
|
for _, pattern := range patterns {
|
||||||
|
files, err := fsys.Glob(pattern)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(files) == 0 {
|
||||||
|
log.Printf("%s has no match", pattern)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, files...)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user