Compare commits

...
7 Commits
Author SHA1 Message Date
Robert Brennan aa0fa876d1 add debug 2022-12-13 08:53:47 -05:00
Robert Brennan feb0ed681a Merge branch 'master' into rb/fix-webhook 2022-12-13 08:50:46 -05:00
Robert Brennan 870ad9114b fix mutate test 2022-11-30 16:17:57 -05:00
Robert Brennan f872aee25c fix test 2022-11-29 15:37:05 -05:00
Robert Brennan f080ba55a8 fix mutations 2022-11-29 12:38:13 -05:00
Robert Brennan 1866a9649b fix test 2022-11-29 12:24:44 -05:00
Robert Brennan a809875682 add webhook test 2022-11-29 12:08:06 -05:00
3 changed files with 66 additions and 4 deletions
+5 -1
View File
@@ -58,7 +58,11 @@ func (m *Mutator) mutate(req admission.Request) ([]jsonpatch.Operation, error) {
if err != nil {
return nil, err
}
return jsonpatch.CreatePatch(originalYaml, []byte(mutatedYamlStr))
mutatedJSONStr, err := yaml.YAMLToJSON([]byte(mutatedYamlStr))
if err != nil {
return nil, err
}
return jsonpatch.CreatePatch(kubeResources.OriginalObjectJSON, []byte(mutatedJSONStr))
}
// Handle for Validator to run validation checks.
@@ -0,0 +1,30 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-mutated
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
securityContext:
allowPrivilegeEscalation: false
privileged: false
readOnlyRootFilesystem: true
runAsNonRoot: true
capabilities:
drop:
- ALL
+31 -3
View File
@@ -25,6 +25,7 @@ function check_webhook_is_ready() {
# loop until this fails (desired condition is we cannot apply this yaml doc, which means the webhook is working
echo "Waiting for webhook to be ready"
kubectl get pods -n polaris
while ! kubectl get pods -n polaris | grep -E "webhook.*1/1.*Running"; do
check_timeout "${timeout_epoch}"
echo -n "."
@@ -33,6 +34,10 @@ function check_webhook_is_ready() {
check_timeout "${timeout_epoch}"
echo "Webhook started!"
kubectl get svc -n polaris
kubectl get pods -n polaris
kubectl get validatingwebhookconfiguration -n polaris
kubectl get mutatingwebhookconfiguration -n polaris
}
# Check if timeout is hit and exit if it is
@@ -50,6 +55,7 @@ function check_timeout() {
function clean_up() {
echo -e "\n\nCleaning up (you may see some errors)...\n\n"
kubectl delete ns scale-test || true
kubectl delete ns mutate-test || true
kubectl delete ns polaris || true
kubectl delete ns tests || true
# Clean up files you've installed (helps with local testing)
@@ -60,7 +66,7 @@ function clean_up() {
echo "Uninstalling webhook and webhook config"
kubectl delete validatingwebhookconfigurations polaris-webhook --wait=false || true
kubectl delete validatingwebhookconfigurations polaris-validate-webhook --wait=false || true
kubectl delete validatingwebhookconfigurations polaris-mutate-webhook --wait=false || true
kubectl delete mutatingwebhookconfigurations polaris-mutate-webhook --wait=false || true
kubectl -n polaris delete deploy -l app=polaris --wait=false || true
echo -e "\n\nDone cleaning up\n\n"
}
@@ -76,9 +82,12 @@ clean_up || true
echo -e "Setting up..."
kubectl create ns scale-test
kubectl create ns mutate-test
kubectl create ns polaris
kubectl create ns tests
kubectl get validatingwebhookconfiguration
echo "Installing a bad deployment"
kubectl apply -n scale-test -f ./test/webhook_cases/failing_test.deployment.yaml
@@ -135,13 +144,32 @@ if [ $pod_count != 2 ]; then
echo "Existing deployment was unable to scale after webhook installed: found $pod_count pods"
fi
echo "Checking mutations"
helm upgrade --install polaris fairwinds-stable/polaris --namespace polaris --create-namespace \
--set dashboard.enable=false \
--set webhook.enable=true \
--set webhook.mutate=true \
--set image.tag=$CI_SHA1
echo "Waiting for the webhook to come online"
check_webhook_is_ready
kubectl apply -n mutate-test -f test/webhook_cases/mutation.deployment.yaml
if ! kubectl get -n mutate-test deployment nginx-deployment-mutated -oyaml | grep "imagePullPolicy: Always"; then
ALL_TESTS_PASSED=0
echo -e "${RED}****Test Failed: Polaris failed to mutate this resource****${NC}"
else
echo -e "${GREEN}****Test Passed: Polaris mutated this resource****${NC}"
fi
kubectl delete -n mutate-test -f test/webhook_cases/mutation.deployment.yaml || true
sleep 5
echo "Done with tests"
if [ -z $SKIP_FINAL_CLEANUP ]; then
echo "Doing final cleanup..."
clean_up
fi
echo "Done with tests"
#Verify that all the tests passed.
if [ $ALL_TESTS_PASSED -eq 1 ]; then
echo "Tests Passed."