Skip to content

Commit

Permalink
Enable caching using memcached
Browse files Browse the repository at this point in the history
Follows openstack-k8s-operators/cinder-operator#286

Jira: OSPRH-1524
Signed-off-by: John Fulton <[email protected]>
  • Loading branch information
fultonj committed Feb 25, 2024
1 parent 5c2668d commit 290cf2e
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
4 changes: 4 additions & 0 deletions api/bases/glance.openstack.org_glances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,9 @@ spec:
keystoneEndpoint:
default: ""
type: string
memcachedInstance:
default: memcached
type: string
nodeSelector:
additionalProperties:
type: string
Expand Down Expand Up @@ -1028,6 +1031,7 @@ spec:
- glanceAPIs
- imageCacheSize
- keystoneEndpoint
- memcachedInstance
- secret
- storageRequest
type: object
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/glance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ type GlanceSpec struct {
// TODO: -> implement needs work in mariadb-operator, right now only glance
DatabaseUser string `json:"databaseUser"`

// +kubebuilder:validation:Required
// +kubebuilder:default=memcached
// Memcached instance name.
MemcachedInstance string `json:"memcachedInstance"`

// +kubebuilder:validation:Required
// Secret containing OpenStack password information for glance GlanceDatabasePassword
Secret string `json:"secret"`
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/glance.openstack.org_glances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,9 @@ spec:
keystoneEndpoint:
default: ""
type: string
memcachedInstance:
default: memcached
type: string
nodeSelector:
additionalProperties:
type: string
Expand Down Expand Up @@ -1028,6 +1031,7 @@ spec:
- glanceAPIs
- imageCacheSize
- keystoneEndpoint
- memcachedInstance
- secret
- storageRequest
type: object
Expand Down
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- memcached.openstack.org
resources:
- memcacheds
verbs:
- get
- list
- watch
- apiGroups:
- rbac.authorization.k8s.io
resources:
Expand Down
96 changes: 95 additions & 1 deletion controllers/glance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"strings"
"time"

"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
Expand All @@ -40,6 +41,7 @@ import (
"github.com/go-logr/logr"
glancev1 "github.com/openstack-k8s-operators/glance-operator/api/v1beta1"
"github.com/openstack-k8s-operators/glance-operator/pkg/glance"
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
"github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
Expand Down Expand Up @@ -80,6 +82,7 @@ type GlanceReconciler struct {
// +kubebuilder:rbac:groups=mariadb.openstack.org,resources=mariadbdatabases,verbs=get;list;watch;create;update;patch;delete;
// +kubebuilder:rbac:groups=mariadb.openstack.org,resources=mariadbaccounts,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=mariadb.openstack.org,resources=mariadbaccounts/finalizers,verbs=update
// +kubebuilder:rbac:groups=memcached.openstack.org,resources=memcacheds,verbs=get;list;watch;
// +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneapis,verbs=get;list;watch;
// +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices,verbs=get;list;watch;create;update;patch;delete;
// +kubebuilder:rbac:groups=k8s.cni.cncf.io,resources=network-attachment-definitions,verbs=get;list;watch
Expand Down Expand Up @@ -156,6 +159,7 @@ func (r *GlanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
cl := condition.CreateList(
condition.UnknownCondition(condition.DBReadyCondition, condition.InitReason, condition.DBReadyInitMessage),
condition.UnknownCondition(condition.DBSyncReadyCondition, condition.InitReason, condition.DBSyncReadyInitMessage),
condition.UnknownCondition(condition.MemcachedReadyCondition, condition.InitReason, condition.MemcachedReadyInitMessage),
condition.UnknownCondition(condition.InputReadyCondition, condition.InitReason, condition.InputReadyInitMessage),
condition.UnknownCondition(condition.ServiceConfigReadyCondition, condition.InitReason, condition.ServiceConfigReadyInitMessage),
condition.UnknownCondition(glancev1.GlanceAPIReadyCondition, condition.InitReason, glancev1.GlanceAPIReadyInitMessage),
Expand Down Expand Up @@ -207,6 +211,37 @@ func (r *GlanceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return err
}

// start define memcachedFn
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")
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
}
// end define memcachedFn

return ctrl.NewControllerManagedBy(mgr).
For(&glancev1.Glance{}).
Owns(&glancev1.GlanceAPI{}).
Expand All @@ -225,6 +260,8 @@ func (r *GlanceReconciler) SetupWithManager(mgr ctrl.Manager) error {
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Watches(&memcachedv1.Memcached{},
handler.EnqueueRequestsFromMapFunc(memcachedFn)).
Complete(r)
}

Expand Down Expand Up @@ -563,6 +600,41 @@ func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glance
common.AppSelector: glance.ServiceName,
}

//
// Check for required memcached used for caching
//
memcached, err := r.getGlanceMemcached(ctx, helper, instance)
if err != nil {
if k8s_errors.IsNotFound(err) {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.MemcachedReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.MemcachedReadyWaitingMessage))
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, fmt.Errorf("memcached %s not found", instance.Spec.MemcachedInstance)
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.MemcachedReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.MemcachedReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}

if !memcached.IsReady() {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.MemcachedReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.MemcachedReadyWaitingMessage))
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, fmt.Errorf("memcached %s is not ready", memcached.Name)
}
// Mark the Memcached Service as Ready if we get to this point with no errors
instance.Status.Conditions.MarkTrue(
condition.MemcachedReadyCondition, condition.MemcachedReadyMessage)
// run check memcached - end

//
// check for required OpenStack secret holding passwords for service/admin user and add hash to the vars map
//
Expand Down Expand Up @@ -593,7 +665,7 @@ func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glance
//

//
err = r.generateServiceConfig(ctx, helper, instance, &configVars, serviceLabels)
err = r.generateServiceConfig(ctx, helper, instance, &configVars, serviceLabels, memcached)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ServiceConfigReadyCondition,
Expand Down Expand Up @@ -792,6 +864,26 @@ func (r *GlanceReconciler) apiDeployment(
return nil
}

// getGlanceMemcached - gets the Memcached instance used for Glance cache backend
func (r *GlanceReconciler) getGlanceMemcached(
ctx context.Context,
h *helper.Helper,
instance *glancev1.Glance,
) (*memcachedv1.Memcached, error) {
memcached := &memcachedv1.Memcached{}
err := h.GetClient().Get(
ctx,
types.NamespacedName{
Name: instance.Spec.MemcachedInstance,
Namespace: instance.Namespace,
},
memcached)
if err != nil {
return nil, err
}
return memcached, err
}

func (r *GlanceReconciler) apiDeploymentCreateOrUpdate(
ctx context.Context,
instance *glancev1.Glance,
Expand Down Expand Up @@ -887,6 +979,7 @@ func (r *GlanceReconciler) generateServiceConfig(
instance *glancev1.Glance,
envVars *map[string]env.Setter,
serviceLabels map[string]string,
memcached *memcachedv1.Memcached,
) error {
labels := labels.GetLabels(instance, labels.GetGroupLabel(glance.ServiceName), serviceLabels)

Expand All @@ -906,6 +999,7 @@ func (r *GlanceReconciler) generateServiceConfig(
glance.DatabaseName,
),
}
templateParameters["MemcachedServersWithInet"] = strings.Join(memcached.Status.ServerListWithInet, ",")
// We set in the main 00-config-default.conf the image-cache bits that will
// be used by CronJobs cleaner and pruner
if len(instance.Spec.ImageCacheSize) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (

networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"

Expand All @@ -57,6 +58,7 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(glancev1.AddToScheme(scheme))
utilruntime.Must(memcachedv1.AddToScheme(scheme))
utilruntime.Must(mariadbv1.AddToScheme(scheme))
utilruntime.Must(keystonev1.AddToScheme(scheme))
utilruntime.Must(cinderv1.AddToScheme(scheme))
Expand Down
1 change: 1 addition & 0 deletions templates/common/config/00-config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ username={{ .ServiceUser }}
{{ if (index . "ServicePassword") }}
password = {{ .ServicePassword }}
{{ end }}
memcached_servers = {{ .MemcachedServersWithInet }}
project_domain_name=Default
user_domain_name=Default
project_name=service
Expand Down

0 comments on commit 290cf2e

Please sign in to comment.