Skip to content

Commit

Permalink
Added environment variables to configure K8s backend default resources
Browse files Browse the repository at this point in the history
  • Loading branch information
zc-devs committed May 27, 2023
1 parent 5a8e8e2 commit 8eb240b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
24 changes: 24 additions & 0 deletions cli/exec/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,28 @@ var flags = []cli.Flag{
Usage: "backend k8s additional worker pod annotations",
Value: "",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_BACKEND_K8S_POD_MEMORY_LIMIT"},
Name: "backend-k8s-pod-memory-limit",
Usage: "backend k8s worker pod memory limit (default 2G)",
Value: "2G",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_BACKEND_K8S_POD_CPU_LIMIT"},
Name: "backend-k8s-pod-cpu-limit",
Usage: "backend k8s worker pod CPU limit (default 2)",
Value: "2",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_BACKEND_K8S_POD_MEMORY_REQUEST"},
Name: "backend-k8s-pod-memory-request",
Usage: "backend k8s worker pod memory request (default 1G)",
Value: "1G",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_BACKEND_K8S_POD_CPU_REQUEST"},
Name: "backend-k8s-pod-cpu-request",
Usage: "backend k8s worker pod CPU request (default 1)",
Value: "1",
},
}
24 changes: 24 additions & 0 deletions cmd/agent/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ var flags = []cli.Flag{
Usage: "backend k8s additional worker pod annotations",
Value: "",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_BACKEND_K8S_POD_MEMORY_LIMIT"},
Name: "backend-k8s-pod-memory-limit",
Usage: "backend k8s worker pod memory limit (default 2G)",
Value: "2G",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_BACKEND_K8S_POD_CPU_LIMIT"},
Name: "backend-k8s-pod-cpu-limit",
Usage: "backend k8s worker pod CPU limit (default 2)",
Value: "2",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_BACKEND_K8S_POD_MEMORY_REQUEST"},
Name: "backend-k8s-pod-memory-request",
Usage: "backend k8s worker pod memory request (default 1G)",
Value: "1G",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_BACKEND_K8S_POD_CPU_REQUEST"},
Name: "backend-k8s-pod-cpu-request",
Usage: "backend k8s worker pod CPU request (default 1)",
Value: "1",
},
&cli.IntFlag{
EnvVars: []string{"WOODPECKER_CONNECT_RETRY_COUNT"},
Name: "connect-retry-count",
Expand Down
11 changes: 10 additions & 1 deletion pipeline/backend/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type Config struct {
StorageRwx bool
PodLabels map[string]string
PodAnnotations map[string]string
PodMemLimit string
PodCpuLimit string
PodMemRequest string
PodCpuRequest string
}

func configFromCliContext(ctx context.Context) (*Config, error) {
Expand All @@ -53,6 +57,10 @@ func configFromCliContext(ctx context.Context) (*Config, error) {
StorageRwx: c.Bool("backend-k8s-storage-rwx"),
PodLabels: make(map[string]string), // just init empty map to prevent nil panic
PodAnnotations: make(map[string]string), // just init empty map to prevent nil panic
PodMemLimit: c.String("backend-k8s-pod-memory-limit"),
PodCpuLimit: c.String("backend-k8s-pod-cpu-limit"),
PodMemRequest: c.String("backend-k8s-pod-memory-request"),
PodCpuRequest: c.String("backend-k8s-pod-cpu-request"),
}
// Unmarshal label and annotation settings here to ensure they're valid on startup
if labels := c.String("backend-k8s-pod-labels"); labels != "" {
Expand Down Expand Up @@ -169,7 +177,8 @@ func (e *kube) Setup(ctx context.Context, conf *types.Config) error {

// Start the pipeline step.
func (e *kube) Exec(ctx context.Context, step *types.Step) error {
pod, err := Pod(e.config.Namespace, step, e.config.PodLabels, e.config.PodAnnotations)
pod, err := Pod(e.config.Namespace, step, e.config.PodLabels, e.config.PodAnnotations,
e.config.PodMemLimit, e.config.PodCpuLimit, e.config.PodMemRequest, e.config.PodCpuRequest)
if err != nil {
return err
}
Expand Down
24 changes: 11 additions & 13 deletions pipeline/backend/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Pod(namespace string, step *types.Step, labels, annotations map[string]string) (*v1.Pod, error) {
func Pod(namespace string, step *types.Step, labels, annotations map[string]string,
memLimit string, cpuLimit string, memRequest string, cpuRequest string) (*v1.Pod, error) {

var (
vols []v1.Volume
volMounts []v1.VolumeMount
Expand Down Expand Up @@ -72,24 +74,20 @@ func Pod(namespace string, step *types.Step, labels, annotations map[string]stri
// memoryLimit := resource.MustParse(step.Resources.MemoryLimit)
// CPULimit := resource.MustParse(step.Resources.CPULimit)

memoryLimit := resource.MustParse("2G")
CPULimit := resource.MustParse("2")

memoryLimitValue, _ := memoryLimit.AsInt64()
CPULimitValue, _ := CPULimit.AsInt64()
loadfactor := 0.5
memLim := resource.MustParse(memLimit)
cpuLim := resource.MustParse(cpuLimit)

memoryRequest := resource.NewQuantity(int64(float64(memoryLimitValue)*loadfactor), resource.DecimalSI)
CPURequest := resource.NewQuantity(int64(float64(CPULimitValue)*loadfactor), resource.DecimalSI)
memReq := resource.MustParse(memRequest)
cpuReq := resource.MustParse(cpuRequest)

resources := v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceMemory: *memoryRequest,
v1.ResourceCPU: *CPURequest,
v1.ResourceMemory: memReq,
v1.ResourceCPU: cpuReq,
},
Limits: v1.ResourceList{
v1.ResourceMemory: memoryLimit,
v1.ResourceCPU: CPULimit,
v1.ResourceMemory: memLim,
v1.ResourceCPU: cpuLim,
},
}

Expand Down

0 comments on commit 8eb240b

Please sign in to comment.