Skip to content

Commit

Permalink
Merge pull request #5825 from ministryofjustice/ap-4499/remove-redis-…
Browse files Browse the repository at this point in the history
…namespace

AP-4499: Remove redis namespacing
  • Loading branch information
jsugarman authored Oct 19, 2023
2 parents 93d7a58 + 869ce27 commit 9032f5f
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 8 deletions.
1 change: 1 addition & 0 deletions .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SimpleCov.start "rails" do
add_filter "lib/tasks/helpers/cfe_payload_recorder.rb"
add_filter "lib/tasks/helpers/cy_helper.rb"
add_filter "lib/utilities/redis_scanner.rb"
add_filter "app/jobs/test_mail_job.rb"

enable_coverage :branch

Expand Down
19 changes: 19 additions & 0 deletions app/jobs/test_mail_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class TestMailJob
include Sidekiq::Worker
sidekiq_options queue: :default

CLIENT_CONFIRMATION_EMAIL = "3b18efe5-7d09-4042-927c-7d1ae03fdd7e".freeze

def perform(email, name)
client = Notifications::Client.new(Rails.configuration.x.govuk_notify_api_key)

client.send_email(
email_address: email,
template_id: CLIENT_CONFIRMATION_EMAIL,
personalisation: {
client_name: name,
ref_number: "testing-whatever",
},
)
end
end
11 changes: 10 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,16 @@ class Application < Rails::Application
production: [],
}

config.x.redis.base_url = ENV["REDIS_HOST"].present? && ENV["REDIS_PASSWORD"].present? ? "rediss://:#{ENV.fetch('REDIS_PASSWORD', nil)}@#{ENV.fetch('REDIS_HOST', nil)}:6379" : "redis://localhost:6379"
redis_protocol = ENV.fetch("REDIS_PROTOCOL", "rediss")
redis_password = ENV.fetch("REDIS_PASSWORD", nil)
redis_host = ENV.fetch("REDIS_HOST", nil)

redis_url = if redis_host.present? && redis_password.present?
"#{redis_protocol}://:#{redis_password}@#{redis_host}:6379"
else
"redis://localhost:6379"
end
config.x.redis.base_url = redis_url
config.x.redis.page_history_url = "#{config.x.redis.base_url}/1"
config.x.redis.oauth_session_url = "#{config.x.redis.base_url}/2"
config.x.redis.rack_attack_url = "#{config.x.redis.base_url}/3"
Expand Down
12 changes: 9 additions & 3 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
require "prometheus_exporter/client"
require "prometheus_exporter/instrumentation"

redis_url = "rediss://:#{ENV.fetch('REDIS_PASSWORD', nil)}@#{ENV.fetch('REDIS_HOST', nil)}:6379" if ENV["REDIS_HOST"].present? && ENV["REDIS_PASSWORD"].present?
redis_url = Rails.configuration.x.redis.base_url
namespace = ENV.fetch("HOST", "laa-apply")

module Dashboard; end

Sidekiq.configure_client do |config|
config.redis = { url: redis_url, namespace: } if redis_url
config.redis = { url: redis_url } if redis_url

# accepts :expiration (optional)
Sidekiq::Status.configure_client_middleware config, expiration: 30.minutes
end

Sidekiq.configure_server do |config|
config.redis = { url: redis_url, namespace: } if redis_url
if redis_url
config.redis = if ENV.fetch("NAMEPSPACED_SIDEKIQ_DRAINER", nil)
{ url: redis_url, namespace: } # continue to poll for old scheduled jobs and retries
else
{ url: redis_url }
end
end

# accepts :expiration (optional)
Sidekiq::Status.configure_server_middleware config, expiration: 30.minutes
Expand Down
7 changes: 5 additions & 2 deletions helm_deploy/apply-for-legal-aid/Chart.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ dependencies:
- name: postgresql
repository: https://charts.bitnami.com/bitnami
version: 11.9.13
digest: sha256:63f0418b276607f2047c1ea7d149ac904de530404e74ef4f7558da6f645137b9
generated: "2023-02-20T17:20:40.628833Z"
- name: redis
repository: https://charts.bitnami.com/bitnami
version: 16.13.2
digest: sha256:334d2a8d2b464929a6c9534bdd4efb53e3f1c9a9a3f172c7851286dcfeda53fd
generated: "2023-10-13T14:00:08.90648+01:00"
10 changes: 8 additions & 2 deletions helm_deploy/apply-for-legal-aid/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# see https://levelup.gitconnected.com/helm-dependencies-1907facbe410
# for decent documentation.
apiVersion: v2
name: apply-for-legal-aid
description: A Helm chart for Kubernetes
Expand All @@ -15,15 +17,19 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.2.0
version: 0.3.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
appVersion: 1.17.0
appVersion: 1.18.0

dependencies:
- name: postgresql
version: ^11.0.0
condition: postgresql.enabled
repository: https://charts.bitnami.com/bitnami
- name: redis
version: ^16.0.0 # redis 6.x.x
condition: redis.enabled
repository: https://charts.bitnami.com/bitnami
Binary file not shown.
12 changes: 12 additions & 0 deletions helm_deploy/apply-for-legal-aid/templates/_envs.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ env:
secretKeyRef:
name: {{ template "apply-for-legal-aid.fullname" . }}
key: sidekiqWebUiPassword
{{ if .Values.redis.enabled }}
- name: REDIS_PROTOCOL
value: "redis"
- name: REDIS_HOST
value: {{ template "apply-for-legal-aid.redis-uat-host" . }}
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: {{ template "apply-for-legal-aid.fullname" . }}
key: redisPassword
{{ else }}
- name: REDIS_HOST
valueFrom:
secretKeyRef:
Expand All @@ -230,6 +241,7 @@ env:
secretKeyRef:
name: apply-for-legal-aid-elasticache-instance-output
key: auth_token
{{ end }}
- name: S3_BUCKET
valueFrom:
secretKeyRef:
Expand Down
17 changes: 17 additions & 0 deletions helm_deploy/apply-for-legal-aid/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ https://pauladamsmith.com/blog/2011/05/go_time.html
{{ "* * * * *" }}
{{- end -}}
{{- end -}}

{{/*
Function to return the name for a UAT redis chart master node host
This duplicates bitnami/redis chart's internal logic whereby
If branch name contains "redis" then the redis-release-name appends "-master", otherwise it appends "-redis-master"
*/}}
{{- define "apply-for-legal-aid.redis-uat-host" -}}
{{- $redis_chart_name := "redis" -}}
{{- if contains $redis_chart_name .Release.Name -}}
{{- $redis_release_name := (.Release.Name | trunc 63 | trimSuffix "-") -}}
{{- printf "%s-master.%s.svc.cluster.local" $redis_release_name .Release.Namespace -}}
{{- else -}}
{{- $redis_release_name := (printf "%s-%s" .Release.Name $redis_chart_name | trunc 63 | trimSuffix "-") -}}
{{- printf "%s-master.%s.svc.cluster.local" $redis_release_name .Release.Namespace -}}
{{- end -}}
{{- end -}}
59 changes: 59 additions & 0 deletions helm_deploy/apply-for-legal-aid/templates/deployment_drainer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ template "apply-for-legal-aid.fullname" . }}-drainer
labels:
app: {{ template "apply-for-legal-aid.name" . }}
chart: {{ template "apply-for-legal-aid.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
replicas: {{ .Values.worker.replicaCount }}
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 100%
selector:
matchLabels:
app: {{ template "apply-for-legal-aid.name" . }}-drainer
release: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ template "apply-for-legal-aid.name" . }}-drainer
release: {{ .Release.Name }}
spec:
serviceAccountName: "{{ .Values.service_account.name }}"
containers:
- name: drainer
image: '{{ .Values.image.repository }}:{{ .Values.image.tag }}'
imagePullPolicy: IfNotPresent
command: ['bundle', 'exec', 'sidekiq']
{{ include "apply-for-legal-aid.envs" . | nindent 10 }}
- name: NAMEPSPACED_SIDEKIQ_DRAINER
value: "true"
resources:
limits:
cpu: 500m
memory: 1Gi
requests:
cpu: 10m
memory: 512Mi
ports:
- containerPort: 7433
livenessProbe:
exec:
command: ['bin/sidekiq_health_check']
initialDelaySeconds: 60
timeoutSeconds: 5
readinessProbe:
exec:
command: ['bin/sidekiq_health_check']
initialDelaySeconds: 60
timeoutSeconds: 5
lifecycle:
preStop:
exec:
command: ['bundle', 'exec', 'sidekiqctl', 'quiet']
terminationGracePeriodSeconds: 60
3 changes: 3 additions & 0 deletions helm_deploy/apply-for-legal-aid/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ data:
postgresqlUsername: {{ .Values.postgresql.postgresqlUsername | b64enc | quote }}
postgresqlPassword: {{ .Values.postgresql.auth.postgresPassword | b64enc | quote }}
{{ end }}
{{if .Values.redis.enabled }}
redisPassword: {{ .Values.redis.auth.password | b64enc | quote }}
{{ end }}
sentryDsn: {{ .Values.sentry.dsn | b64enc | quote }}
govukNotifyApiKey: {{ .Values.govuk_notify.api_key | b64enc | quote }}
ordnanceSurveyApiKey: {{ .Values.ordnance_survey.api_key | b64enc | quote }}
Expand Down
Binary file modified helm_deploy/apply-for-legal-aid/values-production.yaml
Binary file not shown.
Binary file modified helm_deploy/apply-for-legal-aid/values-staging.yaml
Binary file not shown.
Binary file modified helm_deploy/apply-for-legal-aid/values-uat.yaml
Binary file not shown.

0 comments on commit 9032f5f

Please sign in to comment.