mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-09-02 10:49:19 +00:00
Ability to set pod annotations and labels from step (#3609)
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
This commit is contained in:
@@ -76,43 +76,76 @@ func podName(step *types.Step) (string, error) {
|
||||
func podMeta(step *types.Step, config *config, options BackendOptions, podName string) (metav1.ObjectMeta, error) {
|
||||
var err error
|
||||
meta := metav1.ObjectMeta{
|
||||
Name: podName,
|
||||
Namespace: config.Namespace,
|
||||
Name: podName,
|
||||
Namespace: config.Namespace,
|
||||
Annotations: podAnnotations(config, options, podName),
|
||||
}
|
||||
|
||||
meta.Labels = config.PodLabels
|
||||
if meta.Labels == nil {
|
||||
meta.Labels = make(map[string]string, 1)
|
||||
}
|
||||
meta.Labels[StepLabel], err = stepLabel(step)
|
||||
meta.Labels, err = podLabels(step, config, options)
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
|
||||
if step.Type == types.StepTypeService {
|
||||
meta.Labels[ServiceLabel], _ = serviceName(step)
|
||||
}
|
||||
|
||||
meta.Annotations = config.PodAnnotations
|
||||
if meta.Annotations == nil {
|
||||
meta.Annotations = make(map[string]string)
|
||||
}
|
||||
|
||||
securityContext := options.SecurityContext
|
||||
if securityContext != nil {
|
||||
key, value := apparmorAnnotation(podName, securityContext.ApparmorProfile)
|
||||
if key != nil && value != nil {
|
||||
meta.Annotations[*key] = *value
|
||||
}
|
||||
}
|
||||
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
func podLabels(step *types.Step, config *config, options BackendOptions) (map[string]string, error) {
|
||||
var err error
|
||||
labels := make(map[string]string)
|
||||
|
||||
if len(options.Labels) > 0 {
|
||||
if config.PodLabelsAllowFromStep {
|
||||
log.Trace().Msgf("using labels from the backend options: %v", options.Labels)
|
||||
maps.Copy(labels, options.Labels)
|
||||
} else {
|
||||
log.Debug().Msg("Pod labels were defined in backend options, but its using disallowed by instance configuration")
|
||||
}
|
||||
}
|
||||
if len(config.PodLabels) > 0 {
|
||||
log.Trace().Msgf("using labels from the configuration: %v", config.PodLabels)
|
||||
maps.Copy(labels, config.PodLabels)
|
||||
}
|
||||
if step.Type == types.StepTypeService {
|
||||
labels[ServiceLabel], _ = serviceName(step)
|
||||
}
|
||||
labels[StepLabel], err = stepLabel(step)
|
||||
if err != nil {
|
||||
return labels, err
|
||||
}
|
||||
|
||||
return labels, nil
|
||||
}
|
||||
|
||||
func stepLabel(step *types.Step) (string, error) {
|
||||
return toDNSName(step.Name)
|
||||
}
|
||||
|
||||
func podAnnotations(config *config, options BackendOptions, podName string) map[string]string {
|
||||
annotations := make(map[string]string)
|
||||
|
||||
if len(options.Annotations) > 0 {
|
||||
if config.PodAnnotationsAllowFromStep {
|
||||
log.Trace().Msgf("using annotations from the backend options: %v", options.Annotations)
|
||||
maps.Copy(annotations, options.Annotations)
|
||||
} else {
|
||||
log.Debug().Msg("Pod annotations were defined in backend options, but its using disallowed by instance configuration ")
|
||||
}
|
||||
}
|
||||
if len(config.PodAnnotations) > 0 {
|
||||
log.Trace().Msgf("using annotations from the configuration: %v", config.PodAnnotations)
|
||||
maps.Copy(annotations, config.PodAnnotations)
|
||||
}
|
||||
securityContext := options.SecurityContext
|
||||
if securityContext != nil {
|
||||
key, value := apparmorAnnotation(podName, securityContext.ApparmorProfile)
|
||||
if key != nil && value != nil {
|
||||
annotations[*key] = *value
|
||||
}
|
||||
}
|
||||
|
||||
return annotations
|
||||
}
|
||||
|
||||
func podSpec(step *types.Step, config *config, options BackendOptions) (v1.PodSpec, error) {
|
||||
var err error
|
||||
spec := v1.PodSpec{
|
||||
|
Reference in New Issue
Block a user