Skip to content

Commit e2d4d9d

Browse files
authored
feat: allow setting AllowedIps through WireguardPeer (#191)
* set default for allowIps * generate manifest * update test
1 parent c6836b2 commit e2d4d9d

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

config/crd/bases/vpn.wireguard-operator.io_wireguardpeers.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ spec:
4545
Important: Run "make" to regenerate code after modifying this file
4646
The address of the peer.
4747
type: string
48+
allowedIPs:
49+
description: The AllowedIPs of the peer.
50+
type: string
4851
disabled:
4952
description: Set to true to temporarily disable the peer.
5053
type: boolean

pkg/api/v1alpha1/wireguardpeer_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type WireguardPeerSpec struct {
3737
// Important: Run "make" to regenerate code after modifying this file
3838
// The address of the peer.
3939
Address string `json:"address,omitempty"`
40+
// The AllowedIPs of the peer.
41+
AllowedIPs string `json:"allowedIPs,omitempty"`
4042
// Set to true to temporarily disable the peer.
4143
Disabled bool `json:"disabled,omitempty"`
4244
// The DNS configuration for the peer.

pkg/controllers/wireguard_controller.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ func (r *WireguardReconciler) updateWireguardPeers(ctx context.Context, req ctrl
193193
dnsConfiguration = dns + ", " + dnsSearchDomain
194194
}
195195

196+
allowIps := peer.Spec.AllowedIPs
197+
198+
if allowIps == "" {
199+
allowIps = "0.0.0.0/0"
200+
}
201+
196202
newConfig := fmt.Sprintf(`
197203
echo "
198204
[Interface]
@@ -208,8 +214,8 @@ DNS = %s`, peer.Spec.PrivateKey.SecretKeyRef.Name, peer.Spec.PrivateKey.SecretKe
208214
209215
[Peer]
210216
PublicKey = %s
211-
AllowedIPs = 0.0.0.0/0
212-
Endpoint = %s:%s"`, serverPublicKey, serverAddress, wireguard.Status.Port)
217+
AllowedIPs = %s
218+
Endpoint = %s:%s"`, serverPublicKey, allowIps, serverAddress, wireguard.Status.Port)
213219
if peer.Status.Config != newConfig || peer.Status.Status != v1alpha1.Ready {
214220
peer.Status.Config = newConfig
215221
peer.Status.Status = v1alpha1.Ready

pkg/controllers/wireguard_controller_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package controllers
33
import (
44
"context"
55
"fmt"
6-
"github.com/jodevsa/wireguard-operator/pkg/api/v1alpha1"
7-
"sigs.k8s.io/controller-runtime/pkg/client"
86
"strconv"
97
"strings"
108
"time"
119

10+
"github.com/jodevsa/wireguard-operator/pkg/api/v1alpha1"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
12+
1213
. "github.com/onsi/ginkgo"
1314
. "github.com/onsi/gomega"
1415
appsv1 "k8s.io/api/apps/v1"
@@ -371,8 +372,8 @@ DNS = %s, %s.svc.cluster.local
371372
372373
[Peer]
373374
PublicKey = %s
374-
AllowedIPs = 0.0.0.0/0
375-
Endpoint = %s:%s"`, peerKey.Name, peer.Spec.Address, dnsServiceIp, peer.Namespace, wgPublicKey, expectedAddress, expectedNodePort),
375+
AllowedIPs = %s
376+
Endpoint = %s:%s"`, peerKey.Name, peer.Spec.AllowedIPs, peer.Spec.Address, dnsServiceIp, peer.Namespace, wgPublicKey, expectedAddress, expectedNodePort),
376377
Status: "ready",
377378
Message: "Peer configured",
378379
}))
@@ -500,8 +501,8 @@ DNS = %s, %s.svc.cluster.local
500501
501502
[Peer]
502503
PublicKey = %s
503-
AllowedIPs = 0.0.0.0/0
504-
Endpoint = %s:%s"`, peerKey.Name, peer.Spec.Address, dnsServiceIp, peer.Namespace, wgPublicKey, expectedExternalHostName, wg.Status.Port),
504+
AllowedIPs = %s
505+
Endpoint = %s:%s"`, peerKey.Name, peer.Spec.AllowedIPs, peer.Spec.Address, dnsServiceIp, peer.Namespace, wgPublicKey, expectedExternalHostName, wg.Status.Port),
505506
Status: "ready",
506507
Message: "Peer configured",
507508
}))
@@ -515,7 +516,7 @@ Endpoint = %s:%s"`, peerKey.Name, peer.Spec.Address, dnsServiceIp, peer.Namespac
515516
for _, useWgUserspace := range []bool{true, false} {
516517
testTextPrefix := "uses"
517518
if !useWgUserspace {
518-
testTextPrefix="does not use"
519+
testTextPrefix = "does not use"
519520
}
520521

521522
It(fmt.Sprintf("%s userspace implementation of wireguard if spec.useWgUserspaceImplementation is set to %t", testTextPrefix, useWgUserspace), func() {

0 commit comments

Comments
 (0)