forked from IBM/CodeEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Doug Davis
committed
Sep 28, 2020
1 parent
0ff7e9d
commit c1f4040
Showing
17 changed files
with
193 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
FROM golang | ||
FROM golang:alpine | ||
COPY helloworld.go / | ||
RUN go build -o /helloworld /helloworld.go | ||
|
||
# Copy the exe into a smaller base image | ||
FROM alpine | ||
COPY --from=0 /helloworld /helloworld | ||
CMD /helloworld |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
FROM golang | ||
FROM golang:alpine | ||
COPY job.go / | ||
RUN go build -o /job /job.go | ||
|
||
# Copy exe into a smaller image | ||
FROM alpine | ||
COPY --from=0 /job /job | ||
CMD /job |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
# Job2App | ||
# Job | ||
|
||
This sample serves two purposes: | ||
- show how to create a simple Batch Job ([`job.go`](./job.go)) | ||
- how to have that Job call an Application running in the same project. This | ||
will use the private network within the project - meaning the private | ||
endpoint of the app, not a public-facing one. | ||
This sample shows up to run a batch job. It will create the batch job two ways: | ||
1 - first, it'll create a Job definition (the config informatin about a job) | ||
and then it submits that Job to actually do the work. | ||
2 - second, it'll submit the Job directly without creating the definition | ||
first. Both will generate the same results though. | ||
|
||
When the Job is run, each instance will call the App 10 times. Which means | ||
by setting the `array-indices` to a range of 50, the App should be hit 500 | ||
times. At the end of `run` it will ask the App for the number of times it | ||
was called, to verify the count. | ||
Each instance of each Job submitted will print, to its logs, its "index", | ||
which is defined by its `JOB_INDEX` environment variable. The `run` script | ||
will print some of the log files to show this. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,55 @@ | ||
#!/bin/bash | ||
|
||
# Env Vars: | ||
# COUNT: number of instances of the job to run | ||
# NUM: number of instances of the job to run | ||
# REPOSITORY: name of the image registry/namespace to get the images | ||
|
||
# Clean up previous run | ||
function clean() { | ||
set +x | ||
echo Cleaning... | ||
ic ce jobrun delete -n job -f > /dev/null 2>&1 || true | ||
ic ce app delete -n app -f --wto=0 > /dev/null 2>&1 || true | ||
ic ce job delete -n myjob -f > /dev/null 2>&1 || true | ||
ic ce jobrun delete -n doit -f > /dev/null 2>&1 || true | ||
ic ce jobrun delete -n doit2 -f > /dev/null 2>&1 || true | ||
rm -f out | ||
} | ||
|
||
clean | ||
[[ "$1" == "clean" ]] && exit 0 | ||
|
||
set -ex | ||
export REPOSITORY=${REPOSITORY:-ibmcom} | ||
export COUNT=${COUNT:-50} | ||
export NUM=${COUNT:-10} | ||
|
||
# Create the app | ||
ic ce app create -n app --min=1 --max=1 --image ${REPOSITORY}/j2a-app | ||
# Create a Job definition | ||
ic ce job create --name myjob --array-indices=1-${NUM} \ | ||
--image ${REPOSITORY}/firstjob | ||
|
||
# Get metadata about the app for later use | ||
URL=$(ic ce app get -n app -o jsonpath={.status.url}) | ||
# Now submit the job using that definition | ||
ic ce jobrun submit --name doit --job myjob | ||
|
||
# Create/run the job, passing in the project/namespace | ||
ic ce jobrun submit -n job --ai=1-${COUNT} --image ${REPOSITORY}/j2a-job | ||
# Wait for it to finish... | ||
until ic ce jobrun get --name doit -o jsonpath={.status.succeeded} | grep $NUM | ||
do | ||
sleep 3 | ||
done | ||
|
||
# Now submit a job w/o creating a job definition first | ||
ic ce jobrun submit --name doit2 --array-indices=1-${NUM} \ | ||
--image ${REPOSITORY}/firstjob | ||
|
||
# Wait for it to finish... | ||
until ic ce jobrun get -n job -o jsonpath={.status.succeeded} | grep $COUNT; do | ||
until ic ce jobrun get --name doit2 -o jsonpath={.status.succeeded} | grep $NUM | ||
do | ||
sleep 3 | ||
done | ||
|
||
# Check the app to see how many times the app was hit | ||
if ! [[ "Count: $((COUNT*10))" == "$(curl -s $URL)" ]]; then | ||
echo "Should be $((COUNT*10))" | ||
exit 1 | ||
fi | ||
# Show the stats about the job | ||
ic ce jobrun get --name doit2 | ||
|
||
# Now look at a view of the logs to make sure it worked | ||
ic ce jobrun logs --instance doit2-1-0 | ||
ic ce jobrun logs --instance doit2-2-0 | grep "Hi from" | ||
|
||
# Clean up | ||
clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM golang:alpine | ||
COPY job.go / | ||
RUN go build -o /job /job.go | ||
|
||
# Copy the exe into a smaller base image | ||
FROM alpine | ||
COPY --from=0 /job /job | ||
CMD /job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM golang:alpine | ||
COPY app.go / | ||
RUN go build -o /app /app.go | ||
|
||
# Copy the exe into a smaller base image | ||
FROM alpine | ||
COPY --from=0 /app /app | ||
CMD /app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Job2App | ||
|
||
This sample serves two purposes: | ||
- show how to create a simple Batch Job ([`job.go`](./job.go)) | ||
- how to have that Job call an Application running in the same project. This | ||
will use the private network within the project - meaning the private | ||
endpoint of the app, not a public-facing one. | ||
|
||
When the Job is run, each instance will call the App 10 times. Which means | ||
by setting the `array-indices` to a range of 50, the App should be hit 500 | ||
times. At the end of `run` it will ask the App for the number of times it | ||
was called, to verify the count. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
|
||
# Env Vars: | ||
# REPOSITORY: name of the image registry/namespace to store the images | ||
|
||
set -ex | ||
export REPOSITORY=${REPOSITORY:-ibmcom} | ||
|
||
# First build the app's image and push it | ||
docker build -t ${REPOSITORY}/j2a-app -f Dockerfile.app . | ||
docker push ${REPOSITORY}/j2a-app | ||
|
||
# Now build the job's | ||
docker build -t ${REPOSITORY}/j2a-job -f Dockerfile . | ||
docker push ${REPOSITORY}/j2a-job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"sync" | ||
) | ||
|
||
func main() { | ||
// Get the namespace we're in so we know how to talk to the App | ||
file := "/var/run/secrets/kubernetes.io/serviceaccount/namespace" | ||
namespace, err := ioutil.ReadFile(file) | ||
if err != nil || len(namespace) == 0 { | ||
fmt.Fprintf(os.Stderr, "Missing namespace: %s\n%s\n", err, namespace) | ||
os.Exit(1) | ||
} | ||
|
||
count := 10 | ||
fmt.Printf("Sending %d requests...\n", count) | ||
wg := sync.WaitGroup{} | ||
|
||
// URL to the App | ||
url := "http://app." + string(namespace) + ".svc.cluster.local" | ||
|
||
// Do all requests to the App in parallel - why not? | ||
for i := 0; i < count; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
res, err := http.Post(url, "", nil) | ||
|
||
// Something went wrong | ||
if err != nil || res.StatusCode > 299 { | ||
fmt.Fprintf(os.Stderr, "%d: %s\n%#v\n", i, err, res) | ||
} | ||
}() | ||
} | ||
|
||
// Wait for all threads to finish before we exit | ||
wg.Wait() | ||
|
||
fmt.Printf("Done\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/bin/bash | ||
|
||
# Env Vars: | ||
# COUNT: number of instances of the job to run | ||
# REPOSITORY: name of the image registry/namespace to get the images | ||
|
||
# Clean up previous run | ||
function clean() { | ||
set +x | ||
echo Cleaning... | ||
ic ce jobrun delete -n job -f > /dev/null 2>&1 || true | ||
ic ce app delete -n app -f --wto=0 > /dev/null 2>&1 || true | ||
} | ||
|
||
clean | ||
[[ "$1" == "clean" ]] && exit 0 | ||
|
||
set -ex | ||
export REPOSITORY=${REPOSITORY:-ibmcom} | ||
export COUNT=${COUNT:-50} | ||
|
||
# Create the app | ||
ic ce app create -n app --min=1 --max=1 --image ${REPOSITORY}/j2a-app | ||
|
||
# Get metadata about the app for later use | ||
URL=$(ic ce app get -n app -o jsonpath={.status.url}) | ||
|
||
# Create/run the job, passing in the project/namespace | ||
ic ce jobrun submit -n job --ai=1-${COUNT} --image ${REPOSITORY}/j2a-job | ||
|
||
# Wait for it to finish... | ||
until ic ce jobrun get -n job -o jsonpath={.status.succeeded} | grep $COUNT; do | ||
sleep 3 | ||
done | ||
|
||
# Check the app to see how many times it was hit | ||
if ! [[ "Count: $((COUNT*10))" == "$(curl -s $URL)" ]]; then | ||
echo "Should be $((COUNT*10))" | ||
exit 1 | ||
fi | ||
|
||
# Clean up | ||
clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
FROM golang | ||
FROM golang:alpine | ||
COPY app.go / | ||
RUN go build -o /app /app.go | ||
|
||
# Copy the exe into a smaller base image | ||
FROM alpine | ||
COPY --from=0 /app /app | ||
CMD /app |