Skip to content

Commit a85fe35

Browse files
committed
++ add Error reason for GarbageCollection condition
Signed-off-by: Ivan Mikheykin <[email protected]>
1 parent e8231ae commit a85fe35

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

images/virtualization-artifact/pkg/common/annotations/annotations.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ const (
192192
AnnDVCRDeploymentSwitchToGarbageCollectionMode = AnnAPIGroupV + "/dvcr-deployment-switch-to-garbage-collection"
193193
// AnnDVCRGarbageCollectionDone is an annotation on maintenance secret that indicates the garbage collection process is done.
194194
AnnDVCRGarbageCollectionDone = AnnAPIGroupV + "/dvcr-garbage-collection-done"
195+
196+
// AnnDVCRGarbageCollectionResult is an annotation on deployment dvcr with last garbage collection result JSON.
197+
AnnDVCRGarbageCollectionResult = AnnAPIGroupV + "/dvcr-garbage-collection-result"
195198
)
196199

197200
// AddAnnotation adds an annotation to an object

images/virtualization-artifact/pkg/controller/dvcr-garbage-collection/internal/life_cycle.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import (
2525
"sigs.k8s.io/controller-runtime/pkg/client"
2626
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2727

28+
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
2829
dvcrcondition "github.com/deckhouse/virtualization-controller/pkg/controller/dvcr-garbage-collection/condition"
2930
dvcrtypes "github.com/deckhouse/virtualization-controller/pkg/controller/dvcr-garbage-collection/types"
30-
dvcr_deployment_condition "github.com/deckhouse/virtualization/api/core/v1alpha2/dvcr-deployment-condition"
31+
dvcrdeploymentcondition "github.com/deckhouse/virtualization/api/core/v1alpha2/dvcr-deployment-condition"
3132
)
3233

3334
type LifeCycleHandler struct {
@@ -51,7 +52,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, req reconcile.Request, dep
5152

5253
if req.Namespace == dvcrtypes.CronSourceNamespace && req.Name == dvcrtypes.CronSourceRunGC {
5354
dvcrcondition.UpdateGarbageCollectionCondition(deploy,
54-
dvcr_deployment_condition.InProgress,
55+
dvcrdeploymentcondition.InProgress,
5556
"Garbage collection initiated.",
5657
)
5758
return reconcile.Result{}, h.dvcrService.InitiateGarbageCollectionMode(ctx)
@@ -78,16 +79,21 @@ func (h LifeCycleHandler) Handle(ctx context.Context, req reconcile.Request, dep
7879
}
7980

8081
if h.dvcrService.IsGarbageCollectionDone(secret) {
81-
dvcrcondition.UpdateGarbageCollectionCondition(deploy,
82-
dvcr_deployment_condition.Done,
83-
"%s", string(secret.Data["result"]),
84-
)
82+
// Extract error or success message from the result.
83+
reason, msg, err := h.dvcrService.ParseGarbageCollectionResult(secret)
84+
if err != nil {
85+
return reconcile.Result{}, err
86+
}
87+
dvcrcondition.UpdateGarbageCollectionCondition(deploy, reason, "%s", msg)
88+
// Put full result JSON into annotation on deployment.
89+
annotations.AddAnnotation(deploy, annotations.AnnDVCRGarbageCollectionResult, h.dvcrService.GetGarbageCollectionResult(secret))
90+
// It is now possible to delete a secret.
8591
return reconcile.Result{}, h.dvcrService.DeleteGarbageCollectionSecret(ctx)
8692
}
8793

8894
if h.dvcrService.IsGarbageCollectionStarted(secret) {
8995
dvcrcondition.UpdateGarbageCollectionCondition(deploy,
90-
dvcr_deployment_condition.InProgress,
96+
dvcrdeploymentcondition.InProgress,
9197
"Wait for garbage collection to finish.",
9298
)
9399
// Wait for done annotation.
@@ -102,7 +108,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, req reconcile.Request, dep
102108
remainInProvisioning := len(resourcesInProvisioning)
103109
if remainInProvisioning > 0 {
104110
dvcrcondition.UpdateGarbageCollectionCondition(deploy,
105-
dvcr_deployment_condition.InProgress,
111+
dvcrdeploymentcondition.InProgress,
106112
"Wait for cvi/vi/vd finish provisioning: %d resources remain.", remainInProvisioning,
107113
)
108114
return reconcile.Result{RequeueAfter: time.Second * 20}, nil
@@ -113,7 +119,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, req reconcile.Request, dep
113119
return reconcile.Result{}, fmt.Errorf("switch to garbage collection mode: %w", err)
114120
}
115121
dvcrcondition.UpdateGarbageCollectionCondition(deploy,
116-
dvcr_deployment_condition.InProgress,
122+
dvcrdeploymentcondition.InProgress,
117123
"Wait for garbage collection to finish.",
118124
)
119125
return reconcile.Result{}, nil

images/virtualization-artifact/pkg/controller/dvcr-garbage-collection/types/interfaces.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
corev1 "k8s.io/api/core/v1"
2323
"sigs.k8s.io/controller-runtime/pkg/client"
24+
25+
dvcrdeploymentcondition "github.com/deckhouse/virtualization/api/core/v1alpha2/dvcr-deployment-condition"
2426
)
2527

2628
type DVCRService interface {
@@ -31,6 +33,9 @@ type DVCRService interface {
3133

3234
IsGarbageCollectionStarted(secret *corev1.Secret) bool
3335
IsGarbageCollectionDone(secret *corev1.Secret) bool
36+
37+
GetGarbageCollectionResult(secret *corev1.Secret) string
38+
ParseGarbageCollectionResult(secret *corev1.Secret) (reason dvcrdeploymentcondition.GarbageCollectionReason, message string, err error)
3439
}
3540

3641
type ProvisioningLister interface {

images/virtualization-artifact/pkg/controller/service/dvcr_service.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package service
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223

2324
corev1 "k8s.io/api/core/v1"
@@ -27,6 +28,7 @@ import (
2728
"sigs.k8s.io/controller-runtime/pkg/client"
2829

2930
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
31+
dvcrdeploymentcondition "github.com/deckhouse/virtualization/api/core/v1alpha2/dvcr-deployment-condition"
3032
)
3133

3234
type DVCRService struct {
@@ -148,3 +150,32 @@ func (d *DVCRService) DeleteGarbageCollectionSecret(ctx context.Context) error {
148150
err := d.client.Delete(ctx, secret)
149151
return client.IgnoreNotFound(err)
150152
}
153+
154+
func (d *DVCRService) GetGarbageCollectionResult(secret *corev1.Secret) string {
155+
if secret == nil {
156+
return ""
157+
}
158+
return string(secret.Data["result"])
159+
}
160+
161+
func (d *DVCRService) ParseGarbageCollectionResult(secret *corev1.Secret) (reason dvcrdeploymentcondition.GarbageCollectionReason, message string, err error) {
162+
var gcResult struct {
163+
result string
164+
error string
165+
message string
166+
}
167+
err = json.Unmarshal(secret.Data["result"], &gcResult)
168+
if err != nil {
169+
return "", "", fmt.Errorf("parse garbage collection result '%s': %w", string(secret.Data["result"]), err)
170+
}
171+
172+
switch gcResult.result {
173+
case "success":
174+
return dvcrdeploymentcondition.Done, gcResult.message, nil
175+
case "fail":
176+
return dvcrdeploymentcondition.Error, gcResult.error, nil
177+
}
178+
179+
// Unexpected format. It should not happen, but we need to show something if it happens.
180+
return dvcrdeploymentcondition.Done, string(secret.Data["result"]), nil
181+
}

0 commit comments

Comments
 (0)