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
5 changes: 3 additions & 2 deletions api/network/v1alpha1/networkservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ type NetworkServiceSpec struct {
// 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.
// namespace. Its backends are the listener's status.backends that core
// reports serving; gateways and backend nodes never select them from
// VPCAttachments.
// +optional
EdgeListenerRef *NetworkServiceEdgeListenerReference `json:"edgeListenerRef,omitempty"`

Expand Down
12 changes: 12 additions & 0 deletions api/v1alpha1/edgelistener_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ type EdgeListenerBackend struct {
// +kubebuilder:validation:MaxLength=317
Attachment string `json:"attachment"`

// UID is the EdgeAttachment's metadata.uid. A gateway health decision
// must name it to apply to this backend.
// +optional
// +kubebuilder:validation:MaxLength=36
UID string `json:"uid,omitempty"`

// Node is the attachment's node.
// +optional
// +kubebuilder:validation:MaxLength=253
Expand All @@ -190,6 +196,12 @@ type EdgeListenerBackend struct {
// +kubebuilder:validation:MaxLength=39
Address string `json:"address,omitempty"`

// HostLink is the host end of the attachment's link, which the gateway
// health check probes through.
// +optional
// +kubebuilder:validation:MaxLength=15
HostLink string `json:"hostLink,omitempty"`

// Serving is whether the attachment receives new flows: it is
// programmed, its instance lease is valid and its provider reports Ready.
// +optional
Expand Down
17 changes: 14 additions & 3 deletions cmd/galactic-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
authorizationv1 "k8s.io/api/authorization/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

galacticv1alpha1 "go.datum.net/galactic/api/v1alpha1"
)

const (
Expand All @@ -42,6 +44,7 @@ const (
cloudAPIVersion = "v1alpha1"
resourceVPCAttachments = "vpcattachments"
resourceVPCs = "vpcs"
resourceEdgeListeners = "edgelisteners"
)

// verbList and verbWatch are the verbs an informer needs on each type it
Expand Down Expand Up @@ -74,7 +77,9 @@ type watchedResource struct{ group, version, kind, resource string }
// is a real requirement here. VPCs are watched only with projectServices on.
// ServiceVIPBindings are watched whatever the IPv4 VIP and backend health
// switches say, since a shared address or port range rule reads them too.
func watchedResources(projectServices bool) []watchedResource {
// EdgeListeners are watched only when the API server serves them (listeners),
// as the gateway controller registers that watch only then.
func watchedResources(projectServices, listeners bool) []watchedResource {
resources := []watchedResource{
{group: networkAPIGroup, version: networkAPIVersion, kind: "NetworkGateway", resource: resourceNetworkGateways},
{group: networkAPIGroup, version: networkAPIVersion, kind: "NetworkService", resource: resourceNetworkServices},
Expand All @@ -92,6 +97,12 @@ func watchedResources(projectServices bool) []watchedResource {
resources = append(resources,
watchedResource{group: cloudAPIGroup, version: cloudAPIVersion, kind: "VPC", resource: resourceVPCs})
}
if listeners {
resources = append(resources, watchedResource{
group: galacticv1alpha1.GroupVersion.Group, version: galacticv1alpha1.GroupVersion.Version,
kind: "EdgeListener", resource: resourceEdgeListeners,
})
}
return resources
}

Expand All @@ -101,11 +112,11 @@ func watchedResources(projectServices bool) []watchedResource {
// startup with the resources it names. A review that cannot be made is logged
// and skipped, since the manager's own cache sync reports a real outage, but
// once ctx ends startup fails rather than passing a check it never finished.
func checkWatchPermissions(ctx context.Context, c client.Client, projectServices bool) error {
func checkWatchPermissions(ctx context.Context, c client.Client, projectServices, listeners bool) error {
logger := ctrl.Log.WithName("rbac-preflight")

var denied []string
for _, r := range watchedResources(projectServices) {
for _, r := range watchedResources(projectServices, listeners) {
var missing []string
for _, verb := range informerVerbs {
review := &authorizationv1.SelfSubjectAccessReview{
Expand Down
35 changes: 25 additions & 10 deletions cmd/galactic-gateway/preflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"slices"
Expand All @@ -16,6 +17,7 @@ import (
"time"

rbacv1 "k8s.io/api/rbac/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -34,6 +36,7 @@ import (

cloudv1alpha1 "go.datum.net/galactic/api/cloud/v1alpha1"
bgpv1alpha1 "go.datum.net/galactic/api/network/v1alpha1"
galacticv1alpha1 "go.datum.net/galactic/api/v1alpha1"
"go.datum.net/galactic/internal/controller"
)

Expand Down Expand Up @@ -100,13 +103,23 @@ func (c *recordingCache) recorded() []schema.GroupVersionKind {

// watchedKinds starts a manager with the field index and controllers
// setupControllers registers, configured by projectServices, ipv4VIPs and
// backendHealth, and returns every kind they watch.
func watchedKinds(t *testing.T, projectServices, ipv4VIPs, backendHealth bool) []schema.GroupVersionKind {
// backendHealth, and returns every kind they watch. listeners is whether the
// manager's discovery serves EdgeListener.
func watchedKinds(t *testing.T, projectServices, ipv4VIPs, backendHealth, listeners bool) []schema.GroupVersionKind {
t.Helper()
scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(bgpv1alpha1.AddToScheme(scheme))
utilruntime.Must(cloudv1alpha1.AddToScheme(scheme))
utilruntime.Must(galacticv1alpha1.AddToScheme(scheme))
// Discovery is static, so it needs no API server either.
mapper := apimeta.NewDefaultRESTMapper(nil)
for gvk := range scheme.AllKnownTypes() {
if gvk.Group == galacticv1alpha1.GroupVersion.Group && gvk.Kind == "EdgeListener" && !listeners {
continue
}
mapper.Add(gvk, apimeta.RESTScopeNamespace)
}
rc := &recordingCache{
FakeInformers: &informertest.FakeInformers{Scheme: scheme},
kinds: map[schema.GroupVersionKind]bool{},
Expand All @@ -118,6 +131,7 @@ func watchedKinds(t *testing.T, projectServices, ipv4VIPs, backendHealth bool) [
HealthProbeBindAddress: "0",
Metrics: metricsserver.Options{BindAddress: "0"},
NewCache: func(*rest.Config, cache.Options) (cache.Cache, error) { return rc, nil },
MapperProvider: func(*rest.Config, *http.Client) (apimeta.RESTMapper, error) { return mapper, nil },
Controller: config.Controller{SkipNameValidation: ptr.To(true)},
})
if err != nil {
Expand Down Expand Up @@ -171,19 +185,20 @@ func watchedKinds(t *testing.T, projectServices, ipv4VIPs, backendHealth bool) [
// watch a type checkWatchPermissions does not check, or the other way round,
// so a new watch cannot ship without its pre-flight entry.
func TestWatchedResourcesMatchControllers(t *testing.T) {
for _, tc := range []struct{ projectServices, ipv4VIPs, backendHealth bool }{
for _, tc := range []struct{ projectServices, ipv4VIPs, backendHealth, listeners bool }{
{},
{ipv4VIPs: true},
{backendHealth: true},
{projectServices: true, ipv4VIPs: true, backendHealth: true},
{listeners: true},
{projectServices: true, ipv4VIPs: true, backendHealth: true, listeners: true},
} {
t.Run(fmt.Sprintf("projectServices=%t,ipv4VIPs=%t,backendHealth=%t",
tc.projectServices, tc.ipv4VIPs, tc.backendHealth), func(t *testing.T) {
t.Run(fmt.Sprintf("projectServices=%t,ipv4VIPs=%t,backendHealth=%t,listeners=%t",
tc.projectServices, tc.ipv4VIPs, tc.backendHealth, tc.listeners), func(t *testing.T) {
want := map[schema.GroupVersionKind]watchedResource{}
for _, r := range watchedResources(tc.projectServices) {
for _, r := range watchedResources(tc.projectServices, tc.listeners) {
want[schema.GroupVersionKind{Group: r.group, Version: r.version, Kind: r.kind}] = r
}
got := watchedKinds(t, tc.projectServices, tc.ipv4VIPs, tc.backendHealth)
got := watchedKinds(t, tc.projectServices, tc.ipv4VIPs, tc.backendHealth, tc.listeners)
for _, gvk := range got {
if _, ok := want[gvk]; !ok {
t.Errorf("a controller watches %s, which watchedResources leaves out", gvk)
Expand All @@ -202,7 +217,7 @@ func TestWatchedResourcesMatchControllers(t *testing.T) {
// other than its kind's CRD does, which would review access to the wrong
// resource.
func TestWatchedResourceNames(t *testing.T) {
for _, r := range watchedResources(true) {
for _, r := range watchedResources(true, true) {
data, err := os.ReadFile(filepath.Join("..", "..", "config", "crd", r.group+"_"+r.resource+".yaml"))
if err != nil {
t.Errorf("%s: no CRD for resource %q: %v", r.kind, r.resource, err)
Expand Down Expand Up @@ -255,7 +270,7 @@ func TestShippedRoleGrantsWatches(t *testing.T) {
slices.Contains(rule.Verbs, verb)
})
}
for _, r := range watchedResources(true) {
for _, r := range watchedResources(true, true) {
for _, verb := range informerVerbs {
if !grants(r.group, r.resource, verb) {
t.Errorf("config/galactic-gateway/rbac.yaml does not grant %s on %s.%s", verb, r.resource, r.group)
Expand Down
12 changes: 6 additions & 6 deletions cmd/galactic-gateway/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestCheckWatchPermissions(t *testing.T) {
denyVPCs := func(_, resource string) bool { return resource != resourceVPCs }

c, asked := accessReviewClient(t, denyVPCs, nil)
if err := checkWatchPermissions(ctx, c, false); err != nil {
if err := checkWatchPermissions(ctx, c, false, false); err != nil {
t.Fatalf("project services off: %v, want nil", err)
}
for _, a := range *asked {
Expand All @@ -139,34 +139,34 @@ func TestCheckWatchPermissions(t *testing.T) {
}

c, _ = accessReviewClient(t, denyVPCs, nil)
err := checkWatchPermissions(ctx, c, true)
err := checkWatchPermissions(ctx, c, true, false)
if err == nil || !strings.Contains(err.Error(), resourceVPCs) {
t.Fatalf("project services on without VPC access: %v, want an error naming %s", err, resourceVPCs)
}

c, _ = accessReviewClient(t, allowAll, nil)
if err := checkWatchPermissions(ctx, c, true); err != nil {
if err := checkWatchPermissions(ctx, c, true, false); err != nil {
t.Fatalf("all granted: %v, want nil", err)
}

denyAddressList := func(verb, resource string) bool {
return verb != verbList || resource != resourceNetworkServiceAddresses
}
c, _ = accessReviewClient(t, denyAddressList, nil)
err = checkWatchPermissions(ctx, c, false)
err = checkWatchPermissions(ctx, c, false, false)
if err == nil || !strings.Contains(err.Error(), verbList+" on "+resourceNetworkServiceAddresses) {
t.Fatalf("list denied on %s: %v, want an error naming it", resourceNetworkServiceAddresses, err)
}

c, _ = accessReviewClient(t, allowAll, errors.New("unavailable"))
if err := checkWatchPermissions(ctx, c, true); err != nil {
if err := checkWatchPermissions(ctx, c, true, false); err != nil {
t.Fatalf("reviews that cannot be made: %v, want nil", err)
}

ended, cancel := context.WithCancel(ctx)
cancel()
c, _ = accessReviewClient(t, allowAll, context.Canceled)
if err := checkWatchPermissions(ended, c, true); err == nil {
if err := checkWatchPermissions(ended, c, true, false); err == nil {
t.Fatal("reviews cut off by the context ending: nil, want an error")
}
}
12 changes: 10 additions & 2 deletions cmd/galactic-gateway/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (

cloudv1alpha1 "go.datum.net/galactic/api/cloud/v1alpha1"
bgpv1alpha1 "go.datum.net/galactic/api/network/v1alpha1"
galacticv1alpha1 "go.datum.net/galactic/api/v1alpha1"
"go.datum.net/galactic/internal/config"
"go.datum.net/galactic/internal/controller"
"go.datum.net/galactic/internal/edge/listener"
"go.datum.net/galactic/internal/gateway"
"go.datum.net/galactic/internal/logging"
"go.datum.net/galactic/internal/metadata"
Expand Down Expand Up @@ -54,6 +56,7 @@ func runCmd(cfg *config.GatewayConfig) error {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(bgpv1alpha1.AddToScheme(scheme))
utilruntime.Must(cloudv1alpha1.AddToScheme(scheme))
utilruntime.Must(galacticv1alpha1.AddToScheme(scheme))

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Expand Down Expand Up @@ -101,9 +104,14 @@ func runCmd(cfg *config.GatewayConfig) error {
grpcSrv.GracefulStop()
}()

// Pre-flight RBAC check: a watch the role denies fails startup.
// Pre-flight RBAC check: a watch the role denies fails startup. The
// EdgeListener watch exists only where the API server serves the kind.
listenersServed, err := listener.ListenerAPIServed(mgr.GetRESTMapper())
if err != nil {
return err
}
rbacCtx, cancelRBAC := context.WithTimeout(ctx, 30*time.Second)
err = checkWatchPermissions(rbacCtx, mgr.GetClient(), cfg.ProjectServices)
err = checkWatchPermissions(rbacCtx, mgr.GetClient(), cfg.ProjectServices, listenersServed)
cancelRBAC()
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions cmd/galactic-router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ func checkWatchPermissions(mgr ctrl.Manager, trafficPolicy bool) {
{group: bgpAPIGroup, version: bgpAPIVersion, resource: resourceProducerReports},
{group: bgpAPIGroup, version: bgpAPIVersion, resource: resourceServiceRoutePolicy},
{group: "galactic.datumapis.com", version: "v1alpha1", resource: resourceVPCAttachments},
// Watched only where the API server serves EdgeListener, but checked
// everywhere: this pre-flight only logs, and the ClusterRole grants it.
{
group: galacticv1alpha1.GroupVersion.Group, version: galacticv1alpha1.GroupVersion.Version,
resource: "edgelisteners",
},
{version: "v1", resource: resourceSecrets},
{version: "v1", resource: resourceNodes},
}
Expand Down
12 changes: 12 additions & 0 deletions config/crd/galactic.datumapis.com_edgelisteners.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ spec:
description: Attachment is the EdgeAttachment's namespace and name.
maxLength: 317
type: string
hostLink:
description: |-
HostLink is the host end of the attachment's link, which the gateway
health check probes through.
maxLength: 15
type: string
node:
description: Node is the attachment's node.
maxLength: 253
Expand All @@ -223,6 +229,12 @@ spec:
Serving is whether the attachment receives new flows: it is
programmed, its instance lease is valid and its provider reports Ready.
type: boolean
uid:
description: |-
UID is the EdgeAttachment's metadata.uid. A gateway health decision
must name it to apply to this backend.
maxLength: 36
type: string
required:
- attachment
type: object
Expand Down
5 changes: 3 additions & 2 deletions config/crd/galactic.datumapis.com_networkservices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ spec:
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.
namespace. Its backends are the listener's status.backends that core
reports serving; gateways and backend nodes never select them from
VPCAttachments.
properties:
name:
description: Name is the EdgeListener's name.
Expand Down
8 changes: 8 additions & 0 deletions config/galactic-gateway/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ rules:
resources:
- vpcattachments
verbs: ["get", "list", "watch"]
# A listener NetworkService's backends are its EdgeListener's serving
# backends, read from the listener's status and watched so a backend that
# stops serving leaves the Maglev table. A gateway skips the watch when
# the API server does not serve EdgeListener.
- apiGroups: ["galactic.datumapis.com"]
resources:
- edgelisteners
verbs: ["get", "list", "watch"]
# With GALACTIC_GATEWAY_PROJECT_SERVICES on, a NetworkService in a project
# namespace is accepted only when a VPC in that namespace carries its
# vpcRef, so the gateway reads VPCs and re-checks its services when one
Expand Down
7 changes: 7 additions & 0 deletions config/galactic-router/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ rules:
- vpcs
- vpcattachments
verbs: ["get", "list", "watch"]
# A listener NetworkService's backends, their bindings and their health
# probes come from its EdgeListener's status. The router skips the watch
# when the API server does not serve EdgeListener.
- apiGroups: ["galactic.datumapis.com"]
resources:
- edgelisteners
verbs: ["get", "list", "watch"]
# GC keeps a VPC's VRF and BGPVRFInstance while an edge attachment on
# this node uses it (internal/edge/maintenance), read uncached.
- apiGroups: ["galactic.datumapis.com"]
Expand Down
2 changes: 2 additions & 0 deletions docs/api/galactic.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,10 @@ _Appears in:_
| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `attachment` _string_ | Attachment is the EdgeAttachment's namespace and name. | | MaxLength: 317 <br /> |
| `uid` _string_ | UID is the EdgeAttachment's metadata.uid. A gateway health decision<br />must name it to apply to this backend. | | MaxLength: 36 <br /> |
| `node` _string_ | Node is the attachment's node. | | MaxLength: 253 <br /> |
| `address` _string_ | Address is the attachment's IPv6 address in the ingress gVPC. | | MaxLength: 39 <br /> |
| `hostLink` _string_ | HostLink is the host end of the attachment's link, which the gateway<br />health check probes through. | | MaxLength: 15 <br /> |
| `serving` _boolean_ | Serving is whether the attachment receives new flows: it is<br />programmed, its instance lease is valid and its provider reports Ready. | | |


Expand Down
Loading
Loading