-
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
11 changed files
with
153 additions
and
133 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
95 changes: 95 additions & 0 deletions
95
pkg/liqo-controller-manager/external-network/wireguard/utils.go
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/klog/v2" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
"github.com/liqotech/liqo/pkg/consts" | ||
liqolabels "github.com/liqotech/liqo/pkg/utils/labels" | ||
) | ||
|
||
func filterWireGuardSecretsPredicate() predicate.Predicate { | ||
filterGatewayResources, err := predicate.LabelSelectorPredicate(liqolabels.GatewayResourceLabelSelector) | ||
utilruntime.Must(err) | ||
|
||
filterResourcesForRemote, err := predicate.LabelSelectorPredicate(liqolabels.ResourceForRemoteClusterLabelSelector) | ||
utilruntime.Must(err) | ||
|
||
return predicate.And(filterGatewayResources, filterResourcesForRemote) | ||
} | ||
|
||
func wireGuardSecretEnquerer(_ context.Context, obj client.Object) []ctrl.Request { | ||
secret, ok := obj.(*corev1.Secret) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return []ctrl.Request{ | ||
{ | ||
NamespacedName: types.NamespacedName{ | ||
Namespace: secret.Namespace, | ||
Name: mapSecretToWireGuardResource(secret.Name), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// TODO:: use generic map function after merge. | ||
func mapSecretToWireGuardResource(secretName string) string { | ||
return secretName | ||
} | ||
|
||
func getWireGuardSecret(ctx context.Context, cl client.Client, wgObj metav1.Object) (*corev1.Secret, error) { | ||
wgObjNsName := types.NamespacedName{Name: wgObj.GetName(), Namespace: wgObj.GetNamespace()} | ||
|
||
remoteClusterID, exists := wgObj.GetLabels()[consts.RemoteClusterID] | ||
if !exists { | ||
err := fmt.Errorf("missing %q label in WireGuard gateway %q", consts.RemoteClusterID, wgObjNsName) | ||
klog.Error(err) | ||
return nil, err | ||
} | ||
wgSecretSelector := client.MatchingLabels{ | ||
consts.GatewayResourceLabel: consts.GatewayResourceLabelValue, | ||
consts.RemoteClusterID: remoteClusterID, | ||
} | ||
|
||
var secrets corev1.SecretList | ||
err := cl.List(ctx, &secrets, client.InNamespace(wgObj.GetNamespace()), wgSecretSelector) | ||
if err != nil { | ||
klog.Errorf("Unable to list secrets associated to WireGuard gateway %q: %v", wgObjNsName, err) | ||
return nil, err | ||
} | ||
|
||
switch len(secrets.Items) { | ||
case 0: | ||
klog.Warningf("Secret associated to WireGuard gateway %q not found", wgObjNsName) | ||
return nil, nil | ||
case 1: | ||
return &secrets.Items[0], nil | ||
default: | ||
return nil, fmt.Errorf("found multiple secrets associated to WireGuard gateway %q", wgObjNsName) | ||
} | ||
} |
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
Oops, something went wrong.