Skip to content

Commit

Permalink
fix: Use autoscaling/v2 (PROJQUAY-5079)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmage authored and openshift-merge-robot committed Jun 8, 2023
1 parent 8bbf9a6 commit 9002834
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 25 deletions.
34 changes: 33 additions & 1 deletion controllers/quay/quayregistry_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ import (
prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/tidwall/sjson"
"gopkg.in/yaml.v2"
autoscalingv2 "k8s.io/api/autoscaling/v2"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -664,6 +668,23 @@ func encode(value interface{}) []byte {
return yamlified
}

func convertHpaToV2beta2(hpa *autoscalingv2.HorizontalPodAutoscaler) (*autoscalingv2beta2.HorizontalPodAutoscaler, error) {
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(hpa)
if err != nil {
return nil, err
}

u := &unstructured.Unstructured{Object: m}
u.SetAPIVersion("autoscaling/v2beta2")

v2beta2hpa := &autoscalingv2beta2.HorizontalPodAutoscaler{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, v2beta2hpa); err != nil {
return nil, err
}

return v2beta2hpa, nil
}

func (r *QuayRegistryReconciler) createOrUpdateObject(
ctx context.Context, obj client.Object, quay v1.QuayRegistry, log logr.Logger,
) error {
Expand Down Expand Up @@ -719,11 +740,22 @@ func (r *QuayRegistryReconciler) createOrUpdateObject(
return nil
}

hpaGVK := schema.GroupVersionKind{Group: "autoscaling", Version: "v2", Kind: "HorizontalPodAutoscaler"}

opts := []client.PatchOption{
client.ForceOwnership,
client.FieldOwner("quay-operator"),
}
if err := r.Client.Patch(ctx, obj, client.Apply, opts...); err != nil {
err := r.Client.Patch(ctx, obj, client.Apply, opts...)
if meta.IsNoMatchError(err) && gvk == hpaGVK {
var hpa *autoscalingv2beta2.HorizontalPodAutoscaler
hpa, err = convertHpaToV2beta2(obj.(*autoscalingv2.HorizontalPodAutoscaler))
if err != nil {
return err
}
err = r.Client.Patch(ctx, hpa, client.Apply, opts...)
}
if err != nil {
log.Error(err, "failed to create/update object")
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: autoscaling/v2beta2
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: clair-app
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: autoscaling/v2beta2
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: quay-mirror
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: autoscaling/v2beta2
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: quay-app
Expand Down
22 changes: 11 additions & 11 deletions pkg/cmpstatus/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

appsv1 "k8s.io/api/apps/v1"
asv2b2 "k8s.io/api/autoscaling/v2beta2"
asv2 "k8s.io/api/autoscaling/v2"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-app",
OwnerReferences: []metav1.OwnerReference{
Expand All @@ -250,7 +250,7 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-mirror",
OwnerReferences: []metav1.OwnerReference{
Expand All @@ -263,7 +263,7 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-clair-app",
OwnerReferences: []metav1.OwnerReference{
Expand Down Expand Up @@ -570,7 +570,7 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-app",
OwnerReferences: []metav1.OwnerReference{
Expand All @@ -583,7 +583,7 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-mirror",
OwnerReferences: []metav1.OwnerReference{
Expand All @@ -596,7 +596,7 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-clair-app",
OwnerReferences: []metav1.OwnerReference{
Expand Down Expand Up @@ -949,7 +949,7 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-app",
OwnerReferences: []metav1.OwnerReference{
Expand All @@ -962,7 +962,7 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-mirror",
OwnerReferences: []metav1.OwnerReference{
Expand All @@ -975,7 +975,7 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-clair-app",
OwnerReferences: []metav1.OwnerReference{
Expand Down Expand Up @@ -1277,7 +1277,7 @@ func TestEvaluate(t *testing.T) {
if err := ocsv1a1.AddToScheme(scheme); err != nil {
t.Fatalf("unexpected error adding ocs to scheme: %s", err)
}
if err := asv2b2.AddToScheme(scheme); err != nil {
if err := asv2.AddToScheme(scheme); err != nil {
t.Fatalf("unexpected error adding hpa to scheme: %s", err)
}
if err := appsv1.AddToScheme(scheme); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmpstatus/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"time"

asv2b2 "k8s.io/api/autoscaling/v2beta2"
asv2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (h *HPA) Check(ctx context.Context, reg qv1.QuayRegistry) (qv1.Condition, e
Name: fmt.Sprintf("%s-%s", reg.Name, hpasuffix),
}

var hpa asv2b2.HorizontalPodAutoscaler
var hpa asv2.HorizontalPodAutoscaler
if err := h.Client.Get(ctx, nsn, &hpa); err != nil {
if errors.IsNotFound(err) {
return qv1.Condition{
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmpstatus/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

asv2b2 "k8s.io/api/autoscaling/v2beta2"
asv2 "k8s.io/api/autoscaling/v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestHPACheck(t *testing.T) {
},
},
objs: []client.Object{
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-app",
},
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestHPACheck(t *testing.T) {
},
},
objs: []client.Object{
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-app",
OwnerReferences: []metav1.OwnerReference{
Expand All @@ -131,7 +131,7 @@ func TestHPACheck(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-mirror",
OwnerReferences: []metav1.OwnerReference{
Expand All @@ -144,7 +144,7 @@ func TestHPACheck(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
&asv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-clair-app",
OwnerReferences: []metav1.OwnerReference{
Expand Down
4 changes: 2 additions & 2 deletions pkg/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"golang.org/x/net/http/httpproxy"
apps "k8s.io/api/apps/v1"
autoscaling "k8s.io/api/autoscaling/v2beta2"
autoscaling "k8s.io/api/autoscaling/v2"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -174,7 +174,7 @@ func ModelFor(gvk schema.GroupVersionKind) client.Object {
return &route.Route{}
case schema.GroupVersionKind{Group: "objectbucket.io", Version: "v1alpha1", Kind: "ObjectBucketClaim"}.String():
return &objectbucket.ObjectBucketClaim{}
case schema.GroupVersionKind{Group: "autoscaling", Version: "v2beta2", Kind: "HorizontalPodAutoscaler"}.String():
case schema.GroupVersionKind{Group: "autoscaling", Version: "v2", Kind: "HorizontalPodAutoscaler"}.String():
return &autoscaling.HorizontalPodAutoscaler{}
case schema.GroupVersionKind{Group: "batch", Version: "v1", Kind: "Job"}.String():
return &batchv1.Job{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
objectbucket "github.com/kube-object-storage/lib-bucket-provisioner/pkg/apis/objectbucket.io/v1alpha1"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
autoscaling "k8s.io/api/autoscaling/v2beta2"
autoscaling "k8s.io/api/autoscaling/v2"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down

0 comments on commit 9002834

Please sign in to comment.