Refactored K8s Pod node selector (#1909)

- using existing map instead of an array
- applying var name convention
- using arch constant
This commit is contained in:
Thomas Anderson 2023-07-07 08:46:48 +03:00 committed by GitHub
parent 205f8ecc16
commit d46b91a94c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,7 @@ import (
"github.com/woodpecker-ci/woodpecker/pipeline/backend/common" "github.com/woodpecker-ci/woodpecker/pipeline/backend/common"
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types" "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
"golang.org/x/exp/maps"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -80,9 +81,9 @@ func Pod(namespace string, step *types.Step, labels, annotations map[string]stri
} }
} }
var ServiceAccountName string var serviceAccountName string
if step.BackendOptions.Kubernetes.ServiceAccountName != "" { if step.BackendOptions.Kubernetes.ServiceAccountName != "" {
ServiceAccountName = step.BackendOptions.Kubernetes.ServiceAccountName serviceAccountName = step.BackendOptions.Kubernetes.ServiceAccountName
} }
podName, err := dnsName(step.Name) podName, err := dnsName(step.Name)
@ -92,18 +93,19 @@ func Pod(namespace string, step *types.Step, labels, annotations map[string]stri
labels["step"] = podName labels["step"] = podName
var platform string var nodeSelector map[string]string
for _, e := range mapToEnvVars(step.Environment) { if platform, exist := step.Environment["CI_SYSTEM_PLATFORM"]; exist {
if e.Name == "CI_SYSTEM_ARCH" { arch := strings.Split(platform, "/")[1]
platform = e.Value nodeSelector = map[string]string{v1.LabelArchStable: arch}
break
}
} }
NodeSelector := map[string]string{"kubernetes.io/arch": strings.Split(platform, "/")[1]} beOptNodeSelector := step.BackendOptions.Kubernetes.NodeSelector
if len(beOptNodeSelector) > 0 {
for key, val := range step.BackendOptions.Kubernetes.NodeSelector { if len(nodeSelector) == 0 {
NodeSelector[key] = val nodeSelector = beOptNodeSelector
} else {
maps.Copy(nodeSelector, beOptNodeSelector)
}
} }
pod := &v1.Pod{ pod := &v1.Pod{
@ -116,8 +118,8 @@ func Pod(namespace string, step *types.Step, labels, annotations map[string]stri
Spec: v1.PodSpec{ Spec: v1.PodSpec{
RestartPolicy: v1.RestartPolicyNever, RestartPolicy: v1.RestartPolicyNever,
HostAliases: hostAliases, HostAliases: hostAliases,
NodeSelector: NodeSelector, NodeSelector: nodeSelector,
ServiceAccountName: ServiceAccountName, ServiceAccountName: serviceAccountName,
Containers: []v1.Container{{ Containers: []v1.Container{{
Name: podName, Name: podName,
Image: step.Image, Image: step.Image,