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

Simultaneously support versions v2 and v2beta2 of Autoscaling #1014

Merged
merged 18 commits into from
Aug 18, 2022

Conversation

kevinearls
Copy link
Member

@kevinearls kevinearls commented Aug 1, 2022

Signed-off-by: Kevin Earls kearls@redhat.com

This is to resolve #943. Choose which version of autoscaling to use depending on what is available at runtime. We need to be able to support both v2 and v2beta2

Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
@kevinearls kevinearls changed the title Update to v2beta2 Simultaneously support versions v2 and v2beta2 of Autoscaling Aug 4, 2022
@kevinearls kevinearls marked this pull request as ready for review August 4, 2022 12:36
@kevinearls kevinearls requested a review from a team August 4, 2022 12:36
@kevinearls
Copy link
Member Author

@pavolloffay Please review. BTW should I need to ask for a review if it already says "open-telemetry/operator-approvers was requested for review". I don't want to be spamming you unnecessarily.

return version.Version, nil
}
}
return "v2beta2", nil
Copy link
Member

Choose a reason for hiding this comment

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

Returning this just like this does not seem right. It should be returned only if that version is present in the cluster.

@@ -51,66 +54,124 @@ func HorizontalPodAutoscalers(ctx context.Context, params Params) error {
return nil
}

func expectedHorizontalPodAutoscalers(ctx context.Context, params Params, expected []autoscalingv1.HorizontalPodAutoscaler) error {
func expectedHorizontalPodAutoscalers(ctx context.Context, params Params, expected []runtime.Object) error {
Copy link
Member

Choose a reason for hiding this comment

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

can we dry this method? I don't like that there is too much repetition/copy-paste for two different versions

Copy link
Member

Choose a reason for hiding this comment

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

Something like the following could work:

func expectedHorizontalPodAutoscalers(ctx context.Context, params Params, expected []runtime.Object) error {
	autoscalingVersion := params.Config.AutoscalingVersion()
	var existing client.Object
	if autoscalingVersion == config.AutoscalingVersionV2Beta2 {
		existing = &autoscalingv2beta2.HorizontalPodAutoscaler{}
	} else {
		existing = &autoscalingv2.HorizontalPodAutoscaler{}
	}

	for _, obj := range expected {
		desired, _ := meta.Accessor(obj)

		if err := controllerutil.SetControllerReference(&params.Instance, desired, params.Scheme); err != nil {
			return fmt.Errorf("failed to set controller reference: %w", err)
		}

		nns := types.NamespacedName{Namespace: desired.GetNamespace(), Name: desired.GetName()}
		err := params.Client.Get(ctx, nns, existing)
		if k8serrors.IsNotFound(err) {
			if err := params.Client.Create(ctx, obj.(client.Object)); err != nil {
				return fmt.Errorf("failed to create: %w", err)
			}
			params.Log.V(2).Info("created", "hpa.name", desired.GetName(), "hpa.namespace", desired.GetNamespace())
			continue
		} else if err != nil {
			return fmt.Errorf("failed to get %w", err)
		}

		existing.SetOwnerReferences(desired.GetOwnerReferences())
		setAutoscalerSpec(params.Instance, existing)

		annos := existing.GetAnnotations()
		for k, v := range desired.GetAnnotations() {
			annos[k] = v
		}
		existing.SetAnnotations(annos)
		labels := existing.GetLabels()
		for k, v := range desired.GetLabels() {
			labels[k] = v
		}
		existing.SetLabels(labels)

		patch := client.MergeFrom(existing)

		if err := params.Client.Patch(ctx, existing, patch); err != nil {
			return fmt.Errorf("failed to apply changes: %w", err)
		}

		params.Log.V(2).Info("applied", "hpa.name", desired.Name, "hpa.namespace", desired.Namespace)
	}

	return nil
}

Copy link
Member

Choose a reason for hiding this comment

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

@kevinearls did you try this de-duplication?

Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
return version.Version, nil
}
}
return "", errors.New("Failed to find appropriate version of apiGroup autoscaling")
Copy link
Member

Choose a reason for hiding this comment

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

We could include which versions are "appropriate".

@@ -26,30 +29,78 @@ import (

const defaultCPUTarget int32 = 90

func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelemetryCollector) autoscalingv1.HorizontalPodAutoscaler {
func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelemetryCollector) runtime.Object {
Copy link
Member

Choose a reason for hiding this comment

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

instread of returning runtime.Object wouldn't it be better to return client.Object? That way we don't have to cast this in the reconcile.

hpaVersion, err := c.autoDetect.HPAVersion()
if err != nil {
return err
} else {
Copy link
Member

Choose a reason for hiding this comment

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

no need for else branch

@@ -51,66 +54,124 @@ func HorizontalPodAutoscalers(ctx context.Context, params Params) error {
return nil
}

func expectedHorizontalPodAutoscalers(ctx context.Context, params Params, expected []autoscalingv1.HorizontalPodAutoscaler) error {
func expectedHorizontalPodAutoscalers(ctx context.Context, params Params, expected []runtime.Object) error {
Copy link
Member

Choose a reason for hiding this comment

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

@kevinearls did you try this de-duplication?

Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
@kevinearls
Copy link
Member Author

@pavolloffay Yes, I have now added the de duplication code. The original code you posted required some changes.

@@ -30,6 +30,9 @@ const (
defaultAutoDetectFrequency = 5 * time.Second
defaultCollectorConfigMapEntry = "collector.yaml"
defaultTargetAllocatorConfigMapEntry = "targetallocator.yaml"
AutoscalingVersionV2 = "v2"
Copy link
Member

Choose a reason for hiding this comment

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

nit: define these as enum (a golang type)

Copy link
Member Author

Choose a reason for hiding this comment

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

@pavolloffay Can you point me to an example of what you want here? Googling "golang enums" returns a bunch of disparate answers

Signed-off-by: Kevin Earls <kearls@redhat.com>
@kevinearls
Copy link
Member Author

@pavolloffay Can you review this? I'd like to get this merged before you go on PTO.

if err != nil {
return err
}
autoscalingVersion := r.config.AutoscalingVersion()
Copy link
Member

Choose a reason for hiding this comment

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

nit: move this closer to the place where it is used - line 192

@@ -102,6 +102,10 @@ type mockAutoDetect struct {
PlatformFunc func() (platform.Platform, error)
}

func (m *mockAutoDetect) HPAVersion() (autodetect.AutoscalingVersion, error) {
return autodetect.DefaultAutoscalingVersion, nil // TODO add a test?
Copy link
Member

Choose a reason for hiding this comment

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

nit: remove todo?

Signed-off-by: Kevin Earls <kearls@redhat.com>
@pavolloffay pavolloffay merged commit cb91777 into open-telemetry:main Aug 18, 2022
@kevinearls kevinearls deleted the hpa-support-v2-and-v2beta branch August 18, 2022 07:34
ItielOlenick pushed a commit to ItielOlenick/opentelemetry-operator that referenced this pull request May 1, 2024
…elemetry#1014)

* Update to v2beta2

Signed-off-by: Kevin Earls <kearls@redhat.com>

* First attempt at supporting v2 and v2beta2 of autoscaling

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Fix ErrorF statement

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Adding whitespace to rerun CI

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Make the stupid linter happy

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Just run on 1.24 to get full test results

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Just run on 1.19 to get full test results

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Cleanup, remove extraneous autodetect() calls, run only on 1.24

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Remove redundant builder.Owns call

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Start of cleanup

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Appease the stupid linter

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Cleanup

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Check before returning v2beta2, reduce copy/pasted code

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Back out DRY changes

Signed-off-by: Kevin Earls <kearls@redhat.com>

* reduc copy/pasted code

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Respond to comments

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Implement autoscaling version constants as an enum

Signed-off-by: Kevin Earls <kearls@redhat.com>

* remove TODO, move declaration

Signed-off-by: Kevin Earls <kearls@redhat.com>

Signed-off-by: Kevin Earls <kearls@redhat.com>
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.

Update HorizontalPodAutoscaler code from v1
2 participants