Skip to content

Commit 9971e8a

Browse files
authored
feat: support runtime configuration of init container image (#1586)
* feat: support runtime configuration of init container image This change provides better flexibility for managing operator and init container versions independently without breaking existing configurations. Signed-off-by: drivebyer <[email protected]> * remove comment Signed-off-by: drivebyer <[email protected]> * update Signed-off-by: drivebyer <[email protected]> --------- Signed-off-by: drivebyer <[email protected]>
1 parent 566db6f commit 9971e8a

File tree

15 files changed

+55
-44
lines changed

15 files changed

+55
-44
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ jobs:
260260
- name: Install Redis Operator
261261
run: |
262262
make deploy IMG=redis-operator:e2e
263-
kubectl set env deployment/redis-operator-redis-operator -n redis-operator-system OPERATOR_IMAGE=redis-operator:e2e FEATURE_GATES=GenerateConfigInInitContainer=true MAX_CONCURRENT_RECONCILES=10
263+
kubectl set env deployment/redis-operator-redis-operator -n redis-operator-system INIT_CONTAINER_IMAGE=redis-operator:e2e FEATURE_GATES=GenerateConfigInInitContainer=true MAX_CONCURRENT_RECONCILES=10
264264
265265
- name: Wait for Redis Operator to be ready
266266
run: |

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ COPY internal/ internal/
2525
COPY mocks/ mocks/
2626

2727
# Build
28-
ARG LDFLAGS="-s -w -X github.com/OT-CONTAINER-KIT/redis-operator/internal/image.operatorImage=${IMG}"
28+
ARG LDFLAGS="-s -w"
2929
ENV GOOS=$TARGETOS
3030
ENV GOARCH=$TARGETARCH
3131
ENV CGO_ENABLED=0

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ CONTAINER_ENGINE ?= docker
2121
PLATFORMS = "linux/arm64,linux/amd64"
2222

2323
# LDFLAGS for setting the operator image during build
24-
LDFLAGS ?= "-s -w -X github.com/OT-CONTAINER-KIT/redis-operator/internal/image.operatorImage=$(IMG)"
24+
LDFLAGS ?= "-s -w"
2525

2626
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
2727
ifeq (,$(shell go env GOBIN))

charts/redis-operator/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ kubectl create secret tls <webhook-server-cert> --key tls.key --cert tls.crt -n
115115
| redisOperator.imagePullPolicy | string | `"Always"` | |
116116
| redisOperator.imagePullSecrets | list | `[]` | |
117117
| redisOperator.imageTag | string | `""` | |
118+
| redisOperator.initContainerImageTag | string | `"v0.22.2"` | initContainerImageTag is the init-config init container image tag. If not specified, defaults to imageTag, then falls back to chart appVersion. Typically only needs to be set when using a different version for the init container. |
118119
| redisOperator.metrics.bindAddress | string | `":8080"` | |
119120
| redisOperator.metrics.enabled | bool | `true` | |
120121
| redisOperator.name | string | `"redis-operator"` | |

charts/redis-operator/templates/operator-deployment.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ spec:
8686
readOnly: true
8787
{{- end }}
8888
env:
89-
- name: OPERATOR_IMAGE
90-
value: {{ .Values.redisOperator.imageName }}:{{ .Values.redisOperator.imageTag | default (printf "v%s" .Chart.AppVersion) }}
89+
- name: INIT_CONTAINER_IMAGE
90+
value: {{ .Values.redisOperator.imageName }}:{{ .Values.redisOperator.initContainerImageTag | default .Values.redisOperator.imageTag | default (printf "v%s" .Chart.AppVersion) }}
9191
- name: ENABLE_WEBHOOKS
9292
value: {{ .Values.redisOperator.webhook | quote }}
9393
{{- if .Values.redisOperator.watchNamespace }}

charts/redis-operator/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ redisOperator:
44
imageName: quay.io/opstree/redis-operator
55
# Overrides the image tag whose default is the chart appVersion.
66
imageTag: ""
7+
# -- initContainerImageTag is the init-config init container image tag.
8+
# If not specified, defaults to imageTag, then falls back to chart appVersion.
9+
# Typically only needs to be set when using a different version for the init container.
10+
initContainerImageTag: "v0.22.2"
711
imagePullPolicy: Always
812
imagePullSecrets: []
913

internal/cmd/manager/cmd.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
redisreplicationcontroller "github.com/OT-CONTAINER-KIT/redis-operator/internal/controller/redisreplication"
3333
redissentinelcontroller "github.com/OT-CONTAINER-KIT/redis-operator/internal/controller/redissentinel"
3434
intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/internal/controllerutil"
35-
internalenv "github.com/OT-CONTAINER-KIT/redis-operator/internal/env"
35+
"github.com/OT-CONTAINER-KIT/redis-operator/internal/envs"
3636
"github.com/OT-CONTAINER-KIT/redis-operator/internal/features"
3737
"github.com/OT-CONTAINER-KIT/redis-operator/internal/k8sutils"
3838
"github.com/OT-CONTAINER-KIT/redis-operator/internal/monitoring"
@@ -95,9 +95,9 @@ func addFlags(cmd *cobra.Command, opts *managerOptions) {
9595
cmd.Flags().StringVar(&opts.probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
9696
cmd.Flags().StringVar(&opts.pprofAddr, "pprof-bind-address", "", "The address the pprof endpoint binds to. If empty, pprof is disabled. Example: ':6060'")
9797
cmd.Flags().BoolVar(&opts.enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
98-
cmd.Flags().BoolVar(&opts.enableWebhooks, "enable-webhooks", internalenv.IsWebhookEnabled(), "Enable webhooks")
98+
cmd.Flags().BoolVar(&opts.enableWebhooks, "enable-webhooks", envs.IsWebhookEnabled(), "Enable webhooks")
9999
cmd.Flags().IntVar(&opts.maxConcurrentReconciles, "max-concurrent-reconciles", 1, "Max concurrent reconciles")
100-
cmd.Flags().StringVar(&opts.featureGatesString, "feature-gates", internalenv.GetFeatureGates(), "A set of key=value pairs that describe feature gates for alpha/experimental features. "+
100+
cmd.Flags().StringVar(&opts.featureGatesString, "feature-gates", envs.GetFeatureGates(), "A set of key=value pairs that describe feature gates for alpha/experimental features. "+
101101
"Options are:\n GenerateConfigInInitContainer=true|false: enables using init container for config generation")
102102
cmd.Flags().Duration(
103103
operator.KubeClientTimeoutMGRFlag,
@@ -200,7 +200,7 @@ func createControllerOptions(opts *managerOptions) ctrl.Options {
200200
options.PprofBindAddress = opts.pprofAddr
201201
}
202202

203-
watchNamespaces := internalenv.GetWatchNamespaces()
203+
watchNamespaces := envs.GetWatchNamespaces()
204204
if len(watchNamespaces) > 0 {
205205
options.Cache.DefaultNamespaces = map[string]cache.Config{}
206206
for _, ns := range watchNamespaces {
@@ -227,7 +227,7 @@ func createK8sClient() (kubernetes.Interface, error) {
227227
// setupControllers sets up all controllers
228228
func setupControllers(mgr ctrl.Manager, k8sClient kubernetes.Interface, maxConcurrentReconciles int) error {
229229
// Get max concurrent reconciles from environment
230-
maxConcurrentReconciles = internalenv.GetMaxConcurrentReconciles(maxConcurrentReconciles)
230+
maxConcurrentReconciles = envs.GetMaxConcurrentReconciles(maxConcurrentReconciles)
231231

232232
healer := redis.NewHealer(k8sClient)
233233

internal/controller/common/redis/heal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
commonapi "github.com/OT-CONTAINER-KIT/redis-operator/api/common/v1beta2"
1111
rsvb2 "github.com/OT-CONTAINER-KIT/redis-operator/api/redissentinel/v1beta2"
1212
"github.com/OT-CONTAINER-KIT/redis-operator/internal/controller/common"
13-
"github.com/OT-CONTAINER-KIT/redis-operator/internal/env"
13+
"github.com/OT-CONTAINER-KIT/redis-operator/internal/envs"
1414
"github.com/OT-CONTAINER-KIT/redis-operator/internal/service/redis"
1515
"github.com/OT-CONTAINER-KIT/redis-operator/internal/util/cryptutil"
1616
v1 "k8s.io/api/core/v1"
@@ -255,7 +255,7 @@ func createConnectionInfo(ctx context.Context, pod v1.Pod, password string, tlsC
255255
// Configure TLS if enabled
256256
if tlsConfig != nil && tlsConfig.Secret.SecretName != "" {
257257
serviceName := common.GetHeadlessServiceNameFromPodName(pod.Name)
258-
connInfo.Host = fmt.Sprintf("%s.%s.%s.svc.%s", pod.Name, serviceName, namespace, env.GetServiceDNSDomain())
258+
connInfo.Host = fmt.Sprintf("%s.%s.%s.svc.%s", pod.Name, serviceName, namespace, envs.GetServiceDNSDomain())
259259
// Get TLS configuration
260260
tlsCfg := getRedisTLSConfig(ctx, k8sClient, namespace, tlsConfig.Secret.SecretName)
261261
connInfo.TLSConfig = tlsCfg

internal/controller/redissentinel/redissentinel_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/OT-CONTAINER-KIT/redis-operator/internal/controller/common"
1111
"github.com/OT-CONTAINER-KIT/redis-operator/internal/controller/common/redis"
1212
intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/internal/controllerutil"
13-
"github.com/OT-CONTAINER-KIT/redis-operator/internal/env"
13+
"github.com/OT-CONTAINER-KIT/redis-operator/internal/envs"
1414
"github.com/OT-CONTAINER-KIT/redis-operator/internal/k8sutils"
1515
appsv1 "k8s.io/api/apps/v1"
1616
"k8s.io/apimachinery/pkg/types"
@@ -133,7 +133,7 @@ func (r *RedisSentinelReconciler) reconcileSentinel(ctx context.Context, instanc
133133
return intctrlutil.RequeueE(ctx, err, "")
134134
} else {
135135
if instance.Spec.RedisSentinelConfig.ResolveHostnames == "yes" {
136-
monitorAddr = fmt.Sprintf("%s.%s.%s.svc.%s", master.Name, common.GetHeadlessServiceNameFromPodName(master.Name), rr.Namespace, env.GetServiceDNSDomain())
136+
monitorAddr = fmt.Sprintf("%s.%s.%s.svc.%s", master.Name, common.GetHeadlessServiceNameFromPodName(master.Name), rr.Namespace, envs.GetServiceDNSDomain())
137137
} else {
138138
monitorAddr = master.Status.PodIP
139139
}

internal/env/env.go renamed to internal/envs/envs.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package env
17+
package envs
1818

1919
import (
2020
"os"
2121
"strconv"
2222
"strings"
23+
"sync"
2324

2425
"github.com/OT-CONTAINER-KIT/redis-operator/internal/util"
2526
)
@@ -38,19 +39,30 @@ const (
3839
// FeatureGatesEnv defines feature gates for alpha/experimental features
3940
FeatureGatesEnv = "FEATURE_GATES"
4041

41-
// OperatorImageEnv defines the image of the operator
42-
OperatorImageEnv = "OPERATOR_IMAGE"
42+
// InitContainerImageEnv defines the image used for init containers
43+
InitContainerImageEnv = "INIT_CONTAINER_IMAGE"
4344

45+
// ServiceDNSDomain defines the DNS domain suffix for Kubernetes services
4446
ServiceDNSDomain = "SERVICE_DNS_DOMAIN"
4547
)
4648

47-
func GetServiceDNSDomain() string {
48-
return util.Coalesce(os.Getenv(ServiceDNSDomain), "cluster.local")
49+
var (
50+
initContainerImage string
51+
initContainerImageOnce sync.Once
52+
)
53+
54+
// GetInitContainerImage returns the image to use for init containers.
55+
func GetInitContainerImage() string {
56+
initContainerImageOnce.Do(func() {
57+
val := os.Getenv(InitContainerImageEnv)
58+
initContainerImage = util.Coalesce(val, "quay.io/opstree/redis-operator:latest")
59+
})
60+
return initContainerImage
4961
}
5062

51-
// GetOperatorImage returns the image of the operator
52-
func GetOperatorImage() string {
53-
return os.Getenv(OperatorImageEnv)
63+
// GetServiceDNSDomain returns the Kubernetes service DNS domain suffix.
64+
func GetServiceDNSDomain() string {
65+
return util.Coalesce(os.Getenv(ServiceDNSDomain), "cluster.local")
5466
}
5567

5668
// GetWatchNamespaces returns a list of namespaces that the operator should watch

0 commit comments

Comments
 (0)