Skip to content

Commit

Permalink
Index named input for NAD and unify across Watches
Browse files Browse the repository at this point in the history
Adds to watch NADs so that the controller reconciles when the
configured NAD changes on the GlanceAPI.

Also unifies the Watches across the named input parameters.

Signed-off-by: Martin Schuppert <[email protected]>
  • Loading branch information
stuggi committed Aug 6, 2024
1 parent 5387db6 commit 53deb16
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 114 deletions.
18 changes: 13 additions & 5 deletions controllers/glance_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package controllers
import (
"context"
"fmt"
"time"

"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
oko_secret "github.com/openstack-k8s-operators/lib-common/modules/common/secret"
"k8s.io/apimachinery/pkg/types"
"time"

glancev1 "github.com/openstack-k8s-operators/glance-operator/api/v1beta1"
"github.com/openstack-k8s-operators/glance-operator/pkg/glance"
Expand All @@ -43,21 +44,28 @@ import (

// fields to index to reconcile when change
const (
passwordSecretField = ".spec.secret"
caBundleSecretNameField = ".spec.tls.caBundleSecretName"
tlsAPIInternalField = ".spec.tls.api.internal.secretName"
tlsAPIPublicField = ".spec.tls.api.public.secretName"
passwordSecretField = ".spec.secret"
caBundleSecretNameField = ".spec.tls.caBundleSecretName"
tlsAPIInternalField = ".spec.tls.api.internal.secretName"
tlsAPIPublicField = ".spec.tls.api.public.secretName"
networkAttachmentsField = ".spec.networkAttachments"
customServiceConfigSecretsField = ".spec.customServiceConfigSecrets"
memcachedInstanceField = ".spec.memcachedInstance"
)

var (
glanceWatchFields = []string{
passwordSecretField,
memcachedInstanceField,
}
glanceAPIWatchFields = []string{
passwordSecretField,
caBundleSecretNameField,
tlsAPIInternalField,
tlsAPIPublicField,
networkAttachmentsField,
customServiceConfigSecretsField,
memcachedInstanceField,
}
)

Expand Down
38 changes: 12 additions & 26 deletions controllers/glance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,33 +215,16 @@ func (r *GlanceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return err
}

memcachedFn := func(ctx context.Context, o client.Object) []reconcile.Request {
result := []reconcile.Request{}

// get all Glance CRs
glances := &glancev1.GlanceList{}
listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), glances, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve Glance CRs %w")
// index memcachedInstanceField
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &glancev1.Glance{}, memcachedInstanceField, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*glancev1.Glance)
if cr.Spec.MemcachedInstance == "" {
return nil
}

for _, cr := range glances.Items {
if o.GetName() == cr.Spec.MemcachedInstance {
name := client.ObjectKey{
Namespace: o.GetNamespace(),
Name: cr.Name,
}
r.Log.Info(fmt.Sprintf("Memcached %s is used by Glance CR %s", o.GetName(), cr.Name))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
if len(result) > 0 {
return result
}
return nil
return []string{cr.Spec.MemcachedInstance}
}); err != nil {
return err
}

return ctrl.NewControllerManagedBy(mgr).
Expand All @@ -263,7 +246,9 @@ func (r *GlanceReconciler) SetupWithManager(mgr ctrl.Manager) error {
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Watches(&memcachedv1.Memcached{},
handler.EnqueueRequestsFromMapFunc(memcachedFn)).
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Complete(r)
}

Expand Down Expand Up @@ -625,6 +610,7 @@ func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glance
return ctrlResult, err
}
}

// Handle service init
ctrlResult, err = r.reconcileInit(ctx, instance, helper, serviceLabels, serviceAnnotations)
if err != nil {
Expand Down
117 changes: 34 additions & 83 deletions controllers/glanceapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,95 +241,40 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
return err
}

// Watch for changes to any CustomServiceConfigSecrets. Global secrets
svcSecretFn := func(ctx context.Context, o client.Object) []reconcile.Request {
var namespace string = o.GetNamespace()
var secretName string = o.GetName()
result := []reconcile.Request{}

// get all API CRs
apis := &glancev1.GlanceAPIList{}
listOpts := []client.ListOption{
client.InNamespace(namespace),
}
if err := r.Client.List(context.Background(), apis, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve API CRs %v")
// index networkAttachmentsField
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &glancev1.GlanceAPI{}, networkAttachmentsField, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*glancev1.GlanceAPI)
if cr.Spec.NetworkAttachments == nil {
return nil
}
for _, cr := range apis.Items {
for _, v := range cr.Spec.CustomServiceConfigSecrets {
if v == secretName {
name := client.ObjectKey{
Namespace: namespace,
Name: cr.Name,
}
r.Log.Info(fmt.Sprintf("Secret %s is used by Glance CR %s", secretName, cr.Name))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
}
if len(result) > 0 {
return result
}
return nil
return cr.Spec.NetworkAttachments
}); err != nil {
return err
}

// Watch for changes to NADs
nadFn := func(ctx context.Context, o client.Object) []reconcile.Request {
result := []reconcile.Request{}

// get all GlanceAPI CRs
glanceAPIs := &glancev1.GlanceAPIList{}
listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), glanceAPIs, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve GlanceAPI CRs %w")
// index customServiceConfigSecretsField
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &glancev1.GlanceAPI{}, customServiceConfigSecretsField, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*glancev1.GlanceAPI)
if cr.Spec.CustomServiceConfigSecrets == nil {
return nil
}
for _, cr := range glanceAPIs.Items {
if util.StringInSlice(o.GetName(), cr.Spec.NetworkAttachments) {
name := client.ObjectKey{
Namespace: cr.GetNamespace(),
Name: cr.GetName(),
}
r.Log.Info(fmt.Sprintf("NAD %s is used by GlanceAPI CR %s", o.GetName(), cr.GetName()))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
if len(result) > 0 {
return result
}
return nil
return cr.Spec.CustomServiceConfigSecrets
}); err != nil {
return err
}

memcachedFn := func(ctx context.Context, o client.Object) []reconcile.Request {
result := []reconcile.Request{}

// get all GlanceAPIs CRs
glanceAPIs := &glancev1.GlanceAPIList{}
listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), glanceAPIs, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve GlanceAPI CRs %w")
// index memcachedInstanceField
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &glancev1.GlanceAPI{}, memcachedInstanceField, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*glancev1.GlanceAPI)
if cr.Spec.MemcachedInstance == "" {
return nil
}

for _, cr := range glanceAPIs.Items {
if o.GetName() == cr.Spec.MemcachedInstance {
name := client.ObjectKey{
Namespace: o.GetNamespace(),
Name: cr.Name,
}
r.Log.Info(fmt.Sprintf("Memcached %s is used by GlanceAPI CR %s", o.GetName(), cr.Name))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
if len(result) > 0 {
return result
}
return nil
return []string{cr.Spec.MemcachedInstance}
}); err != nil {
return err
}

return ctrl.NewControllerManagedBy(mgr).
Expand All @@ -339,16 +284,22 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&corev1.Secret{}).
Owns(&appsv1.StatefulSet{}).
Watches(&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(svcSecretFn)).
Watches(&networkv1.NetworkAttachmentDefinition{},
handler.EnqueueRequestsFromMapFunc(nadFn)).
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Watches(
&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Watches(&memcachedv1.Memcached{},
handler.EnqueueRequestsFromMapFunc(memcachedFn)).
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Watches(&networkv1.NetworkAttachmentDefinition{},
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Complete(r)
}

Expand Down

0 comments on commit 53deb16

Please sign in to comment.