1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2025-07-12 22:21:40 +02:00

Allow adding additional labels/annotations to kubernetes worker pods (#1510)

Example agent environment configuration using the new value:
```yaml
  - env:
    - name: WOODPECKER_BACKEND
      value: kubernetes
    - name: WOODPECKER_BACKEND_K8S_NAMESPACE
      value: default
    - name: WOODPECKER_BACKEND_K8S_POD_LABELS
      value: '{"sidecar.istio.io/inject":"false"}'
```
This commit is contained in:
Stephen Muth
2022-12-30 19:37:09 -05:00
committed by GitHub
parent 0923aa2624
commit 1816f6c715
5 changed files with 57 additions and 14 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
"gopkg.in/yaml.v3"
"github.com/urfave/cli/v2"
v1 "k8s.io/api/core/v1"
@ -39,21 +40,35 @@ type kube struct {
}
type Config struct {
Namespace string
StorageClass string
VolumeSize string
StorageRwx bool
Namespace string
StorageClass string
VolumeSize string
PodLabels map[string]string
PodAnnotations map[string]string
StorageRwx bool
}
func configFromCliContext(ctx context.Context) (*Config, error) {
if ctx != nil {
if c, ok := ctx.Value(types.CliContext).(*cli.Context); ok {
return &Config{
config := Config{
Namespace: c.String("backend-k8s-namespace"),
StorageClass: c.String("backend-k8s-storage-class"),
VolumeSize: c.String("backend-k8s-volume-size"),
StorageRwx: c.Bool("backend-k8s-storage-rwx"),
}, nil
}
// Unmarshal label and annotation settings here to ensure they're valid on startup
err := yaml.Unmarshal([]byte(c.String("backend-k8s-pod-labels")), &config.PodLabels)
if err != nil {
log.Error().Msgf("could not unmarshal pod labels '%s': %s", c.String("backend-k8s-pod-labels"), err)
return nil, err
}
err = yaml.Unmarshal([]byte(c.String("backend-k8s-pod-annotations")), &config.PodAnnotations)
if err != nil {
log.Error().Msgf("could not unmarshal pod annotations '%s': %s", c.String("backend-k8s-pod-annotations"), err)
return nil, err
}
return &config, nil
}
}
@ -147,7 +162,7 @@ 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 := Pod(e.config.Namespace, step)
pod := Pod(e.config.Namespace, step, e.config.PodLabels, e.config.PodAnnotations)
log.Trace().Msgf("Creating pod: %s", pod.Name)
_, err := e.client.CoreV1().Pods(e.config.Namespace).Create(ctx, pod, metav1.CreateOptions{})
return err
@ -232,7 +247,8 @@ func (e *kube) Tail(ctx context.Context, step *types.Step) (io.ReadCloser, error
<-up
opts := &v1.PodLogOptions{
Follow: true,
Follow: true,
Container: podName,
}
logs, err := e.client.CoreV1().RESTClient().Get().