From 02507f744fd9c88e935972775fbcc4bf67fdcede Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Mon, 26 Feb 2024 19:44:27 +1100 Subject: [PATCH] feat: support cancelling restores --- controller-test.sh | 46 +++++++- internal/helpers/helper_types.go | 32 ++++++ internal/messenger/consumer.go | 46 +++++++- internal/messenger/tasks_restore.go | 164 +++++++++++++++++++++------- test-resources/results/k8upv1.yaml | 2 +- 5 files changed, 243 insertions(+), 47 deletions(-) diff --git a/controller-test.sh b/controller-test.sh index c51aa2f1..acdfd256 100755 --- a/controller-test.sh +++ b/controller-test.sh @@ -458,7 +458,7 @@ echo ' {"properties":{"delivery_mode":2},"routing_key":"ci-local-controller-kubernetes:misc", "payload":"{ \"misc\":{ - \"miscResource\":\"eyJtZXRhZGF0YSI6eyJuYW1lIjoicmVzdG9yZS1iZjA3MmEwLXVxeHFvNCJ9LCJzcGVjIjp7InNuYXBzaG90IjoiYmYwNzJhMDllMTc3MjZkYTU0YWRjNzk5MzZlYzg3NDU1MjE5OTM1OTlkNDEyMTFkZmM5NDY2ZGZkNWJjMzJhNSIsInJlc3RvcmVNZXRob2QiOnsiczMiOnt9fSwiYmFja2VuZCI6eyJzMyI6eyJidWNrZXQiOiJiYWFzLW5naW54LWV4YW1wbGUifSwicmVwb1Bhc3N3b3JkU2VjcmV0UmVmIjp7ImtleSI6InJlcG8tcHciLCJuYW1lIjoiYmFhcy1yZXBvLXB3In19fX0=\" + \"miscResource\":\"eyJtZXRhZGF0YSI6eyJuYW1lIjoicmVzdG9yZS1iZjA3MmEwOWUxNzcyNmRhNTRhZGM3OTkzNmVjODc0NTUyMTk5MzU5OWQ0MTIxMWRmYzk0NjZkZmQ1YmMzMmE1In0sInNwZWMiOnsic25hcHNob3QiOiJiZjA3MmEwOWUxNzcyNmRhNTRhZGM3OTkzNmVjODc0NTUyMTk5MzU5OWQ0MTIxMWRmYzk0NjZkZmQ1YmMzMmE1IiwicmVzdG9yZU1ldGhvZCI6eyJzMyI6e319LCJiYWNrZW5kIjp7InMzIjp7ImJ1Y2tldCI6ImJhYXMtbmdpbngtZXhhbXBsZSJ9LCJyZXBvUGFzc3dvcmRTZWNyZXRSZWYiOnsia2V5IjoicmVwby1wdyIsIm5hbWUiOiJiYWFzLXJlcG8tcHcifX19fQ==\" }, \"key\":\"deploytarget:restic:backup:restore\", \"environment\":{ @@ -477,7 +477,7 @@ echo "" sleep 10 CHECK_COUNTER=1 kubectl -n nginx-example-main get restores.k8up.io -until $(kubectl -n nginx-example-main get restores.k8up.io restore-bf072a0-uqxqo4 &> /dev/null) +until $(kubectl -n nginx-example-main get restores.k8up.io restore-bf072a09e17726da54adc79936ec8745521993599d41211dfc9466dfd5bc32a5 &> /dev/null) do if [ $CHECK_COUNTER -lt 14 ]; then let CHECK_COUNTER=CHECK_COUNTER+1 @@ -492,7 +492,7 @@ else exit 1 fi done -kubectl -n nginx-example-main get restores.k8up.io restore-bf072a0-uqxqo4 -o yaml | kubectl-neat > test-resources/results/k8upv1-cluster.yaml +kubectl -n nginx-example-main get restores.k8up.io restore-bf072a09e17726da54adc79936ec8745521993599d41211dfc9466dfd5bc32a5 -o yaml | kubectl-neat > test-resources/results/k8upv1-cluster.yaml if cmp --silent -- "test-resources/results/k8upv1.yaml" "test-resources/results/k8upv1-cluster.yaml"; then echo "Resulting restores match" else @@ -509,6 +509,46 @@ else exit 1 fi +# test that a cancellation works when a cancellation message is received from rabbitmq, and that the restore resource is deleted from the namespace +echo "==> Trigger a lagoon restore cancellation using rabbitmq" +echo ' +{"properties":{"delivery_mode":2},"routing_key":"ci-local-controller-kubernetes:misc", + "payload":"{ + \"misc\":{ + \"miscResource\":\"eyJyZXN0b3JlTmFtZSI6InJlc3RvcmUtYmYwNzJhMDllMTc3MjZkYTU0YWRjNzk5MzZlYzg3NDU1MjE5OTM1OTlkNDEyMTFkZmM5NDY2ZGZkNWJjMzJhNSIsImJhY2t1cElkIjoiYmYwNzJhMDllMTc3MjZkYTU0YWRjNzk5MzZlYzg3NDU1MjE5OTM1OTlkNDEyMTFkZmM5NDY2ZGZkNWJjMzJhNSJ9\" + }, + \"key\":\"deploytarget:restic:cancel:restore\", + \"environment\":{ + \"name\":\"main\", + \"openshiftProjectName\":\"nginx-example-main\" + }, + \"project\":{ + \"name\":\"nginx-example\" + }, + \"advancedTask\":{} + }", +"payload_encoding":"string" +}' >payload.json +curl -s -u guest:guest -H "Accept: application/json" -H "Content-Type:application/json" -X POST -d @payload.json http://172.17.0.1:15672/api/exchanges/%2f/lagoon-tasks/publish +echo "" +sleep 10 +# check that the restore resource gets removed +until ! $(kubectl -n nginx-example-main get restores.k8up.io restore-bf072a09e17726da54adc79936ec8745521993599d41211dfc9466dfd5bc32a5 &> /dev/null) +do +if [ $CHECK_COUNTER -lt 14 ]; then + let CHECK_COUNTER=CHECK_COUNTER+1 + echo "Restore not deleted yet" + sleep 5 +else + echo "Timeout of 70seconds for restore to be deleted" + check_controller_log + tear_down + echo "================ END ================" + echo "============== FAILED ===============" + exit 1 +fi +done + echo "==> Delete the environment" echo ' {"properties":{"delivery_mode":2},"routing_key":"ci-local-controller-kubernetes:remove", diff --git a/internal/helpers/helper_types.go b/internal/helpers/helper_types.go index 61ec1fce..a9f16f68 100644 --- a/internal/helpers/helper_types.go +++ b/internal/helpers/helper_types.go @@ -1,5 +1,13 @@ package helpers +import ( + "context" + + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" +) + // LagoonEnvironmentVariable is used to define Lagoon environment variables. type LagoonEnvironmentVariable struct { Name string `json:"name"` @@ -31,3 +39,27 @@ type LagoonAPIConfiguration struct { SSHHost string SSHPort string } + +func K8UPVersions(ctx context.Context, c client.Client) (bool, bool, error) { + k8upv1alpha1Exists := false + k8upv1Exists := false + crdv1alpha1 := &apiextensionsv1.CustomResourceDefinition{} + if err := c.Get(context.TODO(), types.NamespacedName{Name: "restores.backup.appuio.ch"}, crdv1alpha1); err != nil { + if err := IgnoreNotFound(err); err != nil { + return k8upv1alpha1Exists, k8upv1Exists, err + } + } + if crdv1alpha1.ObjectMeta.Name == "restores.backup.appuio.ch" { + k8upv1alpha1Exists = true + } + crdv1 := &apiextensionsv1.CustomResourceDefinition{} + if err := c.Get(context.TODO(), types.NamespacedName{Name: "restores.k8up.io"}, crdv1); err != nil { + if err := IgnoreNotFound(err); err != nil { + return k8upv1alpha1Exists, k8upv1Exists, err + } + } + if crdv1.ObjectMeta.Name == "restores.k8up.io" { + k8upv1Exists = true + } + return k8upv1alpha1Exists, k8upv1Exists, nil +} diff --git a/internal/messenger/consumer.go b/internal/messenger/consumer.go index 28f3acac..2f792422 100644 --- a/internal/messenger/consumer.go +++ b/internal/messenger/consumer.go @@ -405,6 +405,17 @@ func (m *Messenger) Consumer(targetName string) { //error { } } case "deploytarget:restic:backup:restore", "kubernetes:restic:backup:restore": + v1alpha1, v1, err := helpers.K8UPVersions(ctx, m.Client) + if err != nil { + //@TODO: send msg back to lagoon and update task to failed? + message.Ack(false) // ack to remove from queue + return + } + if !v1alpha1 && !v1 { + // k8up not installed + message.Ack(false) // ack to remove from queue + return + } opLog.Info( fmt.Sprintf( "Received backup restoration for project %s, environment %s", @@ -412,7 +423,7 @@ func (m *Messenger) Consumer(targetName string) { //error { jobSpec.Environment.Name, ), ) - err := m.ResticRestore(namespace, jobSpec) + err = m.ResticRestore(ctx, namespace, jobSpec, v1alpha1, v1, false) if err != nil { opLog.Error(err, fmt.Sprintf( @@ -425,6 +436,39 @@ func (m *Messenger) Consumer(targetName string) { //error { message.Ack(false) // ack to remove from queue return } + case "deploytarget:restic:cancel:restore": + v1alpha1, v1, err := helpers.K8UPVersions(ctx, m.Client) + if err != nil { + //@TODO: send msg back to lagoon and update task to failed? + message.Ack(false) // ack to remove from queue + return + } + if !v1alpha1 && !v1 { + // k8up not installed + message.Ack(false) // ack to remove from queue + return + } + // if this is a request to cancel a restore attempt + opLog.Info( + fmt.Sprintf( + "Received restore cancellation for project %s, environment %s", + jobSpec.Project.Name, + jobSpec.Environment.Name, + ), + ) + err = m.ResticRestore(ctx, namespace, jobSpec, v1alpha1, v1, true) + if err != nil { + opLog.Error(err, + fmt.Sprintf( + "Cancel restore for project %s, environment %s failed", + jobSpec.Project.Name, + jobSpec.Environment.Name, + ), + ) + //@TODO: send msg back to lagoon and update task to failed? + message.Ack(false) // ack to remove from queue + return + } case "deploytarget:route:migrate", "kubernetes:route:migrate", "openshift:route:migrate": opLog.Info( fmt.Sprintf( diff --git a/internal/messenger/tasks_restore.go b/internal/messenger/tasks_restore.go index a380c4a1..fcf397db 100644 --- a/internal/messenger/tasks_restore.go +++ b/internal/messenger/tasks_restore.go @@ -6,18 +6,24 @@ import ( "fmt" "github.com/go-logr/logr" + "github.com/uselagoon/machinery/api/schema" lagoonv1beta2 "github.com/uselagoon/remote-controller/apis/lagoon/v1beta2" "github.com/uselagoon/remote-controller/internal/helpers" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" k8upv1 "github.com/k8up-io/k8up/v2/api/v1" k8upv1alpha1 "github.com/vshn/k8up/api/v1alpha1" - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/types" ) +type cancelRestore struct { + RestoreName string `json:"restoreName"` + BackupID string `json:"backupId"` +} + // ResticRestore handles creating the restic restore jobs. -func (m *Messenger) ResticRestore(namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error { +func (m *Messenger) ResticRestore(ctx context.Context, namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec, v1alpha1, v1, cancel bool) error { opLog := ctrl.Log.WithName("handlers").WithName("LagoonTasks") vers, err := checkRestoreVersionFromCore(jobSpec.Misc.MiscResource) if err != nil { @@ -31,51 +37,41 @@ func (m *Messenger) ResticRestore(namespace string, jobSpec *lagoonv1beta2.Lagoo return nil } - // check if k8up crds exist in the cluster - k8upv1alpha1Exists := false - k8upv1Exists := false - crdv1alpha1 := &apiextensionsv1.CustomResourceDefinition{} - if err = m.Client.Get(context.TODO(), types.NamespacedName{Name: "restores.backup.appuio.ch"}, crdv1alpha1); err != nil { - if err := helpers.IgnoreNotFound(err); err != nil { - return err - } - } - if crdv1alpha1.ObjectMeta.Name == "restores.backup.appuio.ch" { - k8upv1alpha1Exists = true - } - crdv1 := &apiextensionsv1.CustomResourceDefinition{} - if err = m.Client.Get(context.TODO(), types.NamespacedName{Name: "restores.k8up.io"}, crdv1); err != nil { - if err := helpers.IgnoreNotFound(err); err != nil { - return err - } - } - if crdv1.ObjectMeta.Name == "restores.k8up.io" { - k8upv1Exists = true - } + handlev1alpha1 := false + handlev1 := false // check the version, if there is no version in the payload, assume it is k8up v2 if m.SupportK8upV2 { if vers == "backup.appuio.ch/v1alpha1" { - if k8upv1alpha1Exists { - return m.createv1alpha1Restore(opLog, namespace, jobSpec) + if v1alpha1 { + handlev1alpha1 = true } } else { - if k8upv1Exists { - if err := m.createv1Restore(opLog, namespace, jobSpec); err != nil { - return err - } + if v1 { + handlev1 = true } else { - if k8upv1alpha1Exists { - if err := m.createv1alpha1Restore(opLog, namespace, jobSpec); err != nil { - return err - } + if v1alpha1 { + handlev1alpha1 = true } } } } else { - if k8upv1alpha1Exists { - if err := m.createv1alpha1Restore(opLog, namespace, jobSpec); err != nil { - return err - } + if v1alpha1 { + handlev1alpha1 = true + } + } + + if handlev1alpha1 { + if cancel { + return m.cancelv1alpha1Restore(ctx, opLog, namespace, jobSpec) + } else { + return m.createv1alpha1Restore(ctx, opLog, namespace, jobSpec) + } + } + if handlev1 { + if cancel { + return m.cancelv1Restore(ctx, opLog, namespace, jobSpec) + } else { + return m.createv1Restore(ctx, opLog, namespace, jobSpec) } } return nil @@ -97,7 +93,7 @@ func checkRestoreVersionFromCore(resource []byte) (string, error) { } // createv1alpha1Restore will create a restore task using the restores.backup.appuio.ch v1alpha1 api (k8up v1) -func (m *Messenger) createv1alpha1Restore(opLog logr.Logger, namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error { +func (m *Messenger) createv1alpha1Restore(ctx context.Context, opLog logr.Logger, namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error { restorev1alpha1 := &k8upv1alpha1.Restore{} if err := json.Unmarshal(jobSpec.Misc.MiscResource, restorev1alpha1); err != nil { opLog.Error(err, @@ -109,7 +105,7 @@ func (m *Messenger) createv1alpha1Restore(opLog logr.Logger, namespace string, j return err } restorev1alpha1.SetNamespace(namespace) - if err := m.Client.Create(context.Background(), restorev1alpha1); err != nil { + if err := m.Client.Create(ctx, restorev1alpha1); err != nil { opLog.Error(err, fmt.Sprintf( "Unable to create restore %s with k8up v1alpha1 api.", @@ -122,7 +118,7 @@ func (m *Messenger) createv1alpha1Restore(opLog logr.Logger, namespace string, j } // createv1Restore will create a restore task using the restores.k8up.io v1 api (k8up v2) -func (m *Messenger) createv1Restore(opLog logr.Logger, namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error { +func (m *Messenger) createv1Restore(ctx context.Context, opLog logr.Logger, namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error { restorev1 := &k8upv1.Restore{} if err := json.Unmarshal(jobSpec.Misc.MiscResource, restorev1); err != nil { opLog.Error(err, @@ -134,7 +130,7 @@ func (m *Messenger) createv1Restore(opLog logr.Logger, namespace string, jobSpec return err } restorev1.SetNamespace(namespace) - if err := m.Client.Create(context.Background(), restorev1); err != nil { + if err := m.Client.Create(ctx, restorev1); err != nil { opLog.Error(err, fmt.Sprintf( "Unable to create restore %s with k8up v1 api.", @@ -145,3 +141,87 @@ func (m *Messenger) createv1Restore(opLog logr.Logger, namespace string, jobSpec } return nil } + +// cancelv1alpha1Restore will attempt to cancel a restore task using the restores.backup.appuio.ch v1alpha1 api (k8up v1) +func (m *Messenger) cancelv1alpha1Restore(ctx context.Context, opLog logr.Logger, namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error { + restorev1alpha1 := &k8upv1alpha1.Restore{} + cr := &cancelRestore{} + if err := json.Unmarshal(jobSpec.Misc.MiscResource, &cr); err != nil { + return err + } + if err := m.Client.Get(ctx, types.NamespacedName{Namespace: namespace, Name: cr.RestoreName}, restorev1alpha1); helpers.IgnoreNotFound(err) != nil { + opLog.Error(err, + fmt.Sprintf( + "Unable to get restore %s with k8up v1alpha1 api.", + cr.RestoreName, + ), + ) + return err + } + if restorev1alpha1.Name != "" { + if err := m.Client.Delete(ctx, restorev1alpha1); err != nil { + opLog.Error(err, + fmt.Sprintf( + "Unable to delete restore %s with k8up v1alpha1 api.", + cr.RestoreName, + ), + ) + return err + } + } + // if no matching restore found, or the restore is deleted, send the cancellation message back to core + m.pubRestoreCancel(opLog, namespace, cr.RestoreName, jobSpec) + return nil +} + +// cancelv1Restore will attempt to cancel a restore task using the restores.k8up.io v1 api (k8up v2) +func (m *Messenger) cancelv1Restore(ctx context.Context, opLog logr.Logger, namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error { + restorev1 := &k8upv1.Restore{} + cr := &cancelRestore{} + if err := json.Unmarshal(jobSpec.Misc.MiscResource, &cr); err != nil { + return err + } + if err := m.Client.Get(ctx, types.NamespacedName{Namespace: namespace, Name: cr.RestoreName}, restorev1); helpers.IgnoreNotFound(err) != nil { + opLog.Error(err, + fmt.Sprintf( + "Unable to get restore %s with k8up v1 api.", + cr.RestoreName, + ), + ) + return err + } + if restorev1.Name != "" { + if err := m.Client.Delete(ctx, restorev1); err != nil { + opLog.Error(err, + fmt.Sprintf( + "Unable to delete restore %s with k8up v1alpha1 api.", + cr.RestoreName, + ), + ) + return err + } + } + // if no matching restore found, or the restore is deleted, send the cancellation message back to core + m.pubRestoreCancel(opLog, namespace, cr.RestoreName, jobSpec) + return nil +} + +func (m *Messenger) pubRestoreCancel(opLog logr.Logger, namespace, restorename string, jobSpec *lagoonv1beta2.LagoonTaskSpec) { + msg := schema.LagoonMessage{ + Type: "restore:cancel", + Namespace: namespace, + Meta: &schema.LagoonLogMeta{ + Environment: jobSpec.Environment.Name, + Project: jobSpec.Project.Name, + JobName: restorename, + }, + } + msgBytes, err := json.Marshal(msg) + if err != nil { + opLog.Error(err, "Unable to encode message as JSON") + } + // publish the cancellation result back to lagoon + if err := m.Publish("lagoon-tasks:controller", msgBytes); err != nil { + opLog.Error(err, "Unable to publish message.") + } +} diff --git a/test-resources/results/k8upv1.yaml b/test-resources/results/k8upv1.yaml index db25b92a..9a6ede7c 100644 --- a/test-resources/results/k8upv1.yaml +++ b/test-resources/results/k8upv1.yaml @@ -1,7 +1,7 @@ apiVersion: k8up.io/v1 kind: Restore metadata: - name: restore-bf072a0-uqxqo4 + name: restore-bf072a09e17726da54adc79936ec8745521993599d41211dfc9466dfd5bc32a5 namespace: nginx-example-main spec: backend: