Skip to content

Commit

Permalink
Supporting ports yaml field in service definition - needed for Kubern…
Browse files Browse the repository at this point in the history
…etes
  • Loading branch information
laszlocph committed Apr 27, 2019
1 parent 43603ed commit a9d2d66
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 9 deletions.
Binary file modified cncd/pipeline/pipec/pipec
Binary file not shown.
11 changes: 9 additions & 2 deletions cncd/pipeline/pipeline/backend/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 11 additions & 4 deletions cncd/pipeline/pipeline/backend/kubernetes/services.go
Original file line number Diff line number Diff line change
@@ -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)},
})
}

Expand All @@ -27,5 +34,5 @@ func Service(namespace, name, podName string, ports []int) *v1.Service {
},
Ports: svcPorts,
},
}
}, nil
}
2 changes: 1 addition & 1 deletion cncd/pipeline/pipeline/backend/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions cncd/pipeline/pipeline/fixtures/02_services/pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@
}
],
"on_success": true,
"auth_config": {}
"auth_config": {},
"ports": [
"6379"
]
},
{
"name": "pipeline_services_1",
Expand Down Expand Up @@ -201,7 +204,10 @@
}
],
"on_success": true,
"auth_config": {}
"auth_config": {},
"ports": [
"6379"
]
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions cncd/pipeline/pipeline/fixtures/02_services/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ pipeline:
services:
redis1:
image: redis:3.0
ports: ["6379"]
redis2:
image: redis:3.0
ports: ["6379"]
2 changes: 2 additions & 0 deletions cncd/pipeline/pipeline/frontend/yaml/compiler/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
}
1 change: 1 addition & 0 deletions cncd/pipeline/pipeline/frontend/yaml/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
)

Expand Down

0 comments on commit a9d2d66

Please sign in to comment.