Skip to content

Commit

Permalink
refactor: use credentials' SetKonnectConsumerIDInStatus() method (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Oct 7, 2024
1 parent e3d7d28 commit 172abaa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 42 deletions.
34 changes: 9 additions & 25 deletions controller/konnect/reconciler_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,32 +843,16 @@ func handleKongConsumerRef[T constraints.SupportedKonnectEntityType, TEnt constr
return ctrl.Result{}, fmt.Errorf("failed to update status: %w", err)
}

// TODO(pmalek): make this generic.
// Consumer ID is not stored in KonnectEntityStatus because not all entities
// have a ConsumerRef, hence the type constraints in the reconciler can't be used.
if cred, ok := any(ent).(*configurationv1alpha1.KongCredentialBasicAuth); ok {
if cred.Status.Konnect == nil {
cred.Status.Konnect = &konnectv1alpha1.KonnectEntityStatusWithControlPlaneAndConsumerRefs{}
}
cred.Status.Konnect.ConsumerID = consumer.Status.Konnect.GetKonnectID()
}
if cred, ok := any(ent).(*configurationv1alpha1.KongCredentialAPIKey); ok {
if cred.Status.Konnect == nil {
cred.Status.Konnect = &konnectv1alpha1.KonnectEntityStatusWithControlPlaneAndConsumerRefs{}
}
cred.Status.Konnect.ConsumerID = consumer.Status.Konnect.GetKonnectID()
}
if cred, ok := any(ent).(*configurationv1alpha1.KongCredentialACL); ok {
if cred.Status.Konnect == nil {
cred.Status.Konnect = &konnectv1alpha1.KonnectEntityStatusWithControlPlaneAndConsumerRefs{}
}
cred.Status.Konnect.ConsumerID = consumer.Status.Konnect.GetKonnectID()
type EntityWithConsumerRef interface {
SetKonnectConsumerIDInStatus(string)
}
if cred, ok := any(ent).(*configurationv1alpha1.KongCredentialJWT); ok {
if cred.Status.Konnect == nil {
cred.Status.Konnect = &konnectv1alpha1.KonnectEntityStatusWithControlPlaneAndConsumerRefs{}
}
cred.Status.Konnect.ConsumerID = consumer.Status.Konnect.GetKonnectID()
if cred, ok := any(ent).(EntityWithConsumerRef); ok {
cred.SetKonnectConsumerIDInStatus(consumer.Status.Konnect.GetKonnectID())
} else {
return ctrl.Result{}, fmt.Errorf(
"cannot set referenced Consumer %s KonnectID in %s %sstatus",
client.ObjectKeyFromObject(&consumer), constraints.EntityTypeName[T](), client.ObjectKeyFromObject(ent),
)
}
if cred, ok := any(ent).(*configurationv1alpha1.KongCredentialHMAC); ok {
if cred.Status.Konnect == nil {
Expand Down
21 changes: 4 additions & 17 deletions controller/konnect/watch_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func kongCredentialRefersToKonnectGatewayControlPlane[
*configurationv1alpha1.KongCredentialJWT |
*configurationv1alpha1.KongCredentialHMAC

GetConsumerRefName() string
GetTypeName() string
GetNamespace() string
},
Expand All @@ -41,27 +42,13 @@ func kongCredentialRefersToKonnectGatewayControlPlane[
return false
}

var consumerRefName string
switch credential := any(credential).(type) {
case *configurationv1alpha1.KongCredentialACL:
consumerRefName = credential.Spec.ConsumerRef.Name
case *configurationv1alpha1.KongCredentialAPIKey:
consumerRefName = credential.Spec.ConsumerRef.Name
case *configurationv1alpha1.KongCredentialBasicAuth:
consumerRefName = credential.Spec.ConsumerRef.Name
case *configurationv1alpha1.KongCredentialJWT:
consumerRefName = credential.Spec.ConsumerRef.Name
case *configurationv1alpha1.KongCredentialHMAC:
consumerRefName = credential.Spec.ConsumerRef.Name
}

nn := types.NamespacedName{
Namespace: credential.GetNamespace(),
Name: consumerRefName,
Name: credential.GetConsumerRefName(),
}
var consumer configurationv1.KongConsumer
if err := cl.Get(context.Background(), nn, &consumer); client.IgnoreNotFound(err) != nil {
return true
if err := cl.Get(context.Background(), nn, &consumer); err != nil {
return false
}

return objHasControlPlaneRefKonnectNamespacedRef(&consumer)
Expand Down

0 comments on commit 172abaa

Please sign in to comment.