Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow setting AllowedIps through WireguardPeer #191

Merged
merged 3 commits into from
Jul 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ spec:
Important: Run "make" to regenerate code after modifying this file
The address of the peer.
type: string
allowedIPs:
description: The AllowedIPs of the peer.
type: string
disabled:
description: Set to true to temporarily disable the peer.
type: boolean
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1alpha1/wireguardpeer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type WireguardPeerSpec struct {
// Important: Run "make" to regenerate code after modifying this file
// The address of the peer.
Address string `json:"address,omitempty"`
// The AllowedIPs of the peer.
AllowedIPs string `json:"allowedIPs,omitempty"`
// Set to true to temporarily disable the peer.
Disabled bool `json:"disabled,omitempty"`
// The DNS configuration for the peer.
Expand Down
10 changes: 8 additions & 2 deletions pkg/controllers/wireguard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ func (r *WireguardReconciler) updateWireguardPeers(ctx context.Context, req ctrl
dnsConfiguration = dns + ", " + dnsSearchDomain
}

allowIps := peer.Spec.AllowedIPs

if allowIps == "" {
allowIps = "0.0.0.0/0"
}

newConfig := fmt.Sprintf(`
echo "
[Interface]
Expand All @@ -208,8 +214,8 @@ DNS = %s`, peer.Name, peer.Namespace, peer.Spec.Address, dnsConfiguration)

[Peer]
PublicKey = %s
AllowedIPs = 0.0.0.0/0
Endpoint = %s:%s"`, serverPublicKey, serverAddress, wireguard.Status.Port)
AllowedIPs = %s
Endpoint = %s:%s"`, serverPublicKey, allowIps, serverAddress, wireguard.Status.Port)
if peer.Status.Config != newConfig || peer.Status.Status != v1alpha1.Ready {
peer.Status.Config = newConfig
peer.Status.Status = v1alpha1.Ready
Expand Down
15 changes: 8 additions & 7 deletions pkg/controllers/wireguard_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package controllers
import (
"context"
"fmt"
"github.com/jodevsa/wireguard-operator/pkg/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
"strconv"
"strings"
"time"

"github.com/jodevsa/wireguard-operator/pkg/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -371,8 +372,8 @@ DNS = %s, %s.svc.cluster.local

[Peer]
PublicKey = %s
AllowedIPs = 0.0.0.0/0
Endpoint = %s:%s"`, peerKey.Name, peer.Spec.Address, dnsServiceIp, peer.Namespace, wgPublicKey, expectedAddress, expectedNodePort),
AllowedIPs = %s
Endpoint = %s:%s"`, peerKey.Name, peer.Spec.AllowedIPs, peer.Spec.Address, dnsServiceIp, peer.Namespace, wgPublicKey, expectedAddress, expectedNodePort),
Status: "ready",
Message: "Peer configured",
}))
Expand Down Expand Up @@ -500,8 +501,8 @@ DNS = %s, %s.svc.cluster.local

[Peer]
PublicKey = %s
AllowedIPs = 0.0.0.0/0
Endpoint = %s:%s"`, peerKey.Name, peer.Spec.Address, dnsServiceIp, peer.Namespace, wgPublicKey, expectedExternalHostName, wg.Status.Port),
AllowedIPs = %s
Endpoint = %s:%s"`, peerKey.Name, peer.Spec.AllowedIPs, peer.Spec.Address, dnsServiceIp, peer.Namespace, wgPublicKey, expectedExternalHostName, wg.Status.Port),
Status: "ready",
Message: "Peer configured",
}))
Expand All @@ -515,7 +516,7 @@ Endpoint = %s:%s"`, peerKey.Name, peer.Spec.Address, dnsServiceIp, peer.Namespac
for _, useWgUserspace := range []bool{true, false} {
testTextPrefix := "uses"
if !useWgUserspace {
testTextPrefix="does not use"
testTextPrefix = "does not use"
}

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