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
20 changes: 20 additions & 0 deletions api/v1alpha1/stackitcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,29 @@ type StackitClusterSpec struct {
}

// StackitClusterNetworkSpec references the network for a StackitCluster.
// +kubebuilder:validation:XValidation:rule="!has(self.id) || (!has(self.prefix) && !has(self.prefixLength))",message="id cannot be combined with prefix or prefixLength"
// +kubebuilder:validation:XValidation:rule="!has(self.prefix) || !has(self.prefixLength)",message="prefix and prefixLength are mutually exclusive"
type StackitClusterNetworkSpec struct {
// id is the STACKIT network ID.
// Omitting the network ID provisions a dedicated managed STACKIT network for the cluster.
// +kubebuilder:validation:Pattern=`^[0-9a-fA-F-]{36}$`
// +optional
ID string `json:"id,omitempty"`

// prefix is the IPv4 CIDR block used when creating a managed network (e.g. "10.0.0.0/24").
// Defaults to "10.0.0.0/24" if neither id, prefix, nor prefixLength is specified.
// Mutually exclusive with id and prefixLength.
// +kubebuilder:validation:Format=cidr
// +optional
Prefix string `json:"prefix,omitempty"`

// prefixLength is the subnet mask size (e.g. 24 for a /24) when letting STACKIT IPAM
// allocate a prefix dynamically from the project's Network Area.
// Mutually exclusive with id and prefix.
// +kubebuilder:validation:Minimum=8
// +kubebuilder:validation:Maximum=29
// +optional
PrefixLength *int32 `json:"prefixLength,omitempty"`
}

// StackitAPIServerLoadBalancerSpec configures an optional load balancer for
Expand Down Expand Up @@ -229,6 +246,9 @@ type StackitNetworkStatus struct {
// managed indicates whether the network is managed (created and owned) by CAPSTK.
// +optional
Managed bool `json:"managed,omitempty"`
// ipv4Prefixes is the list of IPv4 prefixes allocated to the network.
// +optional
IPv4Prefixes []string `json:"ipv4Prefixes,omitempty"`
}

// Condition types maintained by the StackitClusterReconciler.
Expand Down
14 changes: 12 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

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

6 changes: 6 additions & 0 deletions cloud/services/network/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func (s NetworkSpec) validate() error {
if len(s.Tags) == 0 {
return fmt.Errorf("%w: network tags are required", shared.ErrInvalidInput)
}
if s.IPv4Prefix != "" && s.IPv4PrefixLength != nil {
return fmt.Errorf("%w: network prefix and prefixLength are mutually exclusive", shared.ErrInvalidInput)
}
return nil
}
if s.ID == "" {
Expand All @@ -176,6 +179,9 @@ func (s NetworkSpec) toCreatePayload() iaas.CreateNetworkPayload {
if s.IPv4Prefix != "" {
ipv4Payload := iaas.NewCreateNetworkIPv4WithPrefix(s.IPv4Prefix)
payload.SetIpv4(iaas.CreateNetworkIPv4WithPrefixAsCreateNetworkIPv4(ipv4Payload))
} else if s.IPv4PrefixLength != nil {
ipv4Payload := iaas.NewCreateNetworkIPv4WithPrefixLength(int64(*s.IPv4PrefixLength))
payload.SetIpv4(iaas.CreateNetworkIPv4WithPrefixLengthAsCreateNetworkIPv4(ipv4Payload))
}
return *payload
}
Expand Down
115 changes: 94 additions & 21 deletions cloud/services/network/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ func writeJSON(t *testing.T, w http.ResponseWriter, value any) {
}
}

func newCreateNetworkHandler(t *testing.T, createdPayload *map[string]any, prefixes []any) http.HandlerFunc {
t.Helper()
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/networks") {
writeJSON(t, w, map[string]any{"items": []any{}})
return
}
if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/networks") {
if createdPayload != nil {
if err := json.NewDecoder(r.Body).Decode(createdPayload); err != nil {
t.Fatalf("decode payload: %v", err)
}
}
writeJSON(t, w, map[string]any{
"id": testNetworkID,
"name": "test-network",
"status": "CREATED",
"ipv4": map[string]any{
"prefixes": prefixes,
},
})
return
}
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String())
}
}

func TestServiceReconcile(t *testing.T) {
t.Run("fetches network and records IPv4 prefixes via SetNetwork", func(t *testing.T) {
var (
Expand Down Expand Up @@ -161,7 +188,9 @@ func TestServiceReconcile(t *testing.T) {
t.Fatalf("Reconcile() error = %v, want ErrInvalidInput", err)
}
})
}

func TestServiceReconcileManagedNetwork(t *testing.T) {
t.Run("creates managed network when none exists", func(t *testing.T) {
var (
recorded *network.Network
Expand All @@ -179,27 +208,8 @@ func TestServiceReconcile(t *testing.T) {
},
}

svc := newTestService(t, spec, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/networks") {
writeJSON(t, w, map[string]any{"items": []any{}})
return
}
if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/networks") {
if err := json.NewDecoder(r.Body).Decode(&createdPayload); err != nil {
t.Fatalf("decode payload: %v", err)
}
writeJSON(t, w, map[string]any{
"id": testNetworkID,
"name": "test-network",
"status": "CREATED",
"ipv4": map[string]any{
"prefixes": []any{"10.0.0.0/24"},
},
})
return
}
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String())
}))
handler := newCreateNetworkHandler(t, &createdPayload, []any{"10.0.0.0/24"})
svc := newTestService(t, spec, handler)

if err := svc.Reconcile(context.Background()); err != nil {
t.Fatalf("Reconcile() error = %v", err)
Expand All @@ -215,6 +225,69 @@ func TestServiceReconcile(t *testing.T) {
}
})

t.Run("creates managed network with custom IPv4 prefix", func(t *testing.T) {
var createdPayload map[string]any
spec := network.NetworkSpec{
Name: "test-network",
IPv4Prefix: "10.0.1.0/24",
Tags: map[string]string{"cluster": "test-cluster"},
Managed: true,
}

handler := newCreateNetworkHandler(t, &createdPayload, []any{"10.0.1.0/24"})
svc := newTestService(t, spec, handler)

if err := svc.Reconcile(context.Background()); err != nil {
t.Fatalf("Reconcile() error = %v", err)
}
ipv4, ok := createdPayload["ipv4"].(map[string]any)
if !ok || ipv4["prefix"] != "10.0.1.0/24" {
t.Fatalf("payload ipv4 prefix = %v, want 10.0.1.0/24", createdPayload["ipv4"])
}
})

t.Run("creates managed network with IPv4 prefix length", func(t *testing.T) {
var createdPayload map[string]any
prefixLen := int32(24)
spec := network.NetworkSpec{
Name: "test-network",
IPv4PrefixLength: &prefixLen,
Tags: map[string]string{"cluster": "test-cluster"},
Managed: true,
}

handler := newCreateNetworkHandler(t, &createdPayload, []any{"10.0.2.0/24"})
svc := newTestService(t, spec, handler)

if err := svc.Reconcile(context.Background()); err != nil {
t.Fatalf("Reconcile() error = %v", err)
}
ipv4, ok := createdPayload["ipv4"].(map[string]any)
if !ok || ipv4["prefixLength"] != float64(24) {
t.Fatalf("payload ipv4 prefixLength = %v, want 24", createdPayload["ipv4"])
}
})

t.Run("returns ErrInvalidInput when both prefix and prefixLength are set", func(t *testing.T) {
prefixLen := int32(24)
spec := network.NetworkSpec{
Name: "test-network",
IPv4Prefix: "10.0.0.0/24",
IPv4PrefixLength: &prefixLen,
Tags: map[string]string{"cluster": "test-cluster"},
Managed: true,
}

svc := newTestService(t, spec, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String())
}))

err := svc.Reconcile(context.Background())
if !errors.Is(err, shared.ErrInvalidInput) {
t.Fatalf("Reconcile() error = %v, want ErrInvalidInput", err)
}
})

t.Run("adopts existing managed network by tags without creating duplicate", func(t *testing.T) {
var (
recorded *network.Network
Expand Down
13 changes: 7 additions & 6 deletions cloud/services/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ type Network struct {

// NetworkSpec describes the desired state of a STACKIT network.
type NetworkSpec struct {
ID string
Name string
IPv4Prefix string
Tags map[string]string
Managed bool
SetNetwork func(network *Network, created bool)
ID string
Name string
IPv4Prefix string
IPv4PrefixLength *int32
Tags map[string]string
Managed bool
SetNetwork func(network *Network, created bool)
}

// Scope is an interface that provides scope data needed for network reconciliation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,28 @@ spec:
Omitting the network ID provisions a dedicated managed STACKIT network for the cluster.
pattern: ^[0-9a-fA-F-]{36}$
type: string
prefix:
description: |-
prefix is the IPv4 CIDR block used when creating a managed network (e.g. "10.0.0.0/24").
Defaults to "10.0.0.0/24" if neither id, prefix, nor prefixLength is specified.
Mutually exclusive with id and prefixLength.
format: cidr
type: string
prefixLength:
description: |-
prefixLength is the subnet mask size (e.g. 24 for a /24) when letting STACKIT IPAM
allocate a prefix dynamically from the project's Network Area.
Mutually exclusive with id and prefix.
format: int32
maximum: 29
minimum: 8
type: integer
type: object
x-kubernetes-validations:
- message: id cannot be combined with prefix or prefixLength
rule: '!has(self.id) || (!has(self.prefix) && !has(self.prefixLength))'
- message: prefix and prefixLength are mutually exclusive
rule: '!has(self.prefix) || !has(self.prefixLength)'
projectID:
description: projectID is the STACKIT project that owns the cluster
infrastructure.
Expand Down Expand Up @@ -400,6 +421,12 @@ spec:
id:
description: id is the STACKIT network ID.
type: string
ipv4Prefixes:
description: ipv4Prefixes is the list of IPv4 prefixes allocated
to the network.
items:
type: string
type: array
managed:
description: managed indicates whether the network is managed
(created and owned) by CAPSTK.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,28 @@ spec:
Omitting the network ID provisions a dedicated managed STACKIT network for the cluster.
pattern: ^[0-9a-fA-F-]{36}$
type: string
prefix:
description: |-
prefix is the IPv4 CIDR block used when creating a managed network (e.g. "10.0.0.0/24").
Defaults to "10.0.0.0/24" if neither id, prefix, nor prefixLength is specified.
Mutually exclusive with id and prefixLength.
format: cidr
type: string
prefixLength:
description: |-
prefixLength is the subnet mask size (e.g. 24 for a /24) when letting STACKIT IPAM
allocate a prefix dynamically from the project's Network Area.
Mutually exclusive with id and prefix.
format: int32
maximum: 29
minimum: 8
type: integer
type: object
x-kubernetes-validations:
- message: id cannot be combined with prefix or prefixLength
rule: '!has(self.id) || (!has(self.prefix) && !has(self.prefixLength))'
- message: prefix and prefixLength are mutually exclusive
rule: '!has(self.prefix) || !has(self.prefixLength)'
projectID:
description: projectID is the STACKIT project that owns the
cluster infrastructure.
Expand Down
17 changes: 7 additions & 10 deletions docs/src/topics/cloud-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ security groups, SSH keys, and images.

## Network

`StackitCluster.spec.network.id` is the STACKIT network ID used by all cluster
machines and the provider-managed API server load balancer.
`StackitCluster.spec.network` configures the cluster network:

If you don't provide a network ID, CAPSTK will automatically create a dedicated /24 network for the cluster.
- **BYO Network:** Set `spec.network.id` to use an existing STACKIT network.
- **Managed Network:** Omit `id` to let CAPSTK provision a dedicated network:
- `spec.network.prefix`: Desired IPv4 CIDR (e.g. `10.0.1.0/24`). Defaults to `10.0.0.0/24`.
- `spec.network.prefixLength`: Prefix length (e.g. `24`) to let STACKIT IPAM allocate a prefix dynamically from the project's Network Area.
- *Note:* `id`, `prefix`, and `prefixLength` are mutually exclusive.

Planning your networks usually involves platform-level policy such as address
planning, routing, peering, VPN, DNS, and shared-service access, but the default
managed network is built for convenience. A newly created (managed) network
will have an attached tag indicating that it is managed by CAPSTK (`cluster-api-provider-stackit=true`).

Only empty networks can be deleted, so when you delete your CAPSTK cluster and
all cluster nodes and load balancers are removed, the managed network will be deleted too.
Managed networks are labeled (`cluster-api-provider-stackit=true`) and deleted when the cluster is torn down. Observed network ID and prefixes are recorded in `StackitCluster.status.network`.

> **Note:** When using a managed network, the network ID is generated dynamically during cluster reconciliation and populated on `StackitCluster.status.network.id`. Because `templates/addons/cloud-provider-stackit.yaml` requires this network ID for its `Service` type `LoadBalancer` controller, you use `templates/cluster-template-managed-network.yaml` and configure the Cloud Controller Manager after the network ID is provisioned. See [Cloud Provider with Managed Networks](../usage/addons.md#managed-networks-and-cloud-provider-stackit) for the step-by-step procedure.

Expand Down
2 changes: 1 addition & 1 deletion docs/src/usage/cluster-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ kubectl apply -f "${CLUSTER_NAME}.yaml"

## Deploying with a Managed Network

When using `templates/cluster-template-managed-network.yaml`, `STACKIT_NETWORK_ID` is omitted. CAPSTK automatically provisions a dedicated `/24` network during `StackitCluster` reconciliation and publishes its ID to `StackitCluster.status.network.id`.
When using `templates/cluster-template-managed-network.yaml`, `STACKIT_NETWORK_ID` is omitted. CAPSTK automatically provisions a dedicated network (customizable via `STACKIT_NETWORK_PREFIX`, defaulting to `10.0.0.0/24`) during `StackitCluster` reconciliation and publishes its ID and prefixes to `StackitCluster.status.network`.

Render and apply the cluster:

Expand Down
Loading