Skip to content
Closed
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
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rules:
- ""
resources:
- configmaps
- secrets
- services
verbs:
- create
Expand Down
1 change: 1 addition & 0 deletions config/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Append samples of your project ##
resources:
- sample_config.yaml
- v1alpha1_valkeycluster.yaml
# +kubebuilder:scaffold:manifestskustomizesamples
12 changes: 12 additions & 0 deletions config/samples/sample_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: valkeycluster-sample-conf
data:
extra.conf: |
maxmemory 50mb
maxmemory-policy allkeys-lfu
aof.conf: |
appendonly yes
appendfilename "orbit.aof"
29 changes: 6 additions & 23 deletions internal/controller/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
valkeyiov1alpha1 "valkey.io/valkey-operator/api/v1alpha1"
)

func createClusterDeployment(cluster *valkeyiov1alpha1.ValkeyCluster) *appsv1.Deployment {
func createClusterDeployment(cluster *valkeyiov1alpha1.ValkeyCluster, configVolumes []corev1.Volume) *appsv1.Deployment {
image := DefaultImage
if cluster.Spec.Image != "" {
image = cluster.Spec.Image
Expand Down Expand Up @@ -105,32 +105,15 @@ func createClusterDeployment(cluster *valkeyiov1alpha1.ValkeyCluster) *appsv1.De
MountPath: "/config",
ReadOnly: true,
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "scripts",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: cluster.Name,
},
DefaultMode: func(i int32) *int32 { return &i }(0755),
},
},
},
{
Name: "valkey-conf",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: cluster.Name,
},
{
Name: "user-conf",
MountPath: "/config/valkey.conf.d",
ReadOnly: true,
},
},
},
},
Volumes: configVolumes,
},
},
},
Expand Down
10 changes: 9 additions & 1 deletion internal/controller/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
valkeyv1 "valkey.io/valkey-operator/api/v1alpha1"
)
Expand All @@ -33,7 +34,9 @@ func TestCreateClusterDeployment(t *testing.T) {
},
}

d := createClusterDeployment(cluster)
volumes := []corev1.Volume{getConfigVolume("mycluster", "myvol", false)}

d := createClusterDeployment(cluster, volumes)

if d.Name != "" {
t.Errorf("Expected empty name field, got %v", d.Name)
Expand All @@ -50,4 +53,9 @@ func TestCreateClusterDeployment(t *testing.T) {
if d.Spec.Template.Spec.Containers[0].Image != "container:version" {
t.Errorf("Expected %v, got %v", "container:version", d.Spec.Template.Spec.Containers[0].Image)
}

// Volume check
if volLen := len(d.Spec.Template.Spec.Volumes); volLen != 1 {
t.Errorf("Expected %v volume, got %v", 1, volLen)
}
}
16 changes: 12 additions & 4 deletions internal/controller/valkeycluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var scripts embed.FS
// +kubebuilder:rbac:groups=valkey.io,resources=valkeyclusters/finalizers,verbs=update
// +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch
// +kubebuilder:rbac:groups="apps",resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
Expand All @@ -87,7 +88,7 @@ func (r *ValkeyClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, err
}

if err := r.upsertConfigMap(ctx, cluster); err != nil {
if err := r.upsertDefaultConfigMap(ctx, cluster); err != nil {
setCondition(cluster, valkeyiov1alpha1.ConditionReady, valkeyiov1alpha1.ReasonConfigMapError, err.Error(), metav1.ConditionFalse)
_ = r.updateStatus(ctx, cluster, nil)
return ctrl.Result{}, err
Expand Down Expand Up @@ -225,7 +226,7 @@ func (r *ValkeyClusterReconciler) upsertService(ctx context.Context, cluster *va
}

// Create or update a basic valkey.conf
func (r *ValkeyClusterReconciler) upsertConfigMap(ctx context.Context, cluster *valkeyiov1alpha1.ValkeyCluster) error {
func (r *ValkeyClusterReconciler) upsertDefaultConfigMap(ctx context.Context, cluster *valkeyiov1alpha1.ValkeyCluster) error {
readiness, err := scripts.ReadFile("scripts/readiness-check.sh")
if err != nil {
return err
Expand All @@ -247,7 +248,8 @@ func (r *ValkeyClusterReconciler) upsertConfigMap(ctx context.Context, cluster *
"valkey.conf": `
cluster-enabled yes
protected-mode no
cluster-node-timeout 2000`,
cluster-node-timeout 2000
include /config/valkey.conf.d/*.conf`,
},
}
if err := controllerutil.SetControllerReference(cluster, cm, r.Scheme); err != nil {
Expand Down Expand Up @@ -281,9 +283,15 @@ func (r *ValkeyClusterReconciler) upsertDeployments(ctx context.Context, cluster

expected := int(cluster.Spec.Shards * (1 + cluster.Spec.Replicas))

// Get script volume, default config, and user config volumes
configVolumes, err := getConfigVolumes(ctx, r.Client, cluster)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move this getConfigVolumes call to inside createDeployment. this way we dont need to pass it as param?

if err != nil {
return err
}

// Create missing deployments
for i := len(existing.Items); i < expected; i++ {
deployment := createClusterDeployment(cluster)
deployment := createClusterDeployment(cluster, configVolumes)
if err := controllerutil.SetControllerReference(cluster, deployment, r.Scheme); err != nil {
return err
}
Expand Down
131 changes: 131 additions & 0 deletions internal/controller/volumes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright 2025 Valkey Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"context"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
valkeyiov1alpha1 "valkey.io/valkey-operator/api/v1alpha1"
)

func getConfigVolumes(ctx context.Context, c client.Client, cluster *valkeyiov1alpha1.ValkeyCluster) ([]corev1.Volume, error) {

// Volume containing health, and liveliness scripts
scriptsVolume := corev1.Volume{
Name: "scripts",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: cluster.Name,
},
DefaultMode: func(i int32) *int32 { return &i }(0755),
},
},
}

// Volume containing default Valkey configuration
defaultConfigVolume := corev1.Volume{
Name: "valkey-conf",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: cluster.Name,
},
},
},
}

// User config volume
userConfigVolume, err := getUserConfigVolume(ctx, c, cluster)
if err != nil {
return nil, err
}

return []corev1.Volume{
scriptsVolume,
defaultConfigVolume,
userConfigVolume,
}, nil
}

// Discover user-created Valkey configuration. This can be either a Secret, or ConfigMap, created by the user
func getUserConfigVolume(ctx context.Context, c client.Client, cluster *valkeyiov1alpha1.ValkeyCluster) (corev1.Volume, error) {
log := logf.FromContext(ctx)

configMapName := cluster.Name + "-conf"
userConfigFilter := types.NamespacedName{
Namespace: cluster.Namespace,
Name: configMapName,
}

// First, look for a Secret named "$clusterName-conf"
err := c.Get(ctx, userConfigFilter, &corev1.Secret{})
if err == nil {
log.V(1).Info("found user-created secret config")
Copy link
Member

@sandeepkunusoth sandeepkunusoth Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should pass "$clusterName-conf" from the input spec so users can optionally provide any existing ConfigMap if needed. Also, getUserConfigVolume / userConfig should only be used when this new custom field is set in the spec; otherwise, the default behavior should remain not to use any existing resource.

we need to agree on spec changes with others. Please update spec design or feel free to start discussion. not sure if this feature is priority for others.

return getSecretConfigVolume("user-conf", configMapName, false), nil
}
if !apierrors.IsNotFound(err) {
log.Error(err, "failed to search for user config Secret")
return corev1.Volume{}, err
}

// Next, look for a ConfigMap named "$clusterName-conf"
err = c.Get(ctx, userConfigFilter, &corev1.ConfigMap{})
if err == nil {
log.V(1).Info("found user-created configMap")
return getConfigVolume("user-conf", configMapName, false), nil
}
if !apierrors.IsNotFound(err) {
log.Error(err, "failed to search for user config ConfigMap")
return corev1.Volume{}, err
}

// If neither found, create empty (optional) config-volume
log.V(1).Info("using empty configMap")
return getConfigVolume("user-conf", configMapName, true), nil
}

func getConfigVolume(configVolumeName, configMapName string, optional bool) corev1.Volume {
return corev1.Volume{
Name: configVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: configMapName,
},
Optional: func(b bool) *bool { return &b }(optional),
},
},
}
}

func getSecretConfigVolume(configVolumeName, configMapName string, optional bool) corev1.Volume {
return corev1.Volume{
Name: configVolumeName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: configMapName,
Optional: func(b bool) *bool { return &b }(optional),
},
},
}
}
74 changes: 74 additions & 0 deletions internal/controller/volumes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2025 Valkey Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
)

func TestGetConfigVolume(t *testing.T) {

volumeName := "test1"
mapName := "alice-conf"
optional := false

vol := getConfigVolume(volumeName, mapName, optional)

if vol.Name != volumeName {
t.Errorf("Expected volume name to be '%v', got %v", volumeName, vol.Name)
}
if reflect.TypeOf(vol.VolumeSource) != reflect.TypeOf(corev1.VolumeSource{}) {
t.Errorf("Expected VolumeSource to be native k8s type")
}
if reflect.TypeOf(vol.VolumeSource.ConfigMap) != reflect.TypeOf(&corev1.ConfigMapVolumeSource{}) {
t.Errorf("Expected VolumeSource.ConfigMap to be native k8s type")
}
if vol.VolumeSource.ConfigMap.Name != mapName {
t.Errorf("Expected ConfigMap name to be '%v', got '%v'", mapName, vol.VolumeSource.ConfigMap.Name)
}
if *vol.VolumeSource.ConfigMap.Optional != optional {
t.Errorf("Expection ConfigMap.Optional to be %v, got %v", optional, vol.VolumeSource.ConfigMap.Optional)
}
}

func TestGetSecretConfigVolume(t *testing.T) {

volumeName := "test2"
secretName := "bob-conf"
optional := true

vol := getSecretConfigVolume(volumeName, secretName, optional)

if vol.Name != volumeName {
t.Errorf("Expected volume name to be '%v', got %v", volumeName, vol.Name)
}
if reflect.TypeOf(vol.VolumeSource) != reflect.TypeOf(corev1.VolumeSource{}) {
t.Errorf("Expected VolumeSource to be native k8s type")
}
if reflect.TypeOf(vol.VolumeSource.Secret) != reflect.TypeOf(&corev1.SecretVolumeSource{}) {
t.Errorf("Expected VolumeSource.Secret to be native k8s type")
}
if vol.VolumeSource.Secret.SecretName != secretName {
t.Errorf("Expected Secret name to be '%v', got '%v'", secretName, vol.VolumeSource.Secret.SecretName)
}
if *vol.VolumeSource.Secret.Optional != optional {
t.Errorf("Expection Secret.Optional to be %v, got %v", optional, vol.VolumeSource.Secret.Optional)
}
}
Loading