Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4ce45b5

Browse files
committedSep 20, 2024··
fix: watch for consumer changes in CredentialBasicAuth reconciler
1 parent a18986c commit 4ce45b5

File tree

3 files changed

+97
-19
lines changed

3 files changed

+97
-19
lines changed
 

‎controller/konnect/watch_credentialbasicauth.go

+41
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func CredentialBasicAuthReconciliationWatchOptions(
4141
),
4242
)
4343
},
44+
func(b *ctrl.Builder) *ctrl.Builder {
45+
return b.Watches(
46+
&configurationv1.KongConsumer{},
47+
handler.EnqueueRequestsFromMapFunc(
48+
credentialBasicAuthForKongConsumer(cl),
49+
),
50+
)
51+
},
4452
func(b *ctrl.Builder) *ctrl.Builder {
4553
return b.Watches(
4654
&konnectv1alpha1.KonnectAPIAuthConfiguration{},
@@ -207,3 +215,36 @@ func credentialBasicAuthForKonnectGatewayControlPlane(
207215
return ret
208216
}
209217
}
218+
219+
func credentialBasicAuthForKongConsumer(
220+
cl client.Client,
221+
) func(ctx context.Context, obj client.Object) []reconcile.Request {
222+
return func(ctx context.Context, obj client.Object) []reconcile.Request {
223+
consumer, ok := obj.(*configurationv1.KongConsumer)
224+
if !ok {
225+
return nil
226+
}
227+
var l configurationv1alpha1.CredentialBasicAuthList
228+
if err := cl.List(ctx, &l,
229+
client.MatchingFields{
230+
IndexFieldCredentialBasicAuthReferencesKongConsumer: consumer.Name,
231+
},
232+
// TODO: change this when cross namespace refs are allowed.
233+
client.InNamespace(consumer.GetNamespace()),
234+
); err != nil {
235+
return nil
236+
}
237+
238+
var ret []reconcile.Request
239+
for _, cred := range l.Items {
240+
ret = append(ret, reconcile.Request{
241+
NamespacedName: types.NamespacedName{
242+
Namespace: cred.Namespace,
243+
Name: cred.Name,
244+
},
245+
},
246+
)
247+
}
248+
return ret
249+
}
250+
}

‎test/envtest/deploy_resources.go

+33
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/google/uuid"
88
"github.com/samber/lo"
99
"github.com/stretchr/testify/require"
10+
corev1 "k8s.io/api/core/v1"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"sigs.k8s.io/controller-runtime/pkg/client"
1213

@@ -187,3 +188,35 @@ func deployKongPluginBinding(
187188
require.NoError(t, cl.Status().Update(ctx, kpb))
188189
return kpb
189190
}
191+
192+
// deployCredentialBasicAuth deploys a CredentialBasicAuth resource and returns the resource.
193+
func deployCredentialBasicAuth(
194+
t *testing.T,
195+
ctx context.Context,
196+
cl client.Client,
197+
consumerName string,
198+
username string,
199+
password string,
200+
) *configurationv1alpha1.CredentialBasicAuth {
201+
t.Helper()
202+
203+
c := &configurationv1alpha1.CredentialBasicAuth{
204+
ObjectMeta: metav1.ObjectMeta{
205+
GenerateName: "basic-auth-",
206+
},
207+
Spec: configurationv1alpha1.CredentialBasicAuthSpec{
208+
ConsumerRef: corev1.LocalObjectReference{
209+
Name: consumerName,
210+
},
211+
CredentialBasicAuthAPISpec: configurationv1alpha1.CredentialBasicAuthAPISpec{
212+
Password: password,
213+
Username: username,
214+
},
215+
},
216+
}
217+
218+
require.NoError(t, cl.Create(ctx, c))
219+
t.Logf("deployed new unmanaged CredentialBasicAuth %s", client.ObjectKeyFromObject(c))
220+
221+
return c
222+
}

‎test/envtest/kongconsumercredential_basicauth_test.go

+23-19
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/mock"
1313
"github.com/stretchr/testify/require"
14-
corev1 "k8s.io/api/core/v1"
15-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
"k8s.io/apimachinery/pkg/watch"
1615
"sigs.k8s.io/controller-runtime/pkg/client"
1716

1817
"github.com/kong/gateway-operator/controller/konnect"
@@ -35,6 +34,10 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) {
3534

3635
mgr, logs := NewManager(t, ctx, cfg, scheme.Get())
3736

37+
clientWithWatch, err := client.NewWithWatch(mgr.GetConfig(), client.Options{
38+
Scheme: scheme.Get(),
39+
})
40+
require.NoError(t, err)
3841
clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name)
3942

4043
apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced)
@@ -64,23 +67,7 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) {
6467

6568
password := "password"
6669
username := "username"
67-
credentialBasicAuth := &configurationv1alpha1.CredentialBasicAuth{
68-
ObjectMeta: metav1.ObjectMeta{
69-
GenerateName: "basic-auth-",
70-
},
71-
Spec: configurationv1alpha1.CredentialBasicAuthSpec{
72-
ConsumerRef: corev1.LocalObjectReference{
73-
Name: consumer.Name,
74-
},
75-
CredentialBasicAuthAPISpec: configurationv1alpha1.CredentialBasicAuthAPISpec{
76-
Password: password,
77-
Username: username,
78-
},
79-
},
80-
}
81-
require.NoError(t, clientNamespaced.Create(ctx, credentialBasicAuth))
82-
t.Logf("deployed %s CredentialBasicAuth resource", client.ObjectKeyFromObject(credentialBasicAuth))
83-
70+
credentialBasicAuth := deployCredentialBasicAuth(t, ctx, clientNamespaced, consumer.Name, username, password)
8471
basicAuthID := uuid.NewString()
8572
tags := []string{
8673
"k8s-generation:1",
@@ -158,4 +145,21 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) {
158145
assert.EventuallyWithT(t, func(c *assert.CollectT) {
159146
assert.True(c, factory.SDK.BasicAuthCredentials.AssertExpectations(t))
160147
}, waitTime, tickTime)
148+
149+
w := setupWatch[configurationv1alpha1.CredentialBasicAuthList](t, ctx, clientWithWatch, client.InNamespace(ns.Name))
150+
151+
credentialBasicAuth = deployCredentialBasicAuth(t, ctx, clientNamespaced, consumer.Name, username, password)
152+
t.Logf("redeployed %s CredentialBasicAuth resource", client.ObjectKeyFromObject(credentialBasicAuth))
153+
t.Logf("checking if KongConsumer %s removal will delete the associated credentials %s",
154+
client.ObjectKeyFromObject(consumer),
155+
client.ObjectKeyFromObject(credentialBasicAuth),
156+
)
157+
158+
require.NoError(t, clientNamespaced.Delete(ctx, consumer))
159+
_ = watchFor(t, ctx, w, watch.Modified,
160+
func(c *configurationv1alpha1.CredentialBasicAuth) bool {
161+
return c.Name == credentialBasicAuth.Name
162+
},
163+
"CredentialBasicAuth wasn't deleted but it should have been",
164+
)
161165
}

0 commit comments

Comments
 (0)
Please sign in to comment.