Skip to content

Commit

Permalink
feat(controller): revert instrumentation when Dash0 resource is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed May 28, 2024
1 parent 211141b commit 14cd907
Show file tree
Hide file tree
Showing 13 changed files with 1,772 additions and 395 deletions.
76 changes: 76 additions & 0 deletions api/v1alpha1/dash0_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package v1alpha1

import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/dash0hq/dash0-operator/internal/util"
)

// Dash0Spec defines the desired state of the Dash0 custom resource.
Expand All @@ -28,6 +31,79 @@ type Dash0 struct {
Status Dash0Status `json:"status,omitempty"`
}

func (d *Dash0) IsAvailable() bool {
if condition := d.getCondition(util.ConditionTypeAvailable); condition != nil {
return condition.Status == metav1.ConditionTrue
}
return false
}

func (d *Dash0) getCondition(conditionType util.ConditionType) *metav1.Condition {
for _, c := range d.Status.Conditions {
if c.Type == string(conditionType) {
return &c

}
}
return nil
}

func (d *Dash0) SetAvailableConditionToUnknown() {
meta.SetStatusCondition(
&d.Status.Conditions,
metav1.Condition{
Type: string(util.ConditionTypeAvailable),
Status: metav1.ConditionUnknown,
Reason: "ReconcileStarted",
Message: "Dash0 has started resource reconciliation.",
})
meta.SetStatusCondition(
&d.Status.Conditions,
metav1.Condition{
Type: string(util.ConditionTypeDegraded),
Status: metav1.ConditionTrue,
Reason: "ReconcileStarted",
Message: "Dash0 is still starting.",
})
}

func (d *Dash0) EnsureResourceIsMarkedAsAvailable() {
// If the available status is already true, the status condition is not updated, except for Reason, Message and
// ObservedGeneration timestamp. In particular, LastTransitionTime is not updated. Thus, this operation is
// effectively idempotent.
meta.SetStatusCondition(
&d.Status.Conditions,
metav1.Condition{
Type: string(util.ConditionTypeAvailable),
Status: metav1.ConditionTrue,
Reason: "ReconcileFinished",
Message: "Dash0 is active in this namespace now.",
})
meta.RemoveStatusCondition(&d.Status.Conditions, string(util.ConditionTypeDegraded))
}

func (d *Dash0) EnsureResourceIsMarkedAsUnavailable() {
// If the available status is already false, the status condition is not updated, except for Reason, Message and
// ObservedGeneration timestamp. In particular, LastTransitionTime is not updated. Thus, this operation is
// effectively idempotent.
meta.SetStatusCondition(
&d.Status.Conditions,
metav1.Condition{
Type: string(util.ConditionTypeAvailable),
Status: metav1.ConditionFalse,
Reason: "Dash0CustomResourceHasBeenRemoved",
Message: "Dash0 is inactive in this namespace now.",
})
meta.SetStatusCondition(
&d.Status.Conditions,
metav1.Condition{
Type: string(util.ConditionTypeDegraded),
Status: metav1.ConditionTrue,
Reason: "Dash0CustomResourceHasBeenRemoved",
Message: "Dash0 is about to be deleted.",
})
}

//+kubebuilder:object:root=true

// Dash0List contains a list of Dash0
Expand Down
Loading

0 comments on commit 14cd907

Please sign in to comment.