diff --git a/control-plane/pkg/autoscaler/keda/mocks/keda_mock.go b/control-plane/pkg/autoscaler/keda/mocks/keda_mock.go new file mode 100644 index 0000000000..ba1d8fe3b4 --- /dev/null +++ b/control-plane/pkg/autoscaler/keda/mocks/keda_mock.go @@ -0,0 +1,174 @@ +/* + * Copyright 2024 The Knative 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 mocks + +import ( + "github.com/google/uuid" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" +) + +type ObjectMock struct{} + +func (ObjectMock) GetNamespace() string { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetNamespace(namespace string) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetName() string { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetName(name string) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetGenerateName() string { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetGenerateName(name string) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetUID() types.UID { + return types.UID(uuid.New().String()) +} + +func (ObjectMock) SetUID(uid types.UID) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetResourceVersion() string { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetResourceVersion(version string) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetGeneration() int64 { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetGeneration(generation int64) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetSelfLink() string { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetSelfLink(selfLink string) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetCreationTimestamp() v1.Time { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetCreationTimestamp(timestamp v1.Time) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetDeletionTimestamp() *v1.Time { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetDeletionTimestamp(timestamp *v1.Time) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetDeletionGracePeriodSeconds() *int64 { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetDeletionGracePeriodSeconds(i *int64) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetLabels() map[string]string { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetLabels(labels map[string]string) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetAnnotations() map[string]string { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetAnnotations(annotations map[string]string) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetFinalizers() []string { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetFinalizers(finalizers []string) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetOwnerReferences() []v1.OwnerReference { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetOwnerReferences(references []v1.OwnerReference) { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) GetManagedFields() []v1.ManagedFieldsEntry { + //TODO implement me + panic("implement me") +} + +func (ObjectMock) SetManagedFields(managedFields []v1.ManagedFieldsEntry) { + //TODO implement me + panic("implement me") +} diff --git a/control-plane/pkg/autoscaler/keda/resources.go b/control-plane/pkg/autoscaler/keda/resources.go index b7e9730d4d..55fcb8e091 100644 --- a/control-plane/pkg/autoscaler/keda/resources.go +++ b/control-plane/pkg/autoscaler/keda/resources.go @@ -31,6 +31,8 @@ var ( KedaSchemeGroupVersion = schema.GroupVersion{Group: "keda.sh", Version: "v1alpha1"} ) +const scaledObjectPrefixName = "so-" + func GenerateScaledObject(obj metav1.Object, gvk schema.GroupVersionKind, scaleTarget *kedav1alpha1.ScaleTarget, triggers []kedav1alpha1.ScaleTriggers, aconfig autoscaler.AutoscalerConfig) (*kedav1alpha1.ScaledObject, error) { cooldownPeriod, err := GetInt32ValueFromMap(obj.GetAnnotations(), autoscaler.AutoscalingCooldownPeriodAnnotation, aconfig.AutoscalerDefaults[autoscaler.AutoscalingCooldownPeriodAnnotation]) @@ -70,7 +72,7 @@ func GenerateScaledObject(obj metav1.Object, gvk schema.GroupVersionKind, scaleT } func GenerateScaledObjectName(obj metav1.Object) string { - return fmt.Sprintf("so-%s", string(obj.GetUID())) + return fmt.Sprintf("%s%s", scaledObjectPrefixName, string(obj.GetUID())) } func GetInt32ValueFromMap(dict map[string]string, key string, defaultValue int32) (*int32, error) { diff --git a/control-plane/pkg/autoscaler/keda/resources_test.go b/control-plane/pkg/autoscaler/keda/resources_test.go new file mode 100644 index 0000000000..97c9c8308c --- /dev/null +++ b/control-plane/pkg/autoscaler/keda/resources_test.go @@ -0,0 +1,64 @@ +/* + * Copyright 2024 The Knative 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 keda + +import ( + "strings" + "testing" + + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + "knative.dev/eventing-kafka-broker/control-plane/pkg/autoscaler/keda/mocks" +) + +func TestGenerateScaledObjectName(t *testing.T) { + type args struct { + obj v1.Object + } + tests := []struct { + name string + args args + wantPrefix string + wantSuffix types.UID + }{ + { + name: "create scaled object name successfully", + args: args{ + obj: mocks.ObjectMock{}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GenerateScaledObjectName(tt.args.obj) + + soNameLength := len(got) + if soNameLength != 39 { + t.Errorf("GenerateScaledObjectName() length = %v, want length %v", len(got), soNameLength) + } + if !strings.HasPrefix(got, scaledObjectPrefixName) { + t.Errorf("GenerateScaledObjectName() prefix = %v, want prefix %v", got, tt.wantPrefix) + } + + uid := types.UID(strings.TrimPrefix(got, scaledObjectPrefixName)) + uidLength := len(uid) + if uidLength != 36 { + t.Errorf("GenerateScaledObjectName() UID length = %v, want length 36", len(string(uid))) + } + }) + } +}