Skip to content

Commit 4eadbc1

Browse files
committed
feat: support cancelling restores
1 parent 23db49f commit 4eadbc1

5 files changed

Lines changed: 241 additions & 47 deletions

File tree

controller-test.sh

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ echo '
444444
{"properties":{"delivery_mode":2},"routing_key":"ci-local-controller-kubernetes:misc",
445445
"payload":"{
446446
\"misc\":{
447-
\"miscResource\":\"eyJtZXRhZGF0YSI6eyJuYW1lIjoicmVzdG9yZS1iZjA3MmEwLXVxeHFvNCJ9LCJzcGVjIjp7InNuYXBzaG90IjoiYmYwNzJhMDllMTc3MjZkYTU0YWRjNzk5MzZlYzg3NDU1MjE5OTM1OTlkNDEyMTFkZmM5NDY2ZGZkNWJjMzJhNSIsInJlc3RvcmVNZXRob2QiOnsiczMiOnt9fSwiYmFja2VuZCI6eyJzMyI6eyJidWNrZXQiOiJiYWFzLW5naW54LWV4YW1wbGUifSwicmVwb1Bhc3N3b3JkU2VjcmV0UmVmIjp7ImtleSI6InJlcG8tcHciLCJuYW1lIjoiYmFhcy1yZXBvLXB3In19fX0=\"
447+
\"miscResource\":\"eyJtZXRhZGF0YSI6eyJuYW1lIjoicmVzdG9yZS1iZjA3MmEwOWUxNzcyNmRhNTRhZGM3OTkzNmVjODc0NTUyMTk5MzU5OWQ0MTIxMWRmYzk0NjZkZmQ1YmMzMmE1In0sInNwZWMiOnsic25hcHNob3QiOiJiZjA3MmEwOWUxNzcyNmRhNTRhZGM3OTkzNmVjODc0NTUyMTk5MzU5OWQ0MTIxMWRmYzk0NjZkZmQ1YmMzMmE1IiwicmVzdG9yZU1ldGhvZCI6eyJzMyI6e319LCJiYWNrZW5kIjp7InMzIjp7ImJ1Y2tldCI6ImJhYXMtbmdpbngtZXhhbXBsZSJ9LCJyZXBvUGFzc3dvcmRTZWNyZXRSZWYiOnsia2V5IjoicmVwby1wdyIsIm5hbWUiOiJiYWFzLXJlcG8tcHcifX19fQ==\"
448448
},
449449
\"key\":\"deploytarget:restic:backup:restore\",
450450
\"environment\":{
@@ -463,7 +463,7 @@ echo ""
463463
sleep 10
464464
CHECK_COUNTER=1
465465
kubectl -n nginx-example-main get restores.k8up.io
466-
until $(kubectl -n nginx-example-main get restores.k8up.io restore-bf072a0-uqxqo4 &> /dev/null)
466+
until $(kubectl -n nginx-example-main get restores.k8up.io restore-bf072a09e17726da54adc79936ec8745521993599d41211dfc9466dfd5bc32a5 &> /dev/null)
467467
do
468468
if [ $CHECK_COUNTER -lt 14 ]; then
469469
let CHECK_COUNTER=CHECK_COUNTER+1
@@ -478,7 +478,7 @@ else
478478
exit 1
479479
fi
480480
done
481-
kubectl -n nginx-example-main get restores.k8up.io restore-bf072a0-uqxqo4 -o yaml | kubectl-neat > test-resources/results/k8upv1-cluster.yaml
481+
kubectl -n nginx-example-main get restores.k8up.io restore-bf072a09e17726da54adc79936ec8745521993599d41211dfc9466dfd5bc32a5 -o yaml | kubectl-neat > test-resources/results/k8upv1-cluster.yaml
482482
if cmp --silent -- "test-resources/results/k8upv1.yaml" "test-resources/results/k8upv1-cluster.yaml"; then
483483
echo "Resulting restores match"
484484
else
@@ -495,6 +495,46 @@ else
495495
exit 1
496496
fi
497497

498+
# test that a cancellation works when a cancellation message is received from rabbitmq, and that the restore resource is deleted from the namespace
499+
echo "==> Trigger a lagoon restore cancellation using rabbitmq"
500+
echo '
501+
{"properties":{"delivery_mode":2},"routing_key":"ci-local-controller-kubernetes:misc",
502+
"payload":"{
503+
\"misc\":{
504+
\"miscResource\":\"eyJyZXN0b3JlTmFtZSI6InJlc3RvcmUtYmYwNzJhMDllMTc3MjZkYTU0YWRjNzk5MzZlYzg3NDU1MjE5OTM1OTlkNDEyMTFkZmM5NDY2ZGZkNWJjMzJhNSIsImJhY2t1cElkIjoiYmYwNzJhMDllMTc3MjZkYTU0YWRjNzk5MzZlYzg3NDU1MjE5OTM1OTlkNDEyMTFkZmM5NDY2ZGZkNWJjMzJhNSJ9\"
505+
},
506+
\"key\":\"deploytarget:restic:cancel:restore\",
507+
\"environment\":{
508+
\"name\":\"main\",
509+
\"openshiftProjectName\":\"nginx-example-main\"
510+
},
511+
\"project\":{
512+
\"name\":\"nginx-example\"
513+
},
514+
\"advancedTask\":{}
515+
}",
516+
"payload_encoding":"string"
517+
}' >payload.json
518+
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
519+
echo ""
520+
sleep 10
521+
# check that the restore resource gets removed
522+
until ! $(kubectl -n nginx-example-main get restores.k8up.io restore-bf072a09e17726da54adc79936ec8745521993599d41211dfc9466dfd5bc32a5 &> /dev/null)
523+
do
524+
if [ $CHECK_COUNTER -lt 14 ]; then
525+
let CHECK_COUNTER=CHECK_COUNTER+1
526+
echo "Restore not deleted yet"
527+
sleep 5
528+
else
529+
echo "Timeout of 70seconds for restore to be deleted"
530+
check_controller_log
531+
tear_down
532+
echo "================ END ================"
533+
echo "============== FAILED ==============="
534+
exit 1
535+
fi
536+
done
537+
498538
echo "==> Delete the environment"
499539
echo '
500540
{"properties":{"delivery_mode":2},"routing_key":"ci-local-controller-kubernetes:remove",

internal/helpers/helper_types.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package helpers
22

3+
import (
4+
"context"
5+
6+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
7+
"k8s.io/apimachinery/pkg/types"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
9+
)
10+
311
// LagoonEnvironmentVariable is used to define Lagoon environment variables.
412
type LagoonEnvironmentVariable struct {
513
Name string `json:"name"`
@@ -31,3 +39,27 @@ type LagoonAPIConfiguration struct {
3139
SSHHost string
3240
SSHPort string
3341
}
42+
43+
func K8UPVersions(ctx context.Context, c client.Client) (bool, bool, error) {
44+
k8upv1alpha1Exists := false
45+
k8upv1Exists := false
46+
crdv1alpha1 := &apiextensionsv1.CustomResourceDefinition{}
47+
if err := c.Get(context.TODO(), types.NamespacedName{Name: "restores.backup.appuio.ch"}, crdv1alpha1); err != nil {
48+
if err := IgnoreNotFound(err); err != nil {
49+
return k8upv1alpha1Exists, k8upv1Exists, err
50+
}
51+
}
52+
if crdv1alpha1.ObjectMeta.Name == "restores.backup.appuio.ch" {
53+
k8upv1alpha1Exists = true
54+
}
55+
crdv1 := &apiextensionsv1.CustomResourceDefinition{}
56+
if err := c.Get(context.TODO(), types.NamespacedName{Name: "restores.k8up.io"}, crdv1); err != nil {
57+
if err := IgnoreNotFound(err); err != nil {
58+
return k8upv1alpha1Exists, k8upv1Exists, err
59+
}
60+
}
61+
if crdv1.ObjectMeta.Name == "restores.k8up.io" {
62+
k8upv1Exists = true
63+
}
64+
return k8upv1alpha1Exists, k8upv1Exists, nil
65+
}

internal/messenger/consumer.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,25 @@ func (m *Messenger) Consumer(targetName string) { //error {
336336
return
337337
}
338338
case "deploytarget:restic:backup:restore", "kubernetes:restic:backup:restore":
339+
v1alpha1, v1, err := helpers.K8UPVersions(ctx, m.Client)
340+
if err != nil {
341+
//@TODO: send msg back to lagoon and update task to failed?
342+
message.Ack(false) // ack to remove from queue
343+
return
344+
}
345+
if !v1alpha1 && !v1 {
346+
// k8up not installed
347+
message.Ack(false) // ack to remove from queue
348+
return
349+
}
339350
opLog.Info(
340351
fmt.Sprintf(
341352
"Received backup restoration for project %s, environment %s",
342353
jobSpec.Project.Name,
343354
jobSpec.Environment.Name,
344355
),
345356
)
346-
err := m.ResticRestore(namespace, jobSpec)
357+
err = m.ResticRestore(ctx, namespace, jobSpec, v1alpha1, v1, false)
347358
if err != nil {
348359
opLog.Error(err,
349360
fmt.Sprintf(
@@ -356,6 +367,39 @@ func (m *Messenger) Consumer(targetName string) { //error {
356367
message.Ack(false) // ack to remove from queue
357368
return
358369
}
370+
case "deploytarget:restic:cancel:restore":
371+
v1alpha1, v1, err := helpers.K8UPVersions(ctx, m.Client)
372+
if err != nil {
373+
//@TODO: send msg back to lagoon and update task to failed?
374+
message.Ack(false) // ack to remove from queue
375+
return
376+
}
377+
if !v1alpha1 && !v1 {
378+
// k8up not installed
379+
message.Ack(false) // ack to remove from queue
380+
return
381+
}
382+
// if this is a request to cancel a restore attempt
383+
opLog.Info(
384+
fmt.Sprintf(
385+
"Received restore cancellation for project %s, environment %s",
386+
jobSpec.Project.Name,
387+
jobSpec.Environment.Name,
388+
),
389+
)
390+
err = m.ResticRestore(ctx, namespace, jobSpec, v1alpha1, v1, true)
391+
if err != nil {
392+
opLog.Error(err,
393+
fmt.Sprintf(
394+
"Cancel restore for project %s, environment %s failed",
395+
jobSpec.Project.Name,
396+
jobSpec.Environment.Name,
397+
),
398+
)
399+
//@TODO: send msg back to lagoon and update task to failed?
400+
message.Ack(false) // ack to remove from queue
401+
return
402+
}
359403
case "deploytarget:route:migrate", "kubernetes:route:migrate", "openshift:route:migrate":
360404
opLog.Info(
361405
fmt.Sprintf(

0 commit comments

Comments
 (0)