Skip to content

Commit

Permalink
[redhat-3.12] bugfix: updated quayComponentLabel generation for HPA (P…
Browse files Browse the repository at this point in the history
…ROJQUAY-5086) (#1005)

* versionupgrade: upgraded k8s autoscaling version in middleware

* bugfix: updated quayComponentLabel generation for HPA

---------

Co-authored-by: shudeshp <[email protected]>
  • Loading branch information
openshift-cherrypick-robot and shudeshp authored Nov 18, 2024
1 parent 06b8f84 commit c7b8015
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
26 changes: 23 additions & 3 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
route "github.com/openshift/api/route/v1"
quaycontext "github.com/quay/quay-operator/pkg/context"
appsv1 "k8s.io/api/apps/v1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -34,7 +34,27 @@ func Process(quay *v1.QuayRegistry, qctx *quaycontext.QuayRegistryContext, obj c
return nil, err
}

quayComponentLabel := labels.Set(objectMeta.GetLabels()).Get("quay-component")
quayComponentLabel := ""

if hpa, ok := obj.(*autoscalingv2.HorizontalPodAutoscaler); ok {
// HPA may have its own specific labels that are not necessarily present in the standard object metadata.
// Therefore, we need to check both labels and annotations specifically for HPA to ensure we capture the correct quay-component label.
if labels := hpa.GetLabels(); labels != nil {
if labelValue, exists := labels["quay-component"]; exists {
quayComponentLabel = labelValue
}
}

if quayComponentLabel == "" {
if annotations := hpa.GetAnnotations(); annotations != nil {
if annotationValue, exists := annotations["quay-component"]; exists {
quayComponentLabel = annotationValue
}
}
}
} else {
quayComponentLabel = labels.Set(objectMeta.GetLabels()).Get("quay-component")
}

// Flatten config bundle `Secret`
if strings.Contains(objectMeta.GetName(), configSecretPrefix+"-") {
Expand Down Expand Up @@ -226,7 +246,7 @@ func Process(quay *v1.QuayRegistry, qctx *quaycontext.QuayRegistryContext, obj c
return pvc, nil
}

if _, ok := obj.(*autoscalingv1.HorizontalPodAutoscaler); ok {
if _, ok := obj.(*autoscalingv2.HorizontalPodAutoscaler); ok {
componentMap := map[string]v1.ComponentKind{
"mirror": v1.ComponentMirror,
"clair": v1.ComponentClair,
Expand Down
34 changes: 34 additions & 0 deletions pkg/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
route "github.com/openshift/api/route/v1"
quaycontext "github.com/quay/quay-operator/pkg/context"
"github.com/stretchr/testify/assert"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -248,3 +249,36 @@ func parseResourceString(s string) *resource.Quantity {
resourceSize := resource.MustParse(s)
return &resourceSize
}

func TestHPAWithUnmanagedMirrorAndClair(t *testing.T) {
quayRegistry := &v1.QuayRegistry{
Spec: v1.QuayRegistrySpec{
Components: []v1.Component{
{Kind: "mirror", Managed: false},
{Kind: "clair", Managed: false},
{Kind: "horizontalpodautoscaler", Managed: true},
},
},
}

hpa := &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-app",
Labels: map[string]string{
"quay-component": "clair",
},
},
}

// Create a mock context and logger
qctx := &quaycontext.QuayRegistryContext{}

// Call the Process function
result, err := Process(quayRegistry, qctx, hpa, false)

// Assert that there is no error
assert.NoError(t, err)

// Assert that the result is nil
assert.Nil(t, result)
}

0 comments on commit c7b8015

Please sign in to comment.