Skip to content

Commit

Permalink
add support for nonkube site delete --all options
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnemorrison committed Dec 17, 2024
1 parent d9d37ea commit 13f1de6
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/skupper/site/nonkube/site_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestNonKubeCmdSiteCreate_ValidateInput(t *testing.T) {
{
name: "kubernetes flags are not valid on this platform",
args: []string{"my-site"},
flags: &common.CommandSiteCreateFlags{BindHost: "bindhost"},
flags: &common.CommandSiteCreateFlags{ServiceAccount: "not-supported"},
expectedErrors: []string{},
cobraGenericFlags: map[string]string{
common.FlagNameContext: "test",
Expand Down
28 changes: 28 additions & 0 deletions internal/cmd/skupper/site/nonkube/site_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import (
type CmdSiteDelete struct {
siteHandler *fs.SiteHandler
routerAccessHandler *fs.RouterAccessHandler
listenerHandler *fs.ListenerHandler
connectorHandler *fs.ConnectorHandler
CobraCmd *cobra.Command
namespace string
siteName string
Flags *common.CommandSiteDeleteFlags
all bool
}

func NewCmdSiteDelete() *CmdSiteDelete {
Expand All @@ -29,6 +32,8 @@ func (cmd *CmdSiteDelete) NewClient(cobraCommand *cobra.Command, args []string)

cmd.siteHandler = fs.NewSiteHandler(cmd.namespace)
cmd.routerAccessHandler = fs.NewRouterAccessHandler(cmd.namespace)
cmd.listenerHandler = fs.NewListenerHandler(cmd.namespace)
cmd.connectorHandler = fs.NewConnectorHandler(cmd.namespace)
}

func (cmd *CmdSiteDelete) ValidateInput(args []string) []error {
Expand Down Expand Up @@ -74,6 +79,7 @@ func (cmd *CmdSiteDelete) InputToOptions() {
if cmd.namespace == "" {
cmd.namespace = "default"
}
cmd.all = cmd.Flags.All
}

func (cmd *CmdSiteDelete) Run() error {
Expand All @@ -85,6 +91,28 @@ func (cmd *CmdSiteDelete) Run() error {
if err != nil {
return err
}
// if delete all, also remove all the other resources
if cmd.all {
listeners, err := cmd.listenerHandler.List()
if listeners != nil && err == nil {
for _, listener := range listeners {
fmt.Println("delete listener", listener.Name)
err = cmd.listenerHandler.Delete(listener.Name)
if err != nil {
return err
}
}
}
connectors, err := cmd.connectorHandler.List()
if connectors != nil && err == nil {
for _, connector := range connectors {
err = cmd.connectorHandler.Delete(connector.Name)
if err != nil {
return err
}
}
}
}
return nil
}

Expand Down
112 changes: 97 additions & 15 deletions internal/cmd/skupper/site/nonkube/site_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,38 +129,120 @@ func TestCmdSiteDelete_Run(t *testing.T) {
skupperObjects []runtime.Object
skupperErrorMessage string
errorMessage string
expectedNamespace string
all bool
}

testTable := []test{
{
name: "run fails default",
deleteName: "my-site",
errorMessage: "error",
name: "run default",
deleteName: "no-site",
errorMessage: "error",
expectedNamespace: "default",
all: false,
},
{
name: "run fails",
namespace: "test",
deleteName: "my-site",
errorMessage: "error",
name: "run delete all",
namespace: "test",
deleteName: "my-site",
errorMessage: "error",
expectedNamespace: "test",
all: true,
},
}

// Add a temp file so listener/connector/site exists for delete tests
listenerResource := v2alpha1.Listener{
TypeMeta: metav1.TypeMeta{
APIVersion: "skupper.io/v2alpha1",
Kind: "Listener",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-listener",
Namespace: "test",
},
}
connectorResource := v2alpha1.Connector{
TypeMeta: metav1.TypeMeta{
APIVersion: "skupper.io/v2alpha1",
Kind: "Connector",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-connector",
Namespace: "test",
},
}
siteResource := v2alpha1.Site{
TypeMeta: metav1.TypeMeta{
APIVersion: "skupper.io/v2alpha1",
Kind: "Site",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-site",
Namespace: "test",
},
}

homeDir, err := os.UserHomeDir()
assert.Check(t, err == nil)
path := filepath.Join(homeDir, "/.local/share/skupper/namespaces/test/", string(api.RuntimeSiteStatePath))

command := &CmdSiteDelete{Flags: &common.CommandSiteDeleteFlags{}}
command.namespace = "test"
command.siteHandler = fs.NewSiteHandler(command.namespace)
command.listenerHandler = fs.NewListenerHandler(command.namespace)
command.connectorHandler = fs.NewConnectorHandler(command.namespace)
command.routerAccessHandler = fs.NewRouterAccessHandler(command.namespace)

content, err := command.siteHandler.EncodeToYaml(siteResource)
assert.Check(t, err == nil)
err = command.siteHandler.WriteFile(path, "my-site.yaml", content, common.Sites)
assert.Check(t, err == nil)
defer command.siteHandler.Delete("my-site")

content, err = command.listenerHandler.EncodeToYaml(listenerResource)
assert.Check(t, err == nil)
err = command.listenerHandler.WriteFile(path, "my-listener.yaml", content, common.Listeners)
assert.Check(t, err == nil)
defer command.listenerHandler.Delete("my-listener")

content, err = command.connectorHandler.EncodeToYaml(connectorResource)
assert.Check(t, err == nil)
err = command.connectorHandler.WriteFile(path, "my-connector.yaml", content, common.Connectors)
assert.Check(t, err == nil)
defer command.connectorHandler.Delete("my-connector")

for _, test := range testTable {
cmd := &CmdSiteDelete{}

t.Run(test.name, func(t *testing.T) {
command.namespace = test.namespace
command.siteName = test.deleteName
command.Flags.All = test.all
command.InputToOptions()

cmd.siteName = test.deleteName
cmd.namespace = test.namespace
cmd.siteHandler = fs.NewSiteHandler(cmd.namespace)
cmd.routerAccessHandler = fs.NewRouterAccessHandler(cmd.namespace)
cmd.InputToOptions()

err := cmd.Run()
err := command.Run()
if err != nil {
assert.Check(t, test.errorMessage == err.Error())
} else {
assert.Check(t, err == nil)
assert.Equal(t, command.namespace, test.expectedNamespace)
if test.all {
// only deleting from input/resources directory
// expect all resources are deleted
opts := fs.GetOptions{RuntimeFirst: false, LogWarning: false}
site, _ := command.siteHandler.Get(command.siteName, opts)
assert.Check(t, site == nil)
listeners, _ := command.listenerHandler.List()
for _, listener := range listeners {
resource, _ := command.listenerHandler.Get(listener.Name, opts)
assert.Check(t, resource == nil)
}
connectors, _ := command.connectorHandler.List()
for _, connector := range connectors {
resource, _ := command.connectorHandler.Get(connector.Name, opts)
assert.Check(t, resource == nil)
}
}
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/skupper/site/nonkube/site_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestCmdSiteUpdate_ValidateInput(t *testing.T) {
{
name: "kubernetes flags are not valid on this platform",
args: []string{"my-site"},
flags: &common.CommandSiteUpdateFlags{},
flags: &common.CommandSiteUpdateFlags{ServiceAccount: "not-supported"},
cobraGenericFlags: map[string]string{
common.FlagNameContext: "test",
common.FlagNameKubeconfig: "test",
Expand Down

0 comments on commit 13f1de6

Please sign in to comment.