openshift-golang-template
openshift-golang-template copied to clipboard
Template for running Go programs on OpenShift v3
Template for running Go programs on OpenShift v3 (version 1.4 and higher)
It supports:
- Golang dep manager (dep is a prototype dependency management tool) - /example-golang-dep
- glide manager - /example-glide
- godep manager - /example-godep
- govendor manager - /example-govendor
- Traditional
go get- /example
Sample data to try golang template
| Data | Value |
|---|---|
| Go | Go v1.9.2 |
| OpenShift | OpenShift Origin v1.5 |
| Git repository | https://github.com/amsokol/openshift-golang-template.git |
| Context directory | /example-golang-dep |
| Folder with main.go to build and run | /example-golang-dep/cmd/server |
How to
- Login with Developer account to OpenShift:
# oc login --insecure-skip-tls-verify -u <username> https://openshift.example.com:8443
- Create new project:
# oc new-project project1
- Select
project1project:
# oc project project1
- Create ImageStream:
# oc create -f ./openshift-golang-template.yaml
-
Login to OpenShift console using browser (e.g. https://openshift.example.com:8443) with Developer account
-
Open
project1project -
Click
Add to Projectand selectGo -
Select
1.9.2 - latestfrom drop down list and clickSelect -
Set
Nametogolang1 -
Set
Git Repository URLtohttps://github.com/amsokol/openshift-golang-template.gitor clickTry It -
Click
advanced optionsto add additional configuration parameters -
Set
Context Dirto/example-golang-dep(sample project with dep manager support) -
Add the following environment variables to
Build Configurationsection:
- GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep
- GOPROJECT_CMD=cmd/server
GOPROJECT_ROOT tells builder the root package of go project
GOPROJECT_CMD tells builder the where "main()" function of "main" package to build and run is located (relative to GOPROJECT_ROOT).
Note: ignore GOPROJECT_CMD if "main()" function of "main" package is located in GOPROJECT_ROOT folder.
In example above "main()" function of "main" package is located in `github.com/amsokol/openshift-golang-template/example-golang-dep/cmd/server`.
GOPROJECT_ROOT is set to `github.com/amsokol/openshift-golang-template/example-golang-dep`.
So GOPROJECT_CMD is set to `cmd/server`
- [Optional] You can easy provide
go buildarguments. Just addGO_BUILD_ARGSenvironment variable toBuild Configurationsection. For example the following value tellsgo buildto print the commands are run and packages are compiled:
GO_BUILD_ARGS=-x -v
Note: please DO NOT override output file name ("-o" argument)!
- [Optional] You can easy provide command line arguments to your binary. Just add
GOPROJECT_CMD_ARGSenvironment variable toDeployment Configurationsection. For example the following value provides--silentand--forcearguments the binary:
GOPROJECT_CMD_ARGS=--silent --force
- [Optional] You can easy provide configuration files to your binary that are copied by the build script to
GOPATH\bin(where your binary file is located). Just addGOPROJECT_CMD_CONFIG,GOPROJECT_CMD_CONFIG1,GOPROJECT_CMD_CONFIG2,GOPROJECT_CMD_CONFIG3, etc. environment variables toBuild Configurationsection. If your configuration files are located in the same project source control repositoryGOPROJECT_CMD_CONFIGxvalues should beGOPROJECT_ROOTrelated path. But you need to know that store configuration files into project repository is BAD PRACTICES (configuration files usually contain credentials, IPs, etc.). It's much more better to inject configuration file from outside (e.g. download from CI server). In this case setGOPROJECT_CMD_CONFIGxby URL value. Build scripts runscurlutility to download configuration files. It supports:
DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet, TFTP, SSL certificates, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos) and more.
Here are some examples:
Provide configuration file is stored into the same source code repository (not recommended for production):
GOPROJECT_CMD_CONFIG=config/settings.yaml
Provide configuration files are stored into the same source code repository (not recommended for production):
GOPROJECT_CMD_CONFIG1=config/db/db.yaml
GOPROJECT_CMD_CONFIG2=config/messaging/broker.json
Download configuration file via HTTPS:
GOPROJECT_CMD_CONFIG=https://config.server.com/myapp/settings.yaml
Download configuration files via HTTPS:
GOPROJECT_CMD_CONFIG1=https://config.server.com/myapp/db/db.yaml
GOPROJECT_CMD_CONFIG2=smb://config.server.com/messaging/broker.json -u "domain\username:passwd"
Notes:
- If you have only one configuration file use
GOPROJECT_CMD_CONFIGenvironment variable. If you have more that one configuration files useGOPROJECT_CMD_CONFIG1,GOPROJECT_CMD_CONFIG2,GOPROJECT_CMD_CONFIG3, etc. environment variables. - As you see above you can provide not URL only but other
curlarguments like credentials. It very important to pass arguments after URL:
Correct:
GOPROJECT_CMD_CONFIG=smb://config.server.com/messaging/broker.json -u "domain\username:passwd"
Wrong:
GOPROJECT_CMD_CONFIG=-u "domain\username:passwd" smb://config.server.com/messaging/broker.json
- Leave other options with default values and click
Createand wait while pod is created
[Optional] How to add health (liveness and readiness) checks
-
Login to OpenShift console using browser (eg https://openshift.example.com:8443) with Developer account
-
Open
project1project -
Click
Applications -> Deployments -
Click
golang1in the list -
Select
Configurationtab -
Click
Add Health Checks -
Click
Add Readiness Probe -
Set
Pathto '/healthz/ready' and leave other options with default values -
Click
Add Liveness Probe -
Set
Pathto '/healthz/live' and leave other options with default values -
Click
Saveand wait while pod is created
Helper #1 - you can try golang template using S2I:
s2i build https://github.com/amsokol/openshift-golang-template.git amsokol/golang-openshift:1.9.2-1 golang1 -e GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep -e GOPROJECT_CMD=cmd/server --context-dir /example-golang-dep
Helper #2 - how to export/edit template from OpenShift
-
Login using oc as OpenShift administrator
-
Run:
# oc get templates -n openshift
# oc edit template -n openshift nodejs-mongo-persistent