Skip to content

Commit

Permalink
refactor (#15)
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Paskal <paskal.maksim@gmail.com>
  • Loading branch information
maksim-paskal committed Dec 14, 2023
1 parent 91cc2a1 commit 216f8db
Show file tree
Hide file tree
Showing 67 changed files with 2,589 additions and 806 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/chart-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure Git
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/chart-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0

Expand All @@ -14,7 +14,7 @@ jobs:
with:
version: v3.8.1

- uses: actions/setup-python@v2
- uses: actions/setup-python@v4
with:
python-version: 3.7

Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
on: pull_request

jobs:
e2e:
runs-on: ubuntu-latest
strategy:
matrix:
k3s_version: ["v1.26.11+k3s2","v1.27.8+k3s2","v1.28.4+k3s2"]
helm_version: ["v3.8.1"]
steps:
- name: Setup Kubernetes
run: curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="${{ matrix.k3s_version }}" K3S_KUBECONFIG_MODE=777 sh -
- name: Setup Helm
uses: azure/setup-helm@v3
with:
version: ${{ matrix.helm_version }}
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Build
run: go run github.com/goreleaser/goreleaser@latest build --clean --snapshot --skip=validate
- name: Copy binary
run: mv ./dist/pod-admission-controller_linux_amd64_v1/pod-admission-controller ./pod-admission-controller
- name: Test binary
run: ./pod-admission-controller -version
- name: Run tests
run: make e2e KUBECONFIG=/etc/rancher/k3s/k3s.yaml image=alpine:latest helm_args=--values=./e2e/values.yaml
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Remove Git Tags with Charts
run: git tag -d $(git tag -l "helm-chart-*")
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Login to Docker Hub
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ jobs:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
stable: 'false'
go-version: '1.21'
- run: make test
- run: make test
- uses: codecov/codecov-action@v3
2 changes: 1 addition & 1 deletion .github/workflows/validate-license.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jobs:
name: validate-license
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- run: ./scripts/validate-license.sh
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
coverage.out
/deploy-*
/config.yaml
/.cr-*
/.cr-*
/patch.json
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ linters:
- exhaustruct
- varnamelen
- musttag
- depguard
- depguard
- maligned
22 changes: 15 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ tag=dev
image=paskalmaksim/pod-admission-controller:$(tag)
config=config.yaml
testnamespace=test-pod-admission-controller
helm_args=

namespace=
pod=

test:
./scripts/validate-license.sh
Expand All @@ -22,8 +26,8 @@ e2e:
kubectl create ns $(testnamespace)
kubectl label ns $(testnamespace) environment=dev
kubectl -n $(testnamespace) apply -f ./e2e/testdata/pods
kubectl -n $(testnamespace) wait --for=condition=Ready=true pods -lapp=test-pod-admission-controller --timeout=600s
go test --race ./e2e -kubeconfig=$(KUBECONFIG)
kubectl -n $(testnamespace) wait --for=condition=Ready=true pods -lapp=test-pod-admission-controller --timeout=60s
go test -v ./e2e -kubeconfig=$(KUBECONFIG)
kubectl delete ns $(testnamespace)

testChart:
Expand All @@ -38,7 +42,9 @@ run:
-cert=./certs/server.crt \
-key=./certs/server.key \
-listen=127.0.0.1:8443 \
-metrics.listen=127.0.0.1:31080
-metrics.listen=127.0.0.1:31080 \
-test.pod=$(pod) \
-test.namespace=$(namespace)

sslInit:
rm -rf ./certs
Expand All @@ -59,16 +65,18 @@ restart:

deploy:
kubectl -n pod-admission-controller scale deploy --all --replicas=0 || true

helm upgrade pod-admission-controller \
--install \
--namespace pod-admission-controller \
--create-namespace \
./charts/pod-admission-controller \
--set registry.image=paskalmaksim/pod-admission-controller:dev \
--set registry.image=$(image) \
--set registry.imagePullPolicy=Always \
--set-file config=$(config)
kubectl -n pod-admission-controller wait pod --for=condition=ready --all --timeout=600s
--set-file config=$(config) $(helm_args)

kubectl -n pod-admission-controller wait --for=condition=available deployment/pod-admission-controller --timeout=60s
kubectl -n pod-admission-controller wait --for=condition=ready pod -lapp=pod-admission-controller --timeout=60s

clean:
helm uninstall pod-admission-controller --namespace pod-admission-controller || true
Expand Down
2 changes: 1 addition & 1 deletion charts/pod-admission-controller/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
icon: https://helm.sh/img/helm.svg
name: pod-admission-controller
version: 0.0.4
version: 0.0.5
description: pod mutating admission controller
maintainers:
- name: maksim-paskal # Maksim Paskal
Expand Down
20 changes: 16 additions & 4 deletions charts/pod-admission-controller/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ spec:
- name: config
configMap:
name: {{ .Release.Name }}-config
{{ if .Values.extraVolumes }}
{{ toYaml .Values.extraVolumes | indent 6 }}
{{ end }}
containers:
- name: {{ .Release.Name }}
image: {{ .Values.registry.image }}
Expand All @@ -54,6 +57,10 @@ spec:
{{ if .Values.env }}
env:
{{ toYaml .Values.env | indent 8 }}
{{ end }}
{{ if .Values.command }}
command:
{{ toYaml .Values.command | indent 8 }}
{{ end }}
args:
- -config=/config/config.yaml
Expand All @@ -63,24 +70,29 @@ spec:
{{ toYaml .Values.args | indent 8 }}
{{ end }}
ports:
- containerPort: 8443
- containerPort: 31080
- name: https
containerPort: 8443
- name: metrics
containerPort: 31080
volumeMounts:
- name: config
mountPath: /config
{{ if .Values.extraVolumeMounts }}
{{ toYaml .Values.extraVolumeMounts | indent 8 }}
{{ end }}
resources:
{{ toYaml .Values.resources | indent 10 }}
readinessProbe:
httpGet:
scheme: HTTPS
path: /ready
port: 8443
port: https
initialDelaySeconds: 3
periodSeconds: 5
livenessProbe:
httpGet:
scheme: HTTPS
path: /healthz
port: 8443
port: https
initialDelaySeconds: 10
periodSeconds: 10
3 changes: 3 additions & 0 deletions charts/pod-admission-controller/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ rules:
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["get"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get","delete","create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
6 changes: 5 additions & 1 deletion charts/pod-admission-controller/templates/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
app: pod-admission-controller
webhooks:
- name: pod-admission-controller.pod-admission-controller.svc.cluster.local
failurePolicy: Ignore
failurePolicy: {{ .Values.webhook.failurePolicy }}
clientConfig:
caBundle: {{ tpl .Values.webhook.caBundle . | quote }}
service:
Expand All @@ -18,6 +18,10 @@ webhooks:
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]
- operations: ["UPDATE"]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["namespaces"]
admissionReviewVersions: ["v1"]
sideEffects: None
timeoutSeconds: 5
Expand Down
5 changes: 5 additions & 0 deletions charts/pod-admission-controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ tolerations: []
# application config
config: ""

extraVolumes: []
extraVolumeMounts: []

# certificates for dev purpuses, generate new certificates
certificates:
caCert: |
Expand Down Expand Up @@ -110,6 +113,8 @@ certificates:
webhook:
caBundle: "{{ b64enc .Values.certificates.caCert }}"
# Fail/Ignore
failurePolicy: Ignore
namespaceSelector:
- key: environment
operator: In
Expand Down
1 change: 0 additions & 1 deletion e2e/requirements/Pods/1/InitContainers/0/Env.json

This file was deleted.

1 change: 0 additions & 1 deletion e2e/requirements/Pods/1/InitContainers/0/Resources.json

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion e2e/requirements/Pods/1/InitContainers/1/Env.json

This file was deleted.

1 change: 0 additions & 1 deletion e2e/requirements/Pods/1/InitContainers/1/Resources.json

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion e2e/requirements/Pods/1/containers/0/Env.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/requirements/Pods/1/containers/1/Env.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/requirements/Pods/1/containers/2/Env.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/requirements/Pods/1/containers/3/Env.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/requirements/Pods/1/initContainers/0/Env.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/requirements/Pods/1/initContainers/0/Resources.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/requirements/Pods/1/initContainers/1/Env.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/requirements/Pods/1/initContainers/1/Resources.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion e2e/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,19 @@ rules:
- name: TEST_PORT
value: "6831"
- name: SERVICE_NAME
value: "{{ .Image.Slug }}"
value: "{{ .Image.Slug }}"

- name: "rule-replaceContainerImageHost-1"
replaceContainerImageHost:
enabled: true
to: docker.io
conditions:
- key: .Image.Domain
operator: regexp
value: ^(test-fake.test.com)$

- name: "rule-replaceContainerImageHost-2"
replaceContainerImageHost:
enabled: true
from: test.test.com
to: docker.io
Loading

0 comments on commit 216f8db

Please sign in to comment.