mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-04-15 01:41:56 +00:00
Services are added as HostAliases to avoid naming collision
This commit is contained in:
@@ -59,7 +59,7 @@ func TestKubeExec(t *testing.T) {
|
||||
}
|
||||
|
||||
// os.Setenv("KUBECONFIG", "/etc/rancher/k3s/k3s.yaml")
|
||||
engine, err := kubernetes.New("default", "local-storage", "100Mi")
|
||||
engine, err := kubernetes.New("default", "example-nfs", "100Mi")
|
||||
if err != nil {
|
||||
t.Errorf("Could not start Kubernetes engine %f", err)
|
||||
}
|
||||
|
||||
@@ -66,30 +66,60 @@ func (e *engine) Setup(ctx context.Context, conf *backend.Config) error {
|
||||
}
|
||||
}
|
||||
|
||||
extraHosts := []string{}
|
||||
|
||||
for _, stage := range conf.Stages {
|
||||
if stage.Alias == "services" {
|
||||
for _, step := range stage.Steps {
|
||||
e.logs.WriteString("Creating service\n")
|
||||
pod := Pod(e.namespace, step)
|
||||
|
||||
var svc *v1.Service
|
||||
for _, n := range step.Networks {
|
||||
if len(n.Aliases) > 0 {
|
||||
svc = Service(e.namespace, n.Aliases[0], pod.Name, step.Ports)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := e.kubeClient.CoreV1().Pods(e.namespace).Create(pod); err != nil {
|
||||
return err
|
||||
}
|
||||
if svc, err := e.kubeClient.CoreV1().Services(e.namespace).Create(svc); err != nil {
|
||||
return err
|
||||
} else {
|
||||
extraHosts = append(extraHosts, step.Networks[0].Aliases[0]+":"+svc.Spec.ClusterIP)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, stage := range conf.Stages {
|
||||
if stage.Alias == "build" {
|
||||
for _, step := range stage.Steps {
|
||||
step.ExtraHosts = extraHosts
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start the pipeline step.
|
||||
func (e *engine) Exec(ctx context.Context, step *backend.Step) error {
|
||||
e.logs.WriteString("Creating pod\n")
|
||||
pod, err := Pod(e.namespace, step)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pod := Pod(e.namespace, step)
|
||||
|
||||
var svc *v1.Service
|
||||
for _, n := range step.Networks {
|
||||
if len(n.Aliases) > 0 {
|
||||
svc := Service(e.namespace, n.Aliases[0], pod.Name, step.Ports)
|
||||
if svc == nil {
|
||||
continue
|
||||
}
|
||||
if _, err := e.kubeClient.CoreV1().Services(e.namespace).Create(svc); err != nil {
|
||||
return err
|
||||
}
|
||||
svc = Service(e.namespace, n.Aliases[0], pod.Name, step.Ports)
|
||||
}
|
||||
}
|
||||
if svc != nil {
|
||||
return nil // Pods with services are created already in Setup()
|
||||
}
|
||||
|
||||
_, err = e.kubeClient.CoreV1().Pods(e.namespace).Create(pod)
|
||||
_, err := e.kubeClient.CoreV1().Pods(e.namespace).Create(pod)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
func Pod(namespace string, step *backend.Step) (*v1.Pod, error) {
|
||||
func Pod(namespace string, step *backend.Step) *v1.Pod {
|
||||
|
||||
var vols []v1.Volume
|
||||
var volMounts []v1.VolumeMount
|
||||
@@ -47,6 +47,12 @@ func Pod(namespace string, step *backend.Step) (*v1.Pod, error) {
|
||||
args = []string{"echo $CI_SCRIPT | base64 -d | /bin/sh -e"}
|
||||
}
|
||||
|
||||
hostAliases := []v1.HostAlias{}
|
||||
for _, extraHost := range step.ExtraHosts {
|
||||
host := strings.Split(extraHost, ":")
|
||||
hostAliases = append(hostAliases, v1.HostAlias{IP: host[1], Hostnames: []string{host[0]}})
|
||||
}
|
||||
|
||||
return &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: podName(step),
|
||||
@@ -57,6 +63,7 @@ func Pod(namespace string, step *backend.Step) (*v1.Pod, error) {
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
RestartPolicy: v1.RestartPolicyNever,
|
||||
HostAliases: hostAliases,
|
||||
Containers: []v1.Container{{
|
||||
Name: podName(step),
|
||||
Image: step.Image,
|
||||
@@ -70,7 +77,7 @@ func Pod(namespace string, step *backend.Step) (*v1.Pod, error) {
|
||||
ImagePullSecrets: []v1.LocalObjectReference{{Name: "regcred"}},
|
||||
Volumes: vols,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func podName(s *backend.Step) string {
|
||||
|
||||
Reference in New Issue
Block a user