initial port of cf-env

This commit is contained in:
Dr Nic Williams 2018-11-27 11:34:44 +10:00
commit 73677945fe
7 changed files with 126 additions and 0 deletions

5
Gemfile Normal file
View File

@ -0,0 +1,5 @@
source 'https://rubygems.org'
gem 'sinatra'
gem 'json_pure'
gem 'puma'

26
Gemfile.lock Normal file
View File

@ -0,0 +1,26 @@
GEM
remote: https://rubygems.org/
specs:
json_pure (2.1.0)
mustermann (1.0.3)
puma (3.12.0)
rack (2.0.6)
rack-protection (2.0.4)
rack
sinatra (2.0.4)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.4)
tilt (~> 2.0)
tilt (2.0.8)
PLATFORMS
ruby
DEPENDENCIES
json_pure
puma
sinatra
BUNDLED WITH
1.16.3

1
Procfile Normal file
View File

@ -0,0 +1 @@
web: rackup -p $PORT

45
README.md Normal file
View File

@ -0,0 +1,45 @@
# Display Knative environment variables
A simple Sinatra application. Once deployed to Knative, it will echo the Environment and HTTP Request Headers.
![demo](docs/images/knative-env.png)
## Deploy
The repo contains a manifest file which creates a new application called `cfenv`, with a random string in the URL to avoid collisions.
To deploy from existing Docker image:
```shell
knctl deploy -s knative-env -i index.docker.io/drnic/knative-env
```
To deploy from source, whilst creating new intermediate Docker image (in Docker Hub in example below):
```shell
knctl basic-auth-secret create -s registry --docker-hub -u <you> -p <password>
knctl service-account create --service-account build -s registry
kubectl apply -f https://raw.githubusercontent.com/knative/build-templates/master/buildpack/buildpack.yaml
DOCKER_IMAGE=index.docker.io/<you>/knative-env
knctl deploy -s knative-env -i ${DOCKER_IMAGE} \
--service-account build \
--template buildpack \
--directory .
```
Deploy with additional environment variables:
```shell
knctl deploy -s knative-env -i index.docker.io/drnic/knative-env \
--env MYVAR1=test1 \
--env MYVAR=test2
```
To view in browser, either setup Ingress and DNS, or use `kwt` as below. All routes from Knative will now work from local machine:
```shell
sudo -E kwt net start --dns-map-exec='knctl dns-map'
```

2
config.ru Normal file
View File

@ -0,0 +1,2 @@
require './env'
run Sinatra::Application

BIN
docs/images/knative-env.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

47
env.rb Normal file
View File

@ -0,0 +1,47 @@
require 'rubygems'
require 'sinatra'
require 'json'
get '/' do
puts "Current env keys: #{ENV.keys.sort.inspect}"
res = "<html><body style=\"margin:0px auto; width:80%; font-family:monospace\">"
res << "<head><title>Knative Environment</title><meta name=\"viewport\" content=\"width=device-width\"></head>"
res << "<h2>Knative Environment</h2>"
res << "<div><table>"
ENV.keys.sort.each do |key|
value = begin
"<pre>" + JSON.pretty_generate(JSON.parse(ENV[key])) + "</pre>"
rescue
ENV[key]
end
res << "<tr><td><strong>#{key}</strong></td><td>#{value}</tr>"
end
res << "</table></div>"
res << "<h2>HTTP Request Headers</h2>"
res << "<div><table>"
env.inject({}){|acc, (k,v)| acc[$1.downcase] = v if k =~ /^http_(.*)/i; acc}.sort.each do |k,v|
res << "<tr><td><strong>#{k}</strong></td><td>#{v}</tr>"
end
res << "</table></div></body></html>"
end
get '/some-error' do
$stderr.puts "This is an error log"
end
get '/v1/:var' do
res = ENV[params['var'] + "\n"]
end
get '/v1/:var/:json_path' do
var = ENV[params['var']]
obj = JSON.parse(var)
params['json_path'].split(".").each do |k|
if obj.is_a?(Array)
obj = obj[k.to_i]
else
obj = obj[k]
end
end
res = obj.to_json + "\n"
end