Skip to content

Commit

Permalink
hpa: Only deploy clair HPA when HPA component is managed (PROJQUAY-2435
Browse files Browse the repository at this point in the history
…) (#520)

- Clair HPA is only deployed when HPA component is managed
  • Loading branch information
jonathankingfc authored and ricardomaraschini committed Oct 28, 2021
1 parent 283983e commit a2d08d8
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 23 deletions.
1 change: 0 additions & 1 deletion kustomize/components/clair/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ resources:
- ./clair.serviceaccount.yaml
- ./postgres.serviceaccount.yaml
- ./clair.deployment.yaml
- ./clair.horizontalpodautoscaler.yaml
- ./clair.service.yaml
- ./postgres.persistentvolumeclaim.yaml
- ./postgres.deployment.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
- ./quay.horizontalpodautoscalar.yaml
- ./clair.horizontalpodautoscaler.yaml
- ./mirror.horizontalpodautoscaler.yaml

1 change: 0 additions & 1 deletion kustomize/components/mirror/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
- ./mirror.deployment.yaml
- ./mirror.horizontalpodautoscaler.yaml
vars:
- name: QUAY_APP_SERVICE_HOST
objref:
Expand Down
78 changes: 78 additions & 0 deletions pkg/cmpstatus/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,32 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-mirror",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "QuayRegistry",
Name: "registry",
APIVersion: "quay.redhat.com/v1",
UID: "uid",
},
},
},
},
&asv2b2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-clair-app",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "QuayRegistry",
Name: "registry",
APIVersion: "quay.redhat.com/v1",
UID: "uid",
},
},
},
},
&monv1.PrometheusRule{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-prometheus-rules",
Expand Down Expand Up @@ -511,6 +537,32 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-mirror",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "QuayRegistry",
Name: "registry",
APIVersion: "quay.redhat.com/v1",
UID: "uid",
},
},
},
},
&asv2b2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-clair-app",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "QuayRegistry",
Name: "registry",
APIVersion: "quay.redhat.com/v1",
UID: "uid",
},
},
},
},
&monv1.PrometheusRule{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-prometheus-rules",
Expand Down Expand Up @@ -840,6 +892,32 @@ func TestEvaluate(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-mirror",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "QuayRegistry",
Name: "registry",
APIVersion: "quay.redhat.com/v1",
UID: "uid",
},
},
},
},
&asv2b2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-clair-app",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "QuayRegistry",
Name: "registry",
APIVersion: "quay.redhat.com/v1",
UID: "uid",
},
},
},
},
&monv1.PrometheusRule{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-prometheus-rules",
Expand Down
40 changes: 21 additions & 19 deletions pkg/cmpstatus/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,35 @@ func (h *HPA) Check(ctx context.Context, reg qv1.QuayRegistry) (qv1.Condition, e
}, nil
}

nsn := types.NamespacedName{
Namespace: reg.Namespace,
Name: fmt.Sprintf("%s-quay-app", reg.Name),
}
for _, hpasuffix := range []string{"quay-app", "clair-app", "quay-mirror"} {
nsn := types.NamespacedName{
Namespace: reg.Namespace,
Name: fmt.Sprintf("%s-%s", reg.Name, hpasuffix),
}

var hpa asv2b2.HorizontalPodAutoscaler
if err := h.Client.Get(ctx, nsn, &hpa); err != nil {
if errors.IsNotFound(err) {
var hpa asv2b2.HorizontalPodAutoscaler
if err := h.Client.Get(ctx, nsn, &hpa); err != nil {
if errors.IsNotFound(err) {
return qv1.Condition{
Type: qv1.ComponentHPAReady,
Status: metav1.ConditionFalse,
Reason: qv1.ConditionReasonComponentNotReady,
Message: "Horizontal pod autoscaler not found",
LastUpdateTime: metav1.NewTime(time.Now()),
}, nil
}
return zero, err
}

if !qv1.Owns(reg, &hpa) {
return qv1.Condition{
Type: qv1.ComponentHPAReady,
Status: metav1.ConditionFalse,
Reason: qv1.ConditionReasonComponentNotReady,
Message: "Horizontal pod autoscaler not found",
Message: "Horizontal pod autoscaler not owned by QuayRegistry",
LastUpdateTime: metav1.NewTime(time.Now()),
}, nil
}
return zero, err
}

if !qv1.Owns(reg, &hpa) {
return qv1.Condition{
Type: qv1.ComponentHPAReady,
Status: metav1.ConditionFalse,
Reason: qv1.ConditionReasonComponentNotReady,
Message: "Horizontal pod autoscaler not owned by QuayRegistry",
LastUpdateTime: metav1.NewTime(time.Now()),
}, nil
}

return qv1.Condition{
Expand Down
26 changes: 26 additions & 0 deletions pkg/cmpstatus/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,32 @@ func TestHPACheck(t *testing.T) {
},
},
},
&asv2b2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-quay-mirror",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "QuayRegistry",
Name: "registry",
APIVersion: "quay.redhat.com/v1",
UID: "uid",
},
},
},
},
&asv2b2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "registry-clair-app",
OwnerReferences: []metav1.OwnerReference{
{
Kind: "QuayRegistry",
Name: "registry",
APIVersion: "quay.redhat.com/v1",
UID: "uid",
},
},
},
},
},
cond: qv1.Condition{
Type: qv1.ComponentHPAReady,
Expand Down
4 changes: 2 additions & 2 deletions pkg/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ var quayComponents = map[string][]client.Object{
&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "clair-config-secret"}},
&appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "clair-app"}},
&corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "clair-app"}},
&autoscaling.HorizontalPodAutoscaler{ObjectMeta: metav1.ObjectMeta{Name: "clair-app"}},
&appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "clair-postgres"}},
&corev1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{Name: "clair-postgres"}},
&corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "clair-postgres"}},
Expand Down Expand Up @@ -283,10 +282,11 @@ var quayComponents = map[string][]client.Object{
},
"mirror": {
&appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "quay-mirror"}},
&autoscaling.HorizontalPodAutoscaler{ObjectMeta: metav1.ObjectMeta{Name: "quay-mirror"}},
},
"horizontalpodautoscaler": {
&autoscaling.HorizontalPodAutoscaler{ObjectMeta: metav1.ObjectMeta{Name: "quay-app"}},
&autoscaling.HorizontalPodAutoscaler{ObjectMeta: metav1.ObjectMeta{Name: "quay-mirror"}},
&autoscaling.HorizontalPodAutoscaler{ObjectMeta: metav1.ObjectMeta{Name: "clair-app"}},
},
}

Expand Down

0 comments on commit a2d08d8

Please sign in to comment.