Use UUID as podName and cleanup arguments for Kubernetes backend (#3135)

to much args are just horrible to maintain. And we already have it nice
structured stored as step.
This commit is contained in:
6543
2024-01-11 16:32:37 +01:00
committed by GitHub
parent 7756c60a33
commit d1fe86b7be
7 changed files with 201 additions and 108 deletions

View File

@@ -32,27 +32,18 @@ import (
const (
StepLabel = "step"
podPrefix = "wp-"
)
func mkPod(namespace, name, image, workDir, goos, serviceAccountName string,
pool, privileged bool,
commands, vols, pullSecretNames []string,
labels, annotations, env, nodeSelector map[string]string,
extraHosts []types.HostAlias, tolerations []types.Toleration, resources types.Resources,
securityContext *types.SecurityContext, securityContextConfig SecurityContextConfig,
) (*v1.Pod, error) {
var err error
func mkPod(step *types.Step, config *config, podName, goos string) (*v1.Pod, error) {
meta := podMeta(step, config, podName)
meta := podMeta(name, namespace, labels, annotations)
spec, err := podSpec(serviceAccountName, vols, pullSecretNames, env, nodeSelector, extraHosts, tolerations,
securityContext, securityContextConfig)
spec, err := podSpec(step, config)
if err != nil {
return nil, err
}
container, err := podContainer(name, image, workDir, goos, pool, privileged, commands, vols, env,
resources, securityContext)
container, err := podContainer(step, podName, goos)
if err != nil {
return nil, err
}
@@ -74,41 +65,37 @@ func stepToPodName(step *types.Step) (name string, err error) {
}
func podName(step *types.Step) (string, error) {
return dnsName(step.Name)
return dnsName(podPrefix + step.UUID)
}
func podMeta(name, namespace string, labels, annotations map[string]string) metav1.ObjectMeta {
func podMeta(step *types.Step, config *config, podName string) metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: annotations,
Name: podName,
Namespace: config.Namespace,
Annotations: config.PodAnnotations,
}
if labels == nil {
labels = make(map[string]string, 1)
}
labels[StepLabel] = name
labels := make(map[string]string, len(config.PodLabels)+1)
// copy to not alter the engine config
maps.Copy(labels, config.PodLabels)
labels[StepLabel] = step.Name
meta.Labels = labels
return meta
}
func podSpec(serviceAccountName string, vols, pullSecretNames []string, env, backendNodeSelector map[string]string,
extraHosts []types.HostAlias, backendTolerations []types.Toleration,
securityContext *types.SecurityContext, securityContextConfig SecurityContextConfig,
) (v1.PodSpec, error) {
func podSpec(step *types.Step, config *config) (v1.PodSpec, error) {
var err error
spec := v1.PodSpec{
RestartPolicy: v1.RestartPolicyNever,
ServiceAccountName: serviceAccountName,
ImagePullSecrets: imagePullSecretsReferences(pullSecretNames),
ServiceAccountName: step.BackendOptions.Kubernetes.ServiceAccountName,
ImagePullSecrets: imagePullSecretsReferences(config.ImagePullSecretNames),
HostAliases: hostAliases(step.ExtraHosts),
NodeSelector: nodeSelector(step.BackendOptions.Kubernetes.NodeSelector, step.Environment["CI_SYSTEM_PLATFORM"]),
Tolerations: tolerations(step.BackendOptions.Kubernetes.Tolerations),
SecurityContext: podSecurityContext(step.BackendOptions.Kubernetes.SecurityContext, config.SecurityContext),
}
spec.HostAliases = hostAliases(extraHosts)
spec.NodeSelector = nodeSelector(backendNodeSelector, env["CI_SYSTEM_PLATFORM"])
spec.Tolerations = tolerations(backendTolerations)
spec.SecurityContext = podSecurityContext(securityContext, securityContextConfig)
spec.Volumes, err = volumes(vols)
spec.Volumes, err = volumes(step.Volumes)
if err != nil {
return spec, err
}
@@ -116,36 +103,34 @@ func podSpec(serviceAccountName string, vols, pullSecretNames []string, env, bac
return spec, nil
}
func podContainer(name, image, workDir, goos string, pull, privileged bool, commands, volumes []string, env map[string]string, resources types.Resources,
securityContext *types.SecurityContext,
) (v1.Container, error) {
func podContainer(step *types.Step, podName, goos string) (v1.Container, error) {
var err error
container := v1.Container{
Name: name,
Image: image,
WorkingDir: workDir,
Name: podName,
Image: step.Image,
WorkingDir: step.WorkingDir,
}
if pull {
if step.Pull {
container.ImagePullPolicy = v1.PullAlways
}
if len(commands) != 0 {
scriptEnv, command, args := common.GenerateContainerConf(commands, goos)
if len(step.Commands) != 0 {
scriptEnv, command, args := common.GenerateContainerConf(step.Commands, goos)
container.Command = command
container.Args = args
maps.Copy(env, scriptEnv)
maps.Copy(step.Environment, scriptEnv)
}
container.Env = mapToEnvVars(env)
container.SecurityContext = containerSecurityContext(securityContext, privileged)
container.Env = mapToEnvVars(step.Environment)
container.SecurityContext = containerSecurityContext(step.BackendOptions.Kubernetes.SecurityContext, step.Privileged)
container.Resources, err = resourceRequirements(resources)
container.Resources, err = resourceRequirements(step.BackendOptions.Kubernetes.Resources)
if err != nil {
return container, err
}
container.VolumeMounts, err = volumeMounts(volumes)
container.VolumeMounts, err = volumeMounts(step.Volumes)
if err != nil {
return container, err
}
@@ -378,12 +363,7 @@ func startPod(ctx context.Context, engine *kube, step *types.Step) (*v1.Pod, err
if err != nil {
return nil, err
}
pod, err := mkPod(engine.config.Namespace, podName, step.Image, step.WorkingDir, engine.goos, step.BackendOptions.Kubernetes.ServiceAccountName,
step.Pull, step.Privileged,
step.Commands, step.Volumes, engine.config.ImagePullSecretNames,
engine.config.PodLabels, engine.config.PodAnnotations, step.Environment, step.BackendOptions.Kubernetes.NodeSelector,
step.ExtraHosts, step.BackendOptions.Kubernetes.Tolerations, step.BackendOptions.Kubernetes.Resources, step.BackendOptions.Kubernetes.SecurityContext, engine.config.SecurityContext)
pod, err := mkPod(step, engine.config, podName, engine.goos)
if err != nil {
return nil, err
}