Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make container resources configurable #163

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
96 changes: 96 additions & 0 deletions pkg/addon/configpolicy/agent_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"strconv"

"github.com/openshift/library-go/pkg/controller/controllercmd"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/klog"
"open-cluster-management.io/addon-framework/pkg/addonfactory"
"open-cluster-management.io/addon-framework/pkg/addonmanager"
"open-cluster-management.io/addon-framework/pkg/agent"
Expand Down Expand Up @@ -195,6 +198,98 @@ func getValues(cluster *clusterv1.ManagedCluster,
return addonfactory.JsonStructToValues(userValues)
}

func toAddonResources(config addonapiv1alpha1.AddOnDeploymentConfig) (addonfactory.Values, error) {
defaultRequestMem, err := resource.ParseQuantity("128Mi")
if err != nil {
return nil, fmt.Errorf("failed to parse default memory request: %w", err)
}

defaultLimitMem, err := resource.ParseQuantity("512Mi")
if err != nil {
return nil, fmt.Errorf("failed to parse default memory limit: %w", err)
}

jsonStruct := struct {
Resources corev1.ResourceRequirements `json:"resources"`
}{
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: defaultRequestMem,
},
Limits: corev1.ResourceList{
corev1.ResourceMemory: defaultLimitMem,
},
},
}

var newRequestMem, newLimitMem resource.Quantity

for _, variable := range config.Spec.CustomizedVariables {
switch variable.Name {
case "RequestsMemory":
newRequestMem, err = resource.ParseQuantity(variable.Value)
if err != nil {
klog.Info(fmt.Sprintf(
"Failed to parse configured memory request '%s'. Falling back to the default %s.",
variable.Value, defaultRequestMem.String(),
))

continue
} else if newRequestMem.Cmp(defaultRequestMem) == -1 {
klog.Info(fmt.Sprintf(
"Refusing to set lower configured memory request '%s'. Falling back to the default %s.",
newRequestMem.String(), defaultRequestMem.String(),
))

continue
}

jsonStruct.Resources.Requests = corev1.ResourceList{
corev1.ResourceMemory: newRequestMem,
}
case "LimitsMemory":
newLimitMem, err = resource.ParseQuantity(variable.Value)
if err != nil {
klog.Info(fmt.Sprintf(
"Failed to parse configured memory limit '%s'. Falling back to the default %s.",
variable.Value, defaultLimitMem.String(),
))

continue
}

if newLimitMem.Cmp(defaultLimitMem) == -1 {
klog.Info(fmt.Sprintf(
"Refusing to set a lower configured memory limit '%s'. Falling back to the default %s.",
newLimitMem.String(), defaultLimitMem.String(),
))

continue
}

jsonStruct.Resources.Limits = corev1.ResourceList{
corev1.ResourceMemory: newLimitMem,
}
}
}

if newRequestMem.Cmp(newLimitMem) == 1 {
klog.Error(fmt.Sprintf("Configured request memory '%s' may not exceed configured limit '%s'. "+
"Setting request equal to limit.", newRequestMem.String(), newLimitMem.String()))

jsonStruct.Resources.Requests = corev1.ResourceList{
corev1.ResourceMemory: newLimitMem,
}
}

values, err := addonfactory.JsonStructToValues(jsonStruct)
if err != nil {
return nil, err
}

return values, nil
}

// mandateValues sets deployment variables regardless of user overrides. As a result, caution should
// be taken when adding settings to this function.
func mandateValues(
Expand Down Expand Up @@ -239,6 +334,7 @@ func GetAgentAddon(ctx context.Context, controllerContext *controllercmd.Control
addonfactory.NewAddOnDeploymentConfigGetter(addonClient),
addonfactory.ToAddOnNodePlacementValues,
addonfactory.ToAddOnCustomizedVariableValues,
toAddonResources,
),
getValues,
addonfactory.GetValuesFromAddonAnnotation,
Expand Down
96 changes: 96 additions & 0 deletions pkg/addon/policyframework/agent_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"strings"

"github.com/openshift/library-go/pkg/controller/controllercmd"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/klog"
"open-cluster-management.io/addon-framework/pkg/addonfactory"
"open-cluster-management.io/addon-framework/pkg/addonmanager"
"open-cluster-management.io/addon-framework/pkg/agent"
Expand Down Expand Up @@ -209,6 +212,98 @@ func getValues(cluster *clusterv1.ManagedCluster,
return addonfactory.JsonStructToValues(userValues)
}

func toAddonResources(config addonapiv1alpha1.AddOnDeploymentConfig) (addonfactory.Values, error) {
defaultRequestMem, err := resource.ParseQuantity("64Mi")
if err != nil {
return nil, fmt.Errorf("failed to parse default memory request: %w", err)
}

defaultLimitMem, err := resource.ParseQuantity("512Mi")
if err != nil {
return nil, fmt.Errorf("failed to parse default memory limit: %w", err)
}

jsonStruct := struct {
Resources corev1.ResourceRequirements `json:"resources"`
}{
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: defaultRequestMem,
},
Limits: corev1.ResourceList{
corev1.ResourceMemory: defaultLimitMem,
},
},
}

var newRequestMem, newLimitMem resource.Quantity

for _, variable := range config.Spec.CustomizedVariables {
switch variable.Name {
case "RequestsMemory":
newRequestMem, err = resource.ParseQuantity(variable.Value)
if err != nil {
klog.Info(fmt.Sprintf(
"Failed to parse configured memory request '%s'. Falling back to the default %s.",
variable.Value, defaultRequestMem.String(),
))

continue
} else if newRequestMem.Cmp(defaultRequestMem) == -1 {
klog.Info(fmt.Sprintf(
"Refusing to set lower configured memory request '%s'. Falling back to the default %s.",
newRequestMem.String(), defaultRequestMem.String(),
))

continue
}

jsonStruct.Resources.Requests = corev1.ResourceList{
corev1.ResourceMemory: newRequestMem,
}
case "LimitsMemory":
newLimitMem, err = resource.ParseQuantity(variable.Value)
if err != nil {
klog.Info(fmt.Sprintf(
"Failed to parse configured memory limit '%s'. Falling back to the default %s.",
variable.Value, defaultLimitMem.String(),
))

continue
}

if newLimitMem.Cmp(defaultLimitMem) == -1 {
klog.Info(fmt.Sprintf(
"Refusing to set a lower configured memory limit '%s'. Falling back to the default %s.",
newLimitMem.String(), defaultLimitMem.String(),
))

continue
}

jsonStruct.Resources.Limits = corev1.ResourceList{
corev1.ResourceMemory: newLimitMem,
}
}
}

if newRequestMem.Cmp(newLimitMem) == 1 {
klog.Error(fmt.Sprintf("Configured request memory '%s' may not exceed configured limit '%s'. "+
"Setting request equal to limit.", newRequestMem.String(), newLimitMem.String()))

jsonStruct.Resources.Requests = corev1.ResourceList{
corev1.ResourceMemory: newLimitMem,
}
}

values, err := addonfactory.JsonStructToValues(jsonStruct)
if err != nil {
return nil, err
}

return values, nil
}

// mandateValues sets deployment variables regardless of user overrides. As a result, caution should
// be taken when adding settings to this function.
func mandateValues(
Expand Down Expand Up @@ -253,6 +348,7 @@ func GetAgentAddon(ctx context.Context, controllerContext *controllercmd.Control
addonfactory.NewAddOnDeploymentConfigGetter(addonClient),
addonfactory.ToAddOnNodePlacementValues,
addonfactory.ToAddOnCustomizedVariableValues,
toAddonResources,
),
getValues,
addonfactory.GetValuesFromAddonAnnotation,
Expand Down
Loading