Skip to content

Commit

Permalink
feat(konnect): add KongCredentialSecretReconciler to reconcile consum…
Browse files Browse the repository at this point in the history
…er Secrets and create Credential resources in response
  • Loading branch information
pmalek committed Sep 19, 2024
1 parent 0f964e8 commit 477b167
Show file tree
Hide file tree
Showing 15 changed files with 703 additions and 4 deletions.
1 change: 1 addition & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ packages:
PluginSDK:
UpstreamsSDK:
MeSDK:
CredentialBasicAuthSDK:
20 changes: 20 additions & 0 deletions config/rbac/role/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,26 @@ rules:
- get
- patch
- update
- apiGroups:
- konnect.konghq.com
resources:
- credentialbasicauths
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- konnect.konghq.com
resources:
- kongconsumers
verbs:
- get
- list
- watch
- apiGroups:
- konnect.konghq.com
resources:
Expand Down
1 change: 1 addition & 0 deletions controller/konnect/constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type SupportedKonnectEntityType interface {
configurationv1.KongConsumer |
configurationv1beta1.KongConsumerGroup |
configurationv1alpha1.KongPluginBinding |
configurationv1alpha1.CredentialBasicAuth |
configurationv1alpha1.KongUpstream
// TODO: add other types

Expand Down
7 changes: 6 additions & 1 deletion controller/konnect/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/kong/gateway-operator/controller/konnect/constraints"

configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
)

Expand All @@ -21,9 +22,13 @@ func ReconciliationIndexOptionsForEntity[
T constraints.SupportedKonnectEntityType,
]() []ReconciliationIndexOption {
var e TEnt
switch any(e).(type) { //nolint:gocritic // TODO: add index options required for other entities
switch any(e).(type) {
case *configurationv1alpha1.KongPluginBinding:
return IndexOptionsForKongPluginBinding()
case *configurationv1.KongConsumer:
return IndexOptionsForKongConsumer()
case *configurationv1alpha1.CredentialBasicAuth:
return IndexOptionsForCredentialsBasicAuth()
}
return nil
}
48 changes: 48 additions & 0 deletions controller/konnect/index_credentials_basicauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package konnect

import (
"sigs.k8s.io/controller-runtime/pkg/client"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
)

const (
// IndexFieldCredentialReferencesKongConsumer is the index name for CredentialBasicAuth -> Consumer.
IndexFieldCredentialReferencesKongConsumer = "kongCredentialsBasicAuthConsumerRef"
// IndexFieldCredentialReferencesKongSecret is the index name for CredentialBasicAuth -> Secret.
IndexFieldCredentialReferencesKongSecret = "kongCredentialsBasicAuthSecretRef"
)

// IndexOptionsForCredentialsBasicAuth returns required Index options for CredentialBasicAuth.
func IndexOptionsForCredentialsBasicAuth() []ReconciliationIndexOption {
return []ReconciliationIndexOption{
{
IndexObject: &configurationv1alpha1.CredentialBasicAuth{},
IndexField: IndexFieldCredentialReferencesKongConsumer,
ExtractValue: kongCredentialBasicAuthReferencesConsumer,
},
{
IndexObject: &configurationv1alpha1.CredentialBasicAuth{},
IndexField: IndexFieldCredentialReferencesKongSecret,
ExtractValue: kongCredentialBasicAuthReferencesSecret,
},
}
}

// kongCredentialBasicAuthReferencesConsumer returns the name of referenced Consumer.
func kongCredentialBasicAuthReferencesConsumer(obj client.Object) []string {
cred, ok := obj.(*configurationv1alpha1.CredentialBasicAuth)
if !ok {
return nil
}
return []string{cred.Spec.ConsumerRef.Name}
}

// kongCredentialBasicAuthReferencesSecret returns the name of referenced Secret.
func kongCredentialBasicAuthReferencesSecret(obj client.Object) []string {
cred, ok := obj.(*configurationv1alpha1.CredentialBasicAuth)
if !ok {
return nil
}
return []string{cred.Spec.SecretRef.Name}
}
32 changes: 32 additions & 0 deletions controller/konnect/index_kongconsumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package konnect

import (
"sigs.k8s.io/controller-runtime/pkg/client"

configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
)

const (
// IndexFieldKongConsumerReferencesSecrets is the index field for Consumer -> Secret.
IndexFieldKongConsumerReferencesSecrets = "kongConsumerSecretRef"
)

// IndexOptionsForKongConsumer returns required Index options for Kong Consumer.
func IndexOptionsForKongConsumer() []ReconciliationIndexOption {
return []ReconciliationIndexOption{
{
IndexObject: &configurationv1.KongConsumer{},
IndexField: IndexFieldKongConsumerReferencesSecrets,
ExtractValue: kongConsumerReferencesSecret,
},
}
}

// kongConsumerReferencesSecret returns name of referenced Secrets.
func kongConsumerReferencesSecret(obj client.Object) []string {
consumer, ok := obj.(*configurationv1.KongConsumer)
if !ok {
return nil
}
return consumer.Credentials
}
14 changes: 14 additions & 0 deletions controller/konnect/ops/credenetialbasicauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ops

import (
"context"

sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
)

// CredentialBasicAuthSDK is the interface for the Konnect CredentialBasicAuthSDK.
type CredentialBasicAuthSDK interface {
CreateBasicAuthWithConsumer(ctx context.Context, req sdkkonnectops.CreateBasicAuthWithConsumerRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreateBasicAuthWithConsumerResponse, error)
DeleteBasicAuthWithConsumer(ctx context.Context, request sdkkonnectops.DeleteBasicAuthWithConsumerRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeleteBasicAuthWithConsumerResponse, error)
UpsertBasicAuthWithConsumer(ctx context.Context, request sdkkonnectops.UpsertBasicAuthWithConsumerRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertBasicAuthWithConsumerResponse, error)
}
Loading

0 comments on commit 477b167

Please sign in to comment.