diff --git a/.chloggen/bump-dotnet-1.0.1.yaml b/.chloggen/bump-dotnet-1.0.2.yaml similarity index 89% rename from .chloggen/bump-dotnet-1.0.1.yaml rename to .chloggen/bump-dotnet-1.0.2.yaml index 4ec74f3012..b2e14cc9e7 100644 --- a/.chloggen/bump-dotnet-1.0.1.yaml +++ b/.chloggen/bump-dotnet-1.0.2.yaml @@ -5,10 +5,10 @@ change_type: enhancement component: autoinstrumentation # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: Bump OpenTelemetry .NET Automatic Instrumentation to 1.0.1 +note: Bump OpenTelemetry .NET Automatic Instrumentation to 1.0.2 # One or more tracking issues related to the change -issues: [2142] +issues: [2168] # (Optional) One or more lines of additional information to render under the primary note. # These lines will be padded with 2 spaces and then inserted directly into the document. diff --git a/.chloggen/discoverymanager_metrics.yaml b/.chloggen/discoverymanager_metrics.yaml new file mode 100755 index 0000000000..7bfe64534e --- /dev/null +++ b/.chloggen/discoverymanager_metrics.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action) +component: target allocator + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Enable discovery manager metrics in target allocator + +# One or more tracking issues related to the change +issues: [2170] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/.chloggen/get-rid-of-autoscaling-v2beta2.yaml b/.chloggen/get-rid-of-autoscaling-v2beta2.yaml new file mode 100644 index 0000000000..554ccd15e4 --- /dev/null +++ b/.chloggen/get-rid-of-autoscaling-v2beta2.yaml @@ -0,0 +1,20 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: breaking + +# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action) +component: operator + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Get rid of autoscaling/v2beta2 + +# One or more tracking issues related to the change +issues: + - 2145 + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: | + Kubernetes 1.23 is the minimum available version everywhere after 1.22 deprecation, + due to it, the minimum required version has been updated to it, dropping support for + autoscaling/v2beta2 diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 0938393168..fa3acafe76 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -10,10 +10,6 @@ concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} cancel-in-progress: true -env: - CURRENT_KIND_VERSION: 0.20.0 - LEGACY_KIND_VERSION: 0.17.0 # for K8s versions latest kind doesn't support anymore - jobs: e2e-tests: name: End-to-end tests @@ -25,7 +21,7 @@ jobs: # All Kubernetes version in between expose the same APIs, hence the operator # should be compatible with them. kube-version: - - "1.19" + - "1.23" - "1.28" group: - e2e @@ -42,7 +38,7 @@ jobs: - name: Setup kind env: - KIND_VERSION: ${{ matrix.kube_version >= 1.21 && env.KIND_VERSION || env.LEGACY_KIND_VERSION }} + KIND_VERSION: "0.20.0" run: go install sigs.k8s.io/kind@v${KIND_VERSION} - name: Check out code into the Go module directory diff --git a/.github/workflows/publish-autoinstrumentation-e2e-images.yaml b/.github/workflows/publish-autoinstrumentation-e2e-images.yaml index a6878ec8b7..f64900e07e 100644 --- a/.github/workflows/publish-autoinstrumentation-e2e-images.yaml +++ b/.github/workflows/publish-autoinstrumentation-e2e-images.yaml @@ -25,7 +25,7 @@ jobs: - uses: actions/checkout@v4 - name: Set up QEMU - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 diff --git a/.github/workflows/scorecard.yaml b/.github/workflows/scorecard.yaml index 6090e640fa..10e6a2d0eb 100644 --- a/.github/workflows/scorecard.yaml +++ b/.github/workflows/scorecard.yaml @@ -10,10 +10,6 @@ concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} cancel-in-progress: true -env: - CURRENT_KIND_VERSION: 0.20.0 - LEGACY_KIND_VERSION: 0.17.0 # for K8s versions latest kind doesn't support anymore - jobs: scorecard-tests: name: test on k8s @@ -21,7 +17,7 @@ jobs: strategy: matrix: kube-version: - - "1.19" + - "1.23" - "1.28" steps: @@ -33,7 +29,7 @@ jobs: - name: Setup kind env: - KIND_VERSION: ${{ matrix.kube_version >= 1.21 && env.KIND_VERSION || env.LEGACY_KIND_VERSION }} + KIND_VERSION: "0.20.0" run: go install sigs.k8s.io/kind@v${KIND_VERSION} - name: Check out code into the Go module directory diff --git a/autoinstrumentation/dotnet/version.txt b/autoinstrumentation/dotnet/version.txt index 7dea76edb3..6d7de6e6ab 100644 --- a/autoinstrumentation/dotnet/version.txt +++ b/autoinstrumentation/dotnet/version.txt @@ -1 +1 @@ -1.0.1 +1.0.2 diff --git a/cmd/otel-allocator/go.mod b/cmd/otel-allocator/go.mod index ddb7f8b587..801bcbed63 100644 --- a/cmd/otel-allocator/go.mod +++ b/cmd/otel-allocator/go.mod @@ -16,7 +16,7 @@ require ( github.com/prometheus-operator/prometheus-operator v0.68.0 github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.68.0 github.com/prometheus-operator/prometheus-operator/pkg/client v0.68.0 - github.com/prometheus/client_golang v1.16.0 + github.com/prometheus/client_golang v1.17.0 github.com/prometheus/common v0.44.0 github.com/prometheus/prometheus v0.47.0 github.com/spf13/pflag v1.0.5 @@ -155,9 +155,9 @@ require ( github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus-community/prom-label-proxy v0.7.0 // indirect github.com/prometheus/alertmanager v0.26.0 // indirect - github.com/prometheus/client_model v0.4.0 // indirect + github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect github.com/prometheus/common/sigv4 v0.1.0 // indirect - github.com/prometheus/procfs v0.11.0 // indirect + github.com/prometheus/procfs v0.11.1 // indirect github.com/scaleway/scaleway-sdk-go v1.0.0-beta.20 // indirect github.com/spf13/cobra v1.7.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect diff --git a/cmd/otel-allocator/go.sum b/cmd/otel-allocator/go.sum index 11685f23c9..cc5a1bdf20 100644 --- a/cmd/otel-allocator/go.sum +++ b/cmd/otel-allocator/go.sum @@ -589,14 +589,14 @@ github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3O github.com/prometheus/client_golang v1.5.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0= -github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= -github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc= +github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q= +github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY= -github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU= +github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 h1:v7DLqVdK4VrYkVD5diGdl4sxJurKJEMnODWRJlxV9oM= +github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= @@ -611,8 +611,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= -github.com/prometheus/procfs v0.11.0 h1:5EAgkfkMl659uZPbe9AS2N68a7Cc1TJbPEuGzFuRbyk= -github.com/prometheus/procfs v0.11.0/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= +github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI= +github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY= github.com/prometheus/prometheus v0.47.0 h1:tIJJKZGlmrMVsvIt6rMfB8he7CRHEc8ZxS5ubcZtbkM= github.com/prometheus/prometheus v0.47.0/go.mod h1:J/bmOSjgH7lFxz2gZhrWEZs2i64vMS+HIuZfmYNhJ/M= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= diff --git a/cmd/otel-allocator/main.go b/cmd/otel-allocator/main.go index 0a7cf8dad8..297b7d58da 100644 --- a/cmd/otel-allocator/main.go +++ b/cmd/otel-allocator/main.go @@ -90,6 +90,8 @@ func main() { discoveryCtx, discoveryCancel := context.WithCancel(ctx) discoveryManager = discovery.NewManager(discoveryCtx, gokitlog.NewNopLogger()) + discovery.RegisterMetrics() // discovery manager metrics need to be enabled explicitly + targetDiscoverer = target.NewDiscoverer(log, discoveryManager, allocatorPrehook, srv) collectorWatcher, collectorWatcherErr := collector.NewClient(log, cfg.ClusterConfig) if collectorWatcherErr != nil { diff --git a/controllers/opentelemetrycollector_controller.go b/controllers/opentelemetrycollector_controller.go index 65caeb303c..4cde37c461 100644 --- a/controllers/opentelemetrycollector_controller.go +++ b/controllers/opentelemetrycollector_controller.go @@ -25,7 +25,6 @@ import ( monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" appsv1 "k8s.io/api/apps/v1" autoscalingv2 "k8s.io/api/autoscaling/v2" - autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -304,12 +303,7 @@ func (r *OpenTelemetryCollectorReconciler) SetupWithManager(mgr ctrl.Manager) er builder.Owns(&monitoringv1.ServiceMonitor{}) } - autoscalingVersion := r.config.AutoscalingVersion() - if autoscalingVersion == autodetect.AutoscalingVersionV2 { - builder = builder.Owns(&autoscalingv2.HorizontalPodAutoscaler{}) - } else { - builder = builder.Owns(&autoscalingv2beta2.HorizontalPodAutoscaler{}) - } + builder = builder.Owns(&autoscalingv2.HorizontalPodAutoscaler{}) return builder.Complete(r) } diff --git a/controllers/opentelemetrycollector_controller_test.go b/controllers/opentelemetrycollector_controller_test.go index 27441d45f4..261115c088 100644 --- a/controllers/opentelemetrycollector_controller_test.go +++ b/controllers/opentelemetrycollector_controller_test.go @@ -44,9 +44,6 @@ import ( var logger = logf.Log.WithName("unit-tests") var mockAutoDetector = &mockAutoDetect{ - HPAVersionFunc: func() (autodetect.AutoscalingVersion, error) { - return autodetect.AutoscalingVersionV2Beta2, nil - }, OpenShiftRoutesAvailabilityFunc: func() (autodetect.OpenShiftRoutesAvailability, error) { return autodetect.OpenShiftRoutesAvailable, nil }, @@ -336,11 +333,6 @@ var _ autodetect.AutoDetect = (*mockAutoDetect)(nil) type mockAutoDetect struct { OpenShiftRoutesAvailabilityFunc func() (autodetect.OpenShiftRoutesAvailability, error) - HPAVersionFunc func() (autodetect.AutoscalingVersion, error) -} - -func (m *mockAutoDetect) HPAVersion() (autodetect.AutoscalingVersion, error) { - return m.HPAVersionFunc() } func (m *mockAutoDetect) OpenShiftRoutesAvailability() (autodetect.OpenShiftRoutesAvailability, error) { diff --git a/controllers/reconcile_test.go b/controllers/reconcile_test.go index 3a87607e45..03ca153b1d 100644 --- a/controllers/reconcile_test.go +++ b/controllers/reconcile_test.go @@ -38,7 +38,6 @@ import ( "github.com/open-telemetry/opentelemetry-operator/internal/manifests" ta "github.com/open-telemetry/opentelemetry-operator/internal/manifests/targetallocator/adapters" "github.com/open-telemetry/opentelemetry-operator/internal/naming" - "github.com/open-telemetry/opentelemetry-operator/pkg/autodetect" ) const ( @@ -276,54 +275,11 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) { }, }, }, - { - name: "hpa v2beta2 deployment collector", - args: args{ - params: paramsWithHPA(autodetect.AutoscalingVersionV2Beta2, 3, 5), - updates: []manifests.Params{paramsWithHPA(autodetect.AutoscalingVersionV2Beta2, 1, 9)}, - }, - want: []want{ - { - result: controllerruntime.Result{}, - checks: []check{ - func(t *testing.T, appliedInstance v1alpha1.OpenTelemetryCollector) { - actual := autoscalingv2beta2.HorizontalPodAutoscaler{} - exists, hpaErr := populateObjectIfExists(t, &actual, namespacedObjectName(appliedInstance, naming.HorizontalPodAutoscaler)) - assert.NoError(t, hpaErr) - require.Len(t, actual.Spec.Metrics, 1) - assert.Equal(t, int32(90), *actual.Spec.Metrics[0].Resource.Target.AverageUtilization) - assert.Equal(t, int32(3), *actual.Spec.MinReplicas) - assert.Equal(t, int32(5), actual.Spec.MaxReplicas) - assert.True(t, exists) - }, - }, - wantErr: assert.NoError, - validateErr: assert.NoError, - }, - { - result: controllerruntime.Result{}, - checks: []check{ - func(t *testing.T, appliedInstance v1alpha1.OpenTelemetryCollector) { - actual := autoscalingv2beta2.HorizontalPodAutoscaler{} - exists, hpaErr := populateObjectIfExists(t, &actual, namespacedObjectName(appliedInstance, naming.HorizontalPodAutoscaler)) - assert.NoError(t, hpaErr) - require.Len(t, actual.Spec.Metrics, 1) - assert.Equal(t, int32(90), *actual.Spec.Metrics[0].Resource.Target.AverageUtilization) - assert.Equal(t, int32(1), *actual.Spec.MinReplicas) - assert.Equal(t, int32(9), actual.Spec.MaxReplicas) - assert.True(t, exists) - }, - }, - wantErr: assert.NoError, - validateErr: assert.NoError, - }, - }, - }, { name: "hpa v2 deployment collector", args: args{ - params: paramsWithHPA(autodetect.AutoscalingVersionV2, 3, 5), - updates: []manifests.Params{paramsWithHPA(autodetect.AutoscalingVersionV2, 1, 9)}, + params: paramsWithHPA(3, 5), + updates: []manifests.Params{paramsWithHPA(1, 9)}, }, want: []want{ { diff --git a/controllers/suite_test.go b/controllers/suite_test.go index bf9bbc1efe..d44eb5f2bd 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -49,7 +49,6 @@ import ( "github.com/open-telemetry/opentelemetry-operator/internal/config" "github.com/open-telemetry/opentelemetry-operator/internal/manifests" "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/testdata" - "github.com/open-telemetry/opentelemetry-operator/pkg/autodetect" // +kubebuilder:scaffold:imports ) @@ -277,7 +276,7 @@ func newParams(taContainerImage string, file string) (manifests.Params, error) { }, nil } -func paramsWithHPA(autoscalingVersion autodetect.AutoscalingVersion, minReps, maxReps int32) manifests.Params { +func paramsWithHPA(minReps, maxReps int32) manifests.Params { configYAML, err := os.ReadFile("testdata/test.yaml") if err != nil { fmt.Printf("Error getting yaml file: %v", err) @@ -285,16 +284,7 @@ func paramsWithHPA(autoscalingVersion autodetect.AutoscalingVersion, minReps, ma cpuUtilization := int32(90) - mockAutoDetector := &mockAutoDetect{ - HPAVersionFunc: func() (autodetect.AutoscalingVersion, error) { - return autoscalingVersion, nil - }, - } - configuration := config.New(config.WithAutoDetect(mockAutoDetector), config.WithCollectorImage(defaultCollectorImage), config.WithTargetAllocatorImage(defaultTaAllocationImage)) - err = configuration.AutoDetect() - if err != nil { - logger.Error(err, "configuration.autodetect failed") - } + configuration := config.New(config.WithCollectorImage(defaultCollectorImage), config.WithTargetAllocatorImage(defaultTaAllocationImage)) return manifests.Params{ Config: configuration, diff --git a/go.mod b/go.mod index 7055c7f2bc..58f9fa7855 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/collector/featuregate v0.77.0 - go.opentelemetry.io/otel v1.18.0 + go.opentelemetry.io/otel v1.19.0 gopkg.in/yaml.v2 v2.4.0 k8s.io/api v0.28.2 k8s.io/apiextensions-apiserver v0.28.2 @@ -133,7 +133,7 @@ require ( github.com/spf13/cobra v1.7.0 // indirect github.com/vultr/govultr/v2 v2.17.2 // indirect go.opencensus.io v0.24.0 // indirect - go.opentelemetry.io/otel/trace v1.18.0 // indirect + go.opentelemetry.io/otel/trace v1.19.0 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.25.0 // indirect golang.org/x/crypto v0.13.0 // indirect diff --git a/go.sum b/go.sum index 3e93d7b6c0..bf42bf712a 100644 --- a/go.sum +++ b/go.sum @@ -531,10 +531,10 @@ go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/collector/featuregate v0.77.0 h1:m1/IzaXoQh6SgF6CM80vrBOCf5zSJ2GVISfA27fYzGU= go.opentelemetry.io/collector/featuregate v0.77.0/go.mod h1:/kVAsGUCyJXIDSgHftCN63QiwAEVHRLX2Kh/S+dqgHY= -go.opentelemetry.io/otel v1.18.0 h1:TgVozPGZ01nHyDZxK5WGPFB9QexeTMXEH7+tIClWfzs= -go.opentelemetry.io/otel v1.18.0/go.mod h1:9lWqYO0Db579XzVuCKFNPDl4s73Voa+zEck3wHaAYQI= -go.opentelemetry.io/otel/trace v1.18.0 h1:NY+czwbHbmndxojTEKiSMHkG2ClNH2PwmcHrdo0JY10= -go.opentelemetry.io/otel/trace v1.18.0/go.mod h1:T2+SGJGuYZY3bjj5rgh/hN7KIrlpWC5nS8Mjvzckz+0= +go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs= +go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY= +go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg= +go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= diff --git a/internal/config/main.go b/internal/config/main.go index 225f590f69..64d2beec8a 100644 --- a/internal/config/main.go +++ b/internal/config/main.go @@ -51,7 +51,6 @@ type Config struct { labelsFilter []string openshiftRoutes openshiftRoutesStore autoDetectFrequency time.Duration - hpaVersion hpaVersionStore } // New constructs a new configuration based on the given options. @@ -63,7 +62,6 @@ func New(opts ...Option) Config { targetAllocatorConfigMapEntry: defaultTargetAllocatorConfigMapEntry, logger: logf.Log.WithName("config"), openshiftRoutes: newOpenShiftRoutesWrapper(), - hpaVersion: newHPAVersionWrapper(), version: version.Get(), onOpenShiftRoutesChange: newOnChange(), } @@ -81,7 +79,6 @@ func New(opts ...Option) Config { targetAllocatorConfigMapEntry: o.targetAllocatorConfigMapEntry, logger: o.logger, openshiftRoutes: o.openshiftRoutes, - hpaVersion: o.hpaVersion, onOpenShiftRoutesChange: o.onOpenShiftRoutesChange, autoInstrumentationJavaImage: o.autoInstrumentationJavaImage, autoInstrumentationNodeJSImage: o.autoInstrumentationNodeJSImage, @@ -129,15 +126,6 @@ func (c *Config) AutoDetect() error { } } - hpaV, err := c.autoDetect.HPAVersion() - if err != nil { - return err - } - if c.hpaVersion.Get() != hpaV { - c.logger.V(1).Info("HPA version detected", "version", hpaV) - c.hpaVersion.Set(hpaV) - } - return nil } @@ -166,11 +154,6 @@ func (c *Config) OpenShiftRoutes() autodetect.OpenShiftRoutesAvailability { return c.openshiftRoutes.Get() } -// AutoscalingVersion represents the preferred version of autoscaling. -func (c *Config) AutoscalingVersion() autodetect.AutoscalingVersion { - return c.hpaVersion.Get() -} - // AutoInstrumentationJavaImage returns OpenTelemetry Java auto-instrumentation container image. func (c *Config) AutoInstrumentationJavaImage() string { return c.autoInstrumentationJavaImage @@ -212,35 +195,6 @@ func (c *Config) RegisterOpenShiftRoutesChangeCallback(f func() error) { c.onOpenShiftRoutesChange.Register(f) } -type hpaVersionStore interface { - Set(hpaV autodetect.AutoscalingVersion) - Get() autodetect.AutoscalingVersion -} - -func newHPAVersionWrapper() hpaVersionStore { - return &hpaVersionWrapper{ - current: autodetect.AutoscalingVersionUnknown, - } -} - -type hpaVersionWrapper struct { - mu sync.Mutex - current autodetect.AutoscalingVersion -} - -func (p *hpaVersionWrapper) Set(hpaV autodetect.AutoscalingVersion) { - p.mu.Lock() - p.current = hpaV - p.mu.Unlock() -} - -func (p *hpaVersionWrapper) Get() autodetect.AutoscalingVersion { - p.mu.Lock() - hpaV := p.current - p.mu.Unlock() - return hpaV -} - type openshiftRoutesStore interface { Set(ora autodetect.OpenShiftRoutesAvailability) Get() autodetect.OpenShiftRoutesAvailability diff --git a/internal/config/main_test.go b/internal/config/main_test.go index e6085ead66..7bda3f64cd 100644 --- a/internal/config/main_test.go +++ b/internal/config/main_test.go @@ -38,7 +38,6 @@ func TestNewConfig(t *testing.T) { assert.Equal(t, "some-image", cfg.CollectorImage()) assert.Equal(t, "some-config.yaml", cfg.CollectorConfigMapEntry()) assert.Equal(t, autodetect.OpenShiftRoutesNotAvailable, cfg.OpenShiftRoutes()) - assert.Equal(t, autodetect.AutoscalingVersionUnknown, cfg.AutoscalingVersion()) } func TestOnPlatformChangeCallback(t *testing.T) { @@ -78,10 +77,6 @@ func TestAutoDetectInBackground(t *testing.T) { wg.Done() return autodetect.OpenShiftRoutesNotAvailable, nil }, - HPAVersionFunc: func() (autodetect.AutoscalingVersion, error) { - wg.Done() - return autodetect.AutoscalingVersionV2, nil - }, } cfg := config.New( config.WithAutoDetect(mock), @@ -90,7 +85,6 @@ func TestAutoDetectInBackground(t *testing.T) { // sanity check require.Equal(t, autodetect.OpenShiftRoutesNotAvailable, cfg.OpenShiftRoutes()) - require.Equal(t, autodetect.AutoscalingVersionUnknown, cfg.AutoscalingVersion()) // test err := cfg.StartAutoDetect() @@ -104,14 +98,6 @@ var _ autodetect.AutoDetect = (*mockAutoDetect)(nil) type mockAutoDetect struct { OpenShiftRoutesAvailabilityFunc func() (autodetect.OpenShiftRoutesAvailability, error) - HPAVersionFunc func() (autodetect.AutoscalingVersion, error) -} - -func (m *mockAutoDetect) HPAVersion() (autodetect.AutoscalingVersion, error) { - if m.HPAVersionFunc != nil { - return m.HPAVersionFunc() - } - return autodetect.DefaultAutoscalingVersion, nil } func (m *mockAutoDetect) OpenShiftRoutesAvailability() (autodetect.OpenShiftRoutesAvailability, error) { diff --git a/internal/config/options.go b/internal/config/options.go index 0219681414..13d88b508a 100644 --- a/internal/config/options.go +++ b/internal/config/options.go @@ -46,7 +46,6 @@ type options struct { onOpenShiftRoutesChange changeHandler labelsFilter []string openshiftRoutes openshiftRoutesStore - hpaVersion hpaVersionStore autoDetectFrequency time.Duration } diff --git a/internal/manifests/collector/horizontalpodautoscaler.go b/internal/manifests/collector/horizontalpodautoscaler.go index c06a16e22e..719e2bf9d3 100644 --- a/internal/manifests/collector/horizontalpodautoscaler.go +++ b/internal/manifests/collector/horizontalpodautoscaler.go @@ -17,7 +17,6 @@ package collector import ( "github.com/go-logr/logr" autoscalingv2 "k8s.io/api/autoscaling/v2" - autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -25,7 +24,6 @@ import ( "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/internal/config" "github.com/open-telemetry/opentelemetry-operator/internal/naming" - "github.com/open-telemetry/opentelemetry-operator/pkg/autodetect" ) func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelemetryCollector) client.Object { @@ -46,7 +44,6 @@ func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1al logger.Info("hpa field is unset in Spec, skipping autoscaler creation") return nil } - autoscalingVersion := cfg.AutoscalingVersion() if otelcol.Spec.Autoscaler.MaxReplicas == nil { otelcol.Spec.Autoscaler.MaxReplicas = otelcol.Spec.MaxReplicas @@ -60,225 +57,64 @@ func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1al } } - if autoscalingVersion == autodetect.AutoscalingVersionV2Beta2 { - metrics := []autoscalingv2beta2.MetricSpec{} + metrics := []autoscalingv2.MetricSpec{} - if otelcol.Spec.Autoscaler.TargetMemoryUtilization != nil { - utilizationTarget := autoscalingv2beta2.MetricSpec{ - Type: autoscalingv2beta2.ResourceMetricSourceType, - Resource: &autoscalingv2beta2.ResourceMetricSource{ - Name: corev1.ResourceMemory, - Target: autoscalingv2beta2.MetricTarget{ - Type: autoscalingv2beta2.UtilizationMetricType, - AverageUtilization: otelcol.Spec.Autoscaler.TargetMemoryUtilization, - }, + if otelcol.Spec.Autoscaler.TargetMemoryUtilization != nil { + memoryTarget := autoscalingv2.MetricSpec{ + Type: autoscalingv2.ResourceMetricSourceType, + Resource: &autoscalingv2.ResourceMetricSource{ + Name: corev1.ResourceMemory, + Target: autoscalingv2.MetricTarget{ + Type: autoscalingv2.UtilizationMetricType, + AverageUtilization: otelcol.Spec.Autoscaler.TargetMemoryUtilization, }, - } - metrics = append(metrics, utilizationTarget) + }, } + metrics = append(metrics, memoryTarget) + } - targetCPUUtilization := autoscalingv2beta2.MetricSpec{ - Type: autoscalingv2beta2.ResourceMetricSourceType, - Resource: &autoscalingv2beta2.ResourceMetricSource{ + if otelcol.Spec.Autoscaler.TargetCPUUtilization != nil { + cpuTarget := autoscalingv2.MetricSpec{ + Type: autoscalingv2.ResourceMetricSourceType, + Resource: &autoscalingv2.ResourceMetricSource{ Name: corev1.ResourceCPU, - Target: autoscalingv2beta2.MetricTarget{ - Type: autoscalingv2beta2.UtilizationMetricType, + Target: autoscalingv2.MetricTarget{ + Type: autoscalingv2.UtilizationMetricType, AverageUtilization: otelcol.Spec.Autoscaler.TargetCPUUtilization, }, }, } - metrics = append(metrics, targetCPUUtilization) - - autoscaler := autoscalingv2beta2.HorizontalPodAutoscaler{ - ObjectMeta: objectMeta, - Spec: autoscalingv2beta2.HorizontalPodAutoscalerSpec{ - ScaleTargetRef: autoscalingv2beta2.CrossVersionObjectReference{ - APIVersion: v1alpha1.GroupVersion.String(), - Kind: "OpenTelemetryCollector", - Name: naming.OpenTelemetryCollector(otelcol.Name), - }, - MinReplicas: otelcol.Spec.Autoscaler.MinReplicas, - MaxReplicas: *otelcol.Spec.Autoscaler.MaxReplicas, - Metrics: metrics, - }, - } - - if otelcol.Spec.Autoscaler.Behavior != nil { - 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) - autoscaler.Spec.Metrics = append(autoscaler.Spec.Metrics, metrics...) - } - - result = &autoscaler - } else { - metrics := []autoscalingv2.MetricSpec{} - - if otelcol.Spec.Autoscaler.TargetMemoryUtilization != nil { - utilizationTarget := autoscalingv2.MetricSpec{ - Type: autoscalingv2.ResourceMetricSourceType, - Resource: &autoscalingv2.ResourceMetricSource{ - Name: corev1.ResourceMemory, - Target: autoscalingv2.MetricTarget{ - Type: autoscalingv2.UtilizationMetricType, - AverageUtilization: otelcol.Spec.Autoscaler.TargetMemoryUtilization, - }, - }, - } - metrics = append(metrics, utilizationTarget) - } - - if otelcol.Spec.Autoscaler.TargetCPUUtilization != nil { - targetCPUUtilization := autoscalingv2.MetricSpec{ - Type: autoscalingv2.ResourceMetricSourceType, - Resource: &autoscalingv2.ResourceMetricSource{ - Name: corev1.ResourceCPU, - Target: autoscalingv2.MetricTarget{ - Type: autoscalingv2.UtilizationMetricType, - AverageUtilization: otelcol.Spec.Autoscaler.TargetCPUUtilization, - }, - }, - } - metrics = append(metrics, targetCPUUtilization) - } - - autoscaler := autoscalingv2.HorizontalPodAutoscaler{ - ObjectMeta: objectMeta, - Spec: autoscalingv2.HorizontalPodAutoscalerSpec{ - ScaleTargetRef: autoscalingv2.CrossVersionObjectReference{ - APIVersion: v1alpha1.GroupVersion.String(), - Kind: "OpenTelemetryCollector", - Name: naming.OpenTelemetryCollector(otelcol.Name), - }, - MinReplicas: otelcol.Spec.Autoscaler.MinReplicas, - MaxReplicas: *otelcol.Spec.Autoscaler.MaxReplicas, - Metrics: metrics, - }, - } - if otelcol.Spec.Autoscaler.Behavior != nil { - autoscaler.Spec.Behavior = otelcol.Spec.Autoscaler.Behavior - } - - // convert from v1alpha1.MetricSpec into a autoscalingv2.MetricSpec. - for _, metric := range otelcol.Spec.Autoscaler.Metrics { - if metric.Type == autoscalingv2.PodsMetricSourceType { - v2metric := autoscalingv2.MetricSpec{ - Type: metric.Type, - Pods: metric.Pods, - } - autoscaler.Spec.Metrics = append(autoscaler.Spec.Metrics, v2metric) - } // pod metrics - } - result = &autoscaler + metrics = append(metrics, cpuTarget) } - return result -} - -func ConvertToV2Beta2PodMetrics(v2metrics []v1alpha1.MetricSpec) []autoscalingv2beta2.MetricSpec { - metrics := make([]autoscalingv2beta2.MetricSpec, len(v2metrics)) - - for i, v2metric := range v2metrics { - metrics[i].Type = autoscalingv2beta2.MetricSourceType(v2metric.Type) - if v2metric.Pods != nil { - metrics[i].Pods = &autoscalingv2beta2.PodsMetricSource{ - Metric: autoscalingv2beta2.MetricIdentifier{ - Name: v2metric.Pods.Metric.Name, - Selector: v2metric.Pods.Metric.Selector, - }, - Target: autoscalingv2beta2.MetricTarget{ - Type: autoscalingv2beta2.MetricTargetType(v2metric.Pods.Target.Type), - AverageValue: v2metric.Pods.Target.AverageValue, - }, - } - } + autoscaler := autoscalingv2.HorizontalPodAutoscaler{ + ObjectMeta: objectMeta, + Spec: autoscalingv2.HorizontalPodAutoscalerSpec{ + ScaleTargetRef: autoscalingv2.CrossVersionObjectReference{ + APIVersion: v1alpha1.GroupVersion.String(), + Kind: "OpenTelemetryCollector", + Name: naming.OpenTelemetryCollector(otelcol.Name), + }, + MinReplicas: otelcol.Spec.Autoscaler.MinReplicas, + MaxReplicas: *otelcol.Spec.Autoscaler.MaxReplicas, + Metrics: metrics, + }, } - - return metrics -} - -// Create a v2beta2 HorizontalPodAutoscalerBehavior from a v2 instance. -func ConvertToV2beta2Behavior(v2behavior autoscalingv2.HorizontalPodAutoscalerBehavior) autoscalingv2beta2.HorizontalPodAutoscalerBehavior { - behavior := &autoscalingv2beta2.HorizontalPodAutoscalerBehavior{} - - if v2behavior.ScaleUp != nil { - scaleUpRules := &autoscalingv2beta2.HPAScalingRules{} - scaleUpTime := *v2behavior.ScaleUp.StabilizationWindowSeconds - scaleUpRules.StabilizationWindowSeconds = &scaleUpTime - - if v2behavior.ScaleUp.SelectPolicy != nil { - scaleUpSelectPolicy := ConvertToV2Beta2SelectPolicy(*v2behavior.ScaleUp.SelectPolicy) - scaleUpRules.SelectPolicy = &scaleUpSelectPolicy - } - if v2behavior.ScaleUp.Policies != nil { - scaleUpPolicies := []autoscalingv2beta2.HPAScalingPolicy{} - for _, policy := range v2behavior.ScaleUp.Policies { - v2beta2policy := ConvertToV2Beta2HPAScalingPolicy(policy) - scaleUpPolicies = append(scaleUpPolicies, v2beta2policy) - } - scaleUpRules.Policies = scaleUpPolicies - } - - behavior.ScaleUp = scaleUpRules + if otelcol.Spec.Autoscaler.Behavior != nil { + autoscaler.Spec.Behavior = otelcol.Spec.Autoscaler.Behavior } - if v2behavior.ScaleDown != nil { - scaleDownRules := &autoscalingv2beta2.HPAScalingRules{} - scaleDownTime := *v2behavior.ScaleDown.StabilizationWindowSeconds - scaleDownRules.StabilizationWindowSeconds = &scaleDownTime - - if v2behavior.ScaleDown.SelectPolicy != nil { - scaleDownSelectPolicy := ConvertToV2Beta2SelectPolicy(*v2behavior.ScaleDown.SelectPolicy) - scaleDownRules.SelectPolicy = &scaleDownSelectPolicy - } - if v2behavior.ScaleDown.Policies != nil { - ScaleDownPolicies := []autoscalingv2beta2.HPAScalingPolicy{} - for _, policy := range v2behavior.ScaleDown.Policies { - v2beta2policy := ConvertToV2Beta2HPAScalingPolicy(policy) - ScaleDownPolicies = append(ScaleDownPolicies, v2beta2policy) + // convert from v1alpha1.MetricSpec into a autoscalingv2.MetricSpec. + for _, metric := range otelcol.Spec.Autoscaler.Metrics { + if metric.Type == autoscalingv2.PodsMetricSourceType { + v2metric := autoscalingv2.MetricSpec{ + Type: metric.Type, + Pods: metric.Pods, } - scaleDownRules.Policies = ScaleDownPolicies - } - - behavior.ScaleDown = scaleDownRules - } - - return *behavior -} - -func ConvertToV2Beta2HPAScalingPolicy(v2policy autoscalingv2.HPAScalingPolicy) autoscalingv2beta2.HPAScalingPolicy { - v2beta2Policy := &autoscalingv2beta2.HPAScalingPolicy{ - Value: v2policy.Value, - PeriodSeconds: v2policy.PeriodSeconds, + autoscaler.Spec.Metrics = append(autoscaler.Spec.Metrics, v2metric) + } // pod metrics } + result = &autoscaler - switch v2policy.Type { - case autoscalingv2.PodsScalingPolicy: - v2beta2Policy.Type = autoscalingv2beta2.PodsScalingPolicy - case autoscalingv2.PercentScalingPolicy: - v2beta2Policy.Type = autoscalingv2beta2.PercentScalingPolicy - } - - return *v2beta2Policy -} - -func ConvertToV2Beta2SelectPolicy(scalingPolicy autoscalingv2.ScalingPolicySelect) autoscalingv2beta2.ScalingPolicySelect { - max := autoscalingv2beta2.MaxPolicySelect - min := autoscalingv2beta2.MinPolicySelect - disabled := autoscalingv2beta2.DisabledPolicySelect - - switch scalingPolicy { - case autoscalingv2.MaxChangePolicySelect: - return max - case autoscalingv2.MinChangePolicySelect: - return min - case autoscalingv2.DisabledPolicySelect: - return disabled - } - - return disabled + return result } diff --git a/internal/manifests/collector/horizontalpodautoscaler_test.go b/internal/manifests/collector/horizontalpodautoscaler_test.go index 294ab16a7b..282271c62f 100644 --- a/internal/manifests/collector/horizontalpodautoscaler_test.go +++ b/internal/manifests/collector/horizontalpodautoscaler_test.go @@ -19,25 +19,20 @@ import ( "github.com/stretchr/testify/assert" autoscalingv2 "k8s.io/api/autoscaling/v2" - autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" "github.com/open-telemetry/opentelemetry-operator/internal/config" . "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector" - "github.com/open-telemetry/opentelemetry-operator/pkg/autodetect" ) func TestHPA(t *testing.T) { type test struct { - name string - autoscalingVersion autodetect.AutoscalingVersion + name string } - v2Test := test{autodetect.AutoscalingVersionV2.String(), autodetect.AutoscalingVersionV2} - v2beta2Test := test{autodetect.AutoscalingVersionV2Beta2.String(), autodetect.AutoscalingVersionV2Beta2} - tests := []test{v2Test, v2beta2Test} + v2Test := test{} + tests := []test{v2Test} var minReplicas int32 = 3 var maxReplicas int32 = 5 @@ -76,47 +71,23 @@ func TestHPA(t *testing.T) { for _, otelcol := range otelcols { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - mockAutoDetector := &mockAutoDetect{ - HPAVersionFunc: func() (autodetect.AutoscalingVersion, error) { - return test.autoscalingVersion, nil - }, - } - configuration := config.New(config.WithAutoDetect(mockAutoDetector)) - err := configuration.AutoDetect() - assert.NoError(t, err) + configuration := config.New() raw := HorizontalPodAutoscaler(configuration, logger, otelcol) - if configuration.AutoscalingVersion() == autodetect.AutoscalingVersionV2Beta2 { - hpa := raw.(*autoscalingv2beta2.HorizontalPodAutoscaler) - - // verify - assert.Equal(t, "my-instance-collector", hpa.Name) - assert.Equal(t, "my-instance-collector", hpa.Labels["app.kubernetes.io/name"]) - assert.Equal(t, int32(3), *hpa.Spec.MinReplicas) - assert.Equal(t, int32(5), hpa.Spec.MaxReplicas) - for _, metric := range hpa.Spec.Metrics { - if metric.Resource.Name == corev1.ResourceCPU { - assert.Equal(t, cpuUtilization, *metric.Resource.Target.AverageUtilization) - } else if metric.Resource.Name == corev1.ResourceMemory { - assert.Equal(t, memoryUtilization, *metric.Resource.Target.AverageUtilization) - } - } - } else { - hpa := raw.(*autoscalingv2.HorizontalPodAutoscaler) + hpa := raw.(*autoscalingv2.HorizontalPodAutoscaler) - // verify - assert.Equal(t, "my-instance-collector", hpa.Name) - assert.Equal(t, "my-instance-collector", hpa.Labels["app.kubernetes.io/name"]) - assert.Equal(t, int32(3), *hpa.Spec.MinReplicas) - assert.Equal(t, int32(5), hpa.Spec.MaxReplicas) - assert.Equal(t, 2, len(hpa.Spec.Metrics)) + // verify + assert.Equal(t, "my-instance-collector", hpa.Name) + assert.Equal(t, "my-instance-collector", hpa.Labels["app.kubernetes.io/name"]) + assert.Equal(t, int32(3), *hpa.Spec.MinReplicas) + assert.Equal(t, int32(5), hpa.Spec.MaxReplicas) + assert.Equal(t, 2, len(hpa.Spec.Metrics)) - for _, metric := range hpa.Spec.Metrics { - if metric.Resource.Name == corev1.ResourceCPU { - assert.Equal(t, cpuUtilization, *metric.Resource.Target.AverageUtilization) - } else if metric.Resource.Name == corev1.ResourceMemory { - assert.Equal(t, memoryUtilization, *metric.Resource.Target.AverageUtilization) - } + for _, metric := range hpa.Spec.Metrics { + if metric.Resource.Name == corev1.ResourceCPU { + assert.Equal(t, cpuUtilization, *metric.Resource.Target.AverageUtilization) + } else if metric.Resource.Name == corev1.ResourceMemory { + assert.Equal(t, memoryUtilization, *metric.Resource.Target.AverageUtilization) } } }) @@ -124,116 +95,3 @@ func TestHPA(t *testing.T) { } } - -func TestConvertToV2Beta2PodMetrics(t *testing.T) { - expectedValues := []int64{int64(10), int64(20)} - expectedNames := []string{"custom1", "custom2"} - - v2metrics := []v1alpha1.MetricSpec{ - { - Type: autoscalingv2.PodsMetricSourceType, - Pods: &autoscalingv2.PodsMetricSource{ - Metric: autoscalingv2.MetricIdentifier{ - Name: "custom1", - }, - Target: autoscalingv2.MetricTarget{ - Type: autoscalingv2.AverageValueMetricType, - AverageValue: resource.NewQuantity(int64(10), resource.DecimalSI), - }, - }, - }, - { - Type: autoscalingv2.PodsMetricSourceType, - Pods: &autoscalingv2.PodsMetricSource{ - Metric: autoscalingv2.MetricIdentifier{ - Name: "custom2", - }, - Target: autoscalingv2.MetricTarget{ - Type: autoscalingv2.AverageValueMetricType, - AverageValue: resource.NewQuantity(int64(20), resource.DecimalSI), - }, - }, - }, - } - - v2beta2metrics := ConvertToV2Beta2PodMetrics(v2metrics) - - for i, metric := range v2beta2metrics { - - assert.Equal(t, expectedNames[i], metric.Pods.Metric.Name) - val, ok := metric.Pods.Target.AverageValue.AsInt64() - assert.True(t, ok) - assert.Equal(t, expectedValues[i], val) - } -} - -func TestConvertToV2beta2Behavior(t *testing.T) { - ten := int32(10) - thirty := int32(30) - max := autoscalingv2.MaxChangePolicySelect - min := autoscalingv2.MinChangePolicySelect - - v2ScaleUp := &autoscalingv2.HPAScalingRules{ - StabilizationWindowSeconds: &thirty, - SelectPolicy: &max, - } - - v2ScaleDown := &autoscalingv2.HPAScalingRules{ - StabilizationWindowSeconds: &ten, - SelectPolicy: &min, - } - - v2Behavior := &autoscalingv2.HorizontalPodAutoscalerBehavior{ - ScaleUp: v2ScaleUp, - ScaleDown: v2ScaleDown, - } - - v2Beta2Behavior := ConvertToV2beta2Behavior(*v2Behavior) - - assert.Equal(t, thirty, *v2Beta2Behavior.ScaleUp.StabilizationWindowSeconds) - assert.Equal(t, ten, *v2Beta2Behavior.ScaleDown.StabilizationWindowSeconds) - assert.Equal(t, autoscalingv2beta2.MaxPolicySelect, *v2Beta2Behavior.ScaleUp.SelectPolicy) - assert.EqualValues(t, autoscalingv2beta2.MinPolicySelect, *v2Beta2Behavior.ScaleDown.SelectPolicy) -} - -func TestConvertToV2Beta2HPAScalingPolicy(t *testing.T) { - v2Policy := autoscalingv2.HPAScalingPolicy{ - Type: autoscalingv2.PodsScalingPolicy, - Value: 5, - PeriodSeconds: 10, - } - - v2Beta2Policy := ConvertToV2Beta2HPAScalingPolicy(v2Policy) - assert.Equal(t, autoscalingv2beta2.PodsScalingPolicy, v2Beta2Policy.Type) - assert.Equal(t, int32(5), v2Beta2Policy.Value) - assert.Equal(t, int32(10), v2Beta2Policy.PeriodSeconds) - -} - -func TestConvertToV2Beta2SelectPolicy(t *testing.T) { - min := autoscalingv2.MinChangePolicySelect - max := autoscalingv2.MaxChangePolicySelect - disabled := autoscalingv2.DisabledPolicySelect - - assert.Equal(t, autoscalingv2beta2.MinPolicySelect, ConvertToV2Beta2SelectPolicy(min)) - assert.Equal(t, autoscalingv2beta2.MaxPolicySelect, ConvertToV2Beta2SelectPolicy(max)) - assert.Equal(t, autoscalingv2beta2.DisabledPolicySelect, ConvertToV2Beta2SelectPolicy(disabled)) -} - -var _ autodetect.AutoDetect = (*mockAutoDetect)(nil) - -type mockAutoDetect struct { - OpenShiftRoutesAvailabilityFunc func() (autodetect.OpenShiftRoutesAvailability, error) - HPAVersionFunc func() (autodetect.AutoscalingVersion, error) -} - -func (m *mockAutoDetect) HPAVersion() (autodetect.AutoscalingVersion, error) { - return m.HPAVersionFunc() -} - -func (m *mockAutoDetect) OpenShiftRoutesAvailability() (autodetect.OpenShiftRoutesAvailability, error) { - if m.OpenShiftRoutesAvailabilityFunc != nil { - return m.OpenShiftRoutesAvailabilityFunc() - } - return autodetect.OpenShiftRoutesNotAvailable, nil -} diff --git a/kind-1.19.yaml b/kind-1.19.yaml deleted file mode 100644 index 6720d5f674..0000000000 --- a/kind-1.19.yaml +++ /dev/null @@ -1,18 +0,0 @@ -kind: Cluster -apiVersion: kind.x-k8s.io/v1alpha4 -nodes: -- role: control-plane - image: kindest/node:v1.19.16@sha256:476cb3269232888437b61deca013832fee41f9f074f9bed79f57e4280f7c48b7 - kubeadmConfigPatches: - - | - kind: InitConfiguration - nodeRegistration: - kubeletExtraArgs: - node-labels: "ingress-ready=true" - extraPortMappings: - - containerPort: 80 - hostPort: 80 - protocol: TCP - - containerPort: 443 - hostPort: 443 - protocol: TCP diff --git a/kind-1.20.yaml b/kind-1.20.yaml deleted file mode 100644 index cd05acf931..0000000000 --- a/kind-1.20.yaml +++ /dev/null @@ -1,18 +0,0 @@ -kind: Cluster -apiVersion: kind.x-k8s.io/v1alpha4 -nodes: -- role: control-plane - image: kindest/node:v1.20.15@sha256:a32bf55309294120616886b5338f95dd98a2f7231519c7dedcec32ba29699394 - kubeadmConfigPatches: - - | - kind: InitConfiguration - nodeRegistration: - kubeletExtraArgs: - node-labels: "ingress-ready=true" - extraPortMappings: - - containerPort: 80 - hostPort: 80 - protocol: TCP - - containerPort: 443 - hostPort: 443 - protocol: TCP diff --git a/kind-1.21.yaml b/kind-1.21.yaml deleted file mode 100644 index c6de06b575..0000000000 --- a/kind-1.21.yaml +++ /dev/null @@ -1,18 +0,0 @@ -kind: Cluster -apiVersion: kind.x-k8s.io/v1alpha4 -nodes: -- role: control-plane - image: kindest/node:v1.21.14@sha256:8a4e9bb3f415d2bb81629ce33ef9c76ba514c14d707f9797a01e3216376ba093 - kubeadmConfigPatches: - - | - kind: InitConfiguration - nodeRegistration: - kubeletExtraArgs: - node-labels: "ingress-ready=true" - extraPortMappings: - - containerPort: 80 - hostPort: 80 - protocol: TCP - - containerPort: 443 - hostPort: 443 - protocol: TCP diff --git a/kind-1.22.yaml b/kind-1.22.yaml deleted file mode 100644 index cde6274200..0000000000 --- a/kind-1.22.yaml +++ /dev/null @@ -1,18 +0,0 @@ -kind: Cluster -apiVersion: kind.x-k8s.io/v1alpha4 -nodes: -- role: control-plane - image: kindest/node:v1.22.17@sha256:f5b2e5698c6c9d6d0adc419c0deae21a425c07d81bbf3b6a6834042f25d4fba2 - kubeadmConfigPatches: - - | - kind: InitConfiguration - nodeRegistration: - kubeletExtraArgs: - node-labels: "ingress-ready=true" - extraPortMappings: - - containerPort: 80 - hostPort: 80 - protocol: TCP - - containerPort: 443 - hostPort: 443 - protocol: TCP diff --git a/kind-1.23.yaml b/kind-1.23.yaml index 9662441c62..09a2521d81 100644 --- a/kind-1.23.yaml +++ b/kind-1.23.yaml @@ -1,18 +1,18 @@ kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes: -- role: control-plane - image: kindest/node:v1.23.17@sha256:59c989ff8a517a93127d4a536e7014d28e235fb3529d9fba91b3951d461edfdb - kubeadmConfigPatches: - - | - kind: InitConfiguration - nodeRegistration: - kubeletExtraArgs: - node-labels: "ingress-ready=true" - extraPortMappings: - - containerPort: 80 - hostPort: 80 - protocol: TCP - - containerPort: 443 - hostPort: 443 - protocol: TCP + - role: control-plane + image: kindest/node:v1.23.17@sha256:59c989ff8a517a93127d4a536e7014d28e235fb3529d9fba91b3951d461edfdb + kubeadmConfigPatches: + - | + kind: InitConfiguration + nodeRegistration: + kubeletExtraArgs: + node-labels: "ingress-ready=true" + extraPortMappings: + - containerPort: 80 + hostPort: 80 + protocol: TCP + - containerPort: 443 + hostPort: 443 + protocol: TCP diff --git a/pkg/autodetect/main.go b/pkg/autodetect/main.go index 8d1d295014..d12d19b297 100644 --- a/pkg/autodetect/main.go +++ b/pkg/autodetect/main.go @@ -16,9 +16,6 @@ package autodetect import ( - "errors" - "sort" - "k8s.io/client-go/discovery" "k8s.io/client-go/rest" ) @@ -28,23 +25,12 @@ var _ AutoDetect = (*autoDetect)(nil) // AutoDetect provides an assortment of routines that auto-detect traits based on the runtime. type AutoDetect interface { OpenShiftRoutesAvailability() (OpenShiftRoutesAvailability, error) - HPAVersion() (AutoscalingVersion, error) } type autoDetect struct { dcl discovery.DiscoveryInterface } -type AutoscalingVersion int - -const ( - AutoscalingVersionV2 AutoscalingVersion = iota - AutoscalingVersionV2Beta2 - AutoscalingVersionUnknown -) - -const DefaultAutoscalingVersion = AutoscalingVersionV2 - // New creates a new auto-detection worker, using the given client when talking to the current cluster. func New(restConfig *rest.Config) (AutoDetect, error) { dcl, err := discovery.NewDiscoveryClientForConfig(restConfig) @@ -76,51 +62,3 @@ func (a *autoDetect) OpenShiftRoutesAvailability() (OpenShiftRoutesAvailability, return OpenShiftRoutesNotAvailable, nil } - -func (a *autoDetect) HPAVersion() (AutoscalingVersion, error) { - apiList, err := a.dcl.ServerGroups() - if err != nil { - return AutoscalingVersionUnknown, err - } - - for _, apiGroup := range apiList.Groups { - if apiGroup.Name == "autoscaling" { - // Sort this so we can make sure to get v2 before v2beta2 - versions := apiGroup.Versions - sort.Slice(versions, func(i, j int) bool { - return versions[i].Version < versions[j].Version - }) - - for _, version := range versions { - if version.Version == "v2" || version.Version == "v2beta2" { - return ToAutoScalingVersion(version.Version), nil - } - } - return AutoscalingVersionUnknown, errors.New("Failed to find appropriate version of apiGroup autoscaling, only v2 and v2beta2 are supported") - } - } - - return AutoscalingVersionUnknown, errors.New("Failed to find apiGroup autoscaling") -} - -func (v AutoscalingVersion) String() string { - switch v { - case AutoscalingVersionV2: - return "v2" - case AutoscalingVersionV2Beta2: - return "v2beta2" - case AutoscalingVersionUnknown: - return "unknown" - } - return "unknown" -} - -func ToAutoScalingVersion(version string) AutoscalingVersion { - switch version { - case "v2": - return AutoscalingVersionV2 - case "v2beta2": - return AutoscalingVersionV2Beta2 - } - return AutoscalingVersionUnknown -} diff --git a/pkg/autodetect/main_test.go b/pkg/autodetect/main_test.go index e41cb1ee61..18a9640f0b 100644 --- a/pkg/autodetect/main_test.go +++ b/pkg/autodetect/main_test.go @@ -70,15 +70,3 @@ func TestDetectPlatformBasedOnAvailableAPIGroups(t *testing.T) { assert.Equal(t, tt.expected, ora) } } - -func TestAutoscalingVersionToString(t *testing.T) { - assert.Equal(t, "v2", autodetect.AutoscalingVersionV2.String()) - assert.Equal(t, "v2beta2", autodetect.AutoscalingVersionV2Beta2.String()) - assert.Equal(t, "unknown", autodetect.AutoscalingVersionUnknown.String()) -} - -func TestToAutoScalingVersion(t *testing.T) { - assert.Equal(t, autodetect.AutoscalingVersionV2, autodetect.ToAutoScalingVersion("v2")) - assert.Equal(t, autodetect.AutoscalingVersionV2Beta2, autodetect.ToAutoScalingVersion("v2beta2")) - assert.Equal(t, autodetect.AutoscalingVersionUnknown, autodetect.ToAutoScalingVersion("fred")) -} diff --git a/tests/e2e-autoscale/autoscale/00-assert.yaml b/tests/e2e-autoscale/autoscale/00-assert.yaml index a0edb12300..2546b5462d 100644 --- a/tests/e2e-autoscale/autoscale/00-assert.yaml +++ b/tests/e2e-autoscale/autoscale/00-assert.yaml @@ -11,3 +11,76 @@ metadata: name: simplest-set-utilization-collector status: readyReplicas: 1 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: simplest-collector +spec: + scaleTargetRef: + kind: OpenTelemetryCollector + name: simplest + apiVersion: opentelemetry.io/v1alpha1 + minReplicas: 1 + maxReplicas: 2 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 90 + behavior: + scaleUp: + stabilizationWindowSeconds: 10 + selectPolicy: Max + policies: + - type: Pods + value: 4 + periodSeconds: 15 + - type: Percent + value: 100 + periodSeconds: 15 + scaleDown: + stabilizationWindowSeconds: 15 + selectPolicy: Max + policies: + - type: Percent + value: 100 + periodSeconds: 15 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: simplest-set-utilization-collector +spec: + scaleTargetRef: + kind: OpenTelemetryCollector + name: simplest-set-utilization + apiVersion: opentelemetry.io/v1alpha1 + minReplicas: 1 + maxReplicas: 2 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 99 + behavior: + scaleUp: + stabilizationWindowSeconds: 300 + selectPolicy: Max + policies: + - type: Pods + value: 4 + periodSeconds: 15 + - type: Percent + value: 100 + periodSeconds: 15 + scaleDown: + selectPolicy: Max + policies: + - type: Percent + value: 100 + periodSeconds: 15 diff --git a/tests/e2e-autoscale/autoscale/01-assert.yaml b/tests/e2e-autoscale/autoscale/01-assert.yaml new file mode 100644 index 0000000000..faae4da95b --- /dev/null +++ b/tests/e2e-autoscale/autoscale/01-assert.yaml @@ -0,0 +1,36 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: simplest-set-utilization-collector +spec: + scaleTargetRef: + kind: OpenTelemetryCollector + name: simplest-set-utilization + apiVersion: opentelemetry.io/v1alpha1 + minReplicas: 1 + maxReplicas: 2 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 60 + behavior: + scaleUp: + stabilizationWindowSeconds: 1 + selectPolicy: Max + policies: + - type: Pods + value: 4 + periodSeconds: 15 + - type: Percent + value: 100 + periodSeconds: 15 + scaleDown: + stabilizationWindowSeconds: 15 + selectPolicy: Max + policies: + - type: Percent + value: 100 + periodSeconds: 15 diff --git a/tests/e2e-autoscale/autoscale/01-check-simplest-collector-hpa.yaml b/tests/e2e-autoscale/autoscale/01-check-simplest-collector-hpa.yaml deleted file mode 100644 index c01035210b..0000000000 --- a/tests/e2e-autoscale/autoscale/01-check-simplest-collector-hpa.yaml +++ /dev/null @@ -1,4 +0,0 @@ -apiVersion: kuttl.dev/v1beta1 -kind: TestStep -commands: - - script: go run ./wait-until-hpa-ready.go --hpa simplest-collector diff --git a/tests/e2e-autoscale/autoscale/01-install.yaml b/tests/e2e-autoscale/autoscale/01-install.yaml new file mode 100644 index 0000000000..a9a6d549c2 --- /dev/null +++ b/tests/e2e-autoscale/autoscale/01-install.yaml @@ -0,0 +1,44 @@ +# This updates an existing deployment with a new Spec. +# Target memory utilization is now added. +# scaleDown and scaleUp behavior is now added. +apiVersion: opentelemetry.io/v1alpha1 +kind: OpenTelemetryCollector +metadata: + name: simplest-set-utilization +spec: + minReplicas: 1 + maxReplicas: 2 + autoscaler: + targetCPUUtilization: 60 + # Without this behavior the HPA will default to a scaledown stabilization + # window of 300 seconds. Tests should fail if this update is not successful. + behavior: + scaleDown: + stabilizationWindowSeconds: 15 + scaleUp: + stabilizationWindowSeconds: 1 + resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 5m + memory: 64Mi + + config: | + receivers: + otlp: + protocols: + grpc: + http: + processors: + + exporters: + logging: + + service: + pipelines: + traces: + receivers: [otlp] + processors: [] + exporters: [logging] \ No newline at end of file diff --git a/tests/e2e-autoscale/autoscale/05-assert.yaml b/tests/e2e-autoscale/autoscale/02-assert.yaml similarity index 88% rename from tests/e2e-autoscale/autoscale/05-assert.yaml rename to tests/e2e-autoscale/autoscale/02-assert.yaml index d67919fa37..8dc83f76b8 100644 --- a/tests/e2e-autoscale/autoscale/05-assert.yaml +++ b/tests/e2e-autoscale/autoscale/02-assert.yaml @@ -4,4 +4,4 @@ metadata: name: simplest-set-utilization status: scale: - replicas: 1 + replicas: 2 diff --git a/tests/e2e-autoscale/autoscale/02-check-simplest-set-utilization-collector-hpa.yaml b/tests/e2e-autoscale/autoscale/02-check-simplest-set-utilization-collector-hpa.yaml deleted file mode 100644 index 21dad9ff4a..0000000000 --- a/tests/e2e-autoscale/autoscale/02-check-simplest-set-utilization-collector-hpa.yaml +++ /dev/null @@ -1,4 +0,0 @@ -apiVersion: kuttl.dev/v1beta1 -kind: TestStep -commands: - - script: go run ./wait-until-hpa-ready.go --hpa simplest-set-utilization-collector diff --git a/tests/e2e-autoscale/autoscale/02-install.yaml b/tests/e2e-autoscale/autoscale/02-install.yaml index a9a6d549c2..d4401cae3d 100644 --- a/tests/e2e-autoscale/autoscale/02-install.yaml +++ b/tests/e2e-autoscale/autoscale/02-install.yaml @@ -1,44 +1,21 @@ -# This updates an existing deployment with a new Spec. -# Target memory utilization is now added. -# scaleDown and scaleUp behavior is now added. -apiVersion: opentelemetry.io/v1alpha1 -kind: OpenTelemetryCollector +apiVersion: batch/v1 +kind: Job metadata: - name: simplest-set-utilization + name: tracegen-set-utilization spec: - minReplicas: 1 - maxReplicas: 2 - autoscaler: - targetCPUUtilization: 60 - # Without this behavior the HPA will default to a scaledown stabilization - # window of 300 seconds. Tests should fail if this update is not successful. - behavior: - scaleDown: - stabilizationWindowSeconds: 15 - scaleUp: - stabilizationWindowSeconds: 1 - resources: - limits: - cpu: 100m - memory: 128Mi - requests: - cpu: 5m - memory: 64Mi - - config: | - receivers: - otlp: - protocols: - grpc: - http: - processors: - - exporters: - logging: - - service: - pipelines: - traces: - receivers: [otlp] - processors: [] - exporters: [logging] \ No newline at end of file + template: + spec: + containers: + - name: tracegen + image: ghcr.io/open-telemetry/opentelemetry-collector-contrib/tracegen:latest + command: + - "./tracegen" + args: + - -otlp-endpoint=simplest-set-utilization-collector-headless:4317 + - -otlp-insecure + # High duration to ensure the trace creation doesn't stop. + # It'll be stopped in step 4 + - -duration=1m + - -workers=20 + restartPolicy: Never + backoffLimit: 4 diff --git a/tests/e2e-autoscale/autoscale/03-assert.yaml b/tests/e2e-autoscale/autoscale/03-assert.yaml index 8dc83f76b8..d67919fa37 100644 --- a/tests/e2e-autoscale/autoscale/03-assert.yaml +++ b/tests/e2e-autoscale/autoscale/03-assert.yaml @@ -4,4 +4,4 @@ metadata: name: simplest-set-utilization status: scale: - replicas: 2 + replicas: 1 diff --git a/tests/e2e-autoscale/autoscale/04-delete.yaml b/tests/e2e-autoscale/autoscale/03-delete.yaml similarity index 100% rename from tests/e2e-autoscale/autoscale/04-delete.yaml rename to tests/e2e-autoscale/autoscale/03-delete.yaml diff --git a/tests/e2e-autoscale/autoscale/03-install.yaml b/tests/e2e-autoscale/autoscale/03-install.yaml deleted file mode 100644 index d4401cae3d..0000000000 --- a/tests/e2e-autoscale/autoscale/03-install.yaml +++ /dev/null @@ -1,21 +0,0 @@ -apiVersion: batch/v1 -kind: Job -metadata: - name: tracegen-set-utilization -spec: - template: - spec: - containers: - - name: tracegen - image: ghcr.io/open-telemetry/opentelemetry-collector-contrib/tracegen:latest - command: - - "./tracegen" - args: - - -otlp-endpoint=simplest-set-utilization-collector-headless:4317 - - -otlp-insecure - # High duration to ensure the trace creation doesn't stop. - # It'll be stopped in step 4 - - -duration=1m - - -workers=20 - restartPolicy: Never - backoffLimit: 4 diff --git a/tests/e2e-autoscale/autoscale/03-verify-simplest-set-collector-hpa-metrics.yaml b/tests/e2e-autoscale/autoscale/03-verify-simplest-set-collector-hpa-metrics.yaml deleted file mode 100644 index 6063958c8f..0000000000 --- a/tests/e2e-autoscale/autoscale/03-verify-simplest-set-collector-hpa-metrics.yaml +++ /dev/null @@ -1,4 +0,0 @@ -apiVersion: kuttl.dev/v1beta1 -kind: TestStep -commands: - - script: go run ./cmd/verify/wait-and-validate-metrics.go --cpu-value 60 --scale-down 15 --scale-up 1 --hpa simplest-set-utilization-collector \ No newline at end of file diff --git a/tests/e2e-autoscale/autoscale/cmd/verify/wait-and-validate-metrics.go b/tests/e2e-autoscale/autoscale/cmd/verify/wait-and-validate-metrics.go deleted file mode 100644 index 9126b3295c..0000000000 --- a/tests/e2e-autoscale/autoscale/cmd/verify/wait-and-validate-metrics.go +++ /dev/null @@ -1,187 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package main - -import ( - "context" - "fmt" - "os" - "path/filepath" - "time" - - "github.com/spf13/pflag" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/clientcmd" - "k8s.io/client-go/util/homedir" -) - -func main() { - var hpaName string - var timeout time.Duration - var numMetrics int - var cpuValue int - var memoryValue int - var scaleDownWindow int - var scaleUpWindow int - - pflag.DurationVar(&timeout, "timeout", 5*time.Minute, "The timeout for the check.") - pflag.StringVar(&hpaName, "hpa", "", "HPA to check") - pflag.IntVar(&numMetrics, "num-metrics", 1, "number of expected metrics in Spec") - pflag.IntVar(&cpuValue, "cpu-value", -1, "value for target CPU utilization") - pflag.IntVar(&memoryValue, "memory-value", -1, "value for target memory utilization") - pflag.IntVar(&scaleDownWindow, "scale-down", -1, "value for scaleDown stabilization window") - pflag.IntVar(&scaleUpWindow, "scale-up", -1, "value for scaleUp stabilization window") - pflag.Parse() - - if len(hpaName) == 0 { - fmt.Println("hpa flag is mandatory") - os.Exit(1) - } - - kubeconfigPath := getKubeconfigPath() - - configLoader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath}, - &clientcmd.ConfigOverrides{}, - ) - - config, err := configLoader.ClientConfig() - if err != nil { - fmt.Printf("Error reading the kubeconfig: %s\n", err) - os.Exit(1) - } - - client, err := kubernetes.NewForConfig(config) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - namespace, err := client.CoreV1().Namespaces().Get(context.Background(), os.Getenv("NAMESPACE"), metav1.GetOptions{}) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - hpaClientV2 := client.AutoscalingV2().HorizontalPodAutoscalers(namespace.Name) - hpaClientV1 := client.AutoscalingV1().HorizontalPodAutoscalers(namespace.Name) - - pollInterval := time.Second - - // Search in v2 and v1 for an HPA with the given name - ctx := context.Background() - err = wait.PollUntilContextTimeout(ctx, pollInterval, timeout, false, func(c context.Context) (done bool, err error) { - hpav2, err := hpaClientV2.Get( - c, - hpaName, - metav1.GetOptions{}, - ) - - if err != nil { - hpav1, err := hpaClientV1.Get( - c, - hpaName, - metav1.GetOptions{}, - ) - if err != nil { - fmt.Printf("HPA %s not found\n", hpaName) - return false, nil - } - - if hpav1.Status.CurrentCPUUtilizationPercentage == nil { - fmt.Printf("Current metrics are not set yet for HPA %s\n", hpaName) - return false, nil - } - - if *hpav1.Spec.TargetCPUUtilizationPercentage != int32(cpuValue) { - fmt.Printf("Incorrect target CPU value found for HPA %s\n", hpaName) - return false, nil - } - return true, nil - } - - if hpav2.Status.CurrentMetrics == nil { - fmt.Printf("Current metrics are not set yet for HPA %s\n", hpaName) - return false, nil - } - - numExpectedMetrics := 0 - if cpuValue != -1 { - numExpectedMetrics += 1 - } - if memoryValue != -1 { - numExpectedMetrics += 1 - } - if len(hpav2.Spec.Metrics) != numExpectedMetrics { - fmt.Printf("Metrics has incorrect length for HPA %s\n", hpaName) - return false, nil - } - - for _, metric := range hpav2.Spec.Metrics { - if metric.Resource.Name == corev1.ResourceCPU { - if cpuValue == -1 { - fmt.Printf("Unexpected CPU metric found for HPA %s\n", hpaName) - return false, nil - } - if int32(cpuValue) != *metric.Resource.Target.AverageUtilization { - fmt.Printf("Incorrect target CPU value found for HPA %s\n", hpaName) - return false, nil - } - } else if metric.Resource.Name == corev1.ResourceMemory { - if memoryValue == -1 { - fmt.Printf("Unexpected memory metric found for HPA %s\n", hpaName) - return false, nil - } - if int32(memoryValue) != *metric.Resource.Target.AverageUtilization { - fmt.Printf("Incorrect target memory value found for HPA %s\n", hpaName) - return false, nil - } - } - } - - // validate HPA behavior - if int32(scaleDownWindow) != *hpav2.Spec.Behavior.ScaleDown.StabilizationWindowSeconds { - fmt.Printf("Incorrect scaleDown stabilization window found for HPA %s\n", hpaName) - return false, nil - } - if int32(scaleUpWindow) != *hpav2.Spec.Behavior.ScaleUp.StabilizationWindowSeconds { - fmt.Printf("Incorrect scaleUp stabilization window found for HPA %s\n", hpaName) - return false, nil - } - - return true, nil - }) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - fmt.Printf("%s is ready!\n", hpaName) -} - -func getKubeconfigPath() string { - kubeconfigEnv := os.Getenv("KUBECONFIG") - if kubeconfigEnv != "" { - if _, err := os.Stat(kubeconfigEnv); err == nil { - return kubeconfigEnv - } - } - - homeDir := homedir.HomeDir() - return filepath.Join(homeDir, ".kube", "config") -} diff --git a/tests/e2e-autoscale/autoscale/wait-until-hpa-ready.go b/tests/e2e-autoscale/autoscale/wait-until-hpa-ready.go deleted file mode 100644 index 51bc7328da..0000000000 --- a/tests/e2e-autoscale/autoscale/wait-until-hpa-ready.go +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package main - -import ( - "context" - "fmt" - "os" - "path/filepath" - "time" - - "github.com/spf13/pflag" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/clientcmd" - "k8s.io/client-go/util/homedir" -) - -func main() { - var hpaName string - var timeout time.Duration - - pflag.DurationVar(&timeout, "timeout", 5*time.Minute, "The timeout for the check.") - pflag.StringVar(&hpaName, "hpa", "", "HPA to check") - pflag.Parse() - - if len(hpaName) == 0 { - fmt.Println("hpa flag is mandatory") - os.Exit(1) - } - - kubeconfigPath := getKubeconfigPath() - - config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) - if err != nil { - fmt.Printf("Error reading the kubeconfig: %s\n", err) - os.Exit(1) - } - - client, err := kubernetes.NewForConfig(config) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - namespace, err := client.CoreV1().Namespaces().Get(context.Background(), os.Getenv("NAMESPACE"), v1.GetOptions{}) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - hpaClientV2 := client.AutoscalingV2().HorizontalPodAutoscalers(namespace.Name) - hpaClientV1 := client.AutoscalingV1().HorizontalPodAutoscalers(namespace.Name) - - pollInterval := time.Second - - // Search in v2 and v1 for an HPA with the given name - - ctx := context.Background() - err = wait.PollUntilContextTimeout(ctx, pollInterval, timeout, false, func(c context.Context) (done bool, err error) { - hpav2, err := hpaClientV2.Get( - c, - hpaName, - v1.GetOptions{}, - ) - if err != nil { - hpav1, err := hpaClientV1.Get( - c, - hpaName, - v1.GetOptions{}, - ) - if err != nil { - fmt.Printf("HPA %s not found\n", hpaName) - return false, nil - } - - if hpav1.Status.CurrentCPUUtilizationPercentage == nil { - fmt.Printf("Current metrics are not set yet for HPA %s\n", hpaName) - return false, nil - } - return true, nil - } - - if hpav2.Status.CurrentMetrics == nil { - fmt.Printf("Current metrics are not set yet for HPA %s\n", hpaName) - return false, nil - } - return true, nil - }) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - fmt.Printf("%s is ready!\n", hpaName) -} - -func getKubeconfigPath() string { - kubeconfigEnv := os.Getenv("KUBECONFIG") - if kubeconfigEnv != "" { - if _, err := os.Stat(kubeconfigEnv); err == nil { - return kubeconfigEnv - } - } - - homeDir := homedir.HomeDir() - return filepath.Join(homeDir, ".kube", "config") -}