Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#2855 from phanimarupaka/DeleteSubst
Browse files Browse the repository at this point in the history
Delete substitution and fix delete setters
  • Loading branch information
k8s-ci-robot authored Aug 19, 2020
2 parents 0ff4e53 + ca04c87 commit 596c39b
Show file tree
Hide file tree
Showing 12 changed files with 799 additions and 169 deletions.
1 change: 1 addition & 0 deletions cmd/config/configcobra/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
CreateSetter = commands.CreateSetterCommand
CreateSubstitution = commands.CreateSubstitutionCommand
DeleteSetter = commands.DeleteSetterCommand
DeleteSubstitution = commands.DeleteSubstitutionCommand
Fmt = commands.FmtCommand
Grep = commands.GrepCommand
Init = commands.InitCommand
Expand Down
4 changes: 3 additions & 1 deletion cmd/config/internal/commands/cmddeletesetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/cmd/config/ext"
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/setters2/settersutil"
)
Expand All @@ -16,7 +17,7 @@ func NewDeleteSetterRunner(parent string) *DeleteSetterRunner {
r := &DeleteSetterRunner{}
c := &cobra.Command{
Use: "delete-setter DIR NAME",
Args: cobra.MinimumNArgs(2),
Args: cobra.ExactArgs(2),
Short: commands.DeleteSetterShort,
Long: commands.DeleteSetterLong,
Example: commands.DeleteSetterExamples,
Expand All @@ -42,6 +43,7 @@ type DeleteSetterRunner struct {
func (r *DeleteSetterRunner) preRunE(c *cobra.Command, args []string) error {
var err error
r.DeleteSetter.Name = args[1]
r.DeleteSetter.DefinitionPrefix = fieldmeta.SetterDefinitionPrefix

r.OpenAPIFile, err = ext.GetOpenAPIFile(args)
if err != nil {
Expand Down
107 changes: 87 additions & 20 deletions cmd/config/internal/commands/cmddeletesetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ func TestDeleteSetterCommand(t *testing.T) {
}{
{
name: "delete replicas",
args: []string{"replicas", "hello world"},
args: []string{"replicas-setter"},
input: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3 # {"$openapi" : "replicas"}}
replicas: 3 # {"$openapi" : "replicas-setter"}
`,
inputOpenAPI: `
apiVersion: v1alpha1
kind: Example
openAPI:
definitions:
io.k8s.cli.setters.replicas:
io.k8s.cli.setters.replicas-setter:
description: hello world
x-k8s-cli:
setter:
name: replicas
name: replicas-setter
value: "3"
setBy: me
`,
Expand All @@ -67,32 +67,33 @@ spec:
},
{
name: "delete only one setter",
args: []string{"replicas", "hello world"},
args: []string{"replicas-setter"},
input: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3 # {"$openapi" : "replicas"}}
replicas: 3 # {"$openapi" : "replicas-setter"}
foo: nginx # {"$openapi" : "image"}
`,
inputOpenAPI: `
apiVersion: v1alpha1
kind: Example
openAPI:
definitions:
io.k8s.cli.setters.replicas:
io.k8s.cli.setters.replicas-setter:
description: hello world
x-k8s-cli:
setter:
name: replicas
name: replicas-setter
value: "3"
setBy: me
io.k8s.cli.setters.image:
x-k8s-cli:
setter:
name: image
value: 1.0
value: nginx
`,
expectedOpenAPI: `
apiVersion: v1alpha1
Expand All @@ -103,7 +104,7 @@ openAPI:
x-k8s-cli:
setter:
name: image
value: 1.0
value: nginx
`,
expectedResources: `
apiVersion: apps/v1
Expand All @@ -112,29 +113,95 @@ metadata:
name: nginx-deployment
spec:
replicas: 3
foo: nginx # {"$openapi" : "image"}
`,
},

{
name: "delete array setter",
args: []string{"list"},
inputOpenAPI: `
apiVersion: v1alpha1
kind: Example
openAPI:
definitions:
io.k8s.cli.setters.list:
items:
type: string
maxItems: 3
type: array
description: hello world
x-k8s-cli:
setter:
name: list
value: ""
listValues:
- a
- b
- c
setBy: me
`,
input: `
apiVersion: example.com/v1beta1
kind: Example1
spec:
list: # {"$openapi":"list"}
- "a"
- "b"
- "c"
---
apiVersion: example.com/v1beta1
kind: Example2
spec:
list: # {"$openapi":"list2"}
- "a"
- "b"
- "c"
`,
expectedResources: `
apiVersion: example.com/v1beta1
kind: Example1
spec:
list:
- "a"
- "b"
- "c"
---
apiVersion: example.com/v1beta1
kind: Example2
spec:
list: # {"$openapi":"list2"}
- "a"
- "b"
- "c"
`,
expectedOpenAPI: `
apiVersion: v1alpha1
kind: Example
`,
},

{
name: "delete non exist setter error",
args: []string{"image", "hello world"},
args: []string{"image"},
input: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3 # {"$openapi" : "replicas"}}
replicas: 3 # {"$openapi" : "replicas-setter"}
`,
inputOpenAPI: `
apiVersion: v1alpha1
kind: Example
openAPI:
definitions:
io.k8s.cli.setters.replicas:
io.k8s.cli.setters.replicas-setter:
description: hello world
x-k8s-cli:
setter:
name: replicas
name: replicas-setter
value: "3"
setBy: me
`,
Expand All @@ -143,11 +210,11 @@ apiVersion: v1alpha1
kind: Example
openAPI:
definitions:
io.k8s.cli.setters.replicas:
io.k8s.cli.setters.replicas-setter:
description: hello world
x-k8s-cli:
setter:
name: replicas
name: replicas-setter
value: "3"
setBy: me
`,
Expand All @@ -157,13 +224,13 @@ kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3 # {"$openapi" : "replicas"}}
replicas: 3 # {"$openapi" : "replicas-setter"}
`,
err: `setter does not exist`,
err: `setter with name image does not exist`,
},
{
name: "delete setter used in substitution error",
args: []string{"image-name", "hello world"},
args: []string{"image-name"},
input: `
apiVersion: apps/v1
kind: Deployment
Expand Down Expand Up @@ -220,7 +287,7 @@ openAPI:
apiVersion: apps/v1
kind: Deployment
`,
err: `setter is used in substitution image, please delete the substitution first`,
err: `setter is used in substitution image, please delete the parent substitution first`,
},
}
for i := range tests {
Expand Down
62 changes: 62 additions & 0 deletions cmd/config/internal/commands/cmddeletesubstitution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package commands

import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/cmd/config/ext"
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/setters2/settersutil"
)

// NewDeleteRunner returns a command runner.
func NewDeleteSubstitutionRunner(parent string) *DeleteSubstitutionRunner {
r := &DeleteSubstitutionRunner{}
c := &cobra.Command{
Use: "delete-subst DIR NAME",
Args: cobra.ExactArgs(2),
PreRunE: r.preRunE,
RunE: r.runE,
}
fixDocs(parent, c)
r.Command = c

return r
}

func DeleteSubstitutionCommand(parent string) *cobra.Command {
return NewDeleteSubstitutionRunner(parent).Command
}

type DeleteSubstitutionRunner struct {
Command *cobra.Command
DeleteSubstitution settersutil.DeleterCreator
OpenAPIFile string
}

func (r *DeleteSubstitutionRunner) preRunE(c *cobra.Command, args []string) error {
var err error
r.DeleteSubstitution.Name = args[1]
r.DeleteSubstitution.DefinitionPrefix = fieldmeta.SubstitutionDefinitionPrefix

r.OpenAPIFile, err = ext.GetOpenAPIFile(args)
if err != nil {
return err
}

if err := openapi.AddSchemaFromFile(r.OpenAPIFile); err != nil {
return err
}

return nil
}

func (r *DeleteSubstitutionRunner) runE(c *cobra.Command, args []string) error {
return handleError(c, r.delete(c, args))
}

func (r *DeleteSubstitutionRunner) delete(c *cobra.Command, args []string) error {
return r.DeleteSubstitution.Delete(r.OpenAPIFile, args[0])
}
Loading

0 comments on commit 596c39b

Please sign in to comment.