Skip to content
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

[HPA] Add v1alpha1.MetricSpec to support for Pod custom metrics #1651

Merged

Conversation

moh-osman3
Copy link
Contributor

@moh-osman3 moh-osman3 commented Apr 13, 2023

Replaces #1561

This PR adds a new object v1alpha1.MetricSpec which is a subset of the metric types in https://pkg.go.dev/k8s.io/api/autoscaling/v2#MetricSpec

Currently v1alpha1.MetricSpec only supports PodsMetricsSource but can support more metric types in the metric array in the future. For example supporting ResourceMetricsSource in the future would allow resources to be configured in the HPA's metric array and allow us to deprecate targetCPUUtilization and targetMemoryUtilization

Resolves #1560

@moh-osman3 moh-osman3 marked this pull request as ready for review April 13, 2023 09:33
@moh-osman3 moh-osman3 requested a review from a team April 13, 2023 09:33
apis/v1alpha1/opentelemetrycollector_webhook.go Outdated Show resolved Hide resolved
config/manager/kustomization.yaml Outdated Show resolved Hide resolved
@@ -86,11 +86,17 @@ func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1al
},
}

if otelcol.Spec.Autoscaler != nil && otelcol.Spec.Autoscaler.Behavior != nil {
if otelcol.Spec.Autoscaler.Behavior != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have we confirmed that Autoscaler isn't nil by this point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm there are several references of using otelcol.Spec.Autoscaler without a nil check in this function. In fact this is the only place that nil checks. If it is unset, otelcol.Spec.Autoscaler is created by the defaulting webhook at https://github.com/open-telemetry/opentelemetry-operator/blob/main/apis/v1alpha1/opentelemetrycollector_webhook.go#L72

So it should never be nil, but now I'm wondering if there cases where the defaulting webhook is never called and this causes a segfault?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats a good q... maybe we add the guard at the start of the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the nil check at the start and returning nil if it is unset!

behavior := ConvertToV2beta2Behavior(*otelcol.Spec.Autoscaler.Behavior)
autoscaler.Spec.Behavior = &behavior
}

// check for custom metrics
if len(otelcol.Spec.Autoscaler.Metrics) > 0 {
metrics := ConvertToV2Beta2PodMetrics(otelcol.Spec.Autoscaler.Metrics)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we able to make this addition to confirm to the v2 spec to start with? Or given it's already using something similar to the v1alpha we want to keep it that way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm maybe i'm not understanding what this should be doing. What's the type of autoscaler.Spec.Metrics and how does that diff from what you're adding in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otelcol.Spec.Autoscaler.Metrics has type v1alpha1.MetricSpec this supports only the PodsMetricSource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HPA is either expecting a v2.MetricSpec or a v2beta2.MetricSpec so this does the necessary conversion for the HPA object depending on the version. This is something similar done for .Spec.Autoscaler.Behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I created v1alpha1.MetricSpec instead of directly using a v2.MetricSpec is because v2.MetricSpec is way too large of an object for the use cases of our HPA (see https://pkg.go.dev/k8s.io/api/autoscaling/v2#MetricSpec). It supports 5 different metric types which would require a lot of validation and the deprecation of targetCPUUtilization and targetMemoryUtilization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1alpha1.MetricSpec is much smaller and can be updated as more functionality is required. I'm totally open to suggestions to change the type of otelcol.Spec.Autoscaler.Metrics though if that's still unclear!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, makes a ton of sense. thanks for explaining!

@@ -184,6 +185,12 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {
}
}

if r.Spec.Ingress.Type == IngressTypeNginx && r.Spec.Mode == ModeSidecar {
return fmt.Errorf("the OptenTelemetry Spec Ingress configuiration is incorrect. Ingress can only be used in combination with the modes: %s, %s, %s",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("the OptenTelemetry Spec Ingress configuiration is incorrect. Ingress can only be used in combination with the modes: %s, %s, %s",
return fmt.Errorf("the OptenTelemetry Spec Ingress configuration is incorrect. Ingress can only be used in combination with the modes: %s, %s, %s",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! Fixed.

@moh-osman3 moh-osman3 force-pushed the mohosman/issue-1560/HPA-pod-metrics branch from f5e0250 to 23109cd Compare May 17, 2023 18:54
@moh-osman3 moh-osman3 force-pushed the mohosman/issue-1560/HPA-pod-metrics branch from 23109cd to e178a4f Compare May 22, 2023 19:15
@jaronoff97 jaronoff97 merged commit 88022b4 into open-telemetry:main May 22, 2023
ItielOlenick pushed a commit to ItielOlenick/opentelemetry-operator that referenced this pull request May 1, 2024
…-telemetry#1651)

* add to api types and validate webhook

* creation of metrics

* chloggen and make generate

* new MetricSpec

* make bundle and api-docs

* lint

* add webhook test case

* make bundle

* updates

* address review feedback

* gofmt

* add nil check and reorder validation

* fix typo

* lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[HPA] support Pod custom metrics in autoscaler spec
3 participants