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

[Feature][Add] Secrets generation by the Operator #391

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 20 additions & 7 deletions api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ import (

// KubernetesConfig will be the JSON struct for Basic Redis Config
type KubernetesConfig struct {
Image string `json:"image"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
ExistingPasswordSecret *ExistingPasswordSecret `json:"redisSecret,omitempty"`
ImagePullSecrets *[]corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
UpdateStrategy appsv1.StatefulSetUpdateStrategy `json:"updateStrategy,omitempty"`
Service *ServiceConfig `json:"service,omitempty"`
Image string `json:"image"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
ExistOrGenerateSecret *ExistOrGenerateSecrets `json:"redisSecret,omitempty"`
ImagePullSecrets *[]corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
shubham-cmyk marked this conversation as resolved.
Show resolved Hide resolved
UpdateStrategy appsv1.StatefulSetUpdateStrategy `json:"updateStrategy,omitempty"`
Service *ServiceConfig `json:"service,omitempty"`
}

// +kubebuilder:validation:MaxProperties=1
type ExistOrGenerateSecrets struct {
ExistingPasswordSecret *ExistingPasswordSecret `json:"existRedisSecret,omitempty"`
GeneratePasswordSecret *GeneratePassword `json:"generatePasswordSecret,omitempty"`
}

// ServiceConfig define the type of service to be created and its annotations
Expand All @@ -50,6 +56,13 @@ type ExistingPasswordSecret struct {
Key *string `json:"key,omitempty"`
}

type GeneratePassword struct {
Name *string `json:"name"`
// +kubebuilder:default=key
Key *string `json:"key,omitempty"`
NameSpace []string `json:"namespace,omitempty"`
}

// Storage is the inteface to add pvc and pv support in redis
type Storage struct {
VolumeClaimTemplate corev1.PersistentVolumeClaim `json:"volumeClaimTemplate,omitempty"`
Expand Down
61 changes: 58 additions & 3 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 24 additions & 6 deletions config/crd/bases/redis.redis.opstreelabs.in_redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -957,13 +957,31 @@ spec:
type: object
type: array
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
maxProperties: 1
properties:
key:
type: string
name:
type: string
existRedisSecret:
description: ExistingPasswordSecret is the struct to access
the existing secret
properties:
key:
type: string
name:
type: string
type: object
generatePasswordSecret:
properties:
key:
default: key
type: string
name:
type: string
namespace:
items:
type: string
type: array
required:
- name
type: object
type: object
resources:
description: ResourceRequirements describes the compute resource
Expand Down
30 changes: 24 additions & 6 deletions config/crd/bases/redis.redis.opstreelabs.in_redisclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,31 @@ spec:
type: object
type: array
redisSecret:
description: ExistingPasswordSecret is the struct to access the
existing secret
maxProperties: 1
properties:
key:
type: string
name:
type: string
existRedisSecret:
description: ExistingPasswordSecret is the struct to access
the existing secret
properties:
key:
type: string
name:
type: string
type: object
generatePasswordSecret:
properties:
key:
default: key
type: string
name:
type: string
namespace:
items:
type: string
type: array
required:
- name
type: object
type: object
resources:
description: ResourceRequirements describes the compute resource
Expand Down
8 changes: 8 additions & 0 deletions controllers/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func (r *RedisReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return ctrl.Result{}, err
}

if instance.Spec.KubernetesConfig.ExistOrGenerateSecret.GeneratePasswordSecret != nil {
err = k8sutils.CreateRedisSecrets(instance)
if err != nil {
reqLogger.Error(err, "Failed to create the Secrets")
return ctrl.Result{RequeueAfter: time.Second * 10}, err
}
}

err = k8sutils.CreateStandaloneRedis(instance)
if err != nil {
return ctrl.Result{}, err
Expand Down
8 changes: 8 additions & 0 deletions controllers/rediscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{RequeueAfter: time.Second * 60}, err
}

if instance.Spec.KubernetesConfig.ExistOrGenerateSecret.GeneratePasswordSecret != nil {
err = k8sutils.CreateRedisClusterSecrets(instance)
if err != nil {
reqLogger.Error(err, "Failed to create the Secrets")
return ctrl.Result{RequeueAfter: time.Second * 10}, err
}
}

err = k8sutils.CreateRedisLeader(instance)
if err != nil {
return ctrl.Result{RequeueAfter: time.Second * 60}, err
Expand Down
78 changes: 78 additions & 0 deletions example/generated_secrets/redis-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
apiVersion: redis.redis.opstreelabs.in/v1beta1
kind: RedisCluster
metadata:
name: redis-cluster
namespace: redis-operator
spec:
clusterSize: 3
clusterVersion: v7
persistenceEnabled: true
securityContext:
runAsUser: 1000
fsGroup: 1000
kubernetesConfig:
image: quay.io/opstree/redis:v7.0.5
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 101m
memory: 128Mi
limits:
cpu: 101m
memory: 128Mi
redisSecret:
generatePasswordSecret:
name: redis-secret-cluster
namespace :
- redis-operator
key : operator-key # Default is set to 'key'
# existRedisSecret:
# name: redis-secret
# key: password
# imagePullSecrets:
# - name: regcred
# redisExporter:
# enabled: false
# image: quay.io/opstree/redis-exporter:v1.44.0
# imagePullPolicy: Always
# resources:
# requests:
# cpu: 100m
# memory: 128Mi
# limits:
# cpu: 100m
# memory: 128Mi
# Environment Variables for Redis Exporter
# env:
# - name: REDIS_EXPORTER_INCL_SYSTEM_METRICS
# value: "true"
# - name: UI_PROPERTIES_FILE_NAME
# valueFrom:
# configMapKeyRef:
# name: game-demo
# key: ui_properties_file_name
# - name: SECRET_USERNAME
# valueFrom:
# secretKeyRef:
# name: mysecret
# key: username
# redisLeader:
# redisConfig:
# additionalRedisConfig: redis-external-config
# redisFollower:
# redisConfig:
# additionalRedisConfig: redis-external-config
storage:
volumeClaimTemplate:
spec:
# storageClassName: standard
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
# nodeSelector:
# kubernetes.io/hostname: minikube
# priorityClassName:
# Affinity:
# Tolerations: []
72 changes: 72 additions & 0 deletions example/generated_secrets/redis-standalone.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
apiVersion: redis.redis.opstreelabs.in/v1beta1
kind: Redis
metadata:
name: redis-standalone
namespace: redis-operator
spec:
# redisConfig:
# additionalRedisConfig: redis-external-config\
securityContext:
runAsUser: 1000
fsGroup: 1000
kubernetesConfig:
image: quay.io/opstree/redis:v7.0.5
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 101m
memory: 128Mi
limits:
cpu: 101m
memory: 128Mi
redisSecret:
generatePasswordSecret:
name: redis-secret-standalone
namespace :
- redis-operator
key : operator-key # Default is set to 'key'
# existRedisSecret:
# name: redis-secret
# key: password
# imagePullSecrets:
# - name: regcred
# redisExporter:
# enabled: false
# image: quay.io/opstree/redis-exporter:v1.44.0
# imagePullPolicy: Always
# resources:
# requests:
# cpu: 100m
# memory: 128Mi
# limits:
# cpu: 100m
# memory: 128Mi
# Environment Variables for Redis Exporter
# env:
# - name: REDIS_EXPORTER_INCL_SYSTEM_METRICS
# value: "true"
# - name: UI_PROPERTIES_FILE_NAME
# valueFrom:
# configMapKeyRef:
# name: game-demo
# key: ui_properties_file_name
# - name: SECRET_USERNAME
# valueFrom:
# secretKeyRef:
# name: mysecret
# key: username
storage:
volumeClaimTemplate:
spec:
# storageClassName: standard
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
# nodeSelector:
# kubernetes.io/hostname: minikube
# securityContext: {}
# priorityClassName:
# affinity:
# Tolerations: []
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/banzaicloud/k8s-objectmatcher v1.7.0
github.com/go-logr/logr v1.2.2
github.com/go-redis/redis v6.15.9+incompatible
github.com/google/uuid v1.3.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
k8s.io/api v0.23.0
Expand Down Expand Up @@ -35,7 +36,6 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
Loading