From 31c689177ae77252410ee1a35e5443f4a613a2e6 Mon Sep 17 00:00:00 2001 From: Thomas Anderson <127358482+zc-devs@users.noreply.github.com> Date: Fri, 27 Oct 2023 03:00:49 +0300 Subject: [PATCH 1/6] Added ports into models --- pipeline/backend/kubernetes/kubernetes.go | 8 ++------ pipeline/backend/kubernetes/service.go | 15 ++++----------- pipeline/backend/types/step.go | 1 + pipeline/frontend/yaml/compiler/convert.go | 6 ++++++ pipeline/frontend/yaml/types/container.go | 1 + pipeline/frontend/yaml/types/container_test.go | 3 +++ 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pipeline/backend/kubernetes/kubernetes.go b/pipeline/backend/kubernetes/kubernetes.go index 458a3f08c0..0dfb4a0114 100644 --- a/pipeline/backend/kubernetes/kubernetes.go +++ b/pipeline/backend/kubernetes/kubernetes.go @@ -154,9 +154,7 @@ func (e *kube) SetupWorkflow(ctx context.Context, conf *types.Config, taskUUID s return err } log.Trace().Str("pod-name", stepName).Msgf("Creating service: %s", step.Name) - // TODO: support ports setting - // svc, err := Service(e.config.Namespace, step.Name, stepName, step.Ports) - svc, err := Service(e.config.Namespace, step.Name, stepName, []string{}) + svc, err := Service(e.config.Namespace, step.Name, step.Alias, step.Ports) if err != nil { return err } @@ -362,9 +360,7 @@ func (e *kube) DestroyWorkflow(_ context.Context, conf *types.Config, taskUUID s if stage.Alias == "services" { for _, step := range stage.Steps { log.Trace().Msgf("Deleting service: %s", step.Name) - // TODO: support ports setting - // svc, err := Service(e.config.Namespace, step.Name, step.Alias, step.Ports) - svc, err := Service(e.config.Namespace, step.Name, step.Alias, []string{}) + svc, err := Service(e.config.Namespace, step.Name, step.Alias, step.Ports) if err != nil { return err } diff --git a/pipeline/backend/kubernetes/service.go b/pipeline/backend/kubernetes/service.go index 224bc7c6c1..45ef58ef83 100644 --- a/pipeline/backend/kubernetes/service.go +++ b/pipeline/backend/kubernetes/service.go @@ -15,24 +15,17 @@ 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 []string) (*v1.Service, error) { +func Service(namespace, name, podName string, ports []int32) (*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) - } + for _, port := range ports { svcPorts = append(svcPorts, v1.ServicePort{ - Port: int32(i), - TargetPort: intstr.IntOrString{IntVal: int32(i)}, + Port: port, + TargetPort: intstr.IntOrString{IntVal: port}, }) } diff --git a/pipeline/backend/types/step.go b/pipeline/backend/types/step.go index a491051fbb..7113e179b2 100644 --- a/pipeline/backend/types/step.go +++ b/pipeline/backend/types/step.go @@ -48,6 +48,7 @@ type Step struct { NetworkMode string `json:"network_mode,omitempty"` IpcMode string `json:"ipc_mode,omitempty"` Sysctls map[string]string `json:"sysctls,omitempty"` + Ports []int32 `json:"ports,omitempty"` BackendOptions BackendOptions `json:"backend_options,omitempty"` } diff --git a/pipeline/frontend/yaml/compiler/convert.go b/pipeline/frontend/yaml/compiler/convert.go index 994da63ddd..6fa6078a7b 100644 --- a/pipeline/frontend/yaml/compiler/convert.go +++ b/pipeline/frontend/yaml/compiler/convert.go @@ -165,6 +165,11 @@ func (c *Compiler) createProcess(name string, container *yaml_types.Container, s cpuSet = c.reslimit.CPUSet } + var ports []int32 + for _, port := range container.Ports { + ports = append(ports, int32(port)) + } + // at least one constraint contain status success, or all constraints have no status set onSuccess := container.When.IncludesStatusSuccess() // at least one constraint must include the status failure. @@ -207,6 +212,7 @@ func (c *Compiler) createProcess(name string, container *yaml_types.Container, s Failure: failure, NetworkMode: networkMode, IpcMode: ipcMode, + Ports: ports, BackendOptions: backendOptions, } } diff --git a/pipeline/frontend/yaml/types/container.go b/pipeline/frontend/yaml/types/container.go index 945816f80d..eca9bd5a0a 100644 --- a/pipeline/frontend/yaml/types/container.go +++ b/pipeline/frontend/yaml/types/container.go @@ -47,6 +47,7 @@ type ( Settings map[string]interface{} `yaml:"settings"` Volumes Volumes `yaml:"volumes,omitempty"` When constraint.When `yaml:"when,omitempty"` + Ports []base.StringOrInt `yaml:"ports,omitempty"` // Docker Specific Privileged bool `yaml:"privileged,omitempty"` diff --git a/pipeline/frontend/yaml/types/container_test.go b/pipeline/frontend/yaml/types/container_test.go index 46f51b2d70..89d6e09f2c 100644 --- a/pipeline/frontend/yaml/types/container_test.go +++ b/pipeline/frontend/yaml/types/container_test.go @@ -68,6 +68,8 @@ when: settings: foo: bar baz: false +ports: + - 8080 `) func TestUnmarshalContainer(t *testing.T) { @@ -126,6 +128,7 @@ func TestUnmarshalContainer(t *testing.T) { "foo": "bar", "baz": false, }, + Ports: []base.StringOrInt{8080}, } got := Container{} err := yaml.Unmarshal(containerYaml, &got) From 8f31cff440e67e612d762f381919b0975feb08a3 Mon Sep 17 00:00:00 2001 From: Thomas Anderson <127358482+zc-devs@users.noreply.github.com> Date: Fri, 27 Oct 2023 16:27:12 +0300 Subject: [PATCH 2/6] Fixed test --- pipeline/backend/kubernetes/service_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipeline/backend/kubernetes/service_test.go b/pipeline/backend/kubernetes/service_test.go index 6702b36d5d..24a0df189d 100644 --- a/pipeline/backend/kubernetes/service_test.go +++ b/pipeline/backend/kubernetes/service_test.go @@ -54,7 +54,7 @@ func TestService(t *testing.T) { } }` - s, _ := Service("foo", "bar", "baz", []string{"1", "2", "3"}) + s, _ := Service("foo", "bar", "baz", []int32{1, 2, 3}) j, err := json.Marshal(s) assert.Nil(t, err) assert.JSONEq(t, expected, string(j)) From 9cd69a680d082609da98954d3830e3ab3681fb53 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 2 Nov 2023 01:55:27 +0100 Subject: [PATCH 3/6] uint16 --- pipeline/backend/kubernetes/service_test.go | 2 +- pipeline/backend/types/step.go | 2 +- pipeline/frontend/yaml/compiler/convert.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pipeline/backend/kubernetes/service_test.go b/pipeline/backend/kubernetes/service_test.go index 24a0df189d..6cbf808788 100644 --- a/pipeline/backend/kubernetes/service_test.go +++ b/pipeline/backend/kubernetes/service_test.go @@ -54,7 +54,7 @@ func TestService(t *testing.T) { } }` - s, _ := Service("foo", "bar", "baz", []int32{1, 2, 3}) + s, _ := Service("foo", "bar", "baz", []uint16{1, 2, 3}) j, err := json.Marshal(s) assert.Nil(t, err) assert.JSONEq(t, expected, string(j)) diff --git a/pipeline/backend/types/step.go b/pipeline/backend/types/step.go index 7113e179b2..714564b819 100644 --- a/pipeline/backend/types/step.go +++ b/pipeline/backend/types/step.go @@ -48,7 +48,7 @@ type Step struct { NetworkMode string `json:"network_mode,omitempty"` IpcMode string `json:"ipc_mode,omitempty"` Sysctls map[string]string `json:"sysctls,omitempty"` - Ports []int32 `json:"ports,omitempty"` + Ports []uint16 `json:"ports,omitempty"` BackendOptions BackendOptions `json:"backend_options,omitempty"` } diff --git a/pipeline/frontend/yaml/compiler/convert.go b/pipeline/frontend/yaml/compiler/convert.go index 6fa6078a7b..967e3b8b3b 100644 --- a/pipeline/frontend/yaml/compiler/convert.go +++ b/pipeline/frontend/yaml/compiler/convert.go @@ -165,9 +165,9 @@ func (c *Compiler) createProcess(name string, container *yaml_types.Container, s cpuSet = c.reslimit.CPUSet } - var ports []int32 + var ports []uint16 for _, port := range container.Ports { - ports = append(ports, int32(port)) + ports = append(ports, uint16(port)) } // at least one constraint contain status success, or all constraints have no status set From 703ba106f28b45fa21cf24ac40e51d19a1b78b9e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 2 Nov 2023 01:57:52 +0100 Subject: [PATCH 4/6] fmt --- pipeline/backend/types/step.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipeline/backend/types/step.go b/pipeline/backend/types/step.go index 714564b819..71bd5a66db 100644 --- a/pipeline/backend/types/step.go +++ b/pipeline/backend/types/step.go @@ -48,7 +48,7 @@ type Step struct { NetworkMode string `json:"network_mode,omitempty"` IpcMode string `json:"ipc_mode,omitempty"` Sysctls map[string]string `json:"sysctls,omitempty"` - Ports []uint16 `json:"ports,omitempty"` + Ports []uint16 `json:"ports,omitempty"` BackendOptions BackendOptions `json:"backend_options,omitempty"` } From 3b139583595b8943c60be0b536d6be614e0904c2 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 2 Nov 2023 02:00:39 +0100 Subject: [PATCH 5/6] uint16 --- pipeline/backend/kubernetes/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipeline/backend/kubernetes/service.go b/pipeline/backend/kubernetes/service.go index 45ef58ef83..9faca43ec6 100644 --- a/pipeline/backend/kubernetes/service.go +++ b/pipeline/backend/kubernetes/service.go @@ -20,7 +20,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" ) -func Service(namespace, name, podName string, ports []int32) (*v1.Service, error) { +func Service(namespace, name, podName string, ports []uint16) (*v1.Service, error) { var svcPorts []v1.ServicePort for _, port := range ports { svcPorts = append(svcPorts, v1.ServicePort{ From 2ee77e7623a125d12e0f458b260d63932948d3ab Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 2 Nov 2023 02:03:33 +0100 Subject: [PATCH 6/6] convert --- pipeline/backend/kubernetes/service.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pipeline/backend/kubernetes/service.go b/pipeline/backend/kubernetes/service.go index 9faca43ec6..2fee9496af 100644 --- a/pipeline/backend/kubernetes/service.go +++ b/pipeline/backend/kubernetes/service.go @@ -24,8 +24,8 @@ func Service(namespace, name, podName string, ports []uint16) (*v1.Service, erro var svcPorts []v1.ServicePort for _, port := range ports { svcPorts = append(svcPorts, v1.ServicePort{ - Port: port, - TargetPort: intstr.IntOrString{IntVal: port}, + Port: int32(port), + TargetPort: intstr.IntOrString{IntVal: int32(port)}, }) }