From 922c30a287ea16d8759df1a1a29d250c0c39d433 Mon Sep 17 00:00:00 2001 From: Rohith Jayawardene Date: Sat, 24 Jun 2023 09:59:39 +0100 Subject: [PATCH] [FEATURE] - Adding Convenience Aliases for Delete & Apply (#868) Adding some convenience aliases to kubectl apply and delete, just makes it easier not to switch between commands --- pkg/cmd/tnctl/apply/apply.go | 81 +++++++++++++++++++++++++++++++++ pkg/cmd/tnctl/delete/delete.go | 83 ++++++++++++++++++++++++++++++++++ pkg/cmd/tnctl/get/get.go | 5 -- pkg/cmd/tnctl/tnctl.go | 4 ++ 4 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 pkg/cmd/tnctl/apply/apply.go create mode 100644 pkg/cmd/tnctl/delete/delete.go diff --git a/pkg/cmd/tnctl/apply/apply.go b/pkg/cmd/tnctl/apply/apply.go new file mode 100644 index 000000000..c91ee3eb9 --- /dev/null +++ b/pkg/cmd/tnctl/apply/apply.go @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2023 Appvia Ltd + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package apply + +import ( + "fmt" + "os/exec" + + "github.com/spf13/cobra" + + "github.com/appvia/terranetes-controller/pkg/cmd" +) + +// Command are the options for the command +type Command struct { + cmd.Factory + // File is the path to the file to apply + File []string + // Namespace is the namespace to apply the resources to + Namespace string + // DryRun is the dry run strategy + DryRun string + // Force is the force flag + Force bool +} + +// NewCommand creates and returns a new command +func NewCommand(factory cmd.Factory) *cobra.Command { + o := &Command{Factory: factory} + + c := &cobra.Command{ + Use: "apply [OPTIONS] [-f PATH...]", + Short: "Used to apply from a file or stdin", + RunE: func(cmd *cobra.Command, args []string) error { + options := []string{"apply"} + + for _, file := range o.File { + options = append(options, "-f", file) + } + if o.Force { + options = append(options, "--force") + } + if o.Namespace != "" { + options = append(options, fmt.Sprintf("--namespace=%s", o.Namespace)) + } + if o.DryRun != "" { + options = append(options, "--dry-run "+o.DryRun) + } + + exe := exec.CommandContext(cmd.Context(), "kubectl", options...) + exe.Stdout = factory.GetStreams().Out + exe.Stderr = factory.GetStreams().ErrOut + exe.Stdin = factory.GetStreams().In + + return exe.Run() + }, + } + + flags := c.Flags() + flags.StringSliceVarP(&o.File, "file", "f", []string{}, "Path to file to apply") + flags.StringVarP(&o.Namespace, "namespace", "n", "", "Namespace the resources will applied to") + flags.StringVar(&o.DryRun, "dry-run", "", `Must be "none", "server", or "client". If client strategy, only print the object that would be sent, without sending it`) + flags.BoolVarP(&o.Force, "force", "", false, "If true, immediately remove resources from API and bypass graceful deletion") + + return c +} diff --git a/pkg/cmd/tnctl/delete/delete.go b/pkg/cmd/tnctl/delete/delete.go new file mode 100644 index 000000000..b4a44b6eb --- /dev/null +++ b/pkg/cmd/tnctl/delete/delete.go @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2023 Appvia Ltd + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package delete + +import ( + "fmt" + "os/exec" + + "github.com/spf13/cobra" + + "github.com/appvia/terranetes-controller/pkg/cmd" +) + +// Command are the options for the command +type Command struct { + cmd.Factory + // File is the file to apply + File []string + // Namespace is the namespace to apply to + Namespace string + // Force, immediately remove resources from API and bypass graceful deletion + Force bool + // Wait, wait for resources to be gone before returning. This waits for finalizers. + Wait bool +} + +// NewCommand creates and returns a new command +func NewCommand(factory cmd.Factory) *cobra.Command { + o := &Command{Factory: factory} + + c := &cobra.Command{ + Use: "delete [OPTIONS] [-f PATH|NAME...]", + Short: "Used to delete resource by file or resource name", + RunE: func(cmd *cobra.Command, args []string) error { + options := []string{"delete"} + + for _, file := range o.File { + options = append(options, "-f", file) + } + if o.Namespace != "" { + options = append(options, fmt.Sprintf("--namespace=%s", o.Namespace)) + } + if o.Force { + options = append(options, "--force") + } + if o.Wait { + options = append(options, "--wait") + } + + options = append(options, args...) + + exe := exec.CommandContext(cmd.Context(), "kubectl", options...) + exe.Stdout = factory.GetStreams().Out + exe.Stderr = factory.GetStreams().ErrOut + exe.Stdin = factory.GetStreams().In + + return exe.Run() + }, + } + + flags := c.Flags() + flags.BoolVarP(&o.Force, "force", "", false, "If true, immediately remove resources from API and bypass graceful deletion") + flags.BoolVarP(&o.Wait, "wait", "", true, "If true, wait for resources to be gone before returning. This waits for finalizers.") + flags.StringSliceVarP(&o.File, "file", "f", []string{}, "Path to file to apply") + flags.StringVarP(&o.Namespace, "namespace", "n", "", "Namespace to apply to") + + return c +} diff --git a/pkg/cmd/tnctl/get/get.go b/pkg/cmd/tnctl/get/get.go index b055e2425..1cf8ca229 100644 --- a/pkg/cmd/tnctl/get/get.go +++ b/pkg/cmd/tnctl/get/get.go @@ -26,11 +26,6 @@ import ( "github.com/appvia/terranetes-controller/pkg/cmd" ) -// Command are the options for the command -type Command struct { - cmd.Factory -} - // NewCommand creates and returns a new command func NewCommand(factory cmd.Factory) *cobra.Command { c := &cobra.Command{ diff --git a/pkg/cmd/tnctl/tnctl.go b/pkg/cmd/tnctl/tnctl.go index 7e96307f0..51b35cc8f 100644 --- a/pkg/cmd/tnctl/tnctl.go +++ b/pkg/cmd/tnctl/tnctl.go @@ -28,10 +28,12 @@ import ( "github.com/spf13/cobra" "github.com/appvia/terranetes-controller/pkg/cmd" + "github.com/appvia/terranetes-controller/pkg/cmd/tnctl/apply" "github.com/appvia/terranetes-controller/pkg/cmd/tnctl/approve" "github.com/appvia/terranetes-controller/pkg/cmd/tnctl/config" "github.com/appvia/terranetes-controller/pkg/cmd/tnctl/convert" "github.com/appvia/terranetes-controller/pkg/cmd/tnctl/create" + "github.com/appvia/terranetes-controller/pkg/cmd/tnctl/delete" "github.com/appvia/terranetes-controller/pkg/cmd/tnctl/describe" "github.com/appvia/terranetes-controller/pkg/cmd/tnctl/generate" "github.com/appvia/terranetes-controller/pkg/cmd/tnctl/get" @@ -76,7 +78,9 @@ func New(factory cmd.Factory) *cobra.Command { command.AddCommand( config.NewCommand(factory), approve.NewCommand(factory), + apply.NewCommand(factory), get.NewCommand(factory), + delete.NewCommand(factory), kubectl.NewCommand(factory), search.NewCommand(factory), convert.NewCommand(factory),