Skip to content

Commit

Permalink
Use Helm. (#5)
Browse files Browse the repository at this point in the history
* Add charts/dsv-injector
* Remove deployments/*, tests/Dockerfile, ...
  • Loading branch information
amigus authored Feb 12, 2022
1 parent 36349bc commit 91b4177
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 260 deletions.
File renamed without changes.
121 changes: 24 additions & 97 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,38 @@ VERSION?=latest

IMAGE_TAG=$(NAME):$(VERSION)

DOCKER=docker
# Podman also works but it assumes that the registry is HTTPS...
#DOCKER=podman
#DOCKER_PUSH_ARGS=--tls-verify=false

# Use the Minikube built-in kubectl by default
KUBECTL=minikube kubectl --

all: image

# Look for a 'registry' service on the cluster unless given one as an argument
REGISTRY?=$(shell $(KUBECTL) get --ignore-not-found -n kube-system service \
registry -o jsonpath="{.spec.clusterIP}{':'}{.spec.ports[0].port}")
registry:
ifeq ($(REGISTRY),)
@echo enabling the Minikube registry addon
@minikube addons enable registry && sleep 6
REGISTRY=$(shell $(KUBECTL) get -n kube-system service registry -o \
jsonpath="{.spec.clusterIP}{':'}{.spec.ports[0].port}")
endif

# Build, tag and push the dsv-injector service 📦
image: registry
$(DOCKER) build . -t $(IMAGE_TAG) -f build/Dockerfile $(DOCKER_BUILD_ARGS)
$(DOCKER) tag $(DOCKER_TAG_ARGS) $(IMAGE_TAG) $(REGISTRY)/$(IMAGE_TAG)
$(DOCKER) push $(DOCKER_PUSH_ARGS) $(REGISTRY)/$(IMAGE_TAG)
HELM_CHART:=charts/$(NAME)

### The remainder builds and deploys a test injector-svc ☑️

# The CA certificate of the Kubernetes cluster 🔐
CA_CRT?=${HOME}/.minikube/ca.crt

# See the "CA certificate" section of README.md 📖
CA_BUNDLE?=$(shell base64 -w0 $(CA_CRT))
DOCKER=docker
HELM=helm

# The Kubernetes Namespace in which to deploy 📁
NAMESPACE?=default

# The JSON file containing a mapping of DSV role names to tenant/credentials 🔑
ROLES_FILE?=configs/roles.json

# The IP address of the host running the dsv-injector service 🖥️
SERVICE_IP?=$(shell ip route get 1.1.1.1 | grep -oP 'src \K\S+')
CA_BUNDLE?=${HOME}/.minikube/ca.crt

# The TCP port on which the service should listen 🌐
SERVICE_PORT?=8543
ROLES_JSON?=configs/roles.json

TEST_IMAGE_TAG?=$(NAME)-test:$(VERSION)
.PHONY: image

IMAGE_PULL_POLICY=Always
all: install

BUILD_DIR=target

$(BUILD_DIR):
mkdir -p $(BUILD_DIR)

deploy_webhook: $(BUILD_DIR)
sed -e "s| namespace: .*$$| namespace: $(NAMESPACE)|" \
-e "s| port: [0-9]*.*$$| port: $(SERVICE_PORT)|" \
-e "s|caBundle:.*$$|caBundle: $(CA_BUNDLE)|" \
deployments/webhook.yml >| $(BUILD_DIR)/webhook.yml
$(KUBECTL) apply -f $(BUILD_DIR)/webhook.yml
# Build the dsv-injector service image 📦
image:
$(DOCKER) build . -t $(IMAGE_TAG) $(DOCKER_BUILD_ARGS)

# Get a certificate from the Kubernetes cluster CA
$(BUILD_DIR)/$(NAME).key $(BUILD_DIR)/$(NAME).pem: $(BUILD_DIR)
sh scripts/get_cert.sh -n "$(NAME)" -N "$(NAMESPACE)" -d "$(BUILD_DIR)"

dsv-injector-svc: cmd/dsv-injector-svc.go
go build $<

# Deploy the service that the webhook uses as a pointer to the host
deploy_host: deploy_webhook $(BUILD_DIR)/$(NAME).key $(BUILD_DIR)/$(NAME).pem dsv-injector-svc
sed -e "s| namespace: .*$$| namespace: $(NAMESPACE)|" \
-e "s|- port: [0-9]*.*$$|- port: $(SERVICE_PORT)|" \
-e "s|- ip: *\"[0-9].*$$|- ip: \"$(SERVICE_IP)\"|" \
deployments/host.yml >| $(BUILD_DIR)/host.yml
$(KUBECTL) apply -f $(BUILD_DIR)/host.yml

# Create the test image Dockerfile
$(BUILD_DIR)/Dockerfile: registry test/Dockerfile $(BUILD_DIR)
sed -e "s|^FROM $(NAME):.*|FROM $(REGISTRY)/$(IMAGE_TAG)|" \
test/Dockerfile >| $(BUILD_DIR)/Dockerfile

# Build the test image 🥼🥽🧪
test_image: registry image $(BUILD_DIR)/$(NAME).key $(BUILD_DIR)/$(NAME).pem $(BUILD_DIR)/Dockerfile
$(DOCKER) build . -t $(TEST_IMAGE_TAG) -f $(BUILD_DIR)/Dockerfile $(DOCKER_BUILD_ARGS) \
--build-arg cert_file="$(BUILD_DIR)/$(NAME).pem" \
--build-arg key_file="$(BUILD_DIR)/$(NAME).key" \
--build-arg roles_file="$(ROLES_FILE)"
$(DOCKER) tag $(DOCKER_TAG_ARGS) $(TEST_IMAGE_TAG) $(REGISTRY)/$(TEST_IMAGE_TAG)
$(DOCKER) push $(DOCKER_PUSH_ARGS) $(REGISTRY)/$(TEST_IMAGE_TAG)

# Deploy the test image that includes the certficate and roles.json ⚠️🔓😧
deploy: deploy_webhook test_image
sed -e "s| namespace: .*$$| namespace: $(NAMESPACE)|" \
-e "s|- port: [0-9]*.*$$|- port: $(SERVICE_PORT)|" \
-e "s|imagePullPolicy:.*$$|imagePullPolicy: $(IMAGE_PULL_POLICY)|" \
-e "s|image:.*$$|image: $(REGISTRY)/$(TEST_IMAGE_TAG)|" \
deployments/pod.yml >| $(BUILD_DIR)/pod.yml
$(KUBECTL) apply -f $(BUILD_DIR)/pod.yml

deploy_clean:
$(KUBECTL) delete --ignore-not-found deployments $(NAME)
$(KUBECTL) delete --ignore-not-found service $(NAME)
$(KUBECTL) delete --ignore-not-found mutatingwebhookconfigurations.admissionregistration.k8s.io $(NAME)

clean: deploy_clean
rm -rf $(BUILD_DIR) dsv-injector-svc
$(HELM_CHART)/$(NAME).key $(HELM_CHART)/$(NAME).pem:
sh scripts/get_cert.sh -n "$(NAME)" -d "$(HELM_CHART)" -N "$(NAMESPACE)"
-rm -f $(HELM_CHART)/$(NAME).csr

install: $(HELM_CHART)/$(NAME).key $(HELM_CHART)/$(NAME).pem image
$(HELM) install $(HELM_INSTALL_ARGS) \
--set-file caBundle=$(CA_BUNDLE) \
--set-file rolesJson=$(ROLES_JSON) \
$(NAME) $(HELM_CHART)

clean:
$(HELM) uninstall $(NAME)
$(DOCKER) rmi -f $(IMAGE_TAG)
-rm -f $(HELM_CHART)/$(NAME).key $(HELM_CHART)/$(NAME).pem
23 changes: 23 additions & 0 deletions charts/dsv-injector/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
14 changes: 14 additions & 0 deletions charts/dsv-injector/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v2
name: dsv-injector
description: A Helm chart for the Delinea DevOps Secrets Vault (DSV) Injector Mutating Webhook.
keywords:
- Delinea
- DevOps
- DSV
- secrets
- vault
type: application
version: 0.1.0
appVersion: latest
maintainers:
- name: Adam Migus
62 changes: 62 additions & 0 deletions charts/dsv-injector/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "dsv.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "dsv.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "dsv.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "dsv.labels" -}}
helm.sh/chart: {{ include "dsv.chart" . }}
{{ include "dsv.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "dsv.selectorLabels" -}}
app.kubernetes.io/name: {{ include "dsv.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "dsv.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "dsv.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
8 changes: 8 additions & 0 deletions charts/dsv-injector/templates/cert-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ include "dsv.name" . }}-cert
data:
cert: {{ .Files.Get "dsv-injector.pem" | b64enc }}
key: {{ .Files.Get "dsv-injector.key" | b64enc }}
type: Opaque
54 changes: 54 additions & 0 deletions charts/dsv-injector/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "dsv.fullname" . }}
labels:
{{- include "dsv.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "dsv.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "dsv.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: {{ .Chart.Name }}
command: ["dsv-injector-svc", "-cert", "/tls/cert", "-key", "/tls/key", "-roles", "/roles/json" ]
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.containerPort }}
protocol: TCP
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: cert
readOnly: true
mountPath: /tls
- name: roles
readOnly: true
mountPath: /roles
serviceAccountName: {{ include "dsv.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
volumes:
- name: roles
secret:
secretName: {{ include "dsv.name" . }}-roles
- name: cert
secret:
secretName: {{ include "dsv.name" . }}-cert
7 changes: 7 additions & 0 deletions charts/dsv-injector/templates/roles-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ include "dsv.name" . }}-roles
data:
json: {{ .Values.rolesJson | b64enc }}
type: Opaque
28 changes: 28 additions & 0 deletions charts/dsv-injector/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
apiVersion: v1
kind: Service
metadata:
name: {{ include "dsv.fullname" . }}
labels:
{{- include "dsv.labels" . | nindent 4 }}
spec:
ports:
- port: {{ .Values.webhookPort }}
targetPort: {{ .Values.containerPort }}
selector:
{{- include "dsv.selectorLabels" . | nindent 4 }}
type: {{ .Values.service.type }}
{{- if eq .Values.service.type "ClusterIP" -}}
---
apiVersion: v1
kind: Endpoints
metadata:
name: {{ include "dsv.fullname" . }}
labels:
{{- include "dsv.labels" . | nindent 4 }}
subsets:
- addresses:
- ip: {{ .Values.service.ipAddress }}
ports:
- port: 8543
{{- end -}}
12 changes: 12 additions & 0 deletions charts/dsv-injector/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "dsv.serviceAccountName" . }}
labels:
{{- include "dsv.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
22 changes: 22 additions & 0 deletions charts/dsv-injector/templates/webhook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: {{ include "dsv.name" . }}
labels:
{{- include "dsv.labels" . | nindent 4 }}
webhooks:
- name: {{ include "dsv.fullname" . }}.default.svc
sideEffects: {{ default "None" .Values.sideEffects }}
admissionReviewVersions: ["v1", "v1beta1"]
clientConfig:
service:
name: {{ include "dsv.name" . }}
namespace: {{ .Release.Namespace }}
path: {{ .Values.webhookUri }}
port: {{ .Values.webhookPort }}
caBundle: {{ .Values.caBundle | b64enc }}
rules:
- operations: ["CREATE", "UPDATE"]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["secrets"]
Loading

0 comments on commit 91b4177

Please sign in to comment.