Skip to content

Commit

Permalink
feat(slo): sli inline support and type fixes
Browse files Browse the repository at this point in the history
- add suuport for inline SLI definition
- fixed SLOSpec.Indicator type
Signed-off-by: Hy3n4 <[email protected]>
  • Loading branch information
Hy3n4 committed Oct 26, 2023
1 parent 43a0efe commit a5300fe
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 88 deletions.
4 changes: 3 additions & 1 deletion apis/openslo/v1/slo_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type TimeWindowSpec struct {
type SLOSpec struct {
Description Description `json:"description,omitempty"`
Service string `json:"service,omitempty"`
Indicator *SLISpec `json:"indicator,omitempty"`
Indicator *Indicator `json:"indicator,omitempty"`
IndicatorRef *string `json:"indicatorRef,omitempty"`
// +kubebuilder:validation:MaxItems=1
TimeWindow []TimeWindowSpec `json:"timeWindow,omitempty"`
Expand All @@ -78,6 +78,8 @@ type SLOStatus struct {
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Status",type=string,JSONPath=.status.ready,description="The reason for the current status of the SLO resource"
//+kubebuilder:printcolumn:name="Window",type=string,JSONPath=.spec.timeWindow[0].duration,description="The time window for the SLO resource"
//+kubebuilder:printcolumn:name="Age",type=date,JSONPath=.metadata.creationTimestamp,description="The time when the SLO resource was created"

// SLO is the Schema for the slos API
type SLO struct {
Expand Down
4 changes: 2 additions & 2 deletions apis/openslo/v1/zz_generated.deepcopy.go

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

138 changes: 85 additions & 53 deletions config/crd/bases/openslo.com_slos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ spec:
jsonPath: .status.ready
name: Status
type: string
- description: The time window for the SLO resource
jsonPath: .spec.timeWindow[0].duration
name: Window
type: string
- description: The time when the SLO resource was created
jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1
schema:
openAPIV3Schema:
Expand Down Expand Up @@ -201,57 +209,93 @@ spec:
maxLength: 1050
type: string
indicator:
description: SLISpec defines the desired state of SLI
properties:
description:
maxLength: 1050
type: string
ratioMetric:
metadata:
properties:
bad:
annotations:
additionalProperties:
type: string
type: object
displayName:
type: string
finalizers:
items:
type: string
type: array
labels:
additionalProperties:
type: string
type: object
name:
type: string
namespace:
type: string
type: object
spec:
description: SLISpec defines the desired state of SLI
properties:
description:
maxLength: 1050
type: string
ratioMetric:
properties:
metricSource:
bad:
properties:
metricSourceRef:
type: string
spec:
type: string
type:
type: string
metricSource:
properties:
metricSourceRef:
type: string
spec:
type: string
type:
type: string
type: object
type: object
type: object
counter:
type: boolean
good:
properties:
metricSource:
counter:
type: boolean
good:
properties:
metricSourceRef:
type: string
spec:
type: string
type:
type: string
metricSource:
properties:
metricSourceRef:
type: string
spec:
type: string
type:
type: string
type: object
type: object
type: object
raw:
properties:
metricSource:
raw:
properties:
metricSourceRef:
type: string
spec:
type: string
type:
type: string
metricSource:
properties:
metricSourceRef:
type: string
spec:
type: string
type:
type: string
type: object
type: object
rawType:
enum:
- success
- failure
type: string
total:
properties:
metricSource:
properties:
metricSourceRef:
type: string
spec:
type: string
type:
type: string
type: object
type: object
type: object
rawType:
enum:
- success
- failure
type: string
total:
thresholdMetric:
properties:
metricSource:
properties:
Expand All @@ -264,18 +308,6 @@ spec:
type: object
type: object
type: object
thresholdMetric:
properties:
metricSource:
properties:
metricSourceRef:
type: string
spec:
type: string
type:
type: string
type: object
type: object
type: object
indicatorRef:
type: string
Expand Down
9 changes: 4 additions & 5 deletions internal/controller/openslo/sli_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ type SLIReconciler struct {
func (r *SLIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)

var sli openslov1.SLI

err := r.Get(ctx, req.NamespacedName, &sli)
sli := &openslov1.SLI{}
err := r.Get(ctx, req.NamespacedName, sli)
if err != nil {
if apierrors.IsNotFound(err) {
log.Info("SLI deleted")
log.Info("SLI resource not found. Object must have been deleted.")
return ctrl.Result{}, nil
}

log.Error(err, errGetDS)
log.Error(err, errGetSLI)
return ctrl.Result{}, nil
}

Expand Down
59 changes: 32 additions & 27 deletions internal/controller/openslo/slo_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,48 +62,53 @@ func (r *SLOReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
// Get SLI from SLO's ref
if slo.Spec.IndicatorRef != nil {
err = r.Get(ctx, client.ObjectKey{Name: *slo.Spec.IndicatorRef, Namespace: slo.Namespace}, sli)
} else if slo.Spec.Indicator != nil {
//TODO: Create SLI from SLO's indicator spec
}
if err != nil {
apierrors.IsNotFound(err)
{
log.Error(err, errGetSLI)
err = utils.UpdateStatus(
ctx,
slo,
r.Client,
"Ready",
metav1.ConditionFalse,
"SLIObjectNotFound",
"SLI Object not found",
)
if err != nil {
log.Error(err, "Failed to update SLO status")
return ctrl.Result{}, err
}
return ctrl.Result{}, err
}
}

if err != nil {
apierrors.IsNotFound(err)
{
log.Error(err, errGetSLI)
// Set SLI instance as the owner and controller.
if err := ctrl.SetControllerReference(slo, sli, r.Scheme); err != nil {
err = utils.UpdateStatus(
ctx,
slo,
r.Client,
"Ready",
metav1.ConditionFalse,
"SLIObjectNotFound",
"SLI Object not found",
"FailedToSetSLIOwner",
"Failed to set SLI owner reference",
)
if err != nil {
log.Error(err, "Failed to update SLO status")
return ctrl.Result{}, err
}
log.Error(err, "Failed to set owner reference for SLI")
return ctrl.Result{}, err
}
}

// Set SLI instance as the owner and controller.
if err := ctrl.SetControllerReference(slo, sli, r.Scheme); err != nil {
err = utils.UpdateStatus(
ctx,
slo,
r.Client,
"Ready",
metav1.ConditionFalse,
"FailedToSetSLIOwner",
"Failed to set SLI owner reference",
)
if err != nil {
log.Error(err, "Failed to update SLO status")
return ctrl.Result{}, err
} else if slo.Spec.Indicator != nil {
log.Info("SLO has an inline SLI")
sli.Name = slo.Spec.Indicator.Metadata.Name
sli.Spec.Description = slo.Spec.Indicator.Spec.Description
if slo.Spec.Indicator.Spec.RatioMetric != (openslov1.RatioMetricSpec{}) {
sli.Spec.RatioMetric = slo.Spec.Indicator.Spec.RatioMetric
}
log.Error(err, "Failed to set owner reference for SLI")
return ctrl.Result{}, err
log.Info("SLI created", "SLI Name", sli.Name, "SLI Namespace", sli.Namespace, "SLI RatioMetric", sli.Spec.RatioMetric)
}

// Check if this PrometheusRule already exists
Expand Down

0 comments on commit a5300fe

Please sign in to comment.