Skip to content

Commit

Permalink
overrides: Add resource request/limit overrides (PROJQUAY-6254)
Browse files Browse the repository at this point in the history
- Allow user to specify resource limits and requests per deployment
  • Loading branch information
jonathankingfc authored and openshift-merge-bot[bot] committed Feb 15, 2024
1 parent 0f0e1b9 commit fc70d9c
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 0 deletions.
43 changes: 43 additions & 0 deletions apis/quay/v1/quayregistry_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ var supportsEnvOverride = []ComponentKind{
ComponentRedis,
}

var supportsResourceOverrides = []ComponentKind{
ComponentQuay,
ComponentClair,
ComponentMirror,
ComponentPostgres,
ComponentClairPostgres,
}

var supportsReplicasOverride = []ComponentKind{
ComponentClair,
ComponentMirror,
Expand Down Expand Up @@ -142,6 +150,13 @@ type Override struct {
Affinity *corev1.Affinity `json:"affinity,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Resources *Resources `json:"resources,omitempty"`
}

// Resources describes the resource limits and requests for a component.
type Resources struct {
Limits corev1.ResourceList `json:"limits,omitempty"`
Requests corev1.ResourceList `json:"requests,omitempty"`
}

type ConditionType string
Expand Down Expand Up @@ -491,6 +506,7 @@ func ValidateOverrides(quay *QuayRegistry) error {
hasaffinity := hasAffinity(component)
hasvolume := component.Overrides.VolumeSize != nil
hasreplicas := component.Overrides.Replicas != nil
hasresources := component.Overrides.Resources != nil
hasenvvar := len(component.Overrides.Env) > 0
hasoverride := hasaffinity || hasvolume || hasenvvar || hasreplicas

Expand Down Expand Up @@ -534,6 +550,13 @@ func ValidateOverrides(quay *QuayRegistry) error {
component.Kind,
)
}

if hasresources && !ComponentSupportsOverride(component.Kind, "resources") {
return fmt.Errorf(
"component %s does not support resources overrides",
component.Kind,
)
}
}

return nil
Expand Down Expand Up @@ -713,6 +736,8 @@ func ComponentSupportsOverride(component ComponentKind, override string) bool {
components = supportsReplicasOverride
case "affinity":
components = supportsAffinityOverride
case "resources":
components = supportsResourceOverrides
}

for _, cmp := range components {
Expand Down Expand Up @@ -762,6 +787,24 @@ func GetVolumeSizeOverrideForComponent(
return
}

// GetResourceOverridesForComponent returns the resource overrides set by the user for the
// provided component. Returns nil if not set.
func GetResourceOverridesForComponent(
quay *QuayRegistry, kind ComponentKind,
) (resources *Resources) {
for _, component := range quay.Spec.Components {
if component.Kind != kind {
continue
}

if component.Overrides != nil && component.Overrides.Resources != nil {
resources = component.Overrides.Resources
}
return
}
return
}

// GetAffinityForComponent returns affinity overrides for the provided component
// if they are present, nil otherwise
func GetAffinityForComponent(quay *QuayRegistry, kind ComponentKind) (affinity *corev1.Affinity) {
Expand Down
34 changes: 34 additions & 0 deletions apis/quay/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions bundle/manifests/quayregistries.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,31 @@ spec:
replicas:
format: int32
type: integer
resources:
description: Resources describes the resource limits and
requests for a component.
properties:
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: ResourceList is a set of (resource name,
quantity) pairs.
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: ResourceList is a set of (resource name,
quantity) pairs.
type: object
type: object
volumeSize:
anyOf:
- type: integer
Expand Down
25 changes: 25 additions & 0 deletions config/crd/bases/quay.redhat.com_quayregistries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,31 @@ spec:
replicas:
format: int32
type: integer
resources:
description: Resources describes the resource limits and
requests for a component.
properties:
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: ResourceList is a set of (resource name,
quantity) pairs.
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: ResourceList is a set of (resource name,
quantity) pairs.
type: object
type: object
volumeSize:
anyOf:
- type: integer
Expand Down
50 changes: 50 additions & 0 deletions e2e/resource_overrides/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: skynet-quay-app
spec:
template:
spec:
containers:
- name: quay-app
resources:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 100m
memory: 100Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: skynet-quay-database
spec:
template:
spec:
containers:
- name: postgres
resources:
limits:
cpu: 200m
memory: 200Mi
requests:
cpu: 200m
memory: 200Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: skynet-clair-app
spec:
template:
spec:
containers:
- name: clair-app
resources:
limits:
cpu: 300m
memory: 300Mi
requests:
cpu: 300m
memory: 300Mi
36 changes: 36 additions & 0 deletions e2e/resource_overrides/00-create-quay-registry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: quay.redhat.com/v1
kind: QuayRegistry
metadata:
name: skynet
spec:
components:
- kind: quay
managed: true
overrides:
resources:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 100m
memory: 100Mi
- kind: postgres
managed: true
overrides:
resources:
limits:
cpu: 200m
memory: 200Mi
requests:
cpu: 200m
memory: 200Mi
- kind: clair
managed: true
overrides:
resources:
limits:
cpu: 300m
memory: 300Mi
requests:
cpu: 300m
memory: 300Mi
10 changes: 10 additions & 0 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func Process(quay *v1.QuayRegistry, qctx *quaycontext.QuayRegistryContext, obj c
}
}

if oresources := v1.GetResourceOverridesForComponent(quay, kind); oresources != nil {
ref := &dep.Spec.Template.Spec.Containers[0]
if oresources.Requests != nil {
ref.Resources.Requests = oresources.Requests
}
if oresources.Limits != nil {
ref.Resources.Limits = oresources.Limits
}
}

if oannot := v1.GetAnnotationsOverrideForComponent(quay, kind); oannot != nil {
if dep.Annotations == nil {
dep.Annotations = map[string]string{}
Expand Down

0 comments on commit fc70d9c

Please sign in to comment.