Skip to content

Commit

Permalink
feat: populate rules status
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSpiritXIII committed Jun 3, 2024
1 parent a4426a3 commit f21c2ba
Show file tree
Hide file tree
Showing 13 changed files with 685 additions and 52 deletions.
38 changes: 38 additions & 0 deletions charts/operator/crds/monitoring.googleapis.com_clusterrules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,44 @@ spec:
type: object
status:
description: Most recently observed status of the resource.
properties:
conditions:
description: Represents the latest available observations of a podmonitor's
current state.
items:
description: MonitoringCondition describes the condition of a PodMonitoring.
properties:
lastTransitionTime:
description: Last time the condition transitioned from one status
to another.
format: date-time
type: string
lastUpdateTime:
description: The last time this condition was updated.
format: date-time
type: string
message:
description: A human-readable message indicating details about
the transition.
type: string
reason:
description: The reason for the condition's last transition.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: MonitoringConditionType is the type of MonitoringCondition.
type: string
required:
- status
- type
type: object
type: array
observedGeneration:
description: The generation observed by the controller.
format: int64
type: integer
type: object
required:
- spec
Expand Down
38 changes: 38 additions & 0 deletions charts/operator/crds/monitoring.googleapis.com_globalrules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,44 @@ spec:
type: object
status:
description: Most recently observed status of the resource.
properties:
conditions:
description: Represents the latest available observations of a podmonitor's
current state.
items:
description: MonitoringCondition describes the condition of a PodMonitoring.
properties:
lastTransitionTime:
description: Last time the condition transitioned from one status
to another.
format: date-time
type: string
lastUpdateTime:
description: The last time this condition was updated.
format: date-time
type: string
message:
description: A human-readable message indicating details about
the transition.
type: string
reason:
description: The reason for the condition's last transition.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: MonitoringConditionType is the type of MonitoringCondition.
type: string
required:
- status
- type
type: object
type: array
observedGeneration:
description: The generation observed by the controller.
format: int64
type: integer
type: object
required:
- spec
Expand Down
38 changes: 38 additions & 0 deletions charts/operator/crds/monitoring.googleapis.com_rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,44 @@ spec:
type: object
status:
description: Most recently observed status of the resource.
properties:
conditions:
description: Represents the latest available observations of a podmonitor's
current state.
items:
description: MonitoringCondition describes the condition of a PodMonitoring.
properties:
lastTransitionTime:
description: Last time the condition transitioned from one status
to another.
format: date-time
type: string
lastUpdateTime:
description: The last time this condition was updated.
format: date-time
type: string
message:
description: A human-readable message indicating details about
the transition.
type: string
reason:
description: The reason for the condition's last transition.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: MonitoringConditionType is the type of MonitoringCondition.
type: string
required:
- status
- type
type: object
type: array
observedGeneration:
description: The generation observed by the controller.
format: int64
type: integer
type: object
required:
- spec
Expand Down
27 changes: 26 additions & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ monitoring resource was created successfully.</p>
</span>
</h3>
<p>
(<em>Appears in: </em><a href="#monitoring.googleapis.com/v1.ClusterNodeMonitoring">ClusterNodeMonitoring</a>, <a href="#monitoring.googleapis.com/v1.PodMonitoringStatus">PodMonitoringStatus</a>)
(<em>Appears in: </em><a href="#monitoring.googleapis.com/v1.ClusterNodeMonitoring">ClusterNodeMonitoring</a>, <a href="#monitoring.googleapis.com/v1.PodMonitoringStatus">PodMonitoringStatus</a>, <a href="#monitoring.googleapis.com/v1.RulesStatus">RulesStatus</a>)
</p>
<div>
<p>MonitoringStatus holds status information of a monitoring resource.</p>
Expand Down Expand Up @@ -2425,6 +2425,31 @@ RulesStatus
<div>
<p>RulesStatus contains status information for a Rules resource.</p>
</div>
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>MonitoringStatus</code><br/>
<em>
<a href="#monitoring.googleapis.com/v1.MonitoringStatus">
MonitoringStatus
</a>
</em>
</td>
<td>
<p>
(Members of <code>MonitoringStatus</code> are embedded into this type.)
</p>
</td>
</tr>
</tbody>
</table>
<h3 id="monitoring.googleapis.com/v1.SampleGroup">
<span id="SampleGroup">SampleGroup
</span>
Expand Down
105 changes: 105 additions & 0 deletions manifests/setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,41 @@ spec:
type: object
status:
description: Most recently observed status of the resource.
properties:
conditions:
description: Represents the latest available observations of a podmonitor's current state.
items:
description: MonitoringCondition describes the condition of a PodMonitoring.
properties:
lastTransitionTime:
description: Last time the condition transitioned from one status to another.
format: date-time
type: string
lastUpdateTime:
description: The last time this condition was updated.
format: date-time
type: string
message:
description: A human-readable message indicating details about the transition.
type: string
reason:
description: The reason for the condition's last transition.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: MonitoringConditionType is the type of MonitoringCondition.
type: string
required:
- status
- type
type: object
type: array
observedGeneration:
description: The generation observed by the controller.
format: int64
type: integer
type: object
required:
- spec
Expand Down Expand Up @@ -1520,6 +1555,41 @@ spec:
type: object
status:
description: Most recently observed status of the resource.
properties:
conditions:
description: Represents the latest available observations of a podmonitor's current state.
items:
description: MonitoringCondition describes the condition of a PodMonitoring.
properties:
lastTransitionTime:
description: Last time the condition transitioned from one status to another.
format: date-time
type: string
lastUpdateTime:
description: The last time this condition was updated.
format: date-time
type: string
message:
description: A human-readable message indicating details about the transition.
type: string
reason:
description: The reason for the condition's last transition.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: MonitoringConditionType is the type of MonitoringCondition.
type: string
required:
- status
- type
type: object
type: array
observedGeneration:
description: The generation observed by the controller.
format: int64
type: integer
type: object
required:
- spec
Expand Down Expand Up @@ -3378,6 +3448,41 @@ spec:
type: object
status:
description: Most recently observed status of the resource.
properties:
conditions:
description: Represents the latest available observations of a podmonitor's current state.
items:
description: MonitoringCondition describes the condition of a PodMonitoring.
properties:
lastTransitionTime:
description: Last time the condition transitioned from one status to another.
format: date-time
type: string
lastUpdateTime:
description: The last time this condition was updated.
format: date-time
type: string
message:
description: A human-readable message indicating details about the transition.
type: string
reason:
description: The reason for the condition's last transition.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: MonitoringConditionType is the type of MonitoringCondition.
type: string
required:
- status
- type
type: object
type: array
observedGeneration:
description: The generation observed by the controller.
format: int64
type: integer
type: object
required:
- spec
Expand Down
16 changes: 10 additions & 6 deletions pkg/operator/apis/monitoring/v1/monitoring_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func NewDefaultConditions(now metav1.Time) []MonitoringCondition {
}
}

func (cond *MonitoringCondition) IsValid() bool {
return cond.Type != "" && cond.Status != ""
}

// MonitoringStatus holds status information of a monitoring resource.
type MonitoringStatus struct {
// The generation observed by the controller.
Expand All @@ -77,17 +81,17 @@ type MonitoringStatus struct {
Conditions []MonitoringCondition `json:"conditions,omitempty"`
}

// SetMonitoringCondition merges the provided condition if the resource generation changed or there is
// a status condition state transition.
func (status *MonitoringStatus) SetMonitoringCondition(gen int64, now metav1.Time, cond *MonitoringCondition) (bool, error) {
// SetMonitoringCondition merges the provided valid condition if the resource generation changed or
// there is a status condition state transition.
func (status *MonitoringStatus) SetMonitoringCondition(gen int64, now metav1.Time, cond *MonitoringCondition) bool {
var (
specChanged = status.ObservedGeneration != gen
statusTransition, update bool
conds = make(map[MonitoringConditionType]*MonitoringCondition)
)

if cond.Type == "" || cond.Status == "" {
return update, errInvalidCond
if !cond.IsValid() {
return false
}

// Set up defaults.
Expand Down Expand Up @@ -124,5 +128,5 @@ func (status *MonitoringStatus) SetMonitoringCondition(gen int64, now metav1.Tim
}
}

return update, nil
return update
}
4 changes: 0 additions & 4 deletions pkg/operator/apis/monitoring/v1/pod_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

var (
errInvalidCond = fmt.Errorf("condition needs both 'Type' and 'Status' fields set")
)

// PodMonitoringCRD represents a Kubernetes CRD that monitors Pod endpoints.
type PodMonitoringCRD interface {
MonitoringCRD
Expand Down
5 changes: 1 addition & 4 deletions pkg/operator/apis/monitoring/v1/pod_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,7 @@ func TestSetMonitoringCondition(t *testing.T) {
for _, c := range cases {
t.Run(c.doc, func(t *testing.T) {
got := c.curr
change, err := got.SetMonitoringCondition(c.generation, c.now, c.cond)
if err != nil {
t.Fatalf("set podmonitoring condition: %s", err)
}
change := got.SetMonitoringCondition(c.generation, c.now, c.cond)

// Get resolved podmonitorings.
if change != c.change {
Expand Down
Loading

0 comments on commit f21c2ba

Please sign in to comment.