Skip to content

Commit

Permalink
Implement prometheus polling
Browse files Browse the repository at this point in the history
  • Loading branch information
dee-kryvenko committed Jan 20, 2025
1 parent 685d9c7 commit d4db18a
Show file tree
Hide file tree
Showing 24 changed files with 919 additions and 528 deletions.
2 changes: 1 addition & 1 deletion PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ resources:
controller: true
domain: argoproj.io
group: autoscaler
kind: Poll
kind: PrometheusPoll
path: github.com/plumber-cd/argocd-autoscaler/api/autoscaler/v1alpha1
version: v1alpha1
version: "3"
23 changes: 23 additions & 0 deletions api/autoscaler/v1alpha1/metric_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v1alpha1

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

// MetricValue is a resulting value for the metric.
type MetricValue struct {
// OwnerRef is the reference to the owner of this metric.
OwnerRef metav1.OwnerReference `json:"ownerRef,omitempty"`
// Poller is the identification of the poller that was used to fetch this metric.
Poller string `json:"poller,omitempty"`
// ID of this metric.
ID string `json:"id,omitempty"`
// Query is the query that was used to fetch this value.
// It will be different for individual implementations.
Query string `json:"query,omitempty"`
// Value is the value of the metric.
// +kubebuilder:validation:Type:=number
// +kubebuilder:validation:Format:=float
Value resource.Quantity `json:"value,omitempty"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,44 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// PrometheusMetric is a configuration for polling from Prometheus
type PrometheusMetric struct {
// Metric is a configuration for polling from Prometheus
type Metric struct {
// ID of this metric.
// +kubebuilder:validation:Required
ID string `json:"id,omitempty"`
// Query is the query used to fetch the metric. It is a Go Template with Sprig functions binding.
// A `.data` of the cluster secret is passed inside as parameters.
// A `.namespace`, `.name` and `.server` are available for the template.
// +kubebuilder:validation:Required
Query string `json:"query,omitempty"`
// Value is the value of the metric.
// NoData if set will be used as the value when no data is available in Prometheus.
// If not set - missing data will result in error.
// There are metrics like argocd_app_k8s_request_total that might have a bug and not being reported for prolonged periods of time.
// +kubebuilder:validation:Type:=number
// +kubebuilder:validation:Format:=float
Value resource.Quantity `json:"value,omitempty"`
NoData *resource.Quantity `json:"noData,omitempty"`
}

// PrometheusSource is a configuration for polling from Prometheus
type PrometheusSource struct {
// PrometheusPollSpec defines the configuration for this poller.
type PrometheusPollSpec struct {
// InitialDelay is the initial delay before polling after creating a poll or adding a new cluster.
// +kubebuilder:validation:Required
InitialDelay metav1.Duration `json:"initialDelay,omitempty"`
// Period is the period of polling.
// +kubebuilder:validation:Required
Period metav1.Duration `json:"period,omitempty"`
// Address is the address of the Prometheus server.
// +kubebuilder:validation:Required
Address string `json:"address,omitempty"`
// Metrics is the list of metrics to poll from Prometheus.
Metrics []PrometheusMetric `json:"metrics,omitempty"`
}

// PollSpec defines the configuration for this poller.
type PollSpec struct {
// Period is the period of polling.
Period metav1.Duration `json:"period,omitempty"`
// PrometheusSource is the configuration for polling from Prometheus.
PrometheusSource *PrometheusSource `json:"prometheusSource,omitempty"`
// +kubebuilder:validation:Required
Metrics []Metric `json:"metrics,omitempty"`
}

// MetricValue is a resulting value from Prometheus
type MetricValue struct {
// Poller is the identification of the poller that was used to fetch this metric.
Poller string `json:"poller,omitempty"`
// ID of this metric.
ID string `json:"id,omitempty"`
// Query is the query that was used to fetch this value.
// It will be different for individual implementations.
Query string `json:"query,omitempty"`
// Value is the value of the metric.
// +kubebuilder:validation:Type:=number
// +kubebuilder:validation:Format:=float
Value resource.Quantity `json:"value,omitempty"`
}

// PollStatus defines the observed state of Poll.
type PollStatus struct {
// PrometheusPollStatus defines the observed state of PrometheusPoll.
type PrometheusPollStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
// Values of the metrics polled.
// +kubebuilder:validation:Type:=number
// +kubebuilder:validation:Format:=float
Values []resource.Quantity `json:"values,omitempty"`
Values []MetricValue `json:"values,omitempty"`
// LastCalculatedTime is the last time the polling was performed for this configuration.
LastPollingTime *metav1.Time `json:"lastPollingTime,omitempty"`
}
Expand All @@ -83,24 +70,24 @@ type PollStatus struct {
// +kubebuilder:printcolumn:name="Last Polled",type="date",JSONPath=".status.lastPollingTime",description="Since last poll"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time since creation"

// Poll is the Schema for the polls API.
type Poll struct {
// PrometheusPoll is the Schema for the prometheuspolls API.
type PrometheusPoll struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec PollSpec `json:"spec,omitempty"`
Status PollStatus `json:"status,omitempty"`
Spec PrometheusPollSpec `json:"spec,omitempty"`
Status PrometheusPollStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&Poll{}, &PollList{})
SchemeBuilder.Register(&PrometheusPoll{}, &PrometheusPollList{})
}
111 changes: 48 additions & 63 deletions api/autoscaler/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ func main() {
os.Exit(1)
}

if err = (&autoscalercontroller.PollReconciler{
if err = (&autoscalercontroller.PrometheusPollReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Poll")
setupLog.Error(err, "unable to create controller", "controller", "PrometheusPoll")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
Expand Down
Loading

0 comments on commit d4db18a

Please sign in to comment.