Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve examples + smoke testing #155

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions .github/workflows/smoke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,33 @@ jobs:
- name: Create Kind cluster
uses: helm/kind-action@v1

- name: Build executor image
- name: Wait for apiserver
run: |
docker build -t eno-controller:smoketest -f ./docker/eno-controller/Dockerfile .
kind load docker-image --name chart-testing eno-controller:smoketest
# Wait for apiserver to be ready
kind export kubeconfig --name chart-testing
kubectl apply -f - <<YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: default
namespace: default
YAML
while true; do
kubectl api-resources
if [[ $? -eq 0 ]]; then
break
else
sleep 1
fi
done

- name: Build images
run: |
set -e
export REGISTRY=localhost
export SKIP_PUSH=yes

./dev/build.sh
./examples/*/build.sh

# Copy images to kind
for image in $(docker images --format "{{.Repository}}:{{.Tag}}" | grep localhost); do
kind load docker-image --name chart-testing $image
done

- name: Run tests
timeout-minutes: 5
run: export EXECUTOR_IMAGE=eno-controller:smoketest && ./hack/smoke-test.sh
run: ./hack/smoke-test.sh
2 changes: 1 addition & 1 deletion dev/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export TAG="$(date +%s)"
function build() {
cmd=$(basename $1)
docker build --quiet -t "$REGISTRY/$cmd:$TAG" -f "$f/Dockerfile" .
docker push "$REGISTRY/$cmd:$TAG"
[[ -z "${SKIP_PUSH}" ]] && docker push "$REGISTRY/$cmd:$TAG"
}

# Build!
Expand Down
73 changes: 0 additions & 73 deletions examples/churntest.yaml

This file was deleted.

16 changes: 16 additions & 0 deletions examples/go-synthesizer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM docker.io/golang:1.21 AS builder
WORKDIR /app

ADD go.mod .
ADD go.sum .
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" ./examples/go-synthesizer

FROM gcr.io/distroless/static

# https://github.com/GoogleContainerTools/distroless/blob/16dc4a6a33838006fe956e4c19f049ece9c18a8d/common/variables.bzl#L18
USER 65532:65532

COPY --from=builder /app/go-synthesizer /bin/synthesize
29 changes: 29 additions & 0 deletions examples/go-synthesizer/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

set -e

if [[ -z "${REGISTRY}" ]]; then
echo "REGISTRY must be set" > /dev/stderr
exit 1
fi

TAG="$(date +%s)"
export IMAGE="$REGISTRY/example-go-synthesizer:$TAG"

docker build --quiet -t ${IMAGE} -f "examples/go-synthesizer/Dockerfile" .
[[ -z "${SKIP_PUSH}" ]] && docker push ${IMAGE}

kubectl apply -f - <<YAML
apiVersion: eno.azure.io/v1
kind: Synthesizer
metadata:
name: go-synth-example-synth
spec:
image: $IMAGE
refs:
- key: example-input
resource:
group: "" # core
version: "v1"
kind: ConfigMap
YAML
25 changes: 25 additions & 0 deletions examples/go-synthesizer/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Run build.sh to push the synthesizer to $REGISTRY before `kubectl apply`ing this manifest
#
# Then, `kubectl edit cm eno-input-example` to set replicas=2. The deployment should be updated almost immediately.

apiVersion: v1
kind: ConfigMap
metadata:
name: eno-input-example
data:
replicas: "1"

---

apiVersion: eno.azure.io/v1
kind: Composition
metadata:
name: go-synth-example
spec:
synthesizer:
name: go-synth-example-synth
bindings:
- key: example-input
resource:
name: eno-input-example
namespace: default
46 changes: 46 additions & 0 deletions examples/go-synthesizer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"strconv"

"github.com/Azure/eno/pkg/function"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)

func main() {
w := function.NewDefaultOutputWriter()
r, err := function.NewDefaultInputReader()
if err != nil {
panic(err) // non-zero exits will be retried
}

input := &corev1.ConfigMap{}
function.ReadInput(r, "example-input", input)

replicas, _ := strconv.Atoi(input.Data["replicas"])

deploy := &appsv1.Deployment{}
deploy.APIVersion = "apps/v1"
deploy.Kind = "Deployment"
deploy.Name = "example-nginx-deployment"
deploy.Namespace = "default"
deploy.Spec.Replicas = ptr.To(int32(replicas))
deploy.Spec.Selector = &metav1.LabelSelector{MatchLabels: map[string]string{"app": "nginx-example"}}
deploy.Spec.Template = corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"app": "nginx-example"},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "nginx",
Image: "nginx:latest",
}},
},
}
w.Add(deploy)

w.Write()
}
56 changes: 0 additions & 56 deletions examples/inputs/eno.yaml

This file was deleted.

8 changes: 5 additions & 3 deletions examples/simple.yaml → examples/minimal/example.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
apiVersion: eno.azure.io/v1
kind: Synthesizer
metadata:
name: test-synth
name: simple-example-synth
namespace: default
spec:
image: docker.io/ubuntu:latest
command:
Expand Down Expand Up @@ -29,7 +30,8 @@ spec:
apiVersion: eno.azure.io/v1
kind: Composition
metadata:
name: test-comp
name: simple-example
namespace: default
spec:
synthesizer:
name: test-synth
name: simple-example-synth
34 changes: 12 additions & 22 deletions hack/smoke-test.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
#!/bin/bash

# Wait for apiserver to be ready
while true; do
kubectl api-resources
if [[ $? -eq 0 ]]; then
break
else
sleep 1
fi
done
set -e

# Start Eno
kubectl apply -f api/v1/config/crd
go run ./cmd/eno-controller --health-probe-addr=:0 --metrics-addr=:0 --synthesizer-pod-namespace=default &
go run ./cmd/eno-reconciler --health-probe-addr=:0 --metrics-addr=:0 &
# Apply examples
for file in ./examples/*/example.yaml; do
kubectl apply -f $file
done

# Apply example
kubectl apply -f ./examples/simple.yaml
set +e

# Wait for the composition to be reconciled
while true; do
json=$(kubectl get composition test-comp -o=json)
echo "${json}"
output=$(kubectl get compositions --no-headers)
echo $output

echo $json | jq --exit-status '.status.currentSynthesis.ready'
echo $output | awk '{ if ($0 !~ "Ready") exit 1 }'
if [[ $? -eq 0 ]]; then
break
else
sleep 1
fi
done

# Delete the example and wait for cleanup
kubectl delete composition test-comp --wait=true --timeout=1m
set -e

# Clean up controller background jobs
kill $(jobs -p)
# Delete the example and wait for cleanup
kubectl delete composition --all --wait=true --timeout=1m
Loading