Merge pull request #3582 from alexal/fix/3575

Kustomize file should be ignored when adding resources and components
This commit is contained in:
Kubernetes Prow Robot
2021-02-11 16:28:47 -08:00
committed by GitHub
6 changed files with 75 additions and 8 deletions

View File

@@ -68,11 +68,13 @@ func (o *addComponentOptions) RunAddComponent(fSys filesys.FileSystem) error {
}
for _, component := range components {
if kustfile.StringInSlice(component, m.Components) {
log.Printf("component %s already in kustomization file", component)
continue
if mf.GetPath() != component {
if kustfile.StringInSlice(component, m.Components) {
log.Printf("component %s already in kustomization file", component)
continue
}
m.Components = append(m.Components, component)
}
m.Components = append(m.Components, component)
}
return mf.Write(m)

View File

@@ -62,6 +62,28 @@ func TestAddComponentAlreadyThere(t *testing.T) {
}
}
func TestAddKustomizationFileAsComponent(t *testing.T) {
fSys := filesys.MakeFsInMemory()
fSys.WriteFile(componentFileName, []byte(componentFileContent))
testutils_test.WriteTestKustomization(fSys)
cmd := newCmdAddComponent(fSys)
args := []string{componentFileName}
err := cmd.RunE(cmd, args)
if err != nil {
t.Fatalf("unexpected cmd error: %v", err)
}
content, err := testutils_test.ReadTestKustomization(fSys)
if err != nil {
t.Errorf("unexpected read error: %v", err)
}
if strings.Contains(string(content), componentFileName) {
t.Errorf("%v shouldn't be in the list of the components", componentFileName)
}
}
func TestAddComponentNoArgs(t *testing.T) {
fSys := filesys.MakeFsInMemory()

View File

@@ -68,11 +68,13 @@ func (o *addResourceOptions) RunAddResource(fSys filesys.FileSystem) error {
}
for _, resource := range resources {
if kustfile.StringInSlice(resource, m.Resources) {
log.Printf("resource %s already in kustomization file", resource)
continue
if mf.GetPath() != resource {
if kustfile.StringInSlice(resource, m.Resources) {
log.Printf("resource %s already in kustomization file", resource)
continue
}
m.Resources = append(m.Resources, resource)
}
m.Resources = append(m.Resources, resource)
}
return mf.Write(m)

View File

@@ -62,6 +62,28 @@ func TestAddResourceAlreadyThere(t *testing.T) {
}
}
func TestAddKustomizationFileAsResource(t *testing.T) {
fSys := filesys.MakeFsInMemory()
fSys.WriteFile(resourceFileName, []byte(resourceFileContent))
testutils_test.WriteTestKustomization(fSys)
cmd := newCmdAddResource(fSys)
args := []string{resourceFileName}
err := cmd.RunE(cmd, args)
if err != nil {
t.Fatalf("unexpected cmd error: %v", err)
}
content, err := testutils_test.ReadTestKustomization(fSys)
if err != nil {
t.Errorf("unexpected read error: %v", err)
}
if strings.Contains(string(content), resourceFileName) {
t.Errorf("%v shouldn't be in the list of the resources", resourceFileName)
}
}
func TestAddResourceNoArgs(t *testing.T) {
fSys := filesys.MakeFsInMemory()

View File

@@ -117,6 +117,13 @@ func NewKustomizationFile(fSys filesys.FileSystem) (*kustomizationFile, error) {
return mf, nil
}
func (mf *kustomizationFile) GetPath() string {
if mf == nil {
return ""
}
return mf.path
}
func (mf *kustomizationFile) validate() error {
match := 0
var path []string

View File

@@ -80,6 +80,18 @@ func TestWriteAndRead(t *testing.T) {
}
}
func TestGetPath(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()
testutils_test.WriteTestKustomization(fSys)
mf, err := NewKustomizationFile(fSys)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
if mf.GetPath() != "kustomization.yaml" {
t.Fatalf("Path expected: kustomization.yaml. Actual path: %v", mf.GetPath())
}
}
func TestNewNotExist(t *testing.T) {
fSys := filesys.MakeFsInMemory()
_, err := NewKustomizationFile(fSys)