@@ -8,14 +8,14 @@ import (
88 "time"
99
1010 "github.com/jodevsa/wireguard-operator/pkg/api/v1alpha1"
11- "sigs.k8s.io/controller-runtime/pkg/client"
12-
1311 . "github.com/onsi/ginkgo"
1412 . "github.com/onsi/gomega"
1513 appsv1 "k8s.io/api/apps/v1"
1614 corev1 "k8s.io/api/core/v1"
1715 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1816 "k8s.io/apimachinery/pkg/types"
17+ "k8s.io/apimachinery/pkg/util/intstr"
18+ "sigs.k8s.io/controller-runtime/pkg/client"
1919)
2020
2121// test helpers
@@ -65,6 +65,21 @@ func reconcileServiceWithTypeLoadBalancer(svcKey client.ObjectKey, hostname stri
6565 return k8sClient .Status ().Update (context .Background (), svc )
6666}
6767
68+ func reconcileServiceWithClusterIP (svcKey client.ObjectKey , port int32 ) error {
69+ svc := & corev1.Service {}
70+ Expect (k8sClient .Get (context .Background (), svcKey , svc )).Should (Succeed ())
71+
72+ if svc .Spec .Type != corev1 .ServiceTypeClusterIP {
73+ return fmt .Errorf ("ReconcileServiceWithClusterIP only reconsiles ClusterIP services" )
74+ }
75+
76+ svc .Spec .Ports = []corev1.ServicePort {{
77+ Port : port ,
78+ TargetPort : intstr .FromInt32 (port ),
79+ }}
80+ return k8sClient .Status ().Update (context .Background (), svc )
81+ }
82+
6883var _ = Describe ("wireguard controller" , func () {
6984
7085 // Define utility constants for object names and testing timeouts/durations and intervals.
@@ -515,6 +530,146 @@ Endpoint = %s:%s"`, peerKey.Name, peer.Spec.AllowedIPs, peer.Spec.Address, dnsSe
515530
516531 })
517532
533+ It ("Should create a WG with ServiceType ClusterIP and WG peer successfully" , func () {
534+ expectedAddress := "test-address"
535+
536+ wgKey := types.NamespacedName {
537+ Name : wgName ,
538+ Namespace : wgNamespace ,
539+ }
540+ created := & v1alpha1.Wireguard {
541+ ObjectMeta : metav1.ObjectMeta {
542+ Name : wgKey .Name ,
543+ Namespace : wgKey .Namespace ,
544+ },
545+ Spec : v1alpha1.WireguardSpec {
546+ ServiceType : corev1 .ServiceTypeClusterIP ,
547+ Address : expectedAddress ,
548+ },
549+ }
550+ expectedLabels := map [string ]string {"app" : "wireguard" , "instance" : wgKey .Name }
551+
552+ Expect (k8sClient .Create (context .Background (), created )).Should (Succeed ())
553+
554+ // service created
555+ serviceName := wgKey .Name + "-svc"
556+ serviceKey := types.NamespacedName {
557+ Namespace : wgKey .Namespace ,
558+ Name : serviceName ,
559+ }
560+
561+ // match labels
562+ Eventually (func () map [string ]string {
563+ svc := & corev1.Service {}
564+ //nolint:errcheck
565+ k8sClient .Get (context .Background (), serviceKey , svc )
566+ return svc .Spec .Selector
567+ }, Timeout , Interval ).Should (BeEquivalentTo (expectedLabels ))
568+
569+ // match service type
570+ Eventually (func () corev1.ServiceType {
571+ svc := & corev1.Service {}
572+ //nolint:errcheck
573+ k8sClient .Get (context .Background (), serviceKey , svc )
574+ return svc .Spec .Type
575+ }, Timeout , Interval ).Should (Equal (corev1 .ServiceTypeClusterIP ))
576+
577+ Eventually (func () v1alpha1.WireguardStatus {
578+ wg := & v1alpha1.Wireguard {}
579+ //nolint:errcheck
580+ k8sClient .Get (context .Background (), wgKey , wg )
581+ return wg .Status
582+ }, Timeout , Interval ).Should (Equal (v1alpha1.WireguardStatus {
583+ Address : "" ,
584+ Status : "pending" ,
585+ Message : "Waiting for service to be ready" ,
586+ }))
587+
588+ Expect (reconcileServiceWithClusterIP (serviceKey , 51820 )).Should (Succeed ())
589+
590+ // check that wireguard resource got the right status after the service is ready
591+ wg := & v1alpha1.Wireguard {}
592+ Eventually (func () v1alpha1.WireguardStatus {
593+ Expect (k8sClient .Get (context .Background (), wgKey , wg )).Should (Succeed ())
594+ return wg .Status
595+ }, Timeout , Interval ).Should (Equal (v1alpha1.WireguardStatus {
596+ Address : expectedAddress ,
597+ Port : "51820" ,
598+ Status : "ready" ,
599+ Dns : dnsServiceIp ,
600+ Message : "VPN is active!" ,
601+ }))
602+
603+ Eventually (func () string {
604+ deploymentKey := types.NamespacedName {
605+ Name : wgName + "-dep" ,
606+ Namespace : wgNamespace ,
607+ }
608+ deployment := & appsv1.Deployment {}
609+ Expect (k8sClient .Get (context .Background (), deploymentKey , deployment )).Should (Succeed ())
610+ Expect (len (deployment .Spec .Template .Spec .Containers )).Should (Equal (2 ))
611+ Expect (deployment .Spec .Template .Spec .Containers [0 ].Image ).Should (Equal (deployment .Spec .Template .Spec .Containers [1 ].Image ))
612+ return deployment .Spec .Template .Spec .Containers [0 ].Image
613+ }, Timeout , Interval ).Should (Equal (wgTestImage ))
614+
615+ // create peer
616+ peerKey := types.NamespacedName {
617+ Name : wgKey .Name + "peer" ,
618+ Namespace : wgKey .Namespace ,
619+ }
620+ peer := & v1alpha1.WireguardPeer {
621+ ObjectMeta : metav1.ObjectMeta {
622+ Name : peerKey .Name ,
623+ Namespace : peerKey .Namespace ,
624+ },
625+ Spec : v1alpha1.WireguardPeerSpec {
626+ WireguardRef : wgKey .Name ,
627+ },
628+ }
629+ Expect (k8sClient .Create (context .Background (), peer )).Should (Succeed ())
630+
631+ //get peer secret
632+ wgSecretKeyName := types.NamespacedName {
633+ Name : wgKey .Name ,
634+ Namespace : wgKey .Namespace ,
635+ }
636+ wgSecret := & corev1.Secret {}
637+ Eventually (func () error {
638+ return k8sClient .Get (context .Background (), wgSecretKeyName , wgSecret )
639+ }, Timeout , Interval ).Should (Succeed ())
640+ wgPublicKey := string (wgSecret .Data ["publicKey" ])
641+
642+ Eventually (func () string {
643+ Expect (k8sClient .Get (context .Background (), peerKey , peer )).Should (Succeed ())
644+ print (peer .Status .Message )
645+ return peer .Spec .Address
646+ }, Timeout , Interval ).Should (Equal ("10.8.0.2" ))
647+
648+ Eventually (func () v1alpha1.WireguardPeerStatus {
649+ Expect (k8sClient .Get (context .Background (), peerKey , peer )).Should (Succeed ())
650+ return peer .Status
651+ }, Timeout , Interval ).Should (Equal (v1alpha1.WireguardPeerStatus {
652+ Config : fmt .Sprintf (`
653+ echo "
654+ [Interface]
655+ PrivateKey = $(kubectl get secret %s-peer --template={{.data.privateKey}} -n default | base64 -d)
656+ Address = %s
657+ DNS = %s, %s.svc.cluster.local
658+
659+ [Peer]
660+ PublicKey = %s
661+ AllowedIPs = 0.0.0.0/0
662+ Endpoint = %s:%s"` , peerKey .Name , peer .Spec .Address , dnsServiceIp , peer .Namespace , wgPublicKey , expectedAddress , wg .Status .Port ),
663+ Status : "ready" ,
664+ Message : "Peer configured" ,
665+ }))
666+
667+ Eventually (func () error {
668+ return k8sClient .Get (context .Background (), wgSecretKeyName , wgSecret )
669+ }, Timeout , Interval ).Should (Succeed ())
670+
671+ })
672+
518673 for _ , useWgUserspace := range []bool {true , false } {
519674 testTextPrefix := "uses"
520675 if ! useWgUserspace {
0 commit comments