Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable caching using memcached #164

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/bases/manila.openstack.org_manilas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,9 @@ spec:
- containerImage
type: object
type: object
memcachedInstance:
default: memcached
type: string
nodeSelector:
additionalProperties:
type: string
Expand Down Expand Up @@ -1103,6 +1106,7 @@ spec:
required:
- manilaAPI
- manilaScheduler
- memcachedInstance
- rabbitMqClusterName
type: object
status:
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/manila_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ type ManilaSpec struct {
// Needed to request a transportURL that is created and used in Manila
RabbitMqClusterName string `json:"rabbitMqClusterName"`

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

// +kubebuilder:validation:Optional
// Debug - enable debug for different deploy stages. If an init container is used, it runs and the
// actual action pod gets started with sleep infinity
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/manila.openstack.org_manilas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,9 @@ spec:
- containerImage
type: object
type: object
memcachedInstance:
default: memcached
type: string
nodeSelector:
additionalProperties:
type: string
Expand Down Expand Up @@ -1103,6 +1106,7 @@ spec:
required:
- manilaAPI
- manilaScheduler
- memcachedInstance
- rabbitMqClusterName
type: object
status:
Expand Down
5 changes: 5 additions & 0 deletions config/dev/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace: openstack
namePrefix: manila-operator-
bases:
- ../crd
- ../rbac
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- memcached.openstack.org
resources:
- memcacheds
verbs:
- get
- list
- watch
- apiGroups:
- rabbitmq.openstack.org
resources:
Expand Down
95 changes: 94 additions & 1 deletion controllers/manila_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package controllers
import (
"context"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1"
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
"github.com/openstack-k8s-operators/lib-common/modules/common"
Expand All @@ -45,6 +47,7 @@ import (
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -99,6 +102,7 @@ type ManilaReconciler struct {
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;create;update;patch;delete;watch
// +kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;create;update;patch;delete;watch
// +kubebuilder:rbac:groups=mariadb.openstack.org,resources=mariadbdatabases,verbs=get;list;watch;create;update;patch;delete
// +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=rabbitmq.openstack.org,resources=transporturls,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 @@ -171,6 +175,7 @@ func (r *ManilaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
condition.UnknownCondition(condition.DBReadyCondition, condition.InitReason, condition.DBReadyInitMessage),
condition.UnknownCondition(condition.DBSyncReadyCondition, condition.InitReason, condition.DBSyncReadyInitMessage),
condition.UnknownCondition(condition.RabbitMqTransportURLReadyCondition, condition.InitReason, condition.RabbitMqTransportURLReadyInitMessage),
condition.UnknownCondition(condition.MemcachedReadyCondition, condition.InitReason, condition.MemcachedReadyInitMessage),
condition.UnknownCondition(condition.InputReadyCondition, condition.InitReason, condition.InputReadyInitMessage),
condition.UnknownCondition(manilav1beta1.ManilaAPIReadyCondition, condition.InitReason, manilav1beta1.ManilaAPIReadyInitMessage),
condition.UnknownCondition(manilav1beta1.ManilaSchedulerReadyCondition, condition.InitReason, manilav1beta1.ManilaSchedulerReadyInitMessage),
Expand Down Expand Up @@ -251,6 +256,35 @@ func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error {
return nil
}

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

// get all Manila CRs
manilas := &manilav1beta1.ManilaList{}
listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), manilas, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve Manila CRs %w")
return nil
}

for _, cr := range manilas.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 Manila CR %s", o.GetName(), cr.Name))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
if len(result) > 0 {
return result
}
return nil
}

return ctrl.NewControllerManagedBy(mgr).
For(&manilav1beta1.Manila{}).
Owns(&mariadbv1.MariaDBDatabase{}).
Expand All @@ -267,6 +301,8 @@ func (r *ManilaReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Watch for TransportURL Secrets which belong to any TransportURLs created by Manila CRs
Watches(&source.Kind{Type: &corev1.Secret{}},
handler.EnqueueRequestsFromMapFunc(transportURLSecretFn)).
Watches(&source.Kind{Type: &memcachedv1.Memcached{}},
handler.EnqueueRequestsFromMapFunc(memcachedFn)).
Complete(r)
}

Expand Down Expand Up @@ -469,6 +505,41 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila

// end transportURL

//
// Check for required memcached used for caching
//
memcached, err := r.getManilaMemcached(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 @@ -507,7 +578,7 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila
// - %-config configmap holding minimal manila config required to get the service up, user can add additional files to be added to the service
// - parameters which has passwords gets added from the OpenStack secret via the init container
//
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 @@ -747,6 +818,7 @@ func (r *ManilaReconciler) generateServiceConfig(
instance *manilav1beta1.Manila,
envVars *map[string]env.Setter,
serviceLabels map[string]string,
memcached *memcachedv1.Memcached,
) error {
//
// create Secret required for manila input
Expand Down Expand Up @@ -801,6 +873,7 @@ func (r *ManilaReconciler) generateServiceConfig(
string(ospSecret.Data[instance.Spec.PasswordSelectors.Database]),
instance.Status.DatabaseHostname,
manila.DatabaseName),
"MemcachedServersWithInet": strings.Join(memcached.Status.ServerListWithInet, ","),
}

configTemplates := []util.Template{
Expand Down Expand Up @@ -976,3 +1049,23 @@ func (r *ManilaReconciler) transportURLCreateOrUpdate(ctx context.Context, insta

return transportURL, op, err
}

// getManilaMemcached - gets the Memcached instance used for Manila cache backend
func (r *ManilaReconciler) getManilaMemcached(
ctx context.Context,
h *helper.Helper,
instance *manilav1beta1.Manila,
) (*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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ require (
k8s.io/component-base v0.26.9 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"

networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1"
keystonev1beta1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
manilav1beta1 "github.com/openstack-k8s-operators/manila-operator/api/v1beta1"
Expand All @@ -51,6 +52,7 @@ var (
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(manilav1beta1.AddToScheme(scheme))
utilruntime.Must(memcachedv1.AddToScheme(scheme))
utilruntime.Must(mariadbv1beta1.AddToScheme(scheme))
utilruntime.Must(keystonev1beta1.AddToScheme(scheme))
utilruntime.Must(rabbitmqv1.AddToScheme(scheme))
Expand Down
1 change: 1 addition & 0 deletions templates/manila/config/00-config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ db_max_retries = -1
[keystone_authtoken]
www_authenticate_uri={{ .KeystonePublicURL }}
auth_url = {{ .KeystoneInternalURL }}
memcached_servers = {{ .MemcachedServersWithInet }}
auth_type = password
project_domain_name = Default
user_domain_name = Default
Expand Down
49 changes: 40 additions & 9 deletions test/functional/manila_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,31 @@ import (
. "github.com/openstack-k8s-operators/lib-common/modules/common/test/helpers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"

memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
util "github.com/openstack-k8s-operators/lib-common/modules/common/util"
manilav1 "github.com/openstack-k8s-operators/manila-operator/api/v1beta1"
)

var _ = Describe("Manila controller", func() {
var memcachedSpec memcachedv1.MemcachedSpec

BeforeEach(func() {
memcachedSpec = memcachedv1.MemcachedSpec{
Replicas: ptr.To(int32(3)),
}
})

When("Manila CR instance is created", func() {
BeforeEach(func() {
DeferCleanup(th.DeleteInstance, CreateManila(manilaTest.Instance, GetDefaultManilaSpec()))
})
It("initializes the status fields", func() {
Eventually(func(g Gomega) {
glance := GetManila(manilaName)
g.Expect(glance.Status.Conditions).To(HaveLen(13))
g.Expect(glance.Status.Conditions).To(HaveLen(14))

g.Expect(glance.Status.DatabaseHostname).To(Equal(""))
}, timeout*2, interval).Should(Succeed())
Expand All @@ -52,6 +62,7 @@ var _ = Describe("Manila controller", func() {
Manila := GetManila(manilaTest.Instance)
Expect(Manila.Spec.DatabaseInstance).Should(Equal("openstack"))
Expect(Manila.Spec.DatabaseUser).Should(Equal(manilaTest.ManilaDataBaseUser))
Expect(Manila.Spec.MemcachedInstance).Should(Equal(manilaTest.MemcachedInstance))
Expect(Manila.Spec.RabbitMqClusterName).Should(Equal(manilaTest.RabbitmqClusterName))
Expect(Manila.Spec.ServiceUser).Should(Equal(manilaTest.ManilaServiceUser))
})
Expand All @@ -63,6 +74,24 @@ var _ = Describe("Manila controller", func() {
Expect(Manila.Status.ManilaAPIReadyCount).To(Equal(int32(0)))
Expect(Manila.Status.ManilaSchedulerReadyCount).To(Equal(int32(0)))
})
It("should have Unknown Conditions initialized", func() {
for _, cond := range []condition.Type{
condition.DBReadyCondition,
condition.DBSyncReadyCondition,
condition.InputReadyCondition,
condition.MemcachedReadyCondition,
manilav1.ManilaAPIReadyCondition,
manilav1.ManilaSchedulerReadyCondition,
manilav1.ManilaShareReadyCondition,
} {
th.ExpectCondition(
manilaTest.Manila,
ConditionGetterFunc(ManilaConditionGetter),
cond,
corev1.ConditionUnknown,
)
}
})
It("should have a finalizer", func() {
// the reconciler loop adds the finalizer so we have to wait for
// it to run
Expand Down Expand Up @@ -103,14 +132,6 @@ var _ = Describe("Manila controller", func() {
Expect(binding.RoleRef.Name).To(Equal(role.Name))
Expect(binding.Subjects[0].Name).To(Equal(sa.Name))
})
It("should have Unknown Conditions initialized as transporturl not created", func() {
th.ExpectCondition(
manilaName,
ConditionGetterFunc(ManilaConditionGetter),
condition.InputReadyCondition,
corev1.ConditionUnknown,
)
})
})
When("Manila DB is created", func() {
BeforeEach(func() {
Expand All @@ -127,6 +148,8 @@ var _ = Describe("Manila controller", func() {
),
)
infra.SimulateTransportURLReady(manilaTest.ManilaTransportURL)
DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, manilaTest.MemcachedInstance, memcachedSpec))
infra.SimulateMemcachedReady(manilaTest.ManilaMemcached)
DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(namespace))
})
It("Should set DBReady Condition and set DatabaseHostname Status when DB is Created", func() {
Expand Down Expand Up @@ -186,6 +209,8 @@ var _ = Describe("Manila controller", func() {
),
)
infra.SimulateTransportURLReady(manilaTest.ManilaTransportURL)
DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, "memcached", memcachedSpec))
infra.SimulateMemcachedReady(manilaTest.ManilaMemcached)
})
It("should create config-data and scripts ConfigMaps", func() {
keystoneAPI := keystone.CreateKeystoneAPI(manilaTest.Instance.Namespace)
Expand Down Expand Up @@ -231,6 +256,8 @@ var _ = Describe("Manila controller", func() {
),
)
infra.SimulateTransportURLReady(manilaTest.ManilaTransportURL)
DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, manilaTest.MemcachedInstance, memcachedSpec))
infra.SimulateMemcachedReady(manilaTest.ManilaMemcached)
DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(manilaTest.Instance.Namespace))
mariadb.SimulateMariaDBDatabaseCompleted(manilaTest.Instance)
th.SimulateJobSuccess(manilaTest.ManilaDBSync)
Expand Down Expand Up @@ -266,6 +293,8 @@ var _ = Describe("Manila controller", func() {
),
)
infra.SimulateTransportURLReady(manilaTest.ManilaTransportURL)
DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, manilaTest.MemcachedInstance, memcachedSpec))
infra.SimulateMemcachedReady(manilaTest.ManilaMemcached)
DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(manilaTest.Instance.Namespace))
mariadb.SimulateMariaDBDatabaseCompleted(manilaTest.Instance)
th.SimulateJobSuccess(manilaTest.ManilaDBSync)
Expand Down Expand Up @@ -339,6 +368,8 @@ var _ = Describe("Manila controller", func() {
),
)
infra.SimulateTransportURLReady(manilaTest.ManilaTransportURL)
DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, manilaTest.MemcachedInstance, memcachedSpec))
infra.SimulateMemcachedReady(manilaTest.ManilaMemcached)
keystoneAPIName := keystone.CreateKeystoneAPI(manilaTest.Instance.Namespace)
DeferCleanup(keystone.DeleteKeystoneAPI, keystoneAPIName)
keystoneAPI := keystone.GetKeystoneAPI(keystoneAPIName)
Expand Down
Loading