Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K8s secrets reference from step #3655

Merged
merged 27 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f33488c
K8s secrets reference from step
zc-devs Apr 27, 2024
a688ae4
PR notes
zc-devs Apr 28, 2024
ad477b2
Merge remote-tracking branch 'upstream/main' into 3582-k8s-secrets-p1
zc-devs Apr 28, 2024
2ffe59f
Merge branch 'main' into 3582-k8s-secrets-p1
qwerty287 May 1, 2024
d4af4a5
Merge remote-tracking branch 'upstream/main' into 3582-k8s-secrets-p1
zc-devs Jun 3, 2024
d4d2c72
PR notes
zc-devs Jun 3, 2024
b875968
new schema
zc-devs Jun 3, 2024
073ec7f
Merge branch 'main' into 3582-k8s-secrets-p1
zc-devs Jun 6, 2024
a038d6a
Merge branch 'woodpecker-ci:main' into 3582-k8s-secrets-p1
zc-devs Jun 13, 2024
d8aad5b
new schema & be options
zc-devs Jun 14, 2024
f6b9bd2
env secrets
zc-devs Jun 14, 2024
0da65d9
file secrets
zc-devs Jun 14, 2024
20fa310
native secrets processor
zc-devs Jun 14, 2024
fac22f9
Merge branch 'main' into 3582-k8s-secrets-p1
zc-devs Jun 14, 2024
255feda
clean up
zc-devs Jun 14, 2024
2c22567
clean up 2
zc-devs Jun 15, 2024
52b6252
Merge branch 'main' into 3582-k8s-secrets-p1
zc-devs Jun 15, 2024
06eb896
pr notes
zc-devs Jun 15, 2024
6df704a
removed empty lines
zc-devs Jun 20, 2024
8e6a778
Merge branch 'main' into 3582-k8s-secrets-p1
zc-devs Jun 20, 2024
fdae76e
Merge branch 'main' into 3582-k8s-secrets-p1
qwerty287 Jun 20, 2024
713f9cb
switch-case + gofumpt
zc-devs Jun 21, 2024
0866d18
Merge remote-tracking branch 'upstream/main' into 3582-k8s-secrets-p1
zc-devs Jun 21, 2024
51471bf
gofumpt
zc-devs Jun 21, 2024
9404453
Merge branch 'main' into 3582-k8s-secrets-p1
zc-devs Jun 21, 2024
20e86b3
Apply suggestions from code review
qwerty287 Jun 23, 2024
c4da333
Merge branch 'main' into 3582-k8s-secrets-p1
qwerty287 Jun 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pipeline/backend/kubernetes/backend_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type BackendOptions struct {
NodeSelector map[string]string `mapstructure:"nodeSelector"`
Tolerations []Toleration `mapstructure:"tolerations"`
SecurityContext *SecurityContext `mapstructure:"securityContext"`
SecretNames []string `mapstructure:"secretNames"`
}

// Resources defines two maps for kubernetes resource definitions
Expand Down
6 changes: 6 additions & 0 deletions pipeline/backend/kubernetes/backend_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func Test_parseBackendOptions(t *testing.T) {
"localhostProfile": "k8s-apparmor-example-deny-write",
},
},
"secretNames": []string{
"test-secret",
},
},
},
})
Expand Down Expand Up @@ -69,5 +72,8 @@ func Test_parseBackendOptions(t *testing.T) {
LocalhostProfile: "k8s-apparmor-example-deny-write",
},
},
SecretNames: []string{
"test-secret",
},
}, got)
}
6 changes: 6 additions & 0 deletions pipeline/backend/kubernetes/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ var Flags = []cli.Flag{
Usage: "backend k8s pull secret names for private registries",
Value: cli.NewStringSlice("regcred"),
},
&cli.BoolFlag{
EnvVars: []string{"WOODPECKER_BACKEND_K8S_ALLOW_NATIVE_SECRETS"},
Name: "backend-k8s-allow-native-secrets",
Usage: "whether to allow existing Kubernetes secrets to be referenced from steps",
Value: false,
},
}
18 changes: 10 additions & 8 deletions pipeline/backend/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ type kube struct {
}

type config struct {
Namespace string
StorageClass string
VolumeSize string
StorageRwx bool
PodLabels map[string]string
PodAnnotations map[string]string
ImagePullSecretNames []string
SecurityContext SecurityContextConfig
Namespace string
StorageClass string
VolumeSize string
StorageRwx bool
PodLabels map[string]string
PodAnnotations map[string]string
ImagePullSecretNames []string
SecurityContext SecurityContextConfig
NativeSecretsAllowFromStep bool
}
type SecurityContextConfig struct {
RunAsNonRoot bool
Expand Down Expand Up @@ -92,6 +93,7 @@ func configFromCliContext(ctx context.Context) (*config, error) {
SecurityContext: SecurityContextConfig{
RunAsNonRoot: c.Bool("backend-k8s-secctx-nonroot"),
},
NativeSecretsAllowFromStep: c.Bool("backend-k8s-allow-native-secrets"),
}
// TODO: remove in next major
if len(config.ImagePullSecretNames) == 1 && config.ImagePullSecretNames[0] == "regcred" {
Expand Down
37 changes: 32 additions & 5 deletions pipeline/backend/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func mkPod(step *types.Step, config *config, podName, goos string, options Backe
return nil, err
}

container, err := podContainer(step, podName, goos, options)
container, err := podContainer(step, config, podName, goos, options)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func podSpec(step *types.Step, config *config, options BackendOptions) (v1.PodSp
return spec, nil
}

func podContainer(step *types.Step, podName, goos string, options BackendOptions) (v1.Container, error) {
func podContainer(step *types.Step, config *config, podName, goos string, options BackendOptions) (v1.Container, error) {
var err error
container := v1.Container{
Name: podName,
Expand All @@ -159,6 +159,14 @@ func podContainer(step *types.Step, podName, goos string, options BackendOptions

container.Env = mapToEnvVars(step.Environment)

if len(options.SecretNames) > 0 {
if config.NativeSecretsAllowFromStep {
container.EnvFrom = containerSecrets(options.SecretNames)
} else {
log.Debug().Msg("Secret names were defined in backend options, but its using disallowed by instance configuration ")
zc-devs marked this conversation as resolved.
Show resolved Hide resolved
}
}

container.Resources, err = resourceRequirements(options.Resources)
if err != nil {
return container, err
Expand Down Expand Up @@ -236,6 +244,25 @@ func containerPort(port types.Port) v1.ContainerPort {
}
}

func containerSecrets(secretNames []string) []v1.EnvFromSource {
if secretNames == nil || len(secretNames) == 0 {
zc-devs marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
secretRefs := make([]v1.EnvFromSource, len(secretNames))
for i, secretName := range secretNames {
secretRefs[i] = containerSecret(secretName)
}
return secretRefs
}

func containerSecret(secretName string) v1.EnvFromSource {
return v1.EnvFromSource{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: secretReference(secretName),
},
}
}

// Here is the service IPs (placed in /etc/hosts in the Pod)
func hostAliases(extraHosts []types.HostAlias) []v1.HostAlias {
var hostAliases []v1.HostAlias
Expand All @@ -258,14 +285,14 @@ func imagePullSecretsReferences(imagePullSecretNames []string) []v1.LocalObjectR

secretReferences := make([]v1.LocalObjectReference, len(imagePullSecretNames))
for i, imagePullSecretName := range imagePullSecretNames {
secretReferences[i] = imagePullSecretsReference(imagePullSecretName)
secretReferences[i] = secretReference(imagePullSecretName)
}
return secretReferences
}

func imagePullSecretsReference(imagePullSecretName string) v1.LocalObjectReference {
func secretReference(secretName string) v1.LocalObjectReference {
return v1.LocalObjectReference{
Name: imagePullSecretName,
Name: secretName,
}
}

Expand Down
25 changes: 20 additions & 5 deletions pipeline/backend/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ func TestFullPod(t *testing.T) {
"protocol": "UDP"
}
],
"envFrom": [
"<<UNORDERED>>",
{
"secretRef": {
"name": "ghcr-push-secret"
}
},
{
"secretRef": {
"name": "aws-ecr"
}
}
],
"env": [
"<<UNORDERED>>",
{
Expand Down Expand Up @@ -328,11 +341,12 @@ func TestFullPod(t *testing.T) {
ExtraHosts: hostAliases,
Ports: ports,
}, &config{
Namespace: "woodpecker",
ImagePullSecretNames: []string{"regcred", "another-pull-secret"},
PodLabels: map[string]string{"app": "test"},
PodAnnotations: map[string]string{"apps.kubernetes.io/pod-index": "0"},
SecurityContext: SecurityContextConfig{RunAsNonRoot: false},
Namespace: "woodpecker",
ImagePullSecretNames: []string{"regcred", "another-pull-secret"},
PodLabels: map[string]string{"app": "test"},
PodAnnotations: map[string]string{"apps.kubernetes.io/pod-index": "0"},
SecurityContext: SecurityContextConfig{RunAsNonRoot: false},
NativeSecretsAllowFromStep: true,
}, "wp-01he8bebctabr3kgk0qj36d2me-0", "linux/amd64", BackendOptions{
NodeSelector: map[string]string{"storage": "ssd"},
RuntimeClassName: &runtimeClass,
Expand All @@ -343,6 +357,7 @@ func TestFullPod(t *testing.T) {
Limits: map[string]string{"memory": "256Mi", "cpu": "2"},
},
SecurityContext: &secCtx,
SecretNames: []string{"ghcr-push-secret", "aws-ecr"},
})
assert.NoError(t, err)

Expand Down
6 changes: 6 additions & 0 deletions pipeline/frontend/yaml/linter/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,12 @@
"runtimeClassName": {
"description": "Read more: https://woodpecker-ci.org/docs/administration/backends/kubernetes#runtimeclassname",
"type": "string"
},
"secretNames": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down