Skip to content

Commit

Permalink
Add support for memcached instance
Browse files Browse the repository at this point in the history
Removing the existing memcached container will
be done in a follow up commit.
  • Loading branch information
cschwede committed Jan 30, 2024
1 parent 678ba24 commit c311872
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 2 deletions.
5 changes: 5 additions & 0 deletions api/bases/swift.openstack.org_swiftproxies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ spec:
containerImageProxy:
description: Swift Proxy Container Image URL
type: string
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment resource
names to expose the services to the given network
Expand Down Expand Up @@ -269,6 +273,7 @@ spec:
required:
- containerImageMemcached
- containerImageProxy
- memcachedInstance
- replicas
- secret
- serviceUser
Expand Down
15 changes: 15 additions & 0 deletions api/bases/swift.openstack.org_swifts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ spec:
spec:
description: SwiftSpec defines the desired state of Swift
properties:
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment resource
names to expose the services to the given network
Expand All @@ -73,6 +77,10 @@ spec:
containerImageProxy:
description: Swift Proxy Container Image URL
type: string
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment
resource names to expose the services to the given network
Expand Down Expand Up @@ -296,6 +304,7 @@ spec:
required:
- containerImageMemcached
- containerImageProxy
- memcachedInstance
- replicas
- secret
- serviceUser
Expand Down Expand Up @@ -342,6 +351,10 @@ spec:
containerImageProxy:
description: Image URL for Swift proxy service
type: string
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment
resource names to expose the services to the given network
Expand Down Expand Up @@ -371,12 +384,14 @@ spec:
- containerImageMemcached
- containerImageObject
- containerImageProxy
- memcachedInstance
- replicas
- storageClass
- storageRequest
- swiftConfSecret
type: object
required:
- memcachedInstance
- storageClass
- swiftConfSecret
- swiftProxy
Expand Down
5 changes: 5 additions & 0 deletions api/bases/swift.openstack.org_swiftstorages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ spec:
containerImageProxy:
description: Image URL for Swift proxy service
type: string
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment resource
names to expose the services to the given network
Expand Down Expand Up @@ -92,6 +96,7 @@ spec:
- containerImageMemcached
- containerImageObject
- containerImageProxy
- memcachedInstance
- replicas
- storageClass
- storageRequest
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/swift_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ type SwiftSpec struct {
// +kubebuilder:validation:Optional
// NetworkAttachments is a list of NetworkAttachment resource names to expose the services to the given network
NetworkAttachments []string `json:"networkAttachments,omitempty"`

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

// SwiftStatus defines the observed state of Swift
Expand Down
8 changes: 8 additions & 0 deletions api/v1beta1/swiftproxy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type SwiftProxySpec struct {
// +kubebuilder:validation:Required
// Image URL for Memcache servicd
ContainerImageMemcached string `json:"containerImageMemcached"`
// +kubebuilder:default=memcached
// Memcached instance name.
MemcachedInstance string `json:"memcachedInstance"`

// +kubebuilder:validation:Required
// +kubebuilder:default=swift
Expand Down Expand Up @@ -76,6 +79,11 @@ type SwiftProxySpec struct {
// +kubebuilder:validation:Optional
// NetworkAttachments is a list of NetworkAttachment resource names to expose the services to the given network
NetworkAttachments []string `json:"networkAttachments,omitempty"`

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

// ProxyOverrideSpec to override the generated manifest of several child resources.
Expand Down
10 changes: 10 additions & 0 deletions api/v1beta1/swiftstorage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type SwiftStorageSpec struct {
// +kubebuilder:validation:Minimum=0
Replicas *int32 `json:"replicas"`

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

// +kubebuilder:validation:Required
// Name of StorageClass to use for Swift PVs
// +kubebuilder:default=""
Expand Down Expand Up @@ -69,6 +74,11 @@ type SwiftStorageSpec struct {
// +kubebuilder:validation:Optional
// NetworkAttachments is a list of NetworkAttachment resource names to expose the services to the given network
NetworkAttachments []string `json:"networkAttachments,omitempty"`

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

// SwiftStorageStatus defines the observed state of SwiftStorage
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/swift.openstack.org_swiftproxies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ spec:
containerImageProxy:
description: Swift Proxy Container Image URL
type: string
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment resource
names to expose the services to the given network
Expand Down Expand Up @@ -269,6 +273,7 @@ spec:
required:
- containerImageMemcached
- containerImageProxy
- memcachedInstance
- replicas
- secret
- serviceUser
Expand Down
15 changes: 15 additions & 0 deletions config/crd/bases/swift.openstack.org_swifts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ spec:
spec:
description: SwiftSpec defines the desired state of Swift
properties:
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment resource
names to expose the services to the given network
Expand All @@ -73,6 +77,10 @@ spec:
containerImageProxy:
description: Swift Proxy Container Image URL
type: string
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment
resource names to expose the services to the given network
Expand Down Expand Up @@ -296,6 +304,7 @@ spec:
required:
- containerImageMemcached
- containerImageProxy
- memcachedInstance
- replicas
- secret
- serviceUser
Expand Down Expand Up @@ -342,6 +351,10 @@ spec:
containerImageProxy:
description: Image URL for Swift proxy service
type: string
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment
resource names to expose the services to the given network
Expand Down Expand Up @@ -371,12 +384,14 @@ spec:
- containerImageMemcached
- containerImageObject
- containerImageProxy
- memcachedInstance
- replicas
- storageClass
- storageRequest
- swiftConfSecret
type: object
required:
- memcachedInstance
- storageClass
- swiftConfSecret
- swiftProxy
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/swift.openstack.org_swiftstorages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ spec:
containerImageProxy:
description: Image URL for Swift proxy service
type: string
memcachedInstance:
default: memcached
description: Memcached instance name.
type: string
networkAttachments:
description: NetworkAttachments is a list of NetworkAttachment resource
names to expose the services to the given network
Expand Down Expand Up @@ -92,6 +96,7 @@ spec:
- containerImageMemcached
- containerImageObject
- containerImageProxy
- memcachedInstance
- replicas
- storageClass
- storageRequest
Expand Down
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- memcached.openstack.org
resources:
- memcacheds
verbs:
- get
- list
- watch
- apiGroups:
- networking.k8s.io
resources:
Expand Down
2 changes: 2 additions & 0 deletions controllers/swift_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ func (r *SwiftReconciler) storageCreateOrUpdate(ctx context.Context, instance *s
ContainerImageMemcached: instance.Spec.SwiftStorage.ContainerImageMemcached,
SwiftConfSecret: instance.Spec.SwiftConfSecret,
NetworkAttachments: instance.Spec.SwiftStorage.NetworkAttachments,
MemcachedInstance: instance.Spec.MemcachedInstance,
}

deployment := &swiftv1.SwiftStorage{
Expand Down Expand Up @@ -387,6 +388,7 @@ func (r *SwiftReconciler) proxyCreateOrUpdate(ctx context.Context, instance *swi
SwiftConfSecret: instance.Spec.SwiftConfSecret,
Override: instance.Spec.SwiftProxy.Override,
NetworkAttachments: instance.Spec.SwiftStorage.NetworkAttachments,
MemcachedInstance: instance.Spec.MemcachedInstance,
}

deployment := &swiftv1.SwiftProxy{
Expand Down
63 changes: 63 additions & 0 deletions controllers/swiftproxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package controllers
import (
"context"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -43,6 +45,7 @@ import (
service "github.com/openstack-k8s-operators/lib-common/modules/common/service"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"

memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
swiftv1beta1 "github.com/openstack-k8s-operators/swift-operator/api/v1beta1"
"github.com/openstack-k8s-operators/swift-operator/pkg/swift"
Expand All @@ -66,6 +69,7 @@ type SwiftProxyReconciler struct {
//+kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete;
//+kubebuilder:rbac:groups=k8s.cni.cncf.io,resources=network-attachment-definitions,verbs=get;list;watch
//+kubebuilder:rbac:groups=memcached.openstack.org,resources=memcacheds,verbs=get;list;watch;

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -286,6 +290,44 @@ func (r *SwiftProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
password := string(sps.Data[instance.Spec.PasswordSelectors.Service])

//
// Check for required memcached used for caching
//
memcached, err := r.getSwiftMemcached(ctx, helper, instance)
if err != nil {
if apierrors.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

// Get the memached server URLs
memcachedServers := strings.Join(memcached.Status.ServerList, ",")

// Create a Secret populated with content from templates/
envVars := make(map[string]env.Setter)
tpl := swiftproxy.SecretTemplates(
Expand All @@ -294,6 +336,7 @@ func (r *SwiftProxyReconciler) Reconcile(ctx context.Context, req ctrl.Request)
keystonePublicURL,
keystoneInternalURL,
password,
memcachedServers,
)
err = secret.EnsureSecrets(ctx, helper, instance, tpl, &envVars)
if err != nil {
Expand Down Expand Up @@ -404,3 +447,23 @@ func (r *SwiftProxyReconciler) reconcileDelete(ctx context.Context, instance *sw

return ctrl.Result{}, nil
}

// getSwiftMemcached - gets the Memcached instance used for Swift cache backend
func (r *SwiftProxyReconciler) getSwiftMemcached(
ctx context.Context,
h *helper.Helper,
instance *swiftv1beta1.SwiftProxy,
) (*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
}
Loading

0 comments on commit c311872

Please sign in to comment.