Skip to content
Merged
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
1 change: 1 addition & 0 deletions .crd-ref-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ references:
# Exclude collection wrappers by name: BGPPrefixList is a resource.
- "[.]EdgeAttachmentList$"
- "[.]EdgeInstanceList$"
- "[.]EdgeListenerList$"
- "[.]EdgeProviderApprovalList$"
- "[.]EdgeProviderList$"
- "[.]EdgeRouteIndexList$"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2026 Datum Cloud, Inc.
// SPDX-License-Identifier: AGPL-3.0-or-later

package v1alpha1_test

import (
"context"
"encoding/json"
"strings"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

networkv1alpha1 "go.datum.net/galactic/api/network/v1alpha1"
)

const (
exactlyOneBackendSourceMsg = "exactly one of backendSelector and edgeListenerRef is required"
emptySelectorMsg = "backendSelector must not be empty"
)

// TestNetworkServiceBackendSource covers the rule that a service takes its
// backends from exactly one of backendSelector and edgeListenerRef, and that a
// backendSelector that is set must still not be empty.
func TestNetworkServiceBackendSource(t *testing.T) {
c, _ := startProtocolAPI(t)
ctx := context.Background()
listener := &networkv1alpha1.NetworkServiceEdgeListenerReference{Name: "masque-public"}

for _, tc := range []struct {
name string
mutate func(*networkv1alpha1.NetworkService)
want string // empty when the service is admitted
}{
{"Selector", func(*networkv1alpha1.NetworkService) {}, ""},
{"EdgeListener", func(s *networkv1alpha1.NetworkService) {
s.Spec.BackendSelector = metav1.LabelSelector{}
s.Spec.EdgeListenerRef = listener
}, ""},
{"Both", func(s *networkv1alpha1.NetworkService) { s.Spec.EdgeListenerRef = listener }, exactlyOneBackendSourceMsg},
{"Neither", func(s *networkv1alpha1.NetworkService) {
s.Spec.BackendSelector = metav1.LabelSelector{}
}, exactlyOneBackendSourceMsg},
{"EmptyListenerName", func(s *networkv1alpha1.NetworkService) {
s.Spec.BackendSelector = metav1.LabelSelector{}
s.Spec.EdgeListenerRef = &networkv1alpha1.NetworkServiceEdgeListenerReference{}
}, "spec.edgeListenerRef.name"},
} {
t.Run(tc.name, func(t *testing.T) {
svc := protocolService("source-"+strings.ToLower(tc.name), networkv1alpha1.NetworkServiceProtocolTCP, 443, 8443)
tc.mutate(svc)
err := c.Create(ctx, svc)
if tc.want == "" {
if err != nil {
t.Fatalf("Create: %v", err)
}
return
}
requireRefusal(t, err, []string{tc.want}, nil)
})
}

// A selector written as {} is present but empty, and keeps its own
// refusal rather than counting as absent.
t.Run("ExplicitEmptySelector", func(t *testing.T) {
svc := protocolService("source-explicit-empty", networkv1alpha1.NetworkServiceProtocolTCP, 443, 8443)
obj := unstructuredService(t, svc)
if err := unstructured.SetNestedMap(obj.Object, map[string]any{}, "spec", "backendSelector"); err != nil {
t.Fatal(err)
}
requireRefusal(t, c.Create(ctx, obj), []string{emptySelectorMsg}, nil)
})
}

// TestNetworkServiceOmitsEmptySelector checks that a service written from Go
// without a selector encodes no backendSelector, which the exactly-one rule
// depends on.
func TestNetworkServiceOmitsEmptySelector(t *testing.T) {
svc := protocolService("encode", networkv1alpha1.NetworkServiceProtocolTCP, 443, 8443)
svc.Spec.BackendSelector = metav1.LabelSelector{}
svc.Spec.EdgeListenerRef = &networkv1alpha1.NetworkServiceEdgeListenerReference{Name: "masque-public"}
data, err := json.Marshal(svc.Spec)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(data), "backendSelector") {
t.Errorf("spec = %s, want no backendSelector", data)
}
}
31 changes: 27 additions & 4 deletions api/network/v1alpha1/networkservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ const (
// serve the service, so the backend set follows attachments as they are
// created, deleted or moved to another node, and galactic-router on each
// node generates the ServiceVIPBinding its backends need from the same
// selection.
// selection. A service in the gateway namespace can instead name an
// EdgeListener with EdgeListenerRef, whose listener attachments are then its
// backends.
//
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
Expand Down Expand Up @@ -146,6 +148,7 @@ type NetworkService struct {
// +kubebuilder:validation:XValidation:rule="!(self.protocol in ['esp', 'gre', 'icmp']) || !has(self.port)",message="port must not be set for esp, gre and icmp, which have no ports"
// +kubebuilder:validation:XValidation:rule="!(self.protocol in ['esp', 'gre', 'icmp']) || !has(self.backendPort)",message="backendPort must not be set for esp, gre and icmp, which have no ports"
// +kubebuilder:validation:XValidation:rule="self.protocol != 'udp' || !has(self.healthCheck) || has(self.healthCheck.port)",message="healthCheck.port is required for udp: a health check probes a TCP port"
// +kubebuilder:validation:XValidation:rule="has(self.backendSelector) != has(self.edgeListenerRef)",message="exactly one of backendSelector and edgeListenerRef is required"
type NetworkServiceSpec struct {
// VPCRef is the opaque identifier of the target VPC this service applies
// to, a VPC's status.vpc. In a gateway namespace, the platform API that
Expand Down Expand Up @@ -236,10 +239,20 @@ type NetworkServiceSpec struct {
// and from IPv6, and its IPv4 addresses are never backends. An attachment
// with no status.node, or no IPv6 address, is not a backend until it has
// both. An empty selector is rejected rather than read as "every
// attachment in the VPC".
// +kubebuilder:validation:Required
// attachment in the VPC". Exactly one of BackendSelector and
// EdgeListenerRef is required.
// +optional
// +kubebuilder:validation:XValidation:rule="(has(self.matchLabels) && size(self.matchLabels) > 0) || (has(self.matchExpressions) && size(self.matchExpressions) > 0)",message="backendSelector must not be empty"
BackendSelector metav1.LabelSelector `json:"backendSelector"`
BackendSelector metav1.LabelSelector `json:"backendSelector,omitzero"`

// EdgeListenerRef names the EdgeListener, in the service's namespace,
// whose listener attachments are the service's backends, in place of
// BackendSelector. Core sets it on the services it writes for a
// listener, one per listener port. It is allowed only in the gateway
// namespace. Until gateways resolve listener backends, a service that
// sets it has no backends and its VIPs are not advertised.
// +optional
EdgeListenerRef *NetworkServiceEdgeListenerReference `json:"edgeListenerRef,omitempty"`

// BackendPort is the destination port on every selected backend, 1-65535.
// Like Port, it is required for tcp, udp and sctp and must be unset for
Expand Down Expand Up @@ -280,6 +293,16 @@ type NetworkServiceSpec struct {
HealthCheck *NetworkServiceHealthCheck `json:"healthCheck,omitempty"`
}

// NetworkServiceEdgeListenerReference names an EdgeListener
// (galactic.datumapis.com/v1alpha1) in the service's namespace.
type NetworkServiceEdgeListenerReference struct {
// Name is the EdgeListener's name.
// +required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
Name string `json:"name"`
}

const (
// NetworkServiceHealthFailOpen retains unknown backends and all-unhealthy sets.
NetworkServiceHealthFailOpen = "FailOpen"
Expand Down
20 changes: 20 additions & 0 deletions api/network/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading