Skip to content
This repository was archived by the owner on Jun 24, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gopkg.lock

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

39 changes: 39 additions & 0 deletions config/300-serving-v1alpha1-knativeserving-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,45 @@ spec:
description: The number of replicas that HA parts of the control plane will be scaled to
type: integer
minimum: 1
resources:
description: A mapping of deployment name to resource requirements
type: array
items:
type: object
properties:
container:
description: The name of the container
type: string
requests:
type: object
properties:
memory:
type: string
pattern: ^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$
cpu:
type: string
pattern: ^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$
storage:
type: string
pattern: ^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$
ephemeral-storage:
type: string
pattern: ^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$
limits:
type: object
properties:
memory:
type: string
pattern: ^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$
cpu:
type: string
pattern: ^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$
storage:
type: string
pattern: ^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$
ephemeral-storage:
type: string
pattern: ^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$
type: object
status:
description: Status defines the observed state of KnativeServing
Expand Down
13 changes: 13 additions & 0 deletions pkg/apis/serving/v1alpha1/knativeserving_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ type HighAvailability struct {
Replicas int32 `json:"replicas"`
}

// ResourceRequirementsOverride enables the user to override any container's
// resource requests/limits specified in the embedded manifest
type ResourceRequirementsOverride struct {
// The container name
Container string `json:"container"`
// The desired ResourceRequirements
corev1.ResourceRequirements
}

// KnativeServingSpec defines the desired state of KnativeServing
// +k8s:openapi-gen=true
type KnativeServingSpec struct {
Expand Down Expand Up @@ -105,6 +114,10 @@ type KnativeServingSpec struct {
// Allows specification of HA control plane
// +optional
HighAvailability *HighAvailability `json:"high-availability,omitempty"`

// Override containers' resource requirements
// +optional
Resources []ResourceRequirementsOverride `json:"resources,omitempty"`
}

// KnativeServingStatus defines the observed state of KnativeServing
Expand Down
24 changes: 24 additions & 0 deletions pkg/apis/serving/v1alpha1/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions pkg/reconciler/knativeserving/common/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (platforms Platforms) Transformers(kubeClientSet kubernetes.Interface, inst
GatewayTransform(instance, log),
CustomCertsTransform(instance, log),
HighAvailabilityTransform(instance, log),
ResourceRequirementsTransform(instance, log),
}
for _, fn := range platforms {
transformer, err := fn(kubeClientSet, log)
Expand Down
73 changes: 73 additions & 0 deletions pkg/reconciler/knativeserving/common/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2020 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 common

import (
mf "github.com/manifestival/manifestival"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes/scheme"
servingv1alpha1 "knative.dev/serving-operator/pkg/apis/serving/v1alpha1"
)

// ResourceRequirementsTransform configures the resource requests for
// all containers within all deployments in the manifest
func ResourceRequirementsTransform(instance *servingv1alpha1.KnativeServing, log *zap.SugaredLogger) mf.Transformer {
Comment thread
jcrossley3 marked this conversation as resolved.
return func(u *unstructured.Unstructured) error {
if u.GetKind() == "Deployment" {
deployment := &appsv1.Deployment{}
if err := scheme.Scheme.Convert(u, deployment, nil); err != nil {
return err
}
containers := deployment.Spec.Template.Spec.Containers
for i := range containers {
if override := find(instance, containers[i].Name); override != nil {
merge(&override.Limits, &containers[i].Resources.Limits)
merge(&override.Requests, &containers[i].Resources.Requests)
}
}
if err := scheme.Scheme.Convert(deployment, u, nil); err != nil {
return err
}
// Avoid superfluous updates from converted zero defaults
u.SetCreationTimestamp(metav1.Time{})
}
return nil
}
}

func merge(src, tgt *v1.ResourceList) {
if len(*tgt) > 0 {
for k, v := range *src {
(*tgt)[k] = v
}
} else {
*tgt = *src
}
}

func find(instance *servingv1alpha1.KnativeServing, name string) *servingv1alpha1.ResourceRequirementsOverride {
for _, override := range instance.Spec.Resources {
if override.Container == name {
return &override
}
}
return nil
}
Loading