mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-02-28 00:56:30 +00:00
kubernetes: create service for detached steps (#4892)
Co-authored-by: Robert Kaussow <mail@thegeeklab.de>
This commit is contained in:
parent
0428d01d6f
commit
f47165ff9f
4 changed files with 61 additions and 4 deletions
|
@ -199,7 +199,7 @@ func (e *kube) SetupWorkflow(ctx context.Context, conf *types.Config, taskUUID s
|
||||||
var extraHosts []types.HostAlias
|
var extraHosts []types.HostAlias
|
||||||
for _, stage := range conf.Stages {
|
for _, stage := range conf.Stages {
|
||||||
for _, step := range stage.Steps {
|
for _, step := range stage.Steps {
|
||||||
if step.Type == types.StepTypeService || step.Detached {
|
if isService(step) {
|
||||||
svc, err := startService(ctx, e, step)
|
svc, err := startService(ctx, e, step)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -416,7 +416,7 @@ func (e *kube) DestroyWorkflow(ctx context.Context, conf *types.Config, taskUUID
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if step.Type == types.StepTypeService {
|
if isService(step) {
|
||||||
err := stopService(ctx, e, step, defaultDeleteOptions)
|
err := stopService(ctx, e, step, defaultDeleteOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -70,7 +70,7 @@ func mkPod(step *types.Step, config *config, podName, goos string, options Backe
|
||||||
}
|
}
|
||||||
|
|
||||||
func stepToPodName(step *types.Step) (name string, err error) {
|
func stepToPodName(step *types.Step) (name string, err error) {
|
||||||
if step.Type == types.StepTypeService {
|
if isService(step) {
|
||||||
return serviceName(step)
|
return serviceName(step)
|
||||||
}
|
}
|
||||||
return podName(step)
|
return podName(step)
|
||||||
|
@ -112,7 +112,7 @@ func podLabels(step *types.Step, config *config, options BackendOptions) (map[st
|
||||||
log.Trace().Msgf("using labels from the configuration: %v", config.PodLabels)
|
log.Trace().Msgf("using labels from the configuration: %v", config.PodLabels)
|
||||||
maps.Copy(labels, config.PodLabels)
|
maps.Copy(labels, config.PodLabels)
|
||||||
}
|
}
|
||||||
if step.Type == types.StepTypeService {
|
if isService(step) {
|
||||||
labels[ServiceLabel], _ = serviceName(step)
|
labels[ServiceLabel], _ = serviceName(step)
|
||||||
}
|
}
|
||||||
labels[StepLabel], err = stepLabel(step)
|
labels[StepLabel], err = stepLabel(step)
|
||||||
|
|
|
@ -53,6 +53,57 @@ func TestStepToPodName(t *testing.T) {
|
||||||
name, err = stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "postgres", Type: types.StepTypeService})
|
name, err = stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "postgres", Type: types.StepTypeService})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, "wp-svc-01he8bebctabr3kg-postgres", name)
|
assert.EqualValues(t, "wp-svc-01he8bebctabr3kg-postgres", name)
|
||||||
|
// Detached service
|
||||||
|
name, err = stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "postgres", Detached: true})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "wp-svc-01he8bebctabr3kg-postgres", name)
|
||||||
|
// Detached long running container
|
||||||
|
name, err = stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "long running", Detached: true})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "wp-01he8bebctabr3kg", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPodMeta(t *testing.T) {
|
||||||
|
meta, err := podMeta(&types.Step{
|
||||||
|
Name: "postgres",
|
||||||
|
UUID: "01he8bebctabr3kg",
|
||||||
|
Type: types.StepTypeService,
|
||||||
|
Image: "postgres:16",
|
||||||
|
WorkingDir: "/woodpecker/src",
|
||||||
|
Environment: map[string]string{"CI": "woodpecker"},
|
||||||
|
}, &config{
|
||||||
|
Namespace: "woodpecker",
|
||||||
|
}, BackendOptions{}, "wp-01he8bebctabr3kg-0")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "wp-svc-01he8bebctabr3kg-postgres", meta.Labels[ServiceLabel])
|
||||||
|
|
||||||
|
// Detached service
|
||||||
|
meta, err = podMeta(&types.Step{
|
||||||
|
Name: "postgres",
|
||||||
|
UUID: "01he8bebctabr3kg",
|
||||||
|
Detached: true,
|
||||||
|
Image: "postgres:16",
|
||||||
|
WorkingDir: "/woodpecker/src",
|
||||||
|
Environment: map[string]string{"CI": "woodpecker"},
|
||||||
|
}, &config{
|
||||||
|
Namespace: "woodpecker",
|
||||||
|
}, BackendOptions{}, "wp-01he8bebctabr3kg-0")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "wp-svc-01he8bebctabr3kg-postgres", meta.Labels[ServiceLabel])
|
||||||
|
|
||||||
|
// Detached long running container
|
||||||
|
meta, err = podMeta(&types.Step{
|
||||||
|
Name: "long running",
|
||||||
|
UUID: "01he8bebctabr3kg",
|
||||||
|
Detached: true,
|
||||||
|
Image: "postgres:16",
|
||||||
|
WorkingDir: "/woodpecker/src",
|
||||||
|
Environment: map[string]string{"CI": "woodpecker"},
|
||||||
|
}, &config{
|
||||||
|
Namespace: "woodpecker",
|
||||||
|
}, BackendOptions{}, "wp-01he8bebctabr3kg-0")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "", meta.Labels[ServiceLabel])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStepLabel(t *testing.T) {
|
func TestStepLabel(t *testing.T) {
|
||||||
|
|
|
@ -24,6 +24,8 @@ import (
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
client_cmd "k8s.io/client-go/tools/clientcmd"
|
client_cmd "k8s.io/client-go/tools/clientcmd"
|
||||||
|
|
||||||
|
"go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -103,6 +105,10 @@ func getClientInsideOfCluster() (kubernetes.Interface, error) {
|
||||||
return kubernetes.NewForConfig(config)
|
return kubernetes.NewForConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isService(step *types.Step) bool {
|
||||||
|
return step.Type == types.StepTypeService || (step.Detached && dnsPattern.FindStringIndex(step.Name) != nil)
|
||||||
|
}
|
||||||
|
|
||||||
func newBool(val bool) *bool {
|
func newBool(val bool) *bool {
|
||||||
ptr := new(bool)
|
ptr := new(bool)
|
||||||
*ptr = val
|
*ptr = val
|
||||||
|
|
Loading…
Reference in a new issue