-
Notifications
You must be signed in to change notification settings - Fork 459
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
Simultaneously support versions v2 and v2beta2 of Autoscaling #1014
Conversation
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>
@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. |
pkg/autodetect/main.go
Outdated
return version.Version, nil | ||
} | ||
} | ||
return "v2beta2", nil |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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(¶ms.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
}
There was a problem hiding this comment.
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>
pkg/autodetect/main.go
Outdated
return version.Version, nil | ||
} | ||
} | ||
return "", errors.New("Failed to find appropriate version of apiGroup autoscaling") |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
internal/config/main.go
Outdated
hpaVersion, err := c.autoDetect.HPAVersion() | ||
if err != nil { | ||
return err | ||
} else { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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>
@pavolloffay Yes, I have now added the de duplication code. The original code you posted required some changes. |
internal/config/main.go
Outdated
@@ -30,6 +30,9 @@ const ( | |||
defaultAutoDetectFrequency = 5 * time.Second | |||
defaultCollectorConfigMapEntry = "collector.yaml" | |||
defaultTargetAllocatorConfigMapEntry = "targetallocator.yaml" | |||
AutoscalingVersionV2 = "v2" |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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>
@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() |
There was a problem hiding this comment.
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
internal/config/main_test.go
Outdated
@@ -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? |
There was a problem hiding this comment.
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>
…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>
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