Skip to content

Commit 13bf3a1

Browse files
committed
fix: liqoctl retrieve nonce only from tenant namespace
1 parent c2dca28 commit 13bf3a1

File tree

10 files changed

+41
-34
lines changed

10 files changed

+41
-34
lines changed

cmd/liqoctl/cmd/unpeer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ offloaded workloads to be rescheduled. The Identity and Tenant are respectively
3838
removed from the consumer and provider clusters, and the networking between the
3939
two clusters is destroyed.
4040
41-
The reverse peering, if any, is preserved, and the remote cluster can continue
41+
The reverse peering, if any, is preserved, and the remote cluster can continue
4242
offloading workloads to its virtual node representing the local cluster.
4343
4444
Examples:
@@ -66,7 +66,7 @@ func newUnpeerCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
6666

6767
cmd.PersistentFlags().DurationVar(&options.Timeout, "timeout", 120*time.Second, "Timeout for unpeering completion")
6868
cmd.PersistentFlags().BoolVar(&options.Wait, "wait", true, "Wait for resource to be deleted before returning")
69-
cmd.PersistentFlags().BoolVar(&options.KeepNamespaces, "keep-namespaces", false, "Keep tenant namespaces after unpeering")
69+
cmd.PersistentFlags().BoolVar(&options.DeleteNamespace, "delete-namespaces", false, "Delete the tenant namespace after unpeering")
7070

7171
options.LocalFactory.AddFlags(cmd.PersistentFlags(), cmd.RegisterFlagCompletionFunc)
7272
options.RemoteFactory.AddFlags(cmd.PersistentFlags(), cmd.RegisterFlagCompletionFunc)

pkg/liqo-controller-manager/authentication/tenant-controller/tenant_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (r *TenantReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
138138
if authv1beta1.GetAuthzPolicyValue(tenant.Spec.AuthzPolicy) != authv1beta1.TolerateNoHandshake {
139139
// get the nonce for the tenant
140140

141-
nonceSecret, err := getters.GetNonceSecretByClusterID(ctx, r.Client, clusterID)
141+
nonceSecret, err := getters.GetNonceSecretByClusterID(ctx, r.Client, clusterID, corev1.NamespaceAll)
142142
if err != nil {
143143
klog.Errorf("Unable to get the nonce for the Tenant %q: %s", req.Name, err)
144144
r.EventRecorder.Event(tenant, corev1.EventTypeWarning, "NonceNotFound", err.Error())

pkg/liqo-controller-manager/authentication/utils/nonce.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func EnsureNonceSecret(ctx context.Context, cl client.Client,
4747
// already a nonce secret in the tenant namespace.
4848
func EnsureSignedNonceSecret(ctx context.Context, cl client.Client,
4949
remoteClusterID liqov1beta1.ClusterID, tenantNamespace string, nonce *string) error {
50-
nonceSecret, err := getters.GetSignedNonceSecretByClusterID(ctx, cl, remoteClusterID)
50+
nonceSecret, err := getters.GetSignedNonceSecretByClusterID(ctx, cl, remoteClusterID, tenantNamespace)
5151
switch {
5252
case errors.IsNotFound(err):
5353
// Secret not found. Create it given the provided nonce.
@@ -80,8 +80,8 @@ func EnsureSignedNonceSecret(ctx context.Context, cl client.Client,
8080
}
8181

8282
// RetrieveNonce retrieves the nonce from the secret in the tenant namespace.
83-
func RetrieveNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID) ([]byte, error) {
84-
nonce, err := getters.GetNonceSecretByClusterID(ctx, cl, remoteClusterID)
83+
func RetrieveNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID, tenantNs string) ([]byte, error) {
84+
nonce, err := getters.GetNonceSecretByClusterID(ctx, cl, remoteClusterID, tenantNs)
8585
if err != nil {
8686
return nil, fmt.Errorf("unable to get nonce secret: %w", err)
8787
}
@@ -90,8 +90,8 @@ func RetrieveNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1
9090
}
9191

9292
// RetrieveSignedNonce retrieves the signed nonce from the secret in the tenant namespace.
93-
func RetrieveSignedNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID) ([]byte, error) {
94-
secret, err := getters.GetSignedNonceSecretByClusterID(ctx, cl, remoteClusterID)
93+
func RetrieveSignedNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID, tenantNs string) ([]byte, error) {
94+
secret, err := getters.GetSignedNonceSecretByClusterID(ctx, cl, remoteClusterID, tenantNs)
9595
if err != nil {
9696
return nil, fmt.Errorf("unable to get signed nonce secret: %w", err)
9797
}

pkg/liqoctl/authenticate/cluster.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ func (c *Cluster) EnsureNonce(ctx context.Context) ([]byte, error) {
104104
s.Success("Nonce secret ensured")
105105

106106
// Wait for secret to be filled with the nonce.
107-
if err := c.waiter.ForNonce(ctx, c.RemoteClusterID, false); err != nil {
107+
if err := c.waiter.ForNonce(ctx, c.RemoteClusterID, c.TenantNamespace, false); err != nil {
108108
return nil, err
109109
}
110110

111111
// Retrieve nonce from secret.
112112
s = c.local.Printer.StartSpinner("Retrieving nonce")
113-
nonceValue, err := authutils.RetrieveNonce(ctx, c.local.CRClient, c.RemoteClusterID)
113+
nonceValue, err := authutils.RetrieveNonce(ctx, c.local.CRClient, c.RemoteClusterID, c.TenantNamespace)
114114
if err != nil {
115115
s.Fail(fmt.Sprintf("Unable to retrieve nonce: %v", output.PrettyErr(err)))
116116
return nil, err
@@ -135,13 +135,13 @@ func (c *Cluster) EnsureSignedNonce(ctx context.Context, nonce []byte) ([]byte,
135135
s.Success("Signed nonce secret ensured")
136136

137137
// Wait for secret to be filled with the signed nonce.
138-
if err := c.waiter.ForSignedNonce(ctx, c.RemoteClusterID, false); err != nil {
138+
if err := c.waiter.ForSignedNonce(ctx, c.RemoteClusterID, false, c.TenantNamespace); err != nil {
139139
return nil, err
140140
}
141141

142142
// Retrieve signed nonce from secret.
143143
s = c.local.Printer.StartSpinner("Retrieving signed nonce")
144-
signedNonceValue, err := authutils.RetrieveSignedNonce(ctx, c.local.CRClient, c.RemoteClusterID)
144+
signedNonceValue, err := authutils.RetrieveSignedNonce(ctx, c.local.CRClient, c.RemoteClusterID, c.TenantNamespace)
145145
if err != nil {
146146
s.Fail(fmt.Sprintf("Unable to retrieve signed nonce: %v", output.PrettyErr(err)))
147147
return nil, err

pkg/liqoctl/rest/nonce/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ func (o *Options) handleCreate(ctx context.Context) error {
105105
s.Success("Nonce created")
106106

107107
// Wait for secret to be filled with the nonce.
108-
if err := waiter.ForNonce(ctx, o.clusterID.GetClusterID(), false); err != nil {
108+
if err := waiter.ForNonce(ctx, o.clusterID.GetClusterID(), tenantNs.GetName(), false); err != nil {
109109
return err
110110
}
111111

112112
// Retrieve nonce from secret.
113113
s = opts.Printer.StartSpinner("Retrieving nonce")
114-
nonceValue, err := authutils.RetrieveNonce(ctx, opts.CRClient, o.clusterID.GetClusterID())
114+
nonceValue, err := authutils.RetrieveNonce(ctx, opts.CRClient, o.clusterID.GetClusterID(), tenantNs.GetName())
115115
if err != nil {
116116
s.Fail(fmt.Sprintf("Unable to retrieve nonce: %v", output.PrettyErr(err)))
117117
return err

pkg/liqoctl/rest/nonce/get.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020

2121
"github.com/spf13/cobra"
22+
corev1 "k8s.io/api/core/v1"
2223
"k8s.io/apimachinery/pkg/util/runtime"
2324

2425
authutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/authentication/utils"
@@ -67,7 +68,7 @@ func (o *Options) Get(ctx context.Context, options *rest.GetOptions) *cobra.Comm
6768
func (o *Options) handleGet(ctx context.Context) error {
6869
opts := o.getOptions
6970

70-
nonceValue, err := authutils.RetrieveNonce(ctx, opts.CRClient, o.clusterID.GetClusterID())
71+
nonceValue, err := authutils.RetrieveNonce(ctx, opts.CRClient, o.clusterID.GetClusterID(), corev1.NamespaceAll)
7172
if err != nil {
7273
opts.Printer.CheckErr(fmt.Errorf("unable to retrieve nonce: %v", output.PrettyErr(err)))
7374
return err

pkg/liqoctl/rest/tenant/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const liqoctlGenerateConfigHelp = `Generate the Tenant resource to be applied on
3535
3636
This commands generates a Tenant filled with all the authentication parameters needed to authenticate with the remote cluster.
3737
It signs the nonce provided by the remote cluster and generates the CSR.
38-
The Nonce can be provided as a flag or it can be retrieved from the secret in the tenant namespace (if existing).
38+
The Nonce can be provided as a flag or it can be retrieved from the secret in the tenant namespace (if existing).
3939
4040
Examples:
4141
$ {{ .Executable }} generate tenant --remote-cluster-id remote-cluster-id`
@@ -98,13 +98,13 @@ func (o *Options) handleGenerate(ctx context.Context) error {
9898
}
9999

100100
// Wait for secret to be filled with the signed nonce.
101-
if err := waiter.ForSignedNonce(ctx, o.remoteClusterID.GetClusterID(), true); err != nil {
101+
if err := waiter.ForSignedNonce(ctx, o.remoteClusterID.GetClusterID(), true, tenantNs.GetName()); err != nil {
102102
opts.Printer.CheckErr(fmt.Errorf("unable to wait for nonce to be signed: %w", err))
103103
return err
104104
}
105105

106106
// Retrieve signed nonce from secret.
107-
signedNonce, err := authutils.RetrieveSignedNonce(ctx, opts.CRClient, o.remoteClusterID.GetClusterID())
107+
signedNonce, err := authutils.RetrieveSignedNonce(ctx, opts.CRClient, o.remoteClusterID.GetClusterID(), tenantNs.GetName())
108108
if err != nil {
109109
opts.Printer.CheckErr(fmt.Errorf("unable to retrieve signed nonce: %w", err))
110110
return err

pkg/liqoctl/unpeer/handler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ type Options struct {
3535
RemoteFactory *factory.Factory
3636
waiter *wait.Waiter
3737

38-
Timeout time.Duration
39-
Wait bool
40-
KeepNamespaces bool
38+
Timeout time.Duration
39+
Wait bool
40+
DeleteNamespace bool
4141

4242
consumerClusterID liqov1beta1.ClusterID
4343
providerClusterID liqov1beta1.ClusterID
@@ -85,8 +85,8 @@ func (o *Options) RunUnpeer(ctx context.Context) error {
8585
o.LocalFactory.Printer.CheckErr(fmt.Errorf("an error occurred while checking bidirectional peering: %v", output.PrettyErr(err)))
8686
return err
8787
}
88-
if bidirectional && !o.KeepNamespaces {
89-
err = fmt.Errorf("cannot unpeer bidirectional peering without keeping namespaces, please set the --keep-namespaces flag")
88+
if bidirectional && o.DeleteNamespace {
89+
err = fmt.Errorf("cannot delete the tenant namespace when a bidirectional is enabled, please remote the --delete-namespaces flag")
9090
o.LocalFactory.Printer.CheckErr(err)
9191
return err
9292
}
@@ -111,7 +111,7 @@ func (o *Options) RunUnpeer(ctx context.Context) error {
111111
}
112112
}
113113

114-
if !o.KeepNamespaces {
114+
if o.DeleteNamespace {
115115
consumer := unauthenticate.NewCluster(o.LocalFactory)
116116
provider := unauthenticate.NewCluster(o.RemoteFactory)
117117

pkg/liqoctl/wait/wait.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,15 @@ func (w *Waiter) ForConnectionEstablished(ctx context.Context, conn *networkingv
333333
}
334334

335335
// ForNonce waits until the secret containing the nonce has been created or the timeout expires.
336-
func (w *Waiter) ForNonce(ctx context.Context, remoteClusterID liqov1beta1.ClusterID, silent bool) error {
336+
func (w *Waiter) ForNonce(ctx context.Context, remoteClusterID liqov1beta1.ClusterID, tenantNamespace string, silent bool) error {
337337
var s *pterm.SpinnerPrinter
338338

339339
if !silent {
340340
s = w.Printer.StartSpinner("Waiting for nonce to be generated")
341341
}
342342

343343
err := wait.PollUntilContextCancel(ctx, 1*time.Second, true, func(ctx context.Context) (done bool, err error) {
344-
secret, err := getters.GetNonceSecretByClusterID(ctx, w.CRClient, remoteClusterID)
344+
secret, err := getters.GetNonceSecretByClusterID(ctx, w.CRClient, remoteClusterID, tenantNamespace)
345345
if err != nil {
346346
return false, client.IgnoreNotFound(err)
347347
}
@@ -366,15 +366,15 @@ func (w *Waiter) ForNonce(ctx context.Context, remoteClusterID liqov1beta1.Clust
366366
}
367367

368368
// ForSignedNonce waits until the signed nonce secret has been signed and returns the signature.
369-
func (w *Waiter) ForSignedNonce(ctx context.Context, remoteClusterID liqov1beta1.ClusterID, silent bool) error {
369+
func (w *Waiter) ForSignedNonce(ctx context.Context, remoteClusterID liqov1beta1.ClusterID, silent bool, tenantNs string) error {
370370
var s *pterm.SpinnerPrinter
371371

372372
if !silent {
373373
s = w.Printer.StartSpinner("Waiting for nonce to be signed")
374374
}
375375

376376
err := wait.PollUntilContextCancel(ctx, 1*time.Second, true, func(ctx context.Context) (done bool, err error) {
377-
secret, err := getters.GetSignedNonceSecretByClusterID(ctx, w.CRClient, remoteClusterID)
377+
secret, err := getters.GetSignedNonceSecretByClusterID(ctx, w.CRClient, remoteClusterID, tenantNs)
378378
if err != nil {
379379
return false, client.IgnoreNotFound(err)
380380
}

pkg/utils/getters/k8sGetters.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,11 @@ func ListNodesByClusterID(ctx context.Context, cl client.Client, clusterID liqov
215215
}
216216

217217
// GetNonceSecretByClusterID returns the secret containing the nonce to be signed by the consumer cluster.
218-
func GetNonceSecretByClusterID(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID) (*corev1.Secret, error) {
218+
func GetNonceSecretByClusterID(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID,
219+
tenantNs string) (*corev1.Secret, error) {
219220
var secrets corev1.SecretList
220221
if err := cl.List(ctx, &secrets, &client.ListOptions{
222+
Namespace: tenantNs,
221223
LabelSelector: labels.SelectorFromSet(map[string]string{
222224
consts.RemoteClusterID: string(remoteClusterID),
223225
consts.NonceSecretLabelKey: "true",
@@ -237,12 +239,16 @@ func GetNonceSecretByClusterID(ctx context.Context, cl client.Client, remoteClus
237239
}
238240

239241
// GetSignedNonceSecretByClusterID returns the secret containing the nonce signed by the consumer cluster.
240-
func GetSignedNonceSecretByClusterID(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID) (*corev1.Secret, error) {
242+
func GetSignedNonceSecretByClusterID(
243+
ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID, tentantNs string) (*corev1.Secret, error) {
241244
var secrets corev1.SecretList
242-
if err := cl.List(ctx, &secrets, client.MatchingLabels{
243-
consts.RemoteClusterID: string(remoteClusterID),
244-
consts.SignedNonceSecretLabelKey: "true",
245-
}); err != nil {
245+
if err := cl.List(ctx, &secrets,
246+
client.MatchingLabels{
247+
consts.RemoteClusterID: string(remoteClusterID),
248+
consts.SignedNonceSecretLabelKey: "true",
249+
},
250+
&client.ListOptions{Namespace: tentantNs},
251+
); err != nil {
246252
return nil, err
247253
}
248254

0 commit comments

Comments
 (0)