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

Add support for container resource requests and limits #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion internal/pkg/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
// PropertiesFile, assigned with --cvfile.
PropertiesFile string
// BuildParams Slice of strings to hold overridden values when using dev-build command.
BuildParams []string
BuildParams []string

// Potentially used by BuildConfig creation to override output imagestream
OutputImagestream string
Expand Down Expand Up @@ -103,4 +103,10 @@ var (
CreateStatefulSetPersistentVolumeClaim bool = false
StatefulSetPersistentVolumeClaimStorageClass string
StatefulSetPersistentVolumeClaimSize string

// strings to hold resource requests and limits
ContainerResourceRequestsCPU string
ContainerResourceRequestsMemory string
ContainerResourceLimitsCPU string
ContainerResourceLimitsMemory string
)
6 changes: 5 additions & 1 deletion mg/cmd/create-deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func init() {
createDeploymentCmd.Flags().BoolVar(&params.WithAffinityRules, "with-affinity-rules", params.WithPodAffinityRulesDefault, "Enable generation of pod affinity or anti-affinity rules.")
createDeploymentCmd.Flags().StringVar(&params.PodAntiAffinityTopologyKey, "anti-affinity-topology-key", "", "Define which node label to use as a topologyKey (describing a datacenter, zone or a rack as an example)")
createDeploymentCmd.Flags().Int32Var(&params.PodAntiAffinityWeight, "pod-anti-affinity-weight", params.PodAntiAffinityWeightDefault, "Weight for WeightedPodAffinityTerm.")
createDeploymentCmd.Flags().BoolVar(&params.DownwardAPIEnvVars,"downward-api-envvars",false,"Enables generation of environment variables from Downward API. An opinionated selection.")
createDeploymentCmd.Flags().BoolVar(&params.DownwardAPIEnvVars, "downward-api-envvars", false, "Enables generation of environment variables from Downward API. An opinionated selection.")
createDeploymentCmd.Flags().StringVar(&params.ContainerResourceRequestsCPU, "resource-requests-cpu", "", "Sets container CPU resource request")
createDeploymentCmd.Flags().StringVar(&params.ContainerResourceRequestsMemory, "resource-requests-memory", "", "Sets container Memory resource request")
createDeploymentCmd.Flags().StringVar(&params.ContainerResourceLimitsCPU, "resource-limits-cpu", "", "Sets container CPU resource limits")
createDeploymentCmd.Flags().StringVar(&params.ContainerResourceLimitsMemory, "resource-limits-memory", "", "Sets container Memory resource limits")
}

var createDeploymentCmd = &cobra.Command{
Expand Down
4 changes: 4 additions & 0 deletions mg/cmd/create-statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func init() {
createStatefulsetCmd.Flags().BoolVar(&params.CreateStatefulSetPersistentVolumeClaim, "create-persistent-volumeclaim", false, "Enables the creation of a persistent volume claims template.")
createStatefulsetCmd.Flags().StringVar(&params.StatefulSetPersistentVolumeClaimStorageClass, "statefulset-pv-storageclass", "", "StatefulSet persistent volume claim storage class")
createStatefulsetCmd.Flags().StringVar(&params.StatefulSetPersistentVolumeClaimSize, "statefulset-pv-size", "", "StatefulSet persistent volume claim size")
createStatefulsetCmd.Flags().StringVar(&params.ContainerResourceRequestsCPU, "resource-requests-cpu", "", "Sets container CPU resource request")
createStatefulsetCmd.Flags().StringVar(&params.ContainerResourceRequestsMemory, "resource-requests-memory", "", "Sets container Memory resource request")
createStatefulsetCmd.Flags().StringVar(&params.ContainerResourceLimitsCPU, "resource-limits-cpu", "", "Sets container CPU resource limits")
createStatefulsetCmd.Flags().StringVar(&params.ContainerResourceLimitsMemory, "resource-limits-memory", "", "Sets container Memory resource limits")
}

var createStatefulsetCmd = &cobra.Command{
Expand Down
25 changes: 25 additions & 0 deletions pkg/modules/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/laetho/metagraf/internal/pkg/params"
"github.com/laetho/metagraf/pkg/metagraf"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"

//corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -126,6 +127,29 @@ func GenDeployment(mg *metagraf.MetaGraf, namespace string) {

Volumes, VolumeMounts = volumes(mg, ImageInfo)
}

// Container resource requests & limits
resources := corev1.ResourceRequirements{
Requests: corev1.ResourceList{},
Limits: corev1.ResourceList{},
}

// Check if any resource requests is set
if len(params.ContainerResourceRequestsCPU) > 0 {
resources.Requests["cpu"] = resource.MustParse(params.ContainerResourceRequestsCPU)
}
if len(params.ContainerResourceRequestsMemory) > 0 {
resources.Requests["memory"] = resource.MustParse(params.ContainerResourceRequestsMemory)
}

// Check if any resource limits is set
if len(params.ContainerResourceLimitsCPU) > 0 {
resources.Limits["cpu"] = resource.MustParse(params.ContainerResourceLimitsCPU)
}
if len(params.ContainerResourceLimitsMemory) > 0 {
resources.Limits["memory"] = resource.MustParse(params.ContainerResourceLimitsMemory)
}

// Tying Container PodSpec together
Container := corev1.Container{
Name: objname,
Expand All @@ -134,6 +158,7 @@ func GenDeployment(mg *metagraf.MetaGraf, namespace string) {
Ports: ContainerPorts,
VolumeMounts: VolumeMounts,
Env: EnvVars,
Resources: resources,
}
// Checking for Probes
probe := corev1.Probe{}
Expand Down
23 changes: 23 additions & 0 deletions pkg/modules/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ func GenStatefulSet(mg *metagraf.MetaGraf, namespace string) {

}

// Container resource requests & limits
resources := corev1.ResourceRequirements{
Requests: corev1.ResourceList{},
Limits: corev1.ResourceList{},
}

// Check if any resource requests is set
if len(params.ContainerResourceRequestsCPU) > 0 {
resources.Requests["cpu"] = resource.MustParse(params.ContainerResourceRequestsCPU)
}
if len(params.ContainerResourceRequestsMemory) > 0 {
resources.Requests["memory"] = resource.MustParse(params.ContainerResourceRequestsMemory)
}

// Check if any resource limits is set
if len(params.ContainerResourceLimitsCPU) > 0 {
resources.Limits["cpu"] = resource.MustParse(params.ContainerResourceLimitsCPU)
}
if len(params.ContainerResourceLimitsMemory) > 0 {
resources.Limits["memory"] = resource.MustParse(params.ContainerResourceLimitsMemory)
}

// Tying Container PodSpec together
Container := corev1.Container{
Name: objname,
Expand All @@ -140,6 +162,7 @@ func GenStatefulSet(mg *metagraf.MetaGraf, namespace string) {
Ports: ContainerPorts,
VolumeMounts: VolumeMounts,
Env: EnvVars,
Resources: resources,
}
// Checking for Probes
probe := corev1.Probe{}
Expand Down