From a2afcdcd3ff0575feee72e968a521f8e3ccf93c4 Mon Sep 17 00:00:00 2001 From: Marco Streich Date: Mon, 27 May 2024 14:48:33 +0200 Subject: [PATCH] delete --- delete/delete.go | 2 +- delete/postgres.go | 32 +++++++++++++++++++++++++++ delete/postgres_test.go | 48 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 delete/postgres.go create mode 100644 delete/postgres_test.go diff --git a/delete/delete.go b/delete/delete.go index e402602..cd86e2b 100644 --- a/delete/delete.go +++ b/delete/delete.go @@ -20,9 +20,9 @@ type Cmd struct { Config configCmd `cmd:"" group:"deplo.io" name:"config" help:"Delete a deplo.io Project Configuration. (Beta - requires access)"` Application applicationCmd `cmd:"" group:"deplo.io" name:"application" aliases:"app" help:"Delete a deplo.io Application. (Beta - requires access)"` MySQL mySQLCmd `cmd:"" group:"storage.nine.ch" name:"mysql" help:"Delete a MySQL instance."` + Postgres postgresCmd `cmd:"" group:"storage.nine.ch" name:"postgres" help:"Delete a PostgreSQL instance."` KeyValueStore keyValueStoreCmd `cmd:"" group:"storage.nine.ch" name:"keyvaluestore" aliases:"kvs" help:"Delete a KeyValueStore instance."` CloudVirtualMachine cloudVMCmd `cmd:"" group:"infrastructure.nine.ch" name:"cloudvirtualmachine" aliases:"cloudvm" help:"Delete a CloudVM."` - //Postgres postgresCmd `cmd:"" group:"storage.nine.ch" name:"postgres" help:"Delete a PostgreSQL instance."` } // cleanupFunc is called after the resource has been deleted in order to do diff --git a/delete/postgres.go b/delete/postgres.go new file mode 100644 index 0000000..e7c3792 --- /dev/null +++ b/delete/postgres.go @@ -0,0 +1,32 @@ +package delete + +import ( + "context" + "fmt" + "time" + + "k8s.io/apimachinery/pkg/types" + + storage "github.com/ninech/apis/storage/v1alpha1" + "github.com/ninech/nctl/api" +) + +type postgresCmd struct { + Name string `arg:"" help:"Name of the PostgreSQL resource."` + Force bool `default:"false" help:"Do not ask for confirmation of deletion."` + Wait bool `default:"true" help:"Wait until PostgreSQL is fully deleted."` + WaitTimeout time.Duration `default:"300s" help:"Duration to wait for the deletion. Only relevant if wait is set."` +} + +func (cmd *postgresCmd) Run(ctx context.Context, client *api.Client) error { + ctx, cancel := context.WithTimeout(ctx, cmd.WaitTimeout) + defer cancel() + + postgres := &storage.Postgres{} + postgresName := types.NamespacedName{Name: cmd.Name, Namespace: client.Project} + if err := client.Get(ctx, postgresName, postgres); err != nil { + return fmt.Errorf("unable to get postgres %q: %w", postgres.Name, err) + } + + return newDeleter(postgres, storage.PostgresKind).deleteResource(ctx, client, cmd.WaitTimeout, cmd.Wait, cmd.Force) +} diff --git a/delete/postgres_test.go b/delete/postgres_test.go new file mode 100644 index 0000000..0039461 --- /dev/null +++ b/delete/postgres_test.go @@ -0,0 +1,48 @@ +package delete + +import ( + "context" + "testing" + "time" + + "github.com/ninech/nctl/api" + "github.com/ninech/nctl/internal/test" + "k8s.io/apimachinery/pkg/api/errors" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +func TestPostgres(t *testing.T) { + cmd := postgresCmd{ + Name: "test", + Force: true, + Wait: false, + WaitTimeout: time.Second, + } + + postgres := test.Postgres("test", "default", "nine-es34") + + scheme, err := api.NewScheme() + if err != nil { + t.Fatal(err) + } + client := fake.NewClientBuilder().WithScheme(scheme).Build() + apiClient := &api.Client{WithWatch: client, Project: "default"} + ctx := context.Background() + + if err := apiClient.Create(ctx, postgres); err != nil { + t.Fatalf("postgres create error, got: %s", err) + } + if err := apiClient.Get(ctx, api.ObjectName(postgres), postgres); err != nil { + t.Fatalf("expected postgres to exist, got: %s", err) + } + if err := cmd.Run(ctx, apiClient); err != nil { + t.Fatal(err) + } + err = apiClient.Get(ctx, api.ObjectName(postgres), postgres) + if err == nil { + t.Fatalf("expected postgres to be deleted, but exists") + } + if !errors.IsNotFound(err) { + t.Fatalf("expected postgres to be deleted, got: %s", err.Error()) + } +}