Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions framework/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2026 The Score Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package framework

import score "github.com/score-spec/score-go/types"

// ServicePort is a resolved port from the workload specification. It differs from types.ServicePort by having all
// fields fully resolved (no optional pointers) and a Name field for the port's key in the service ports map.
type ServicePort struct {
// Name is the name of the port from the workload specification.
Name string `yaml:"name"`
// Port is the numeric port intended to be published.
Port int `yaml:"port"`
// TargetPort is the port on the workload that hosts the actual traffic.
TargetPort int `yaml:"target_port"`
// Protocol is TCP or UDP.
Protocol score.ServicePortProtocol `yaml:"protocol"`
}

// NetworkService describes how to contact ports exposed by another workload. Provisioners store this in SharedState
// so that downstream workloads can discover service endpoints without target-specific knowledge.
type NetworkService struct {
// ServiceName is the DNS-resolvable or container-resolvable hostname for the workload.
ServiceName string `yaml:"service_name"`
// Ports maps a port key (name or numeric string) to its resolved ServicePort.
Ports map[string]ServicePort `yaml:"ports"`
}
95 changes: 95 additions & 0 deletions framework/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2026 The Score Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package framework

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

score "github.com/score-spec/score-go/types"
)

func TestNetworkService(t *testing.T) {
t.Run("round trip TCP", func(t *testing.T) {
original := NetworkService{
ServiceName: "hello",
Ports: map[string]ServicePort{
"http": {
Name: "http",
Port: 8080,
TargetPort: 80,
Protocol: score.ServicePortProtocolTCP,
},
"8080": {
Name: "http",
Port: 8080,
TargetPort: 80,
Protocol: score.ServicePortProtocolTCP,
},
},
}

raw, err := yaml.Marshal(original)
require.NoError(t, err)

var decoded NetworkService
require.NoError(t, yaml.Unmarshal(raw, &decoded))
assert.Equal(t, original, decoded)
})

t.Run("round trip UDP", func(t *testing.T) {
original := NetworkService{
ServiceName: "hello",
Ports: map[string]ServicePort{
"dns": {
Name: "dns",
Port: 53,
TargetPort: 5353,
Protocol: score.ServicePortProtocolUDP,
},
},
}

raw, err := yaml.Marshal(original)
require.NoError(t, err)

var decoded NetworkService
require.NoError(t, yaml.Unmarshal(raw, &decoded))
assert.Equal(t, original, decoded)
})

t.Run("yaml field names", func(t *testing.T) {
input := `
service_name: my-service
ports:
http:
name: http
port: 80
target_port: 8080
protocol: TCP
`

var ns NetworkService
require.NoError(t, yaml.Unmarshal([]byte(input), &ns))
assert.Equal(t, "my-service", ns.ServiceName)
assert.Equal(t, "http", ns.Ports["http"].Name)
assert.Equal(t, 80, ns.Ports["http"].Port)
assert.Equal(t, 8080, ns.Ports["http"].TargetPort)
assert.Equal(t, score.ServicePortProtocol("TCP"), ns.Ports["http"].Protocol)
})
}