mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-04-15 01:41:56 +00:00
Supporting ports yaml field in service definition - needed for Kubernetes
This commit is contained in:
Binary file not shown.
@@ -73,9 +73,13 @@ func (e *engine) Setup(ctx context.Context, conf *backend.Config) error {
|
||||
for _, step := range stage.Steps {
|
||||
e.logs.WriteString("Creating service\n")
|
||||
var svc *v1.Service
|
||||
var err error
|
||||
for _, n := range step.Networks {
|
||||
if len(n.Aliases) > 0 {
|
||||
svc = Service(e.namespace, n.Aliases[0], podName(step), step.Ports)
|
||||
svc, err = Service(e.namespace, n.Aliases[0], podName(step), step.Ports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +251,10 @@ func (e *engine) Destroy(ctx context.Context, conf *backend.Config) error {
|
||||
for _, step := range stage.Steps {
|
||||
e.logs.WriteString("Deleting service\n")
|
||||
for _, n := range step.Networks {
|
||||
svc := Service(e.namespace, n.Aliases[0], step.Alias, step.Ports)
|
||||
svc, err := Service(e.namespace, n.Aliases[0], step.Alias, step.Ports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := e.kubeClient.CoreV1().Services(e.namespace).Delete(svc.Name, deleteOpts); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
package kubernetes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
)
|
||||
|
||||
func Service(namespace, name, podName string, ports []int) *v1.Service {
|
||||
func Service(namespace, name, podName string, ports []string) (*v1.Service, error) {
|
||||
var svcPorts []v1.ServicePort
|
||||
for _, p := range ports {
|
||||
i, err := strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not parse service port %s as integer", p)
|
||||
}
|
||||
svcPorts = append(svcPorts, v1.ServicePort{
|
||||
Port: int32(p),
|
||||
TargetPort: intstr.IntOrString{IntVal: int32(p)},
|
||||
Port: int32(i),
|
||||
TargetPort: intstr.IntOrString{IntVal: int32(i)},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -27,5 +34,5 @@ func Service(namespace, name, podName string, ports []int) *v1.Service {
|
||||
},
|
||||
Ports: svcPorts,
|
||||
},
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestService(t *testing.T) {
|
||||
}
|
||||
}`
|
||||
|
||||
s := Service("foo", "bar", "baz", []int{1, 2, 3})
|
||||
s, _ := Service("foo", "bar", "baz", []string{"1", "2", "3"})
|
||||
j, err := json.Marshal(s)
|
||||
assert.Nil(t, err)
|
||||
assert.JSONEq(t, expected, string(j))
|
||||
|
||||
@@ -48,7 +48,7 @@ type (
|
||||
NetworkMode string `json:"network_mode,omitempty"`
|
||||
IpcMode string `json:"ipc_mode,omitempty"`
|
||||
Sysctls map[string]string `json:"sysctls,omitempty"`
|
||||
Ports []int `json:"ports,omitempty"`
|
||||
Ports []string `json:"ports,omitempty"`
|
||||
}
|
||||
|
||||
// Auth defines registry authentication credentials.
|
||||
|
||||
@@ -137,7 +137,10 @@
|
||||
}
|
||||
],
|
||||
"on_success": true,
|
||||
"auth_config": {}
|
||||
"auth_config": {},
|
||||
"ports": [
|
||||
"6379"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "pipeline_services_1",
|
||||
@@ -201,7 +204,10 @@
|
||||
}
|
||||
],
|
||||
"on_success": true,
|
||||
"auth_config": {}
|
||||
"auth_config": {},
|
||||
"ports": [
|
||||
"6379"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -13,5 +13,7 @@ pipeline:
|
||||
services:
|
||||
redis1:
|
||||
image: redis:3.0
|
||||
ports: ["6379"]
|
||||
redis2:
|
||||
image: redis:3.0
|
||||
ports: ["6379"]
|
||||
|
||||
@@ -142,6 +142,7 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
|
||||
if c.reslimit.CPUSet != "" {
|
||||
cpuSet = c.reslimit.CPUSet
|
||||
}
|
||||
ports := container.Ports
|
||||
|
||||
return &backend.Step{
|
||||
Name: name,
|
||||
@@ -176,5 +177,6 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
|
||||
container.Constraints.Status.Match("failure"),
|
||||
NetworkMode: network_mode,
|
||||
IpcMode: ipc_mode,
|
||||
Ports: ports,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ type (
|
||||
Sysctls libcompose.SliceorMap `yaml:"sysctls,omitempty"`
|
||||
Constraints Constraints `yaml:"when,omitempty"`
|
||||
Vargs map[string]interface{} `yaml:",inline"`
|
||||
Ports []string `yaml:"ports,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user