Skip to content

Commit

Permalink
Add WorkloadMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
kvaps committed Jan 6, 2025
1 parent ce6e96f commit 9c7fd1d
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package v1alpha1

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

// WorkloadMonitorSpec defines the desired state of WorkloadMonitor
type WorkloadMonitorSpec struct {
// Selector is a label selector to find workloads to monitor
// +required
Selector map[string]string `json:"selector"`

// Version specifies the version of the workload
// +optional
Version string `json:"version,omitempty"`

// MinReplicas specifies the minimum number of replicas that should be available
// +kubebuilder:validation:Minimum=0
// +optional
MinReplicas *int32 `json:"minReplicas,omitempty"`

// Replicas is the desired number of replicas
// If not specified, will use observedReplicas as the target
// +kubebuilder:validation:Minimum=0
// +optional
Replicas *int32 `json:"replicas,omitempty"`
}

// WorkloadMonitorStatus defines the observed state of WorkloadMonitor
type WorkloadMonitorStatus struct {
// Operational indicates if the workload meets all operational requirements
// +optional
Operational bool `json:"operational"`

// AvailableReplicas is the number of ready replicas
// +optional
AvailableReplicas int32 `json:"availableReplicas"`

// ObservedReplicas is the total number of pods observed
// +optional
ObservedReplicas int32 `json:"observedReplicas"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Version",type="string",JSONPath=".spec.version"
// +kubebuilder:printcolumn:name="MinReplicas",type="integer",JSONPath=".spec.minReplicas"
// +kubebuilder:printcolumn:name="Available",type="integer",JSONPath=".status.availableReplicas"
// +kubebuilder:printcolumn:name="Observed",type="integer",JSONPath=".status.observedReplicas"
// +kubebuilder:printcolumn:name="Operational",type="boolean",JSONPath=".status.operational"

// WorkloadMonitor is the Schema for the workloadmonitors API
type WorkloadMonitor struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec WorkloadMonitorSpec `json:"spec,omitempty"`
Status WorkloadMonitorStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// WorkloadMonitorList contains a list of WorkloadMonitor
type WorkloadMonitorList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []WorkloadMonitor `json:"items"`
}

func init() {
SchemeBuilder.Register(&WorkloadMonitor{}, &WorkloadMonitorList{})
}

// GetSelector returns the label selector from metadata
func (w *WorkloadMonitor) GetSelector() map[string]string {
return w.Spec.Selector
}

// Selector specifies the label selector for workloads
type Selector map[string]string

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

8 changes: 8 additions & 0 deletions packages/system/cozystack-workload-controller/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Workload")
os.Exit(1)
}

if err = (&controller.WorkloadMonitorReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "WorkloadMonitor")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.4
name: workloadmonitors.cozystack.io
spec:
group: cozystack.io
names:
kind: WorkloadMonitor
listKind: WorkloadMonitorList
plural: workloadmonitors
singular: workloadmonitor
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .spec.version
name: Version
type: string
- jsonPath: .spec.minReplicas
name: MinReplicas
type: integer
- jsonPath: .status.availableReplicas
name: Available
type: integer
- jsonPath: .status.observedReplicas
name: Observed
type: integer
- jsonPath: .status.operational
name: Operational
type: boolean
name: v1alpha1
schema:
openAPIV3Schema:
description: WorkloadMonitor is the Schema for the workloadmonitors API
properties:
apiVersion:
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
spec:
description: WorkloadMonitorSpec defines the desired state of WorkloadMonitor
properties:
minReplicas:
description: MinReplicas specifies the minimum number of replicas
that should be available
format: int32
minimum: 0
type: integer
replicas:
description: |-
Replicas is the desired number of replicas
If not specified, will use observedReplicas as the target
format: int32
minimum: 0
type: integer
selector:
additionalProperties:
type: string
description: Selector is a label selector to find workloads to monitor
type: object
version:
description: Version specifies the version of the workload
type: string
required:
- selector
type: object
status:
description: WorkloadMonitorStatus defines the observed state of WorkloadMonitor
properties:
availableReplicas:
description: AvailableReplicas is the number of ready replicas
format: int32
type: integer
observedReplicas:
description: ObservedReplicas is the total number of pods observed
format: int32
type: integer
operational:
description: Operational indicates if the workload meets all operational
requirements
type: boolean
type: object
type: object
served: true
storage: true
subresources:
status: {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rules:
- apiGroups:
- cozystack.io
resources:
- workloadmonitors
- workloads
verbs:
- create
Expand All @@ -27,6 +28,13 @@ rules:
- apiGroups:
- cozystack.io
resources:
- workloadmonitors/finalizers
verbs:
- update
- apiGroups:
- cozystack.io
resources:
- workloadmonitors/status
- workloads/status
verbs:
- get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (r *WorkloadReconciler) reconcilePod(ctx context.Context, pod *corev1.Pod)
return err
}

workload.Labels = pod.Labels
workload.Status.Kind = kind
workload.Status.Type = workloadType
workload.Status.Resources = resources
Expand Down
Loading

0 comments on commit 9c7fd1d

Please sign in to comment.