Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 875f14e

Browse files
author
odacremolbap
committed
support external redis instance
1 parent 04261e8 commit 875f14e

File tree

7 files changed

+134
-28
lines changed

7 files changed

+134
-28
lines changed

config/300-redisbroker.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ spec:
8282
type: string
8383
key:
8484
type: string
85+
tlsEnabled:
86+
description: Use TLS enctrypted Redis connection.
87+
type: boolean
88+
tlsSkipVerify:
89+
description: Skip TLS certificate verification.
90+
type: boolean
8591
required:
8692
- url
8793

pkg/apis/eventing/v1alpha1/deepcopy_generated.go

Lines changed: 28 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/eventing/v1alpha1/redisbroker_lifecycle.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const (
2727
RedisBrokerBrokerServiceEndpointsConditionReady apis.ConditionType = "BrokerEndpointsReady"
2828
RedisBrokerConfigSecret apis.ConditionType = "BrokerConfigSecretReady"
2929
RedisBrokerConditionAddressable apis.ConditionType = "Addressable"
30+
31+
RedisBrokerReasonUserProvided string = "ReasonUserProvidedRedis"
3032
)
3133

3234
var redisBrokerCondSet = apis.NewLivingConditionSet(
@@ -59,6 +61,14 @@ func (b *RedisBroker) GetConditionSet() apis.ConditionSet {
5961
return redisBrokerCondSet
6062
}
6163

64+
// IsExternalRedis returns if the Redis instance is user provided.
65+
func (b *RedisBroker) IsUserProvidedRedis() bool {
66+
if b.Spec.Redis != nil && b.Spec.Redis.Connection != nil {
67+
return true
68+
}
69+
return false
70+
}
71+
6272
// GetConditionSet retrieves the condition set for this resource.
6373
func (bs *RedisBrokerStatus) GetConditionSet() apis.ConditionSet {
6474
redisBrokerCondSetLock.RLock()
@@ -215,3 +225,9 @@ func (bs *RedisBrokerStatus) MarkBrokerEndpointsUnknown(reason, messageFormat st
215225
func (bs *RedisBrokerStatus) MarkBrokerEndpointsTrue() {
216226
redisBrokerCondSet.Manage(bs).MarkTrue(RedisBrokerBrokerServiceEndpointsConditionReady)
217227
}
228+
229+
func (bs *RedisBrokerStatus) MarkRedisUserProvided() {
230+
redisBrokerCondSet.Manage(bs).MarkTrueWithReason(RedisBrokerRedisDeployment, RedisBrokerReasonUserProvided, "Redis instance is externally provided")
231+
redisBrokerCondSet.Manage(bs).MarkTrueWithReason(RedisBrokerRedisService, RedisBrokerReasonUserProvided, "Redis instance is externally provided")
232+
redisBrokerCondSet.Manage(bs).MarkTrueWithReason(RedisBrokerRedisServiceEndpointsConditionReady, RedisBrokerReasonUserProvided, "Redis instance is externally provided")
233+
}

pkg/apis/eventing/v1alpha1/redisbroker_types.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
corev1 "k8s.io/api/core/v1"
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
"k8s.io/apimachinery/pkg/runtime"
10-
"knative.dev/pkg/apis"
1110
duckv1 "knative.dev/pkg/apis/duck/v1"
1211
"knative.dev/pkg/kmeta"
1312
)
@@ -46,16 +45,24 @@ var (
4645

4746
type RedisConnection struct {
4847
// Redis URL.
49-
URL apis.URL `json:"url"`
48+
URL string `json:"url"`
49+
5050
// Redis username.
51-
Username SecretValueFromSource `json:"username,omitempty"`
51+
Username *SecretValueFromSource `json:"username,omitempty"`
52+
5253
// Redis password.
53-
Password SecretValueFromSource `json:"password,omitempty"`
54+
Password *SecretValueFromSource `json:"password,omitempty"`
55+
56+
// Use TLS enctrypted connection.
57+
TLSEnabled *bool `json:"tlsEnabled,omitempty"`
58+
59+
// Skip TLS certificate verification.
60+
TLSSkipVerify *bool `json:"tlsSkipVerify,omitempty"`
5461
}
5562

5663
type Redis struct {
5764
// Redis connection data.
58-
Connection RedisConnection `json:"connection,omitempty"`
65+
Connection *RedisConnection `json:"connection,omitempty"`
5966

6067
// Stream name used by the broker.
6168
Stream *string `json:"stream,omitempty"`
@@ -67,7 +74,7 @@ type Redis struct {
6774
// SecretValueFromSource represents the source of a secret value
6875
type SecretValueFromSource struct {
6976
// The Secret key to select from.
70-
SecretKeyRef *corev1.SecretKeySelector `json:"secretKeyRef,omitempty"`
77+
SecretKeyRef corev1.SecretKeySelector `json:"secretKeyRef,omitempty"`
7178
}
7279

7380
type Broker struct {

pkg/reconciler/redisbroker/reconcile_broker.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,51 @@ func buildBrokerDeployment(rb *eventingv1alpha1.RedisBroker, redis *corev1.Servi
7070
vm := resources.NewVolumeMount("config", configSecretPath,
7171
resources.VolumeMountWithReadOnlyOption(true))
7272

73-
redisService := fmt.Sprintf("%s:%d", redis.Name, redis.Spec.Ports[0].Port)
73+
var stream string
74+
if rb.Spec.Redis != nil && rb.Spec.Redis.Stream != nil && *rb.Spec.Redis.Stream != "" {
75+
stream = *rb.Spec.Redis.Stream
76+
} else {
77+
stream = rb.Namespace + "." + rb.Name
78+
}
79+
80+
opts := []resources.ContainerOption{
81+
resources.ContainerAddArgs("start"),
82+
resources.ContainerAddVolumeMount(vm),
83+
resources.ContainerAddEnvFromValue("BROKER_CONFIG_PATH", configMountedPath),
84+
resources.ContainerAddEnvFromValue("REDIS_STREAM", stream),
85+
}
86+
87+
if rb.Spec.Redis != nil && rb.Spec.Redis.StreamMaxLen != nil && *rb.Spec.Redis.StreamMaxLen != 0 {
88+
opts = append(opts, resources.ContainerAddEnvFromValue("REDIS_STREAM_MAXLEN", stream))
89+
}
90+
91+
if rb.IsUserProvidedRedis() {
92+
opts = append(opts, resources.ContainerAddEnvFromValue("REDIS_ADDRESS", rb.Spec.Redis.Connection.URL))
93+
94+
if rb.Spec.Redis.Connection.Username != nil {
95+
opts = append(opts, resources.ContainerAddEnvVarFromSecret("REDIS_USERNAME",
96+
rb.Spec.Redis.Connection.Username.SecretKeyRef.Name,
97+
rb.Spec.Redis.Connection.Username.SecretKeyRef.Key))
98+
}
99+
100+
if rb.Spec.Redis.Connection.Password != nil {
101+
opts = append(opts, resources.ContainerAddEnvVarFromSecret("REDIS_PASSWORD",
102+
rb.Spec.Redis.Connection.Password.SecretKeyRef.Name,
103+
rb.Spec.Redis.Connection.Password.SecretKeyRef.Key))
104+
}
105+
106+
if rb.Spec.Redis.Connection.TLSEnabled != nil && *rb.Spec.Redis.Connection.TLSEnabled {
107+
opts = append(opts, resources.ContainerAddEnvFromValue("REDIS_TLS_ENABLED", "true"))
108+
}
109+
110+
if rb.Spec.Redis.Connection.TLSSkipVerify != nil && *rb.Spec.Redis.Connection.TLSSkipVerify {
111+
opts = append(opts, resources.ContainerAddEnvFromValue("REDIS_TLS_SKIP_VERIFY", "true"))
112+
}
113+
114+
} else {
115+
opts = append(opts, resources.ContainerAddEnvFromValue("REDIS_ADDRESS",
116+
fmt.Sprintf("%s:%d", redis.Name, redis.Spec.Ports[0].Port)))
117+
}
74118

75119
return resources.NewDeployment(rb.Namespace, rb.Name+"-"+brokerResourceSuffix,
76120
resources.DeploymentWithMetaOptions(
@@ -83,15 +127,7 @@ func buildBrokerDeployment(rb *eventingv1alpha1.RedisBroker, redis *corev1.Servi
83127
resources.DeploymentWithTemplateOptions(
84128
resources.PodSpecAddVolume(v),
85129
resources.PodSpecAddContainer(
86-
resources.NewContainer("broker", image,
87-
resources.ContainerAddArgs("start"+
88-
" --redis.address "+redisService+
89-
" --broker-config-path "+configMountedPath+
90-
" --redis.stream "+rb.Namespace+"."+rb.Name),
91-
resources.ContainerAddVolumeMount(vm),
92-
),
93-
),
94-
))
130+
resources.NewContainer("broker", image, opts...))))
95131
}
96132

97133
func (r *brokerReconciler) reconcileDeployment(ctx context.Context, rb *eventingv1alpha1.RedisBroker, redis *corev1.Service, secret *corev1.Secret) (*appsv1.Deployment, error) {

pkg/reconciler/redisbroker/reconcile_redis.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ type redisReconciler struct {
3535
}
3636

3737
func (r *redisReconciler) reconcile(ctx context.Context, rb *eventingv1alpha1.RedisBroker) (*appsv1.Deployment, *corev1.Service, error) {
38+
if rb.IsUserProvidedRedis() {
39+
// Nothing to do but mark the status for each of the elements reconciled.
40+
rb.Status.MarkRedisUserProvided()
41+
return nil, nil, nil
42+
}
43+
3844
d, err := r.reconcileDeployment(ctx, rb)
3945
if err != nil {
4046
return nil, nil, err

pkg/reconciler/resources/container.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ func ContainerAddEnvFromValue(name, value string) ContainerOption {
3636
}
3737
}
3838

39+
func ContainerAddEnvVarFromSecret(name, secretName, secretKey string) ContainerOption {
40+
return func(c *corev1.Container) {
41+
if c.Env == nil {
42+
c.Env = make([]corev1.EnvVar, 0, 1)
43+
}
44+
c.Env = append(c.Env, corev1.EnvVar{
45+
Name: name,
46+
ValueFrom: &corev1.EnvVarSource{
47+
SecretKeyRef: &corev1.SecretKeySelector{
48+
LocalObjectReference: corev1.LocalObjectReference{
49+
Name: secretName,
50+
},
51+
Key: secretKey,
52+
},
53+
},
54+
})
55+
}
56+
}
57+
3958
func ContainerAddArgs(s string) ContainerOption {
4059
return func(c *corev1.Container) {
4160
args := strings.Split(s, " ")

0 commit comments

Comments
 (0)