-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
219 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,73 @@ | ||
// Copyright 2019-2023 The Liqo Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package wireguard | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"golang.zx2c4.com/wireguard/wgctrl" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/tools/record" | ||
"k8s.io/klog/v2" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1" | ||
networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1" | ||
) | ||
|
||
type WireguardController struct { | ||
client.Client | ||
// PublicKeysReconciler updates the PublicKey resource used to establish the Wireguard connection. | ||
type PublicKeysReconciler struct { | ||
Wgctrl *wgctrl.Client | ||
Client client.Client | ||
Scheme *runtime.Scheme | ||
EventsRecorder record.EventRecorder | ||
} | ||
|
||
func NewWireguardController(cl client.Client, s *runtime.Scheme, er record.EventRecorder) *WireguardController { | ||
return &WireguardController{ | ||
// NewPublicKeysReconciler returns a new PublicKeysReconciler. | ||
func NewPublicKeysReconciler(cl client.Client, s *runtime.Scheme, er record.EventRecorder) (*PublicKeysReconciler, error) { | ||
wgctrl, err := wgctrl.New() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create wireguard client: %w", err) | ||
} | ||
return &PublicKeysReconciler{ | ||
Wgctrl: wgctrl, | ||
Client: cl, | ||
Scheme: s, | ||
EventsRecorder: er, | ||
} | ||
}, nil | ||
} | ||
|
||
// Reconcile manage PublicKey resources. | ||
func (r *WireguardController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
func (r *PublicKeysReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
publicKey := &networkingv1alpha1.PublicKey{} | ||
if err := r.Get(ctx, req.NamespacedName, publicKey); err != nil { | ||
if err := r.Client.Get(ctx, req.NamespacedName, publicKey); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
klog.Infof("There is no publicKey %s", req.String()) | ||
return ctrl.Result{}, nil | ||
} | ||
return ctrl.Result{}, fmt.Errorf("unable to get the publicKey %q: %w", req.NamespacedName, err) | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager register the ConfigurationReconciler to the manager. | ||
func (r *WireguardController) SetupWithManager(mgr ctrl.Manager) error { | ||
func (r *PublicKeysReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&networkingv1alpha1.Configuration{}). | ||
Owns(&ipamv1alpha1.Network{}). | ||
For(&networkingv1alpha1.PublicKey{}). | ||
Complete(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters