Files
cozystack/packages/apps/vpc/templates/vpc.yaml
T
Mattia Eleuteri 3c657a4cc1 feat(vpc): expose allowSubnets per subnet
Every VPC subnet is rendered private, so kube-ovn drops any packet
whose source lies outside the subnet's own CIDR. A tenant that runs a
VPN or router VM on a subnet and points a static route at it gets the
forward path from the existing routes value, but traffic arriving
from the remote network is then dropped by the private ACL.

Subnet is cluster-scoped, so the only workaround so far has been an
operator patching spec.allowSubnets by hand: something the tenant cannot
do, that no application value records, and that is lost whenever the
subnet is recreated. Declaring it per subnet keeps the widening in the
tenant's own VPC and scoped to the one subnet that needs it.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
2026-09-29 18:47:18 +03:00

133 lines
4.2 KiB
YAML

## Release.Namespace == tenant name
## Release.Name == vpc name
{{ $vpcId := print "vpc-" (print .Release.Namespace "/" .Release.Name | sha256sum | trunc 6) }}
---
apiVersion: kubeovn.io/v1
kind: Vpc
metadata:
name: {{ $vpcId }}
labels:
cozystack.io/vpcName: {{ .Release.Name }}
cozystack.io/tenantName: {{ .Release.Namespace }}
spec:
enableExternal: false
namespaces:
- {{ .Release.Namespace }}
{{- if .Values.peers }}
vpcPeerings:
{{- range .Values.peers }}
{{- $remoteRelease := printf "virtualprivatecloud-%s" .vpcName }}
{{- $remoteVpcId := printf "vpc-%s" (printf "%s/%s" .tenantNamespace $remoteRelease | sha256sum | trunc 6) }}
{{- $sorted := list $vpcId $remoteVpcId | sortAlpha }}
{{- $pairKey := join "/" $sorted }}
{{- $pairHash := sha256sum $pairKey }}
{{- $byte0 := int (include "cozy-lib.strings.hexToInt" (substr 0 2 $pairHash)) }}
{{- $byte1 := int (include "cozy-lib.strings.hexToInt" (substr 2 4 $pairHash)) }}
{{- $oct3 := int (add (mod $byte0 254) 1) }}
{{- $base4 := int (mul (mod $byte1 64) 4) }}
{{- if eq $vpcId (index $sorted 0) }}
- remoteVpc: {{ $remoteVpcId }}
localConnectIP: {{ printf "169.254.%d.%d" $oct3 (int (add $base4 1)) }}
{{- else }}
- remoteVpc: {{ $remoteVpcId }}
localConnectIP: {{ printf "169.254.%d.%d" $oct3 (int (add $base4 2)) }}
{{- end }}
{{- end }}
{{- end }}
{{- if .Values.routes }}
staticRoutes:
{{- range .Values.routes }}
- cidr: {{ .cidr | quote }}
nextHopIP: {{ .nextHopIP | quote }}
{{- end }}
{{- end }}
{{- range .Values.subnets }}
{{- $subnetId := print "subnet-" (print $.Release.Namespace "/" $vpcId "/" .name | sha256sum | trunc 8) }}
---
apiVersion: k8s.cni.cncf.io/v1
kind: NetworkAttachmentDefinition
metadata:
name: {{ $subnetId }}
namespace: {{ $.Release.Namespace }}
labels:
cozystack.io/subnetName: {{ .name }}
cozystack.io/vpcId: {{ $vpcId }}
cozystack.io/vpcName: {{ $.Release.Name }}
cozystack.io/tenantName: {{ $.Release.Namespace }}
spec:
config: '{
"cniVersion": "0.3.0",
"type": "kube-ovn",
"server_socket": "/run/openvswitch/kube-ovn-daemon.sock",
"provider": "{{ $subnetId }}.{{ $.Release.Namespace }}.ovn"
}'
---
apiVersion: kubeovn.io/v1
kind: Subnet
metadata:
name: {{ $subnetId }}
labels:
cozystack.io/subnetName: {{ .name }}
cozystack.io/vpcId: {{ $vpcId }}
cozystack.io/vpcName: {{ $.Release.Name }}
cozystack.io/tenantName: {{ $.Release.Namespace }}
spec:
vpc: {{ $vpcId }}
cidrBlock: {{ .cidr }}
provider: "{{ $subnetId }}.{{ $.Release.Namespace }}.ovn"
protocol: IPv4
enableLb: false
private: true
{{- with .allowSubnets }}
allowSubnets:
{{- range . }}
- {{ . | quote }}
{{- end }}
{{- end }}
{{- end }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $.Release.Name }}-subnets
labels:
apps.cozystack.io/application.group: apps.cozystack.io
apps.cozystack.io/application.kind: VirtualPrivateCloud
apps.cozystack.io/application.name: {{ trimPrefix "virtualprivatecloud-" .Release.Name }}
cozystack.io/vpcId: {{ $vpcId }}
cozystack.io/tenantName: {{ $.Release.Namespace }}
data:
{{- range .Values.subnets }}
{{ .name }}.ID: {{ print "subnet-" (print $.Release.Namespace "/" $vpcId "/" .name | sha256sum | trunc 8) }}
{{ .name }}.CIDR: {{ .cidr }}
{{- end }}
{{- range .Values.peers }}
{{- $remoteRelease := printf "virtualprivatecloud-%s" .vpcName }}
{{- $remoteVpcId := printf "vpc-%s" (printf "%s/%s" .tenantNamespace $remoteRelease | sha256sum | trunc 6) }}
peer.{{ .tenantNamespace }}.{{ .vpcName }}.vpcId: {{ $remoteVpcId | quote }}
peer.{{ .tenantNamespace }}.{{ .vpcName }}.tenantNamespace: {{ .tenantNamespace | quote }}
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: "{{ .Release.Name }}-subnets"
subjects: {{- include "cozy-lib.rbac.subjectsForTenantAndAccessLevel" (list "view" .Release.Namespace ) | nindent 2 }}
roleRef:
kind: Role
name: "{{ .Release.Name }}-subnets"
apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: "{{ .Release.Name }}-subnets"
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get","list","watch"]
resourceNames: ["{{ .Release.Name }}-subnets"]