Skip to content

Commit 29a79fd

Browse files
committedDec 3, 2021
Replacing docker-compose by k3s
1 parent 8a8f520 commit 29a79fd

File tree

11 files changed

+406
-21
lines changed

11 files changed

+406
-21
lines changed
 

‎.env.default

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
COMPOSE_FILE=./docker/docker-compose.yml:./docker/docker-compose.override.yml
2-
COMPOSE_PROJECT_NAME=projectname
1+
PROJECT_NAME=projectname
32
PROFILE_NAME=druxxy
43
THEME_NAME=NA
54
SITE_NAME=Example

‎Makefile

+34-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Add utility functions and scripts to the container
1+
# Include utility functions and scripts
22
include scripts/makefile/*.mk
33

44
.PHONY: all fast allfast provision si exec exec0 down clean dev drush info phpcs phpcbf hooksymlink clang cinsp compval watchdogval drupalrectorval upgradestatusval behat sniffers tests front front-install front-build clear-front lintval lint storybook back behatdl behatdi browser_driver browser_driver_stop statusreportval contentgen newlineeof localize local-settings redis-settings content patchval diff
@@ -10,9 +10,14 @@ include scripts/makefile/*.mk
1010

1111
# Prepare enviroment variables from defaults
1212
$(shell false | cp -i \.env.default \.env 2>/dev/null)
13-
$(shell false | cp -i \.\/docker\/docker-compose\.override\.yml\.default \.\/docker\/docker-compose\.override\.yml 2>/dev/null)
1413
include .env
1514

15+
# Sanitize PROJECT_NAME input
16+
COMPOSE_PROJECT_NAME := $(shell echo "$(PROJECT_NAME)" | tr -cd '[a-zA-Z0-9]' | tr '[:upper:]' '[:lower:]')
17+
18+
kk:
19+
helm install --kubeconfig=/etc/rancher/k3s/k3s.yaml --set projectName=$(COMPOSE_PROJECT_NAME) sdc ./helm/
20+
1621
# Get user/group id to manage permissions between host and containers
1722
LOCAL_UID := $(shell id -u)
1823
LOCAL_GID := $(shell id -g)
@@ -24,7 +29,7 @@ CGID ?= $(LOCAL_GID)
2429
# Define network name.
2530
COMPOSE_NET_NAME := $(COMPOSE_PROJECT_NAME)_front
2631

27-
SDC_SERVICES=$(shell docker-compose config --services)
32+
# SDC_SERVICES=$(shell docker-compose config --services) # TODO: Replace or remove
2833
# Determine database data directory if defined
2934
DB_MOUNT_DIR=$(shell cd docker && realpath $(DB_DATA_DIR))/
3035
ifeq ($(findstring mysql,$(SDC_SERVICES)),mysql)
@@ -38,10 +43,15 @@ endif
3843
CURDIR=$(shell pwd)
3944

4045
# Execute php container as regular user
41-
php = docker-compose exec -T --user $(CUID):$(CGID) php ${1}
46+
php = kubectl exec -it deploy/sdc -c php -- su -s /bin/ash www-data -c "${1}"
4247
# Execute php container as root user
43-
php-0 = docker-compose exec -T --user 0:0 php ${1}
48+
php-0 = kubectl exec -it deploy/sdc -c php -- ${1}
49+
50+
killall:
51+
/usr/local/bin/k3s-killall.sh
52+
/usr/local/bin/k3s-uninstall.sh
4453

54+
# Variables
4555
ADDITIONAL_PHP_PACKAGES := tzdata graphicsmagick # php7-intl php7-redis wkhtmltopdf gnu-libiconv php7-pdo_pgsql postgresql-client postgresql-contrib
4656
DC_MODULES := project_default_content better_normalizers default_content hal serialization
4757
MG_MODULES := migrate_generator migrate migrate_plus migrate_source_csv migrate_tools
@@ -63,17 +73,25 @@ provision:
6373
# Check if enviroment variables has been defined
6474
ifeq ($(strip $(COMPOSE_PROJECT_NAME)),projectname)
6575
$(info Project name can not be default, please enter project name.)
66-
$(eval COMPOSE_PROJECT_NAME = $(strip $(shell read -p "Project name: " REPLY;echo -n $$REPLY)))
76+
$(eval COMPOSE_PROJECT_NAME = $(strip $(shell read -p "Project name: " REPLY;echo -n $$REPLY))) # TODO: Sanitize lowercase/nospecialchar
6777
$(shell sed -i -e '/COMPOSE_PROJECT_NAME=/ s/=.*/=$(COMPOSE_PROJECT_NAME)/' .env)
6878
$(info Please review your project settings and run `make all` again.)
6979
exit 1
7080
endif
7181
ifdef DB_MOUNT_DIR
7282
$(shell [ ! -d $(DB_MOUNT_DIR) ] && mkdir -p $(DB_MOUNT_DIR) && chmod 777 $(DB_MOUNT_DIR))
7383
endif
74-
make -s down
84+
# make -s down
85+
@echo "Downloading and installing container orchestrator..."
86+
curl -sfL https://get.k3s.io | K3S_NODE_NAME=sdc K3S_KUBECONFIG_MODE="644" sh - # TODO: Check behavior if k3s already install + lock version
87+
curl -sfL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | sh - # TODO: rm $(which helm)
88+
# kubectl config view --raw >~/.kube/config # TODO: Simlink to /etc/rancher/k3s/k3s.yaml OR use helm install --kubeconfig=/etc/rancher/k3s/k3s.yaml sdc ./kubernetes/sdc-chart
7589
@echo "Build and run containers..."
76-
docker-compose up -d --remove-orphans
90+
# TODO: Rename file
91+
# kubectl apply -f kubernetes/sdc.yaml
92+
helm install --kubeconfig=/etc/rancher/k3s/k3s.yaml --set projectName=$(COMPOSE_PROJECT_NAME) sdc ./helm/
93+
# helm install --kubeconfig=/etc/rancher/k3s/k3s.yaml sdc ./helm/ # https://github.com/k3s-io/k3s/issues/1126#issuecomment-567591888
94+
for i in {1..50}; do echo "Waiting for PHP container..." && kubectl exec -it deploy/sdc -c php -- "whoami" &> /dev/null && break || sleep 1; done; echo "Container is up !"
7795
# Set composer2 as default
7896
$(call php-0, ln -fs composer2 /usr/bin/composer)
7997
ifneq ($(strip $(ADDITIONAL_PHP_PACKAGES)),)
@@ -157,37 +175,31 @@ localize:
157175
info:
158176
$(info )
159177
$(info Containers for "$(COMPOSE_PROJECT_NAME)" info:)
160-
$(eval CONTAINERS = $(shell docker ps -f name=$(COMPOSE_PROJECT_NAME) --format "{{ .ID }}" -f 'label=traefik.enable=true'))
161-
$(foreach CONTAINER, $(CONTAINERS),$(info http://$(shell printf '%-19s \n' $(shell docker inspect --format='{{(index .NetworkSettings.Networks "$(COMPOSE_NET_NAME)").IPAddress}}:{{index .Config.Labels "traefik.port"}} {{range $$p, $$conf := .NetworkSettings.Ports}}{{$$p}}{{end}} {{.Name}}' $(CONTAINER) | rev | sed "s/pct\//,pct:/g" | sed "s/,//" | rev | awk '{ print $0}')) ))
162178
$(info )
163179
ifdef REVIEW_DOMAIN
164180
$(eval BASE_URL := $(MAIN_DOMAIN_NAME))
165181
else
166-
$(eval BASE_URL := $(shell docker inspect --format='{{(index .NetworkSettings.Networks "$(COMPOSE_NET_NAME)").IPAddress}}:{{index .Config.Labels "traefik.port"}}' $(COMPOSE_PROJECT_NAME)_web))
182+
$(eval BASE_URL := $(shell kubectl get pods -l name=sdc --template '{{range .items}}{{.status.podIP}}{{"\n"}}{{end}}'))
167183
endif
168-
$(info Login as System Admin: http://$(shell printf '%-19s \n' $(shell echo "$(BASE_URL)"$(shell $(call php, drush user:login --name="$(ADMIN_NAME)" /admin/content/ | awk -F "default" '{print $$2}')))))
169-
$(info Login as Contributor: http://$(shell printf '%-19s \n' $(shell echo "$(BASE_URL)"$(shell $(call php, drush user:login --name="$(TESTER_NAME)" /admin/content/ | awk -F "default" '{print $$2}')))))
184+
$(info Login as System Admin: http://$(shell printf '%-19s \n' $(shell echo "$(BASE_URL)"$(shell $(call php, drush user:login --name="$(ADMIN_NAME)" /admin/content/ | awk -F "default" '{print \$$2}')))))
185+
$(info Login as Contributor: http://$(shell printf '%-19s \n' $(shell echo "$(BASE_URL)"$(shell $(call php, drush user:login --name="$(TESTER_NAME)" /admin/content/ | awk -F "default" '{print \$$2}')))))
170186
$(info )
171187
ifneq ($(shell diff .env .env.default -q),)
172188
@echo -e "\x1b[33mWARNING\x1b[0m - .env and .env.default files differ. Use 'make diff' to see details."
173189
endif
174-
ifneq ($(shell diff docker/docker-compose.override.yml docker/docker-compose.override.yml.default -q),)
175-
@echo -e "\x1b[33mWARNING\x1b[0m - docker/docker-compose.override.yml and docker/docker-compose.override.yml.default files differ. Use 'make diff' to see details."
176-
endif
177190

178191
## Output diff between local and versioned files
179192
diff:
180193
diff -u0 --color .env .env.default || true; echo ""
181-
diff -u0 --color docker/docker-compose.override.yml docker/docker-compose.override.yml.default || true; echo ""
182194

183195

184196
## Run shell in PHP container as regular user
185197
exec:
186-
docker-compose exec --user $(CUID):$(CGID) php ash
198+
kubectl exec -it deploy/sdc -c php -- su -s /bin/ash www-data -c ash
187199

188200
## Run shell in PHP container as root
189201
exec0:
190-
docker-compose exec --user 0:0 php ash
202+
kubectl exec -it deploy/sdc -c php -- ash
191203

192204
down:
193205
@echo "Removing network & containers for $(COMPOSE_PROJECT_NAME)"
@@ -232,3 +244,6 @@ dev:
232244
drush:
233245
$(call php, $(filter-out "$@",$(MAKECMDGOALS)))
234246
$(info "To pass arguments use double dash: "make drush en devel -- -y"")
247+
248+
logs:
249+
kubectl logs -f deploy/sdc --all-containers=true

‎helm/.helmignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*.orig
18+
*~
19+
# Various IDEs
20+
.project
21+
.idea/
22+
*.tmproj
23+
.vscode/

‎helm/Chart.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: v2
2+
name: sdc
3+
description: A Helm chart for Kubernetes
4+
5+
# A chart can be either an 'application' or a 'library' chart.
6+
#
7+
# Application charts are a collection of templates that can be packaged into versioned archives
8+
# to be deployed.
9+
#
10+
# Library charts provide useful utilities or functions for the chart developer. They're included as
11+
# a dependency of application charts to inject those utilities and functions into the rendering
12+
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
13+
type: application
14+
15+
# This is the chart version. This version number should be incremented each time you make changes
16+
# to the chart and its templates, including the app version.
17+
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18+
version: 0.1.0
19+
20+
# This is the version number of the application being deployed. This version number should be
21+
# incremented each time you make changes to the application. Versions are not expected to
22+
# follow Semantic Versioning. They should reflect the version the application is using.
23+
# It is recommended to use it with quotes.
24+
appVersion: "1.16.0"

‎helm/templates/_helpers.tpl

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{{/*
2+
Expand the name of the chart.
3+
*/}}
4+
{{- define "sdc.name" -}}
5+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
6+
{{- end }}
7+
8+
{{/*
9+
Create a default fully qualified app name.
10+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
11+
If release name contains chart name it will be used as a full name.
12+
*/}}
13+
{{- define "sdc.fullname" -}}
14+
{{- if .Values.fullnameOverride }}
15+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
16+
{{- else }}
17+
{{- $name := default .Chart.Name .Values.nameOverride }}
18+
{{- if contains $name .Release.Name }}
19+
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
20+
{{- else }}
21+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
22+
{{- end }}
23+
{{- end }}
24+
{{- end }}
25+
26+
{{/*
27+
Create chart name and version as used by the chart label.
28+
*/}}
29+
{{- define "sdc.chart" -}}
30+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
31+
{{- end }}
32+
33+
{{/*
34+
Common labels
35+
*/}}
36+
{{- define "sdc.labels" -}}
37+
helm.sh/chart: {{ include "sdc.chart" . }}
38+
{{ include "sdc.selectorLabels" . }}
39+
{{- if .Chart.AppVersion }}
40+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
41+
{{- end }}
42+
app.kubernetes.io/managed-by: {{ .Release.Service }}
43+
{{- end }}
44+
45+
{{/*
46+
Selector labels
47+
*/}}
48+
{{- define "sdc.selectorLabels" -}}
49+
app.kubernetes.io/name: {{ include "sdc.name" . }}
50+
app.kubernetes.io/instance: {{ .Release.Name }}
51+
{{- end }}
52+
53+
{{/*
54+
Create the name of the service account to use
55+
*/}}
56+
{{- define "sdc.serviceAccountName" -}}
57+
{{- if .Values.serviceAccount.create }}
58+
{{- default (include "sdc.fullname" .) .Values.serviceAccount.name }}
59+
{{- else }}
60+
{{- default "default" .Values.serviceAccount.name }}
61+
{{- end }}
62+
{{- end }}

‎helm/templates/configmap.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: nginx-config
5+
data:
6+
upstream: |
7+
upstream upstream {
8+
server localhost:9000;
9+
}

‎helm/templates/deployment.yaml

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: {{ .Values.projectName }}
5+
labels:
6+
name: {{ .Values.projectName }}
7+
spec:
8+
revisionHistoryLimit: 0
9+
replicas: 1
10+
selector:
11+
matchLabels:
12+
name: {{ .Values.projectName }}
13+
strategy:
14+
type: Recreate
15+
template:
16+
metadata:
17+
labels:
18+
name: {{ .Values.projectName }}
19+
spec:
20+
volumes:
21+
- name: codebase
22+
hostPath:
23+
path: /home/dferlay/Sources/df-sdc # TODO: Variable for relative path
24+
type: Directory
25+
- name: nginx-config
26+
configMap:
27+
name: nginx-config
28+
- name: cache-js
29+
emptyDir:
30+
medium: Memory
31+
- name: cache-css
32+
emptyDir:
33+
medium: Memory
34+
- name: cache-php
35+
emptyDir:
36+
medium: Memory
37+
containers:
38+
- name: php
39+
image: skilldlabs/php:74-fpm
40+
env:
41+
- name: COMPOSER_MEMORY_LIMIT
42+
value: "-1"
43+
ports:
44+
- containerPort: 9000
45+
protocol: TCP
46+
volumeMounts:
47+
- mountPath: /var/www/html
48+
name: codebase
49+
- mountPath: /var/www/html/web/sites/default/files/css
50+
name: cache-css
51+
- mountPath: /var/www/html/web/sites/default/files/js
52+
name: cache-js
53+
- mountPath: /var/www/html/web/sites/default/files/php
54+
name: cache-php
55+
- name: nginx
56+
image: skilldlabs/nginx:1.20
57+
ports:
58+
- containerPort: 80
59+
protocol: TCP
60+
volumeMounts:
61+
- mountPath: /var/www/html
62+
name: codebase
63+
- mountPath: /etc/nginx/upstream
64+
name: nginx-config
65+
subPath: upstream
66+
- mountPath: /var/www/html/web/sites/default/files/css
67+
name: cache-css
68+
- mountPath: /var/www/html/web/sites/default/files/js
69+
name: cache-js
70+
- name: mail
71+
image: skilldlabs/mailhog
72+
restartPolicy: Always
73+
nodeSelector:
74+
kubernetes.io/hostname: sdc

‎helm/templates/ingress.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: networking.k8s.io/v1beta1
2+
kind: Ingress
3+
metadata:
4+
name: {{ .Values.projectName }}
5+
labels:
6+
mrn: {{ .Values.projectName }}
7+
annotations:
8+
kubernetes.io/tls-acme: "true"
9+
kubernetes.io/ingress.class: web-ingress
10+
cert-manager.io/cluster-issuer: letsencrypt-prod
11+
nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
12+
spec:
13+
rules:
14+
- host: local.skilld.cloud
15+
http:
16+
paths:
17+
- path: /
18+
backend:
19+
serviceName: {{ .Values.projectName }}
20+
servicePort: 80
21+
tls:
22+
- hosts:
23+
- local.skilld.cloud
24+
secretName: {{ .Values.projectName }}-tls

‎helm/templates/service.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: {{ .Values.projectName }}
5+
spec:
6+
ports:
7+
- name: http
8+
port: 80
9+
protocol: TCP
10+
- name: mailhog
11+
port: 8025
12+
protocol: TCP
13+
selector:
14+
mrn: {{ .Values.projectName }}

‎helm/values.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Default values for sdc.
2+
# This is a YAML-formatted file.
3+
# Declare variables to be passed into your templates.
4+
5+
projectName: yay
6+
7+
image:
8+
9+
ingress:
10+
enabled: true
11+
12+
service:
13+
type: ClusterIP
14+
port: 80
15+

0 commit comments

Comments
 (0)
Please sign in to comment.