Skip to content

Commit

Permalink
Add script for generating kubernetes objects
Browse files Browse the repository at this point in the history
  • Loading branch information
daaang committed Jul 26, 2022
1 parent 618c270 commit a8efb3a
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
173 changes: 173 additions & 0 deletions generate_kubernetes_yaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/usr/bin/env bash
PROG="$0"
USAGE="WORKER_1 [WORKER_2...]"

errorout() {
echo "usage: ${PROG} -h" >&2
echo " or ${PROG} ${USAGE}" >&2
[ -n "$1" ] && echo "${PROG}: error: $@" >&2
exit 1
}

printhelp() {
cat <<EOF
usage: ${PROG} ${USAGE}
This generates some yaml that you can put in a file and apply to run in
a kubernetes cluster. For example:
$PROG worker-{30..37}.macc.test
That will generate the yaml to create 16 new deployments and 16 new
services on 8 worker nodes, each configured to connect to each other
node.
positional arguments:
WORKER... a complete list of worker nodes
optional arguments:
-h print this help text and exit
EOF
}

while getopts ':hn:' opt; do
case "$opt" in
h)
printhelp
exit 0
;;

?)
errorout "unrecognized argument: \`-$opt'"
;;
esac
done
shift $((OPTIND - 1))

main() {
INDICATORS=`join_with_comma "$@"`
for node in "$@"; do
print_kubernetes "$node"
done
}

join_with_comma() {
echo -n "http://network-indicator-${1//./-}:4567"
shift
for i in "$@"; do
echo -n ",http://network-indicator-${i//./-}:4567"
done
}

print_kubernetes() {
cat <<EOF
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: network-indicator-${1//./-}
spec:
selector:
matchLabels:
app: network-indicator
node: $1
template:
metadata:
labels:
app: network-indicator
node: $1
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchFields:
- key: metadata.name
operator: In
values:
- $1
containers:
- name: indicator
image: ghcr.io/mlibrary/kubernetes-network-test-indicator:1.0.0
env:
- name: RACK_ENV
value: production
ports:
- name: http
containerPort: 4567
livenessProbe:
httpGet:
path: /-/liveness
port: 4567
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: network-monitor-${1//./-}
spec:
selector:
matchLabels:
app: network-monitor
node: $1
template:
metadata:
labels:
app: network-monitor
node: $1
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchFields:
- key: metadata.name
operator: In
values:
- $1
containers:
- name: monitor
image: ghcr.io/mlibrary/kubernetes-network-test-monitor:1.0.0
env:
- name: RACK_ENV
value: production
- name: INDICATORS
value: $INDICATORS
ports:
- name: http
containerPort: 4568
livenessProbe:
httpGet:
path: /-/liveness
port: 4568
---
apiVersion: v1
kind: Service
metadata:
name: network-indicator-${1//./-}
spec:
selector:
app: network-indicator
node: $1
ports:
- name: http
port: 4567
---
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "4568"
prometheus.io/path: "/metrics"
name: network-monitor-${1//./-}
spec:
selector:
app: network-monitor
node: $1
ports:
- name: http
port: 4568
EOF
}

main "$@"
1 change: 1 addition & 0 deletions indicator/Containerfile.serve
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ RUN mkdir -p /usr/src/app
COPY Gemfile Gemfile.lock indicator_app.rb /usr/src/app
WORKDIR /usr/src/app
RUN bundle config set --local without development && bundle install
ENV RACK_ENV=production
EXPOSE 4567
CMD ["bundle", "exec", "ruby", "indicator_app.rb"]
1 change: 1 addition & 0 deletions monitor/Containerfile.serve
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ COPY lib /usr/src/app/lib
WORKDIR /usr/src/app
RUN bundle config set --local without development && bundle install
ENV INDICATORS=http://indicator-a:1234,http://indicator-b:5678
ENV RACK_ENV=production
EXPOSE 4568
CMD ["bundle", "exec", "ruby", "monitor_app.rb", "-p", "4568"]
2 changes: 1 addition & 1 deletion monitor/Containerfile.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM docker.io/library/ruby:3.1.2
RUN mkdir -p /usr/src/app
COPY Gemfile Gemfile.lock /usr/src/app
COPY Gemfile Gemfile.lock monitor_app.rb /usr/src/app
COPY lib /usr/src/app/lib
COPY spec /usr/src/app/spec
WORKDIR /usr/src/app
Expand Down

0 comments on commit a8efb3a

Please sign in to comment.