Skip to content

Commit

Permalink
Merge pull request #2940 from phanimarupaka/FixSettersSubPkgsFriction
Browse files Browse the repository at this point in the history
Fix setters subpkgs friction
  • Loading branch information
k8s-ci-robot committed Sep 3, 2020
2 parents 980f407 + 8d74b8c commit 0f49fef
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 19 deletions.
5 changes: 2 additions & 3 deletions cmd/config/internal/commands/cmdcreatesetter.go
Expand Up @@ -214,18 +214,17 @@ func (r *CreateSetterRunner) set(c *cobra.Command, args []string) error {
if err := openapi.AddSchemaFromFile(r.CreateSetter.OpenAPIPath); err != nil {
return err
}

err := r.CreateSetter.Create()
if err != nil {
// return err if there is only package
if len(resourcePackagesPaths) == 1 {
return err
} else {
// print error message and continue if there are multiple packages to set
fmt.Fprintf(c.OutOrStdout(), "%s in package %q\n", err.Error(), r.CreateSetter.ResourcesPath)
fmt.Fprintf(c.OutOrStdout(), "%s in package %q\n\n", err.Error(), r.CreateSetter.ResourcesPath)
}
} else {
fmt.Fprintf(c.OutOrStdout(), "created setter %q in package %q\n", r.CreateSetter.Name, r.CreateSetter.ResourcesPath)
fmt.Fprintf(c.OutOrStdout(), "created setter %q in package %q\n\n", r.CreateSetter.Name, r.CreateSetter.ResourcesPath)
}

// Delete schema present in openAPI file for current package
Expand Down
3 changes: 3 additions & 0 deletions cmd/config/internal/commands/cmdcreatesetter_test.go
Expand Up @@ -855,6 +855,7 @@ func TestCreateSetterSubPackages(t *testing.T) {
args: []string{"namespace", "myspace", "-R"},
expected: `
created setter "namespace" in package "${baseDir}/mysql"
created setter "namespace" in package "${baseDir}/mysql/storage"
`,
},
Expand All @@ -878,7 +879,9 @@ created setter "namespace" in package "${baseDir}/mysql/storage"
packagePath: "mysql",
args: []string{"namespace", "myspace", "-R"},
expected: `setter with name "namespace" already exists, if you want to modify it, please delete the existing setter and recreate it in package "${baseDir}/mysql"
created setter "namespace" in package "${baseDir}/mysql/nosetters"
setter with name "namespace" already exists, if you want to modify it, please delete the existing setter and recreate it in package "${baseDir}/mysql/storage"`,
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/config/internal/commands/cmdcreatesubstitution.go
Expand Up @@ -89,10 +89,10 @@ func (r *CreateSubstitutionRunner) runE(c *cobra.Command, args []string) error {
return err
} else {
// print error message and continue if there are multiple packages to set
fmt.Fprintf(c.OutOrStdout(), "%s in package %q\n", err.Error(), r.CreateSubstitution.ResourcesPath)
fmt.Fprintf(c.OutOrStdout(), "%s in package %q\n\n", err.Error(), r.CreateSubstitution.ResourcesPath)
}
} else {
fmt.Fprintf(c.OutOrStdout(), "created substitution %q in package %q\n", r.CreateSubstitution.Name, r.CreateSubstitution.ResourcesPath)
fmt.Fprintf(c.OutOrStdout(), "created substitution %q in package %q\n\n", r.CreateSubstitution.Name, r.CreateSubstitution.ResourcesPath)
}

// Delete schema present in openAPI file for current package
Expand Down
3 changes: 3 additions & 0 deletions cmd/config/internal/commands/cmdcreatesubstitution_test.go
Expand Up @@ -441,6 +441,7 @@ func TestCreateSubstSubPackages(t *testing.T) {
args: []string{"image-tag", "--field-value", "mysql:1.7.9", "--pattern", "${image}:${tag}", "-R"},
expected: `
created substitution "image-tag" in package "${baseDir}/mysql"
created substitution "image-tag" in package "${baseDir}/mysql/storage"
`,
},
Expand All @@ -464,7 +465,9 @@ created substitution "image-tag" in package "${baseDir}/mysql/storage"
packagePath: "mysql",
args: []string{"image-tag", "--field-value", "mysql:1.7.9", "--pattern", "${image}:${tag}", "-R"},
expected: `substitution with name "image-tag" already exists in package "${baseDir}/mysql"
created substitution "image-tag" in package "${baseDir}/mysql/nosetters"
created substitution "image-tag" in package "${baseDir}/mysql/storage"`,
},
}
Expand Down
17 changes: 10 additions & 7 deletions cmd/config/internal/commands/cmdlistsetters.go
Expand Up @@ -37,6 +37,8 @@ func NewListSettersRunner(parent string) *ListSettersRunner {
"output as github markdown")
c.Flags().BoolVar(&r.IncludeSubst, "include-subst", false,
"include substitutions in the output")
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", true,
"list setters recursively in all the nested subpackages")
fixDocs(parent, c)
r.Command = c
return r
Expand All @@ -47,11 +49,12 @@ func ListSettersCommand(parent string) *cobra.Command {
}

type ListSettersRunner struct {
Command *cobra.Command
Lookup setters.LookupSetters
List setters2.List
Markdown bool
IncludeSubst bool
Command *cobra.Command
Lookup setters.LookupSetters
List setters2.List
Markdown bool
IncludeSubst bool
RecurseSubPackages bool
}

func (r *ListSettersRunner) preRunE(c *cobra.Command, args []string) error {
Expand All @@ -71,7 +74,7 @@ func (r *ListSettersRunner) runE(c *cobra.Command, args []string) error {
return err
}

resourcePaths, err := pathutil.DirsWithFile(args[0], openAPIFileName, true)
resourcePaths, err := pathutil.DirsWithFile(args[0], openAPIFileName, r.RecurseSubPackages)
if err != nil {
return err
}
Expand All @@ -86,7 +89,7 @@ func (r *ListSettersRunner) runE(c *cobra.Command, args []string) error {
OpenAPIFileName: openAPIFileName,
}
openAPIPath := filepath.Join(resourcePath, openAPIFileName)
fmt.Fprintf(c.OutOrStdout(), "%s\n", resourcePath)
fmt.Fprintf(c.OutOrStdout(), "\n%s/\n", resourcePath)
if err := r.ListSetters(c, openAPIPath, resourcePath); err != nil {
return err
}
Expand Down
22 changes: 19 additions & 3 deletions cmd/config/internal/commands/cmdlistsetters_test.go
Expand Up @@ -474,19 +474,35 @@ func TestListSettersSubPackages(t *testing.T) {
dataset: "dataset-with-setters",
args: []string{"--include-subst"},
expected: `
test/testdata/dataset-with-setters/mysql
test/testdata/dataset-with-setters/mysql/
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
image mysql 1 No
namespace myspace 1 No
tag 1.7.9 1 No
--------------- ----------------- --------------
SUBSTITUTION PATTERN REFERENCES
image-tag ${image}:${tag} [image,tag]
test/testdata/dataset-with-setters/mysql/nosetters
test/testdata/dataset-with-setters/mysql/nosetters/
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
test/testdata/dataset-with-setters/mysql/storage
test/testdata/dataset-with-setters/mysql/storage/
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
namespace myspace 1 No
`,
},
{
name: "list-replicas",
dataset: "dataset-with-setters/mysql",
args: []string{"--recurse-subpackages=false"},
expected: `
test/testdata/dataset-with-setters/mysql/
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
image mysql 1 No
namespace myspace 1 No
tag 1.7.9 1 No
`,
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/internal/commands/e2e/list_setters_test.go
Expand Up @@ -34,7 +34,7 @@ openAPI:
`,
},
expectedStdOut: `
.
./
NAME VALUE SET BY DESCRIPTION COUNT REQUIRED
replicas 3 1 No
`,
Expand Down
2 changes: 1 addition & 1 deletion kyaml/setters2/settersutil/settercreator.go
Expand Up @@ -87,7 +87,7 @@ func (c SetterCreator) Create() error {
}.Execute()
if a.Count == 0 {
fmt.Printf("setter %q doesn't match any field in resource configs, "+
"but creating setter definition\n", c.Name)
"but creating setter definition in package %q\n", c.Name, c.ResourcesPath)
}
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions kyaml/setters2/settersutil/substitutioncreator.go
Expand Up @@ -138,7 +138,7 @@ func (c SubstitutionCreator) Create() error {
}

// Update the resources with the substitution reference
inout := &kio.LocalPackageReadWriter{PackagePath: c.ResourcesPath}
inout := &kio.LocalPackageReadWriter{PackagePath: c.ResourcesPath, PackageFileName: c.OpenAPIFileName}
err = kio.Pipeline{
Inputs: []kio.Reader{inout},
Filters: []kio.Filter{kio.FilterAll(a)},
Expand All @@ -147,7 +147,7 @@ func (c SubstitutionCreator) Create() error {

if a.Count == 0 {
fmt.Printf("substitution %s doesn't match any field value in resource configs, "+
"but creating substitution definition\n", c.Name)
"but creating substitution definition in package %q\n", c.Name, c.ResourcesPath)
}
return err
}
Expand Down

0 comments on commit 0f49fef

Please sign in to comment.