Skip to content

Commit

Permalink
add mapenv
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Davis <[email protected]>
  • Loading branch information
Doug Davis committed Oct 6, 2020
1 parent 86d9608 commit 69a0ebe
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ $ ic ce project create --name demos --target
Starts a stateful application that scales based on load. The state is kept
in an instance of Redis, also running within Code Engine. Demonstrates the
use of non-http components and private networking between components.
- [mapenv](mapenv)<br>
Shows how to inject a Secret and a ConfigMap as environment variables
into an Application.

## Resources

Expand Down
2 changes: 1 addition & 1 deletion app-n-job/run
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ic ce app create -n myapp --image ${REPOSITORY}/app-n-job
URL=$(ic ce app get -n myapp -o jsonpath={.status.url})

# And call it
curl -s $URL | tee out
curl -Ls $URL | tee out

if ! grep "Hello from.*app" out > /dev/null ; then
echo "Unexpected output"
Expand Down
4 changes: 2 additions & 2 deletions hello/run
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ ic ce app create -n hello --image ${REPOSITORY}/hello
URL=$(ic ce app get -n hello -o jsonpath={.status.url})

# Now call it
curl -s $URL
curl -Ls $URL

if ! [[ "$(curl -s $URL)" == "Hello World" ]]; then
if ! [[ "$(curl -Ls $URL)" == "Hello World" ]]; then
echo "Unexpected output"
exit 1
fi
Expand Down
4 changes: 2 additions & 2 deletions helloworld/run
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ ic ce app create -n helloworld --image ${REPOSITORY}/helloworld
URL=$(ic ce app get -n helloworld -o jsonpath={.status.url})

# Now call it
curl -s $URL
curl -Ls $URL

if ! [[ "$(curl -s $URL -d "Hi")" == "Hi" ]]; then
if ! [[ "$(curl -Ls ${URL} --post301 -d "Hi")" == "Hi" ]]; then
echo "Unexpected output"
exit 1
fi
Expand Down
2 changes: 1 addition & 1 deletion job2app/run
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ until ic ce jobrun get -n job -o jsonpath={.status.succeeded} | grep $COUNT; do
done

# Check the app to see how many times it was hit
if ! [[ "Count: $((COUNT*10))" == "$(curl -s $URL)" ]]; then
if ! [[ "Count: $((COUNT*10))" == "$(curl -Ls $URL)" ]]; then
echo "Should be $((COUNT*10))"
exit 1
fi
Expand Down
8 changes: 8 additions & 0 deletions mapenv/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM golang:alpine
COPY mapenv.go /
RUN go build -o /mapenv /mapenv.go

# Copy the exe into a smaller base image
FROM alpine
COPY --from=0 /mapenv /mapenv
CMD /mapenv
12 changes: 12 additions & 0 deletions mapenv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# MapEnv

This sample shows how inject a secret and a configMap into an application
as a set of environment variables. Secrets and ConfigMaps are both key/value
pair objects - except secrets' data are protected/encrypted at rest. Which
makes them a great choice for private data such as credentials or passwords.

In both cases, the "key" of the secret/configMap will become the "name" of
the environment variable and the corresponding "value" in the secret/configMap
will be the "value" of that environment variable.

The application will log (print to stdout) all of its environment variables.
13 changes: 13 additions & 0 deletions mapenv/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Env Vars:
# REPOSITORY: name of the image registry/namespace to store the images

set -ex
export REPOSITORY=${REPOSITORY:-ibmcom}

# Build the image
docker build -t ${REPOSITORY}/mapenv .

# And push it
docker push ${REPOSITORY}/mapenv
25 changes: 25 additions & 0 deletions mapenv/mapenv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"net/http"
"os"
"sort"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Sort the env vars
envs := os.Environ()
sort.Strings(envs)

// And print them
fmt.Printf("Environment variables - look for the ones with MY_")
for _, e := range envs {
fmt.Printf("%s\n", e)
}
})

fmt.Printf("Listening on port 8080\n")
http.ListenAndServe(":8080", nil)
}
46 changes: 46 additions & 0 deletions mapenv/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# Env Vars:
# REPOSITORY: name of the image registry/namespace to get the images

# Clean up previous run
function clean() {
set +x
echo Cleaning...
ic ce app delete -n mapenv -f --wto=0 > /dev/null 2>&1 || true
ic ce secret delete -n mysecret -f > /dev/null 2>&1 || true
ic ce configmap delete -n myconfig -f > /dev/null 2>&1 || true
rm -f out
}

clean
[[ "$1" == "clean" ]] && exit 0

set -ex
export REPOSITORY=${REPOSITORY:-ibmcom}

# Create a Secret and a ConfigMap
ic ce secret create --name mysecret --from-literal MY_SECRET_APIKEY=letmein
ic ce configmap create --name myconfig --from-literal MY_CONFIG_KNOB=42

# Create the app
ic ce app create -n mapenv --image ${REPOSITORY}/mapenv \
--env-from-secret mysecret --env-from-configmap myconfig

# Get the URL of the app for later use
URL=$(ic ce app get -n mapenv -o jsonpath={.status.url})

# Just force it to log something by hitting ig
curl -Ls $URL

# Extract the instance name from `ic ce app get`
ic ce app get -n mapenv | tee out
name=$(grep "mapenv.*Running" out | tail -1 | sed "s/ [12].*//")
echo Instance name: $name

# Now wait check the logs to see if the env vars were set
ic ce app logs --instance $name | tee out
grep MY_ out

# Clean up
clean
2 changes: 1 addition & 1 deletion s2i-buildpacks/run
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ done
# Test the image we just built - deploy an app and 'curl' it
ic ce app create -n s2i-buildpacks --image "us.icr.io/${ICR_NS}/app" --rs icr
URL=$(ic ce app get -n s2i-buildpacks -o jsonpath={.status.url})
curl -s "${URL}" | tee out
curl -Ls "${URL}" | tee out

if ! grep "I was built" out > /dev/null ; then
echo "Missing expected outout"
Expand Down
2 changes: 1 addition & 1 deletion s2i-dockerfile/run
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ done
# Test the image we just built - deploy an app and 'curl' it
ic ce app create -n s2i-dockerfile --image "us.icr.io/${ICR_NS}/app" --rs icr
URL=$(ic ce app get -n s2i-dockerfile -o jsonpath={.status.url})
curl -s "${URL}" | tee out
curl -Ls "${URL}" | tee out

if ! grep "I was built" out > /dev/null ; then
echo "Missing expected outout"
Expand Down
6 changes: 3 additions & 3 deletions sessions/run
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ ic ce app create -n myapp -i ${REPOSITORY}/sessions -e IP=$IP --cn=5 --min=3
URL=$(ic ce app get -n myapp -o jsonpath={.status.url})

# Now call the app to make sure it works
curl -s $URL
curl -Ls $URL

# Now call it 98 more times in parallel. Notice the hostname changes
set +x
for i in `seq 1 98` ; do
curl $URL &
curl -L $URL &
done
wait
set -x

# Verify the app was hit 100 times total
if ! [[ "$(curl -s $URL)" == "Counter: 100 "* ]]; then
if ! [[ "$(curl -Ls $URL)" == "Counter: 100 "* ]]; then
echo "Unexpected output"
exit 1
fi
Expand Down

0 comments on commit 69a0ebe

Please sign in to comment.