Skip to content

Feat(Grafana): Initial implementationtion of Conditions #2035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/v1beta1/grafana_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type GrafanaStatus struct {
Folders NamespacedResourceList `json:"folders,omitempty"`
LibraryPanels NamespacedResourceList `json:"libraryPanels,omitempty"`
Version string `json:"version,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions config/crd/bases/grafana.integreatly.org_grafanas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9508,6 +9508,62 @@ spec:
properties:
adminUrl:
type: string
conditions:
items:
description: Condition contains details for one aspect of the current
state of this API Resource.
properties:
lastTransitionTime:
description: |-
lastTransitionTime is the last time the condition transitioned from one status to another.
This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: |-
message is a human readable message indicating details about the transition.
This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: |-
observedGeneration represents the .metadata.generation that the condition was set based upon.
For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date
with respect to the current state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: |-
reason contains a programmatic identifier indicating the reason for the condition's last transition.
Producers of specific condition types may define expected values and meanings for this field,
and whether the values are considered a guaranteed API.
The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
contactPoints:
items:
type: string
Expand Down
18 changes: 18 additions & 0 deletions controllers/grafana_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/grafana/grafana-operator/v5/controllers/config"
"github.com/grafana/grafana-operator/v5/controllers/metrics"
Expand All @@ -29,6 +30,8 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -40,6 +43,10 @@ import (
grafanav1beta1 "github.com/grafana/grafana-operator/v5/api/v1beta1"
)

const (
ConditionTypeGrafanaReady = "GrafanaReady"
)

// GrafanaReconciler reconciles a Grafana object
type GrafanaReconciler struct {
client.Client
Expand Down Expand Up @@ -101,6 +108,7 @@ func (r *GrafanaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
cr.Spec.Version = targetVersion
if err := r.Update(ctx, cr); err != nil {
meta.RemoveStatusCondition(&cr.Status.Conditions, ConditionTypeGrafanaReady)
return ctrl.Result{}, fmt.Errorf("updating grafana version in spec: %w", err)
}
}
Expand All @@ -124,13 +132,23 @@ func (r *GrafanaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
cr.Status.LastMessage = err.Error()

metrics.GrafanaFailedReconciles.WithLabelValues(cr.Namespace, cr.Name, string(stage)).Inc()
meta.RemoveStatusCondition(&cr.Status.Conditions, ConditionTypeGrafanaReady)
return ctrl.Result{}, fmt.Errorf("reconciler error in stage '%s': %w", stage, err)
}
}

cr.Status.StageStatus = grafanav1beta1.OperatorStageResultSuccess
cr.Status.LastMessage = ""

meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{
Type: ConditionTypeGrafanaReady, // Maybe use Grafana instead to be consistent with other conditions
Reason: "GrafanaReady",
Message: "Grafana reconcile completed",
ObservedGeneration: cr.Generation,
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.Time{Time: time.Now()},
})

return ctrl.Result{}, nil
}

Expand Down
Loading