├── .gitignore ├── .vscode └── cSpell.json ├── LICENSE ├── README.md ├── S2I ├── .s2i │ └── bin │ │ ├── assemble │ │ ├── run │ │ ├── save-artifacts │ │ └── usage ├── Dockerfile └── Makefile ├── example-glide ├── cmd │ └── server │ │ └── main.go ├── glide.yaml └── pkg │ ├── cmd │ ├── cli │ │ └── params.go │ └── server │ │ ├── healthz.go │ │ ├── hello.go │ │ └── server.go │ └── fake │ ├── healthz.go │ ├── healthz_test.go │ ├── hello.go │ └── hello_test.go ├── example-godep ├── Godeps │ ├── Godeps.json │ └── Readme ├── cmd │ └── server │ │ └── main.go └── pkg │ ├── cmd │ ├── cli │ │ └── params.go │ └── server │ │ ├── healthz.go │ │ ├── hello.go │ │ └── server.go │ └── fake │ ├── healthz.go │ ├── healthz_test.go │ ├── hello.go │ └── hello_test.go ├── example-golang-dep ├── Gopkg.toml ├── cmd │ └── server │ │ └── main.go └── pkg │ ├── cmd │ ├── cli │ │ └── params.go │ └── server │ │ ├── healthz.go │ │ ├── hello.go │ │ └── server.go │ └── fake │ ├── healthz.go │ ├── healthz_test.go │ ├── hello.go │ └── hello_test.go ├── example-govendor ├── cmd │ └── server │ │ └── main.go ├── pkg │ ├── cmd │ │ ├── cli │ │ │ └── params.go │ │ └── server │ │ │ ├── healthz.go │ │ │ ├── hello.go │ │ │ └── server.go │ └── fake │ │ ├── healthz.go │ │ ├── healthz_test.go │ │ ├── hello.go │ │ └── hello_test.go └── vendor │ └── vendor.json ├── example ├── cmd │ └── server │ │ └── main.go └── pkg │ ├── cmd │ ├── cli │ │ └── params.go │ └── server │ │ ├── healthz.go │ │ ├── hello.go │ │ └── server.go │ └── fake │ ├── healthz.go │ ├── healthz_test.go │ ├── hello.go │ └── hello_test.go └── openshift-golang-template.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | .kube/ 26 | /example-golang-dep/vendor 27 | /example-golang-dep/Gopkg.lock 28 | /example-godep/vendor 29 | /example-glide/vendor 30 | /example-glide/glide.lock -------------------------------------------------------------------------------- /.vscode/cSpell.json: -------------------------------------------------------------------------------- 1 | // cSpell Settings 2 | { 3 | // Version of the setting file. Always 0.1 4 | "version": "0.1", 5 | // language - current active spelling language 6 | "language": "en", 7 | // words - list of words to be always considered correct 8 | "words": [ 9 | "CONFI", 10 | "GOPATH", 11 | "GOPROJECT", 12 | "IMAPS", 13 | "Kerberos", 14 | "LDAP", 15 | "LDAPS", 16 | "NTLM", 17 | "RTMP", 18 | "RTSP", 19 | "SMBS", 20 | "SMTP", 21 | "SMTPS", 22 | "TFTP", 23 | "godep", 24 | "govendor", 25 | "healthz", 26 | "openshift", 27 | "passwd" 28 | ], 29 | // flagWords - list of words to be always considered incorrect 30 | // This is useful for offensive words and common spelling errors. 31 | // For example "hte" should be "the" 32 | "flagWords": [ 33 | "hte" 34 | ] 35 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Template for running Go programs on OpenShift v3 (version 1.4 and higher) 2 | 3 | It supports: 4 | 5 | - Golang [dep manager](https://github.com/golang/dep) (dep is a prototype dependency management tool) - [/example-golang-dep](https://github.com/amsokol/openshift-golang-template/tree/master/example-golang-dep) 6 | - [glide](https://github.com/Masterminds/glide) manager - [/example-glide](https://github.com/amsokol/openshift-golang-template/tree/master/example-glide) 7 | - [godep](https://github.com/tools/godep) manager - [/example-godep](https://github.com/amsokol/openshift-golang-template/tree/master/example-godep) 8 | - [govendor](https://github.com/kardianos/govendor) manager - [/example-govendor](https://github.com/amsokol/openshift-golang-template/tree/master/example-govendor) 9 | - Traditional `go get` - [/example](https://github.com/amsokol/openshift-golang-template/tree/master/example) 10 | 11 | ## Sample data to try golang template 12 | 13 | | Data | Value | 14 | |--------------------------------------|------------------------------------------------------------| 15 | | Go | Go v1.9.2 | 16 | | OpenShift | OpenShift Origin v1.5 | 17 | | Git repository | https://github.com/smile1130/golang-openshift-skeleton.git | 18 | | Context directory | /example-golang-dep | 19 | | Folder with main.go to build and run | /example-golang-dep/cmd/server | 20 | 21 | ## How to 22 | 23 | 1. Login with Developer account to OpenShift: 24 | 25 | ```bash 26 | # oc login --insecure-skip-tls-verify -u https://openshift.example.com:8443 27 | ``` 28 | 29 | 2. Create new project: 30 | 31 | ```bash 32 | # oc new-project project1 33 | ``` 34 | 35 | 3. Select `project1` project: 36 | 37 | ```bash 38 | # oc project project1 39 | ``` 40 | 41 | 4. Create ImageStream: 42 | 43 | ```bash 44 | # oc create -f ./openshift-golang-template.yaml 45 | ``` 46 | 47 | 5. Login to OpenShift console using browser (e.g. [https://openshift.example.com:8443](https://openshift.example.com:8443)) with Developer account 48 | 49 | 6. Open `project1` project 50 | 51 | 7. Click `Add to Project` and select `Go` 52 | 53 | 8. Select `1.9.2 - latest` from drop down list and click `Select` 54 | 55 | 9. Set `Name` to `golang1` 56 | 57 | 10. Set `Git Repository URL` to `https://github.com/smile1130/golang-openshift-skeleton.git` or click `Try It` 58 | 59 | 11. Click `advanced options` to add additional configuration parameters 60 | 61 | 12. Set `Context Dir` to `/example-golang-dep` (sample project with [dep manager](https://github.com/golang/dep) support) 62 | 63 | 13. Add the following environment variables to `Build Configuration` section: 64 | - GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep 65 | - GOPROJECT_CMD=cmd/server 66 | 67 | ```bash 68 | GOPROJECT_ROOT tells builder the root package of go project 69 | GOPROJECT_CMD tells builder the where "main()" function of "main" package to build and run is located (relative to GOPROJECT_ROOT). 70 | Note: ignore GOPROJECT_CMD if "main()" function of "main" package is located in GOPROJECT_ROOT folder. 71 | 72 | In example above "main()" function of "main" package is located in `github.com/amsokol/openshift-golang-template/example-golang-dep/cmd/server`. 73 | GOPROJECT_ROOT is set to `github.com/amsokol/openshift-golang-template/example-golang-dep`. 74 | So GOPROJECT_CMD is set to `cmd/server` 75 | ``` 76 | 77 | 14. [Optional] You can easy provide `go build` arguments. Just add `GO_BUILD_ARGS` environment variable to `Build Configuration` section. For example the following value tells `go build` to print the commands are run and packages are compiled: 78 | 79 | ```bash 80 | GO_BUILD_ARGS=-x -v 81 | 82 | Note: please DO NOT override output file name ("-o" argument)! 83 | ``` 84 | 85 | 15. [Optional] You can easy provide command line arguments to your binary. Just add `GOPROJECT_CMD_ARGS` environment variable to `Deployment Configuration` section. For example the following value provides `--silent` and `--force` arguments the binary: 86 | 87 | ```bash 88 | GOPROJECT_CMD_ARGS=--silent --force 89 | ``` 90 | 91 | 16. [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 add `GOPROJECT_CMD_CONFIG`, `GOPROJECT_CMD_CONFIG1`, `GOPROJECT_CMD_CONFIG2`, `GOPROJECT_CMD_CONFIG3`, etc. environment variables to `Build Configuration` section. If your configuration files are located in the same project source control repository `GOPROJECT_CMD_CONFIGx` values should be `GOPROJECT_ROOT` related 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 set `GOPROJECT_CMD_CONFIGx` by URL value. Build scripts runs `curl` utility to download configuration files. It supports: 92 | 93 | `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.` 94 | Here are some examples: 95 | 96 | ```bash 97 | Provide configuration file is stored into the same source code repository (not recommended for production): 98 | GOPROJECT_CMD_CONFIG=config/settings.yaml 99 | 100 | Provide configuration files are stored into the same source code repository (not recommended for production): 101 | GOPROJECT_CMD_CONFIG1=config/db/db.yaml 102 | GOPROJECT_CMD_CONFIG2=config/messaging/broker.json 103 | 104 | Download configuration file via HTTPS: 105 | GOPROJECT_CMD_CONFIG=https://config.server.com/myapp/settings.yaml 106 | 107 | Download configuration files via HTTPS: 108 | GOPROJECT_CMD_CONFIG1=https://config.server.com/myapp/db/db.yaml 109 | GOPROJECT_CMD_CONFIG2=smb://config.server.com/messaging/broker.json -u "domain\username:passwd" 110 | ``` 111 | 112 | Notes: 113 | 114 | - If you have only one configuration file use `GOPROJECT_CMD_CONFIG` environment variable. If you have more that one configuration files use `GOPROJECT_CMD_CONFIG1`, `GOPROJECT_CMD_CONFIG2`, `GOPROJECT_CMD_CONFIG3`, etc. environment variables. 115 | - As you see above you can provide not URL only but other `curl` arguments like credentials. It very important to pass arguments after URL: 116 | 117 | ```bash 118 | Correct: 119 | GOPROJECT_CMD_CONFIG=smb://config.server.com/messaging/broker.json -u "domain\username:passwd" 120 | 121 | Wrong: 122 | GOPROJECT_CMD_CONFIG=-u "domain\username:passwd" smb://config.server.com/messaging/broker.json 123 | ``` 124 | 125 | 17. Leave other options with default values and click `Create` and wait while pod is created 126 | 127 | ## [Optional] How to add health (liveness and readiness) checks 128 | 129 | 1. Login to OpenShift console using browser (eg https://openshift.example.com:8443) with Developer account 130 | 131 | 1. Open `project1` project 132 | 133 | 1. Click `Applications -> Deployments` 134 | 135 | 1. Click `golang1` in the list 136 | 137 | 1. Select `Configuration` tab 138 | 139 | 1. Click `Add Health Checks` 140 | 141 | 1. Click `Add Readiness Probe` 142 | 143 | 1. Set `Path` to '/healthz/ready' and leave other options with default values 144 | 145 | 1. Click `Add Liveness Probe` 146 | 147 | 1. Set `Path` to '/healthz/live' and leave other options with default values 148 | 149 | 1. Click `Save` and wait while pod is created 150 | 151 | ## Helper #1 - you can try golang template using S2I: 152 | 153 | ```bash 154 | s2i build https://github.com/smile1130/golang-openshift-skeleton.git amsokol/golang-openshift:1.9.2-1 golang1 -e GOPROJECT_ROOT=github.com/smile1130/golang-openshift-skeleton/example-golang-dep -e GOPROJECT_CMD=cmd/server --context-dir /example-golang-dep 155 | ``` 156 | 157 | ## Helper #2 - how to export/edit template from OpenShift 158 | 159 | 1. Login using oc as OpenShift administrator 160 | 161 | 1. Run: 162 | 163 | ```bash 164 | # oc get templates -n openshift 165 | # oc edit template -n openshift nodejs-mongo-persistent 166 | ``` 167 | -------------------------------------------------------------------------------- /S2I/.s2i/bin/assemble: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # S2I assemble script for the 'amsokol/golang-openshift:1.9.x-x' image. 4 | # The 'assemble' script builds your application source so that it is ready to run. 5 | # 6 | # For more information refer to the documentation: 7 | # https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md 8 | # 9 | 10 | if [[ "$1" == "-h" ]]; then 11 | # If the 'test1' assemble script is executed with '-h' flag, 12 | # print the usage. 13 | exec /usr/libexec/s2i/usage 14 | fi 15 | 16 | # Restore artifacts from the previous build (if they exist). 17 | # 18 | if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then 19 | echo "---> Restoring build artifacts..." 20 | mv /tmp/artifacts/. ./ 21 | fi 22 | 23 | echo "---> Сlarify the root package for go application" 24 | if [[ -n "${GOPROJECT_ROOT/[ ]*\n/}" ]] 25 | then 26 | echo "Root package is set to \"$GOPROJECT_ROOT\"" 27 | ROOT_PACKAGE=$GOPROJECT_ROOT 28 | else 29 | ROOT_PACKAGE=$(echo "$SOURCE_REPOSITORY" | sed -re "s/(https:\/\/|git@)([^?]+)\.git/\2/g" | sed -re "s/:/\//g") 30 | if [[ -n "${SOURCE_CONTEXT_DIR/[ ]*\n/}" ]] 31 | then 32 | ROOT_PACKAGE="$ROOT_PACKAGE/$SOURCE_CONTEXT_DIR" 33 | echo "SOURCE_CONTEXT_DIR is \"$SOURCE_CONTEXT_DIR\", so root package is \"$ROOT_PACKAGE\"" 34 | else 35 | echo "SOURCE_CONTEXT_DIR is empty, so root package is \"$ROOT_PACKAGE\"" 36 | fi 37 | fi 38 | 39 | SOURCE_CODE="$GOPATH/src/$ROOT_PACKAGE" 40 | echo "---> Installing application source in folder \"$SOURCE_CODE\"" 41 | mkdir -p $SOURCE_CODE 42 | cd $SOURCE_CODE 43 | if test -d /tmp/src 44 | then 45 | cp -Rf /tmp/src/. ./ 46 | else 47 | # for backwards compatibility 48 | cp -Rf /tmp/s2i*/upload/src/. ./ 49 | fi 50 | 51 | mkdir -p $GOPATH/bin 52 | 53 | if [ -f "./Gopkg.toml" ] 54 | then 55 | echo "---> Using golang dep manager (dep is a prototype dependency management tool)" 56 | echo "---> Downloading golang dep manager from github.com/golang/dep ..." 57 | go get -u github.com/golang/dep/cmd/dep 58 | echo "---> Downloading dependencies (running 'dep ensure') ..." 59 | dep ensure 60 | elif [ -f "./Godeps/Godeps.json" ] 61 | then 62 | echo "---> Using godep manager" 63 | echo "---> Downloading godep manager from github.com/tools/godep ..." 64 | go get -u github.com/tools/godep 65 | echo "---> Downloading dependencies (running 'godep restore') ..." 66 | godep restore 67 | elif [ -f "./glide.yaml" ] 68 | then 69 | echo "---> Using glide manager" 70 | echo "---> Downloading glide manager from github.com/Masterminds/glide ..." 71 | go get -u github.com/Masterminds/glide 72 | echo "---> Downloading dependencies (running 'glide install') ..." 73 | glide install 74 | elif [ -f "./vendor/vendor.json" ] 75 | then 76 | echo "---> Using govendor manager" 77 | echo "---> Downloading govendor manager from github.com/kardianos/govendor ..." 78 | go get -u github.com/kardianos/govendor 79 | echo "---> Downloading dependencies (running 'govendor sync') ..." 80 | govendor sync 81 | else 82 | echo "---> Do not use dependency manager - run 'go get'" 83 | echo "---> Downloading dependencies..." 84 | go get -v ./... 85 | fi 86 | 87 | echo "---> Building application from source" 88 | cd ./$GOPROJECT_CMD 89 | go build -o $GOPATH/bin/goapplication2run $GO_BUILD_ARGS 90 | 91 | echo "---> Deploy configuration files (if existed)" 92 | cd $GOPATH/bin 93 | while read GOPROJECT_CMD_CONFIG 94 | do 95 | if [[ $GOPROJECT_CMD_CONFIG =~ .+:// ]] 96 | then 97 | echo "---> Downloading \"$GOPROJECT_CMD_CONFIG\" to \"$GOPATH/bin/\"" 98 | curl -O $GOPROJECT_CMD_CONFIG 99 | else 100 | echo "---> Coping \"$SOURCE_CODE/$GOPROJECT_CMD_CONFIG\" to \"$GOPATH/bin/\"" 101 | cp -f $SOURCE_CODE/$GOPROJECT_CMD_CONFIG ./ 102 | fi 103 | done < <((env | grep '^GOPROJECT_CMD_CONFIG[0-9]*=') | sed 's/^.*=\(.*\)$/\1/') 104 | -------------------------------------------------------------------------------- /S2I/.s2i/bin/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # S2I run script for the 'amsokol/golang-openshift:1.9.x-x' image. 4 | # The run script executes the server that runs your application. 5 | # 6 | # For more information see the documentation: 7 | # https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md 8 | # 9 | 10 | cd $GOPATH/bin 11 | 12 | echo "---> Run apllication" 13 | exec ./goapplication2run $GOPROJECT_CMD_ARGS 14 | -------------------------------------------------------------------------------- /S2I/.s2i/bin/save-artifacts: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | # 3 | # S2I save-artifacts script for the 'amsokol/golang-openshift:1.9.x-x' image. 4 | # The save-artifacts script streams a tar archive to standard output. 5 | # The archive contains the files and folders you want to re-use in the next build. 6 | # 7 | # For more information see the documentation: 8 | # https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md 9 | # 10 | # tar cf - 11 | -------------------------------------------------------------------------------- /S2I/.s2i/bin/usage: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | cat < amsokol/golang-openshift:1.9.x-x 9 | 10 | You can then run the resulting image via: 11 | docker run 12 | EOF 13 | -------------------------------------------------------------------------------- /S2I/Dockerfile: -------------------------------------------------------------------------------- 1 | # amsokol/golang-openshift:1.9.2 2 | FROM centos/s2i-base-centos7 3 | 4 | # Maintainer name in the image metadata 5 | LABEL maintainer="github@amsokol.digital" 6 | 7 | # Builder environment variable to inform users about application you provide them 8 | ENV GOLANG_OPENSHIFT_BUILDER_VERSION 1.0 9 | 10 | # Set labels used in OpenShift to describe the builder image 11 | LABEL io.k8s.description="Platform for building golang web application" \ 12 | io.k8s.display-name="builder 1.9.2" \ 13 | io.openshift.expose-services="8080:http" \ 14 | io.openshift.tags="builder,1.9.2,golang" 15 | 16 | ENV GOLANG_VERSION 1.9.2 17 | ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux-amd64.tar.gz 18 | ENV GOLANG_DOWNLOAD_SHA256 de874549d9a8d8d8062be05808509c09a88a248e77ec14eb77453530829ac02b 19 | RUN curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \ 20 | && echo "$GOLANG_DOWNLOAD_SHA256 golang.tar.gz" | sha256sum -c - \ 21 | && tar -C /usr/local -xzf golang.tar.gz \ 22 | && rm golang.tar.gz 23 | ENV PATH /usr/local/go/bin:$PATH 24 | 25 | # TODO (optional): Copy the builder files into /opt/app-root 26 | # COPY .// /opt/app-root/ 27 | 28 | # TODO: Copy the S2I scripts to /usr/libexec/s2i, since openshift/base-centos7 image sets io.openshift.s2i.scripts-url label that way, or update that label 29 | COPY ./.s2i/bin/ $STI_SCRIPTS_PATH 30 | RUN chmod -R a+x $STI_SCRIPTS_PATH 31 | ENV PATH $STI_SCRIPTS_PATH:$PATH 32 | 33 | # Drop the root user and make the content of /opt/app-root owned by user 1001 34 | RUN chown -R 1001:0 /opt/app-root 35 | 36 | # This default user is created in the openshift/base-centos7 image 37 | USER 1001 38 | 39 | # Set GOPATH env 40 | ENV GOPATH /opt/app-root 41 | 42 | # Set the default port for applications built using this image 43 | EXPOSE 8080 44 | ENV PORT 8080 45 | 46 | # Directory with the sources is set as the working directory so all STI scripts 47 | # can execute relative to this path. 48 | WORKDIR ${HOME} 49 | 50 | # Set the default CMD for the image 51 | CMD ["usage"] 52 | -------------------------------------------------------------------------------- /S2I/Makefile: -------------------------------------------------------------------------------- 1 | 2 | IMAGE_NAME = amsokol/golang-openshift:1.9.2-1 3 | 4 | build: 5 | docker build -t $(IMAGE_NAME) . 6 | -------------------------------------------------------------------------------- /example-glide/cmd/server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-glide/pkg/cmd/cli" 7 | "github.com/amsokol/openshift-golang-template/example-glide/pkg/cmd/server" 8 | ) 9 | 10 | func main() { 11 | cli.EchoArgs() 12 | if err := server.Start(cli.GetPort()); err != nil { 13 | os.Exit(1) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example-glide/glide.yaml: -------------------------------------------------------------------------------- 1 | package: github.com/amsokol/openshift-golang-template/example-glide 2 | import: 3 | - package: github.com/pressly/chi 4 | version: ^2.1.0 5 | -------------------------------------------------------------------------------- /example-glide/pkg/cmd/cli/params.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // EchoArgs prints arguments are provided via command line 9 | func EchoArgs() { 10 | if len(os.Args) > 1 { 11 | fmt.Println("Command line arguments:", os.Args[1:]) 12 | } else { 13 | fmt.Println("No command line arguments are provided") 14 | } 15 | } 16 | 17 | // GetPort returns HTTP port to listen from environment variable value or default value 8080 18 | func GetPort() string { 19 | port := os.Getenv("PORT") 20 | if len(port) == 0 { 21 | port = "8080" 22 | } 23 | return port 24 | } 25 | -------------------------------------------------------------------------------- /example-glide/pkg/cmd/server/healthz.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-glide/pkg/fake" 7 | ) 8 | 9 | // live provides HTTP endpoint for application liveness probe 10 | func live(w http.ResponseWriter, r *http.Request) { 11 | s, err := fake.Live() 12 | if err != nil { 13 | w.WriteHeader(http.StatusServiceUnavailable) 14 | } else { 15 | w.Write([]byte(s)) 16 | } 17 | } 18 | 19 | // ready provides HTTP endpoint for application readiness probe 20 | func ready(w http.ResponseWriter, r *http.Request) { 21 | s, err := fake.Ready() 22 | if err != nil { 23 | w.WriteHeader(http.StatusServiceUnavailable) 24 | } else { 25 | w.Write([]byte(s)) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /example-glide/pkg/cmd/server/hello.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-glide/pkg/fake" 7 | ) 8 | 9 | // hello provides HTTP endpoint for "Hello" method 10 | func hello(w http.ResponseWriter, r *http.Request) { 11 | w.Write([]byte(fake.Hello())) 12 | } 13 | -------------------------------------------------------------------------------- /example-glide/pkg/cmd/server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "net/http" 7 | "os" 8 | "os/signal" 9 | "time" 10 | 11 | "syscall" 12 | 13 | "github.com/pressly/chi" 14 | ) 15 | 16 | // Start starts HTTP server 17 | func Start(port string) error { 18 | logger := log.New(os.Stdout, "", 0) 19 | 20 | r := chi.NewRouter() 21 | r.Get("/", hello) 22 | r.Get("/healthz/live", live) 23 | r.Get("/healthz/ready", ready) 24 | 25 | h := &http.Server{Addr: ":" + port, Handler: r} 26 | 27 | // Wait for interrupt signal to gracefully shutdown the server with 28 | // a timeout of 10 seconds. 29 | quit := make(chan os.Signal) 30 | signal.Notify(quit, os.Interrupt, os.Kill, syscall.SIGTERM) 31 | go func() { 32 | <-quit 33 | logger.Println("\nShutting down the server...") 34 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 35 | defer cancel() 36 | if err := h.Shutdown(ctx); err != nil { 37 | logger.Println("Error when stopping the server: " + err.Error()) 38 | } else { 39 | logger.Println("Server gracefully stopped") 40 | } 41 | }() 42 | 43 | logger.Printf("Listening on http://0.0.0.0:%s\n", port) 44 | if err := h.ListenAndServe(); err != nil { 45 | logger.Println("Listening returns error: " + err.Error()) 46 | return err 47 | } 48 | 49 | logger.Println("\nServer stopped") 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /example-glide/pkg/fake/healthz.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | // Live emulates application liveness probe 5 | // It returns response if success or error if check failed 6 | func Live() (string, error) { 7 | return "I'm alive!", nil 8 | } 9 | 10 | // Ready emulates application readiness probe 11 | // It returns response if success or error if check failed 12 | func Ready() (string, error) { 13 | return "I'm ready!", nil 14 | } 15 | -------------------------------------------------------------------------------- /example-glide/pkg/fake/healthz_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import "testing" 4 | 5 | func TestLive(t *testing.T) { 6 | tests := []struct { 7 | name string 8 | want string 9 | wantErr bool 10 | }{ 11 | {"test #1", "I'm alive!", false}, 12 | } 13 | for _, tt := range tests { 14 | t.Run(tt.name, func(t *testing.T) { 15 | got, err := Live() 16 | if (err != nil) != tt.wantErr { 17 | t.Errorf("Live() error = %v, wantErr %v", err, tt.wantErr) 18 | return 19 | } 20 | if got != tt.want { 21 | t.Errorf("Live() = %v, want %v", got, tt.want) 22 | } 23 | }) 24 | } 25 | } 26 | 27 | func TestReady(t *testing.T) { 28 | tests := []struct { 29 | name string 30 | want string 31 | wantErr bool 32 | }{ 33 | {"test #1", "I'm ready!", false}, 34 | } 35 | for _, tt := range tests { 36 | t.Run(tt.name, func(t *testing.T) { 37 | got, err := Ready() 38 | if (err != nil) != tt.wantErr { 39 | t.Errorf("Ready() error = %v, wantErr %v", err, tt.wantErr) 40 | return 41 | } 42 | if got != tt.want { 43 | t.Errorf("Ready() = %v, want %v", got, tt.want) 44 | } 45 | }) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /example-glide/pkg/fake/hello.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | import ( 5 | "os" 6 | "runtime" 7 | "strings" 8 | "time" 9 | ) 10 | 11 | // Hello is simple method 12 | // It returns greetings contains server hostname and date/time 13 | func Hello() string { 14 | host, err := os.Hostname() 15 | if err != nil { 16 | host = err.Error() 17 | } 18 | s := []string{"Hello World from server ", host, "! ", 19 | "Now is ", time.Now().String(), "! ", 20 | "Go version ", runtime.Version(), " ", runtime.GOOS, "-", runtime.GOARCH, "!"} 21 | return strings.Join(s, "") 22 | } 23 | -------------------------------------------------------------------------------- /example-glide/pkg/fake/hello_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import ( 4 | "strings" 5 | "testing" 6 | ) 7 | 8 | func TestHello(t *testing.T) { 9 | tests := []struct { 10 | name string 11 | want string 12 | }{ 13 | {"test #1", "Hello World from server"}, 14 | } 15 | for _, tt := range tests { 16 | t.Run(tt.name, func(t *testing.T) { 17 | if got := Hello(); strings.Index(got, tt.want) != 0 { 18 | t.Errorf("Hello() = %v, want %v", got, tt.want) 19 | } 20 | }) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /example-godep/Godeps/Godeps.json: -------------------------------------------------------------------------------- 1 | { 2 | "ImportPath": "github.com/amsokol/openshift-golang-template/example-godep", 3 | "GoVersion": "go1.9", 4 | "GodepVersion": "v79", 5 | "Packages": [ 6 | "./..." 7 | ], 8 | "Deps": [ 9 | { 10 | "ImportPath": "github.com/pressly/chi", 11 | "Comment": "v2.1.0-8-g35ec20b", 12 | "Rev": "35ec20b432d9c9d57c5da99623ebffd172433da5" 13 | } 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /example-godep/Godeps/Readme: -------------------------------------------------------------------------------- 1 | This directory tree is generated automatically by godep. 2 | 3 | Please do not edit. 4 | 5 | See https://github.com/tools/godep for more information. 6 | -------------------------------------------------------------------------------- /example-godep/cmd/server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-godep/pkg/cmd/cli" 7 | "github.com/amsokol/openshift-golang-template/example-godep/pkg/cmd/server" 8 | ) 9 | 10 | func main() { 11 | cli.EchoArgs() 12 | if err := server.Start(cli.GetPort()); err != nil { 13 | os.Exit(1) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example-godep/pkg/cmd/cli/params.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // EchoArgs prints arguments are provided via command line 9 | func EchoArgs() { 10 | if len(os.Args) > 1 { 11 | fmt.Println("Command line arguments:", os.Args[1:]) 12 | } else { 13 | fmt.Println("No command line arguments are provided") 14 | } 15 | } 16 | 17 | // GetPort returns HTTP port to listen from environment variable value or default value 8080 18 | func GetPort() string { 19 | port := os.Getenv("PORT") 20 | if len(port) == 0 { 21 | port = "8080" 22 | } 23 | return port 24 | } 25 | -------------------------------------------------------------------------------- /example-godep/pkg/cmd/server/healthz.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-godep/pkg/fake" 7 | ) 8 | 9 | // live provides HTTP endpoint for application liveness probe 10 | func live(w http.ResponseWriter, r *http.Request) { 11 | s, err := fake.Live() 12 | if err != nil { 13 | w.WriteHeader(http.StatusServiceUnavailable) 14 | } else { 15 | w.Write([]byte(s)) 16 | } 17 | } 18 | 19 | // ready provides HTTP endpoint for application readiness probe 20 | func ready(w http.ResponseWriter, r *http.Request) { 21 | s, err := fake.Ready() 22 | if err != nil { 23 | w.WriteHeader(http.StatusServiceUnavailable) 24 | } else { 25 | w.Write([]byte(s)) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /example-godep/pkg/cmd/server/hello.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-godep/pkg/fake" 7 | ) 8 | 9 | // hello provides HTTP endpoint for "Hello" method 10 | func hello(w http.ResponseWriter, r *http.Request) { 11 | w.Write([]byte(fake.Hello())) 12 | } 13 | -------------------------------------------------------------------------------- /example-godep/pkg/cmd/server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "net/http" 7 | "os" 8 | "os/signal" 9 | "time" 10 | 11 | "syscall" 12 | 13 | "github.com/pressly/chi" 14 | ) 15 | 16 | // Start starts HTTP server 17 | func Start(port string) error { 18 | logger := log.New(os.Stdout, "", 0) 19 | 20 | r := chi.NewRouter() 21 | r.Get("/", hello) 22 | r.Get("/healthz/live", live) 23 | r.Get("/healthz/ready", ready) 24 | 25 | h := &http.Server{Addr: ":" + port, Handler: r} 26 | 27 | // Wait for interrupt signal to gracefully shutdown the server with 28 | // a timeout of 10 seconds. 29 | quit := make(chan os.Signal) 30 | signal.Notify(quit, os.Interrupt, os.Kill, syscall.SIGTERM) 31 | go func() { 32 | <-quit 33 | logger.Println("\nShutting down the server...") 34 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 35 | defer cancel() 36 | if err := h.Shutdown(ctx); err != nil { 37 | logger.Println("Error when stopping the server: " + err.Error()) 38 | } else { 39 | logger.Println("Server gracefully stopped") 40 | } 41 | }() 42 | 43 | logger.Printf("Listening on http://0.0.0.0:%s\n", port) 44 | if err := h.ListenAndServe(); err != nil { 45 | logger.Println("Listening returns error: " + err.Error()) 46 | return err 47 | } 48 | 49 | logger.Println("\nServer stopped") 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /example-godep/pkg/fake/healthz.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | // Live emulates application liveness probe 5 | // It returns response if success or error if check failed 6 | func Live() (string, error) { 7 | return "I'm alive!", nil 8 | } 9 | 10 | // Ready emulates application readiness probe 11 | // It returns response if success or error if check failed 12 | func Ready() (string, error) { 13 | return "I'm ready!", nil 14 | } 15 | -------------------------------------------------------------------------------- /example-godep/pkg/fake/healthz_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import "testing" 4 | 5 | func TestLive(t *testing.T) { 6 | tests := []struct { 7 | name string 8 | want string 9 | wantErr bool 10 | }{ 11 | {"test #1", "I'm alive!", false}, 12 | } 13 | for _, tt := range tests { 14 | t.Run(tt.name, func(t *testing.T) { 15 | got, err := Live() 16 | if (err != nil) != tt.wantErr { 17 | t.Errorf("Live() error = %v, wantErr %v", err, tt.wantErr) 18 | return 19 | } 20 | if got != tt.want { 21 | t.Errorf("Live() = %v, want %v", got, tt.want) 22 | } 23 | }) 24 | } 25 | } 26 | 27 | func TestReady(t *testing.T) { 28 | tests := []struct { 29 | name string 30 | want string 31 | wantErr bool 32 | }{ 33 | {"test #1", "I'm ready!", false}, 34 | } 35 | for _, tt := range tests { 36 | t.Run(tt.name, func(t *testing.T) { 37 | got, err := Ready() 38 | if (err != nil) != tt.wantErr { 39 | t.Errorf("Ready() error = %v, wantErr %v", err, tt.wantErr) 40 | return 41 | } 42 | if got != tt.want { 43 | t.Errorf("Ready() = %v, want %v", got, tt.want) 44 | } 45 | }) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /example-godep/pkg/fake/hello.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | import ( 5 | "os" 6 | "runtime" 7 | "strings" 8 | "time" 9 | ) 10 | 11 | // Hello is simple method 12 | // It returns greetings contains server hostname and date/time 13 | func Hello() string { 14 | host, err := os.Hostname() 15 | if err != nil { 16 | host = err.Error() 17 | } 18 | s := []string{"Hello World from server ", host, "! ", 19 | "Now is ", time.Now().String(), "! ", 20 | "Go version ", runtime.Version(), " ", runtime.GOOS, "-", runtime.GOARCH, "!"} 21 | return strings.Join(s, "") 22 | } 23 | -------------------------------------------------------------------------------- /example-godep/pkg/fake/hello_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import ( 4 | "strings" 5 | "testing" 6 | ) 7 | 8 | func TestHello(t *testing.T) { 9 | tests := []struct { 10 | name string 11 | want string 12 | }{ 13 | {"test #1", "Hello World from server"}, 14 | } 15 | for _, tt := range tests { 16 | t.Run(tt.name, func(t *testing.T) { 17 | if got := Hello(); strings.Index(got, tt.want) != 0 { 18 | t.Errorf("Hello() = %v, want %v", got, tt.want) 19 | } 20 | }) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /example-golang-dep/Gopkg.toml: -------------------------------------------------------------------------------- 1 | 2 | ## Gopkg.toml example (these lines may be deleted) 3 | 4 | ## "metadata" defines metadata about the project that could be used by other independent 5 | ## systems. The metadata defined here will be ignored by dep. 6 | # [metadata] 7 | # key1 = "value that convey data to other systems" 8 | # system1-data = "value that is used by a system" 9 | # system2-data = "value that is used by another system" 10 | 11 | ## "required" lists a set of packages (not projects) that must be included in 12 | ## Gopkg.lock. This list is merged with the set of packages imported by the current 13 | ## project. Use it when your project needs a package it doesn't explicitly import - 14 | ## including "main" packages. 15 | # required = ["github.com/user/thing/cmd/thing"] 16 | 17 | ## "ignored" lists a set of packages (not projects) that are ignored when 18 | ## dep statically analyzes source code. Ignored packages can be in this project, 19 | ## or in a dependency. 20 | # ignored = ["github.com/user/project/badpkg"] 21 | 22 | ## Constraints are rules for how directly imported projects 23 | ## may be incorporated into the depgraph. They are respected by 24 | ## dep whether coming from the Gopkg.toml of the current project or a dependency. 25 | # [[constraint]] 26 | ## Required: the root import path of the project being constrained. 27 | # name = "github.com/user/project" 28 | # 29 | ## Recommended: the version constraint to enforce for the project. 30 | ## Only one of "branch", "version" or "revision" can be specified. 31 | # version = "1.0.0" 32 | # branch = "master" 33 | # revision = "abc123" 34 | # 35 | ## Optional: an alternate location (URL or import path) for the project's source. 36 | # source = "https://github.com/myfork/package.git" 37 | # 38 | ## "metadata" defines metadata about the dependency or override that could be used 39 | ## by other independent systems. The metadata defined here will be ignored by dep. 40 | # [metadata] 41 | # key1 = "value that convey data to other systems" 42 | # system1-data = "value that is used by a system" 43 | # system2-data = "value that is used by another system" 44 | 45 | ## Overrides have the same structure as [[constraint]], but supersede all 46 | ## [[constraint]] declarations from all projects. Only [[override]] from 47 | ## the current project's are applied. 48 | ## 49 | ## Overrides are a sledgehammer. Use them only as a last resort. 50 | # [[override]] 51 | ## Required: the root import path of the project being constrained. 52 | # name = "github.com/user/project" 53 | # 54 | ## Optional: specifying a version constraint override will cause all other 55 | ## constraints on this project to be ignored; only the overridden constraint 56 | ## need be satisfied. 57 | ## Again, only one of "branch", "version" or "revision" can be specified. 58 | # version = "1.0.0" 59 | # branch = "master" 60 | # revision = "abc123" 61 | # 62 | ## Optional: specifying an alternate source location as an override will 63 | ## enforce that the alternate location is used for that project, regardless of 64 | ## what source location any dependent projects specify. 65 | # source = "https://github.com/myfork/package.git" 66 | 67 | 68 | 69 | [[constraint]] 70 | name = "github.com/pressly/chi" 71 | version = "2.1.0" 72 | -------------------------------------------------------------------------------- /example-golang-dep/cmd/server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-golang-dep/pkg/cmd/cli" 7 | "github.com/amsokol/openshift-golang-template/example-golang-dep/pkg/cmd/server" 8 | ) 9 | 10 | func main() { 11 | cli.EchoArgs() 12 | if err := server.Start(cli.GetPort()); err != nil { 13 | os.Exit(1) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example-golang-dep/pkg/cmd/cli/params.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // EchoArgs prints arguments are provided via command line 9 | func EchoArgs() { 10 | if len(os.Args) > 1 { 11 | fmt.Println("Command line arguments:", os.Args[1:]) 12 | } else { 13 | fmt.Println("No command line arguments are provided") 14 | } 15 | } 16 | 17 | // GetPort returns HTTP port to listen from environment variable value or default value 8080 18 | func GetPort() string { 19 | port := os.Getenv("PORT") 20 | if len(port) == 0 { 21 | port = "8080" 22 | } 23 | return port 24 | } 25 | -------------------------------------------------------------------------------- /example-golang-dep/pkg/cmd/server/healthz.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-golang-dep/pkg/fake" 7 | ) 8 | 9 | // live provides HTTP endpoint for application liveness probe 10 | func live(w http.ResponseWriter, r *http.Request) { 11 | s, err := fake.Live() 12 | if err != nil { 13 | w.WriteHeader(http.StatusServiceUnavailable) 14 | } else { 15 | w.Write([]byte(s)) 16 | } 17 | } 18 | 19 | // ready provides HTTP endpoint for application readiness probe 20 | func ready(w http.ResponseWriter, r *http.Request) { 21 | s, err := fake.Ready() 22 | if err != nil { 23 | w.WriteHeader(http.StatusServiceUnavailable) 24 | } else { 25 | w.Write([]byte(s)) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /example-golang-dep/pkg/cmd/server/hello.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-golang-dep/pkg/fake" 7 | ) 8 | 9 | // hello provides HTTP endpoint for "Hello" method 10 | func hello(w http.ResponseWriter, r *http.Request) { 11 | w.Write([]byte(fake.Hello())) 12 | } 13 | -------------------------------------------------------------------------------- /example-golang-dep/pkg/cmd/server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "net/http" 7 | "os" 8 | "os/signal" 9 | "time" 10 | 11 | "syscall" 12 | 13 | "github.com/pressly/chi" 14 | ) 15 | 16 | // Start starts HTTP server 17 | func Start(port string) error { 18 | logger := log.New(os.Stdout, "", 0) 19 | 20 | r := chi.NewRouter() 21 | r.Get("/", hello) 22 | r.Get("/healthz/live", live) 23 | r.Get("/healthz/ready", ready) 24 | 25 | h := &http.Server{Addr: ":" + port, Handler: r} 26 | 27 | // Wait for interrupt signal to gracefully shutdown the server with 28 | // a timeout of 10 seconds. 29 | quit := make(chan os.Signal) 30 | signal.Notify(quit, os.Interrupt, os.Kill, syscall.SIGTERM) 31 | go func() { 32 | <-quit 33 | logger.Println("\nShutting down the server...") 34 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 35 | defer cancel() 36 | if err := h.Shutdown(ctx); err != nil { 37 | logger.Println("Error when stopping the server: " + err.Error()) 38 | } else { 39 | logger.Println("Server gracefully stopped") 40 | } 41 | }() 42 | 43 | logger.Printf("Listening on http://0.0.0.0:%s\n", port) 44 | if err := h.ListenAndServe(); err != nil { 45 | logger.Println("Listening returns error: " + err.Error()) 46 | return err 47 | } 48 | 49 | logger.Println("\nServer stopped") 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /example-golang-dep/pkg/fake/healthz.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | // Live emulates application liveness probe 5 | // It returns response if success or error if check failed 6 | func Live() (string, error) { 7 | return "I'm alive!", nil 8 | } 9 | 10 | // Ready emulates application readiness probe 11 | // It returns response if success or error if check failed 12 | func Ready() (string, error) { 13 | return "I'm ready!", nil 14 | } 15 | -------------------------------------------------------------------------------- /example-golang-dep/pkg/fake/healthz_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import "testing" 4 | 5 | func TestLive(t *testing.T) { 6 | tests := []struct { 7 | name string 8 | want string 9 | wantErr bool 10 | }{ 11 | {"test #1", "I'm alive!", false}, 12 | } 13 | for _, tt := range tests { 14 | t.Run(tt.name, func(t *testing.T) { 15 | got, err := Live() 16 | if (err != nil) != tt.wantErr { 17 | t.Errorf("Live() error = %v, wantErr %v", err, tt.wantErr) 18 | return 19 | } 20 | if got != tt.want { 21 | t.Errorf("Live() = %v, want %v", got, tt.want) 22 | } 23 | }) 24 | } 25 | } 26 | 27 | func TestReady(t *testing.T) { 28 | tests := []struct { 29 | name string 30 | want string 31 | wantErr bool 32 | }{ 33 | {"test #1", "I'm ready!", false}, 34 | } 35 | for _, tt := range tests { 36 | t.Run(tt.name, func(t *testing.T) { 37 | got, err := Ready() 38 | if (err != nil) != tt.wantErr { 39 | t.Errorf("Ready() error = %v, wantErr %v", err, tt.wantErr) 40 | return 41 | } 42 | if got != tt.want { 43 | t.Errorf("Ready() = %v, want %v", got, tt.want) 44 | } 45 | }) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /example-golang-dep/pkg/fake/hello.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | import ( 5 | "os" 6 | "runtime" 7 | "strings" 8 | "time" 9 | ) 10 | 11 | // Hello is simple method 12 | // It returns greetings contains server hostname and date/time 13 | func Hello() string { 14 | host, err := os.Hostname() 15 | if err != nil { 16 | host = err.Error() 17 | } 18 | s := []string{"Hello World from server ", host, "! ", 19 | "Now is ", time.Now().String(), "! ", 20 | "Go version ", runtime.Version(), " ", runtime.GOOS, "-", runtime.GOARCH, "!"} 21 | return strings.Join(s, "") 22 | } 23 | -------------------------------------------------------------------------------- /example-golang-dep/pkg/fake/hello_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import ( 4 | "strings" 5 | "testing" 6 | ) 7 | 8 | func TestHello(t *testing.T) { 9 | tests := []struct { 10 | name string 11 | want string 12 | }{ 13 | {"test #1", "Hello World from server"}, 14 | } 15 | for _, tt := range tests { 16 | t.Run(tt.name, func(t *testing.T) { 17 | if got := Hello(); strings.Index(got, tt.want) != 0 { 18 | t.Errorf("Hello() = %v, want %v", got, tt.want) 19 | } 20 | }) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /example-govendor/cmd/server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-govendor/pkg/cmd/cli" 7 | "github.com/amsokol/openshift-golang-template/example-govendor/pkg/cmd/server" 8 | ) 9 | 10 | func main() { 11 | cli.EchoArgs() 12 | if err := server.Start(cli.GetPort()); err != nil { 13 | os.Exit(1) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example-govendor/pkg/cmd/cli/params.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // EchoArgs prints arguments are provided via command line 9 | func EchoArgs() { 10 | if len(os.Args) > 1 { 11 | fmt.Println("Command line arguments:", os.Args[1:]) 12 | } else { 13 | fmt.Println("No command line arguments are provided") 14 | } 15 | } 16 | 17 | // GetPort returns HTTP port to listen from environment variable value or default value 8080 18 | func GetPort() string { 19 | port := os.Getenv("PORT") 20 | if len(port) == 0 { 21 | port = "8080" 22 | } 23 | return port 24 | } 25 | -------------------------------------------------------------------------------- /example-govendor/pkg/cmd/server/healthz.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-govendor/pkg/fake" 7 | ) 8 | 9 | // live provides HTTP endpoint for application liveness probe 10 | func live(w http.ResponseWriter, r *http.Request) { 11 | s, err := fake.Live() 12 | if err != nil { 13 | w.WriteHeader(http.StatusServiceUnavailable) 14 | } else { 15 | w.Write([]byte(s)) 16 | } 17 | } 18 | 19 | // ready provides HTTP endpoint for application readiness probe 20 | func ready(w http.ResponseWriter, r *http.Request) { 21 | s, err := fake.Ready() 22 | if err != nil { 23 | w.WriteHeader(http.StatusServiceUnavailable) 24 | } else { 25 | w.Write([]byte(s)) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /example-govendor/pkg/cmd/server/hello.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example-govendor/pkg/fake" 7 | ) 8 | 9 | // hello provides HTTP endpoint for "Hello" method 10 | func hello(w http.ResponseWriter, r *http.Request) { 11 | w.Write([]byte(fake.Hello())) 12 | } 13 | -------------------------------------------------------------------------------- /example-govendor/pkg/cmd/server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "net/http" 7 | "os" 8 | "os/signal" 9 | "time" 10 | 11 | "syscall" 12 | 13 | "github.com/pressly/chi" 14 | ) 15 | 16 | // Start starts HTTP server 17 | func Start(port string) error { 18 | logger := log.New(os.Stdout, "", 0) 19 | 20 | r := chi.NewRouter() 21 | r.Get("/", hello) 22 | r.Get("/healthz/live", live) 23 | r.Get("/healthz/ready", ready) 24 | 25 | h := &http.Server{Addr: ":" + port, Handler: r} 26 | 27 | // Wait for interrupt signal to gracefully shutdown the server with 28 | // a timeout of 10 seconds. 29 | quit := make(chan os.Signal) 30 | signal.Notify(quit, os.Interrupt, os.Kill, syscall.SIGTERM) 31 | go func() { 32 | <-quit 33 | logger.Println("\nShutting down the server...") 34 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 35 | defer cancel() 36 | if err := h.Shutdown(ctx); err != nil { 37 | logger.Println("Error when stopping the server: " + err.Error()) 38 | } else { 39 | logger.Println("Server gracefully stopped") 40 | } 41 | }() 42 | 43 | logger.Printf("Listening on http://0.0.0.0:%s\n", port) 44 | if err := h.ListenAndServe(); err != nil { 45 | logger.Println("Listening returns error: " + err.Error()) 46 | return err 47 | } 48 | 49 | logger.Println("\nServer stopped") 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /example-govendor/pkg/fake/healthz.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | // Live emulates application liveness probe 5 | // It returns response if success or error if check failed 6 | func Live() (string, error) { 7 | return "I'm alive!", nil 8 | } 9 | 10 | // Ready emulates application readiness probe 11 | // It returns response if success or error if check failed 12 | func Ready() (string, error) { 13 | return "I'm ready!", nil 14 | } 15 | -------------------------------------------------------------------------------- /example-govendor/pkg/fake/healthz_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import "testing" 4 | 5 | func TestLive(t *testing.T) { 6 | tests := []struct { 7 | name string 8 | want string 9 | wantErr bool 10 | }{ 11 | {"test #1", "I'm alive!", false}, 12 | } 13 | for _, tt := range tests { 14 | t.Run(tt.name, func(t *testing.T) { 15 | got, err := Live() 16 | if (err != nil) != tt.wantErr { 17 | t.Errorf("Live() error = %v, wantErr %v", err, tt.wantErr) 18 | return 19 | } 20 | if got != tt.want { 21 | t.Errorf("Live() = %v, want %v", got, tt.want) 22 | } 23 | }) 24 | } 25 | } 26 | 27 | func TestReady(t *testing.T) { 28 | tests := []struct { 29 | name string 30 | want string 31 | wantErr bool 32 | }{ 33 | {"test #1", "I'm ready!", false}, 34 | } 35 | for _, tt := range tests { 36 | t.Run(tt.name, func(t *testing.T) { 37 | got, err := Ready() 38 | if (err != nil) != tt.wantErr { 39 | t.Errorf("Ready() error = %v, wantErr %v", err, tt.wantErr) 40 | return 41 | } 42 | if got != tt.want { 43 | t.Errorf("Ready() = %v, want %v", got, tt.want) 44 | } 45 | }) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /example-govendor/pkg/fake/hello.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | import ( 5 | "os" 6 | "runtime" 7 | "strings" 8 | "time" 9 | ) 10 | 11 | // Hello is simple method 12 | // It returns greetings contains server hostname and date/time 13 | func Hello() string { 14 | host, err := os.Hostname() 15 | if err != nil { 16 | host = err.Error() 17 | } 18 | s := []string{"Hello World from server ", host, "! ", 19 | "Now is ", time.Now().String(), "! ", 20 | "Go version ", runtime.Version(), " ", runtime.GOOS, "-", runtime.GOARCH, "!"} 21 | return strings.Join(s, "") 22 | } 23 | -------------------------------------------------------------------------------- /example-govendor/pkg/fake/hello_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import ( 4 | "strings" 5 | "testing" 6 | ) 7 | 8 | func TestHello(t *testing.T) { 9 | tests := []struct { 10 | name string 11 | want string 12 | }{ 13 | {"test #1", "Hello World from server"}, 14 | } 15 | for _, tt := range tests { 16 | t.Run(tt.name, func(t *testing.T) { 17 | if got := Hello(); strings.Index(got, tt.want) != 0 { 18 | t.Errorf("Hello() = %v, want %v", got, tt.want) 19 | } 20 | }) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /example-govendor/vendor/vendor.json: -------------------------------------------------------------------------------- 1 | { 2 | "comment": "", 3 | "ignore": "test", 4 | "package": [ 5 | { 6 | "checksumSHA1": "N+jpK3LNT+lFjGuBIw9RLOMnQx0=", 7 | "path": "github.com/pressly/chi", 8 | "revision": "e6033ea75479391a4bce3918fc119cad31e1cb30", 9 | "revisionTime": "2017-03-30T18:40:54Z", 10 | "version": "v2", 11 | "versionExact": "v2.1.0" 12 | } 13 | ], 14 | "rootPath": "github.com/amsokol/openshift-golang-template/example-govendor" 15 | } 16 | -------------------------------------------------------------------------------- /example/cmd/server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/amsokol/openshift-golang-template/example/pkg/cmd/cli" 7 | "github.com/amsokol/openshift-golang-template/example/pkg/cmd/server" 8 | ) 9 | 10 | func main() { 11 | cli.EchoArgs() 12 | if err := server.Start(cli.GetPort()); err != nil { 13 | os.Exit(1) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example/pkg/cmd/cli/params.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | // EchoArgs prints arguments are provided via command line 9 | func EchoArgs() { 10 | if len(os.Args) > 1 { 11 | fmt.Println("Command line arguments:", os.Args[1:]) 12 | } else { 13 | fmt.Println("No command line arguments are provided") 14 | } 15 | } 16 | 17 | // GetPort returns HTTP port to listen from environment variable value or default value 8080 18 | func GetPort() string { 19 | port := os.Getenv("PORT") 20 | if len(port) == 0 { 21 | port = "8080" 22 | } 23 | return port 24 | } 25 | -------------------------------------------------------------------------------- /example/pkg/cmd/server/healthz.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example/pkg/fake" 7 | ) 8 | 9 | // live provides HTTP endpoint for application liveness probe 10 | func live(w http.ResponseWriter, r *http.Request) { 11 | s, err := fake.Live() 12 | if err != nil { 13 | w.WriteHeader(http.StatusServiceUnavailable) 14 | } else { 15 | w.Write([]byte(s)) 16 | } 17 | } 18 | 19 | // ready provides HTTP endpoint for application readiness probe 20 | func ready(w http.ResponseWriter, r *http.Request) { 21 | s, err := fake.Ready() 22 | if err != nil { 23 | w.WriteHeader(http.StatusServiceUnavailable) 24 | } else { 25 | w.Write([]byte(s)) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /example/pkg/cmd/server/hello.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/amsokol/openshift-golang-template/example/pkg/fake" 7 | ) 8 | 9 | // hello provides HTTP endpoint for "Hello" method 10 | func hello(w http.ResponseWriter, r *http.Request) { 11 | w.Write([]byte(fake.Hello())) 12 | } 13 | -------------------------------------------------------------------------------- /example/pkg/cmd/server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "net/http" 7 | "os" 8 | "os/signal" 9 | "time" 10 | 11 | "syscall" 12 | 13 | "github.com/pressly/chi" 14 | ) 15 | 16 | // Start starts HTTP server 17 | func Start(port string) error { 18 | logger := log.New(os.Stdout, "", 0) 19 | 20 | r := chi.NewRouter() 21 | r.Get("/", hello) 22 | r.Get("/healthz/live", live) 23 | r.Get("/healthz/ready", ready) 24 | 25 | h := &http.Server{Addr: ":" + port, Handler: r} 26 | 27 | // Wait for interrupt signal to gracefully shutdown the server with 28 | // a timeout of 10 seconds. 29 | quit := make(chan os.Signal) 30 | signal.Notify(quit, os.Interrupt, os.Kill, syscall.SIGTERM) 31 | go func() { 32 | <-quit 33 | logger.Println("\nShutting down the server...") 34 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 35 | defer cancel() 36 | if err := h.Shutdown(ctx); err != nil { 37 | logger.Println("Error when stopping the server: " + err.Error()) 38 | } else { 39 | logger.Println("Server gracefully stopped") 40 | } 41 | }() 42 | 43 | logger.Printf("Listening on http://0.0.0.0:%s\n", port) 44 | if err := h.ListenAndServe(); err != nil { 45 | logger.Println("Listening returns error: " + err.Error()) 46 | return err 47 | } 48 | 49 | logger.Println("\nServer stopped") 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /example/pkg/fake/healthz.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | // Live emulates application liveness probe 5 | // It returns response if success or error if check failed 6 | func Live() (string, error) { 7 | return "I'm alive!", nil 8 | } 9 | 10 | // Ready emulates application readiness probe 11 | // It returns response if success or error if check failed 12 | func Ready() (string, error) { 13 | return "I'm ready!", nil 14 | } 15 | -------------------------------------------------------------------------------- /example/pkg/fake/healthz_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import "testing" 4 | 5 | func TestLive(t *testing.T) { 6 | tests := []struct { 7 | name string 8 | want string 9 | wantErr bool 10 | }{ 11 | {"test #1", "I'm alive!", false}, 12 | } 13 | for _, tt := range tests { 14 | t.Run(tt.name, func(t *testing.T) { 15 | got, err := Live() 16 | if (err != nil) != tt.wantErr { 17 | t.Errorf("Live() error = %v, wantErr %v", err, tt.wantErr) 18 | return 19 | } 20 | if got != tt.want { 21 | t.Errorf("Live() = %v, want %v", got, tt.want) 22 | } 23 | }) 24 | } 25 | } 26 | 27 | func TestReady(t *testing.T) { 28 | tests := []struct { 29 | name string 30 | want string 31 | wantErr bool 32 | }{ 33 | {"test #1", "I'm ready!", false}, 34 | } 35 | for _, tt := range tests { 36 | t.Run(tt.name, func(t *testing.T) { 37 | got, err := Ready() 38 | if (err != nil) != tt.wantErr { 39 | t.Errorf("Ready() error = %v, wantErr %v", err, tt.wantErr) 40 | return 41 | } 42 | if got != tt.want { 43 | t.Errorf("Ready() = %v, want %v", got, tt.want) 44 | } 45 | }) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /example/pkg/fake/hello.go: -------------------------------------------------------------------------------- 1 | // Package fake provides sample methods 2 | package fake 3 | 4 | import ( 5 | "os" 6 | "runtime" 7 | "strings" 8 | "time" 9 | ) 10 | 11 | // Hello is simple method 12 | // It returns greetings contains server hostname and date/time 13 | func Hello() string { 14 | host, err := os.Hostname() 15 | if err != nil { 16 | host = err.Error() 17 | } 18 | s := []string{"Hello World from server ", host, "! ", 19 | "Now is ", time.Now().String(), "! ", 20 | "Go version ", runtime.Version(), " ", runtime.GOOS, "-", runtime.GOARCH, "!"} 21 | return strings.Join(s, "") 22 | } 23 | -------------------------------------------------------------------------------- /example/pkg/fake/hello_test.go: -------------------------------------------------------------------------------- 1 | package fake 2 | 3 | import ( 4 | "strings" 5 | "testing" 6 | ) 7 | 8 | func TestHello(t *testing.T) { 9 | tests := []struct { 10 | name string 11 | want string 12 | }{ 13 | {"test #1", "Hello World from server"}, 14 | } 15 | for _, tt := range tests { 16 | t.Run(tt.name, func(t *testing.T) { 17 | if got := Hello(); strings.Index(got, tt.want) != 0 { 18 | t.Errorf("Hello() = %v, want %v", got, tt.want) 19 | } 20 | }) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /openshift-golang-template.yaml: -------------------------------------------------------------------------------- 1 | kind: ImageStream 2 | apiVersion: v1 3 | metadata: 4 | name: golang 5 | annotations: 6 | openshift.io/display-name: Golang 7 | spec: 8 | tags: 9 | - name: latest 10 | annotations: 11 | openshift.io/display-name: Golang (Latest) 12 | description: Build and run Go applications on CentOS 7. 13 | iconClass: icon-go-gopher 14 | tags: builder,golang 15 | supports: nodejs 16 | sampleRepo: https://github.com/amsokol/openshift-golang-template.git 17 | from: 18 | kind: ImageStreamTag 19 | name: '1.9.2' 20 | - name: '1.8.1' 21 | annotations: 22 | openshift.io/display-name: Golang 1.8.1 23 | description: | 24 | Build and run Go 1.8.1 applications on CentOS 7. 25 | 26 | IMPORTANT: environment variables GOPROJECT_ROOT and GOPROJECT_CMD must be set by corresponding values in "Build Configuration" section, "Environment Variables (Build and Runtime)" list. 27 | For example, git URL is "https://github.com/amsokol/openshift-golang-template.git", context directory is "/example-golang-dep" and "main()" function of "main" package is located in "/example-golang-dep/cmd/server". 28 | In this case set GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep and GOPROJECT_CMD=cmd/server 29 | iconClass: icon-go-gopher 30 | tags: builder,golang 31 | supports: golang:1.8.1,golang 32 | version: '1.8.1' 33 | sampleRepo: https://github.com/amsokol/openshift-golang-template.git 34 | from: 35 | kind: DockerImage 36 | name: docker.io/amsokol/golang-openshift:1.8.1-7 37 | - name: '1.8.2' 38 | annotations: 39 | openshift.io/display-name: Golang 1.8.2 40 | description: | 41 | Build and run Go 1.8.2 applications on CentOS 7. 42 | 43 | IMPORTANT: environment variables GOPROJECT_ROOT and GOPROJECT_CMD must be set by corresponding values in "Build Configuration" section, "Environment Variables (Build and Runtime)" list. 44 | For example, git URL is "https://github.com/amsokol/openshift-golang-template.git", context directory is "/example-golang-dep" and "main()" function of "main" package is located in "/example-golang-dep/cmd/server". 45 | In this case set GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep and GOPROJECT_CMD=cmd/server 46 | iconClass: icon-go-gopher 47 | tags: builder,golang 48 | supports: golang:1.8.2,golang 49 | version: '1.8.2' 50 | sampleRepo: https://github.com/amsokol/openshift-golang-template.git 51 | from: 52 | kind: DockerImage 53 | name: docker.io/amsokol/golang-openshift:1.8.2-3 54 | - name: '1.8.3' 55 | annotations: 56 | openshift.io/display-name: Golang 1.8.3 57 | description: | 58 | Build and run Go 1.8.3 applications on CentOS 7. 59 | 60 | IMPORTANT: environment variables GOPROJECT_ROOT and GOPROJECT_CMD must be set by corresponding values in "Build Configuration" section, "Environment Variables (Build and Runtime)" list. 61 | For example, git URL is "https://github.com/amsokol/openshift-golang-template.git", context directory is "/example-golang-dep" and "main()" function of "main" package is located in "/example-golang-dep/cmd/server". 62 | In this case set GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep and GOPROJECT_CMD=cmd/server 63 | iconClass: icon-go-gopher 64 | tags: builder,golang 65 | supports: golang:1.8.3,golang 66 | version: '1.8.3' 67 | sampleRepo: https://github.com/amsokol/openshift-golang-template.git 68 | from: 69 | kind: DockerImage 70 | name: docker.io/amsokol/golang-openshift:1.8.3-3 71 | - name: '1.8.4' 72 | annotations: 73 | openshift.io/display-name: Golang 1.8.4 74 | description: | 75 | Build and run Go 1.8.4 applications on CentOS 7. 76 | 77 | IMPORTANT: environment variables GOPROJECT_ROOT and GOPROJECT_CMD must be set by corresponding values in "Build Configuration" section, "Environment Variables (Build and Runtime)" list. 78 | For example, git URL is "https://github.com/amsokol/openshift-golang-template.git", context directory is "/example-golang-dep" and "main()" function of "main" package is located in "/example-golang-dep/cmd/server". 79 | In this case set GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep and GOPROJECT_CMD=cmd/server 80 | iconClass: icon-go-gopher 81 | tags: builder,golang 82 | supports: golang:1.8.4,golang 83 | version: '1.8.4' 84 | sampleRepo: https://github.com/amsokol/openshift-golang-template.git 85 | from: 86 | kind: DockerImage 87 | name: docker.io/amsokol/golang-openshift:1.8.4-1 88 | - name: '1.8.5' 89 | annotations: 90 | openshift.io/display-name: Golang 1.8.5 91 | description: | 92 | Build and run Go 1.8.5 applications on CentOS 7. 93 | 94 | IMPORTANT: environment variables GOPROJECT_ROOT and GOPROJECT_CMD must be set by corresponding values in "Build Configuration" section, "Environment Variables (Build and Runtime)" list. 95 | For example, git URL is "https://github.com/amsokol/openshift-golang-template.git", context directory is "/example-golang-dep" and "main()" function of "main" package is located in "/example-golang-dep/cmd/server". 96 | In this case set GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep and GOPROJECT_CMD=cmd/server 97 | iconClass: icon-go-gopher 98 | tags: builder,golang 99 | supports: golang:1.8.5,golang 100 | version: '1.8.5' 101 | sampleRepo: https://github.com/amsokol/openshift-golang-template.git 102 | from: 103 | kind: DockerImage 104 | name: docker.io/amsokol/golang-openshift:1.8.5-1 105 | - name: '1.9.0' 106 | annotations: 107 | openshift.io/display-name: Golang 1.9.0 108 | description: | 109 | Build and run Go 1.9.0 applications on CentOS 7. 110 | 111 | IMPORTANT: environment variables GOPROJECT_ROOT and GOPROJECT_CMD must be set by corresponding values in "Build Configuration" section, "Environment Variables (Build and Runtime)" list. 112 | For example, git URL is "https://github.com/amsokol/openshift-golang-template.git", context directory is "/example-golang-dep" and "main()" function of "main" package is located in "/example-golang-dep/cmd/server". 113 | In this case set GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep and GOPROJECT_CMD=cmd/server 114 | iconClass: icon-go-gopher 115 | tags: builder,golang 116 | supports: golang:1.9.0,golang 117 | version: '1.9.0' 118 | sampleRepo: https://github.com/amsokol/openshift-golang-template.git 119 | from: 120 | kind: DockerImage 121 | name: docker.io/amsokol/golang-openshift:1.9.0-1 122 | - name: '1.9.1' 123 | annotations: 124 | openshift.io/display-name: Golang 1.9.1 125 | description: | 126 | Build and run Go 1.9.1 applications on CentOS 7. 127 | 128 | IMPORTANT: environment variables GOPROJECT_ROOT and GOPROJECT_CMD must be set by corresponding values in "Build Configuration" section, "Environment Variables (Build and Runtime)" list. 129 | For example, git URL is "https://github.com/amsokol/openshift-golang-template.git", context directory is "/example-golang-dep" and "main()" function of "main" package is located in "/example-golang-dep/cmd/server". 130 | In this case set GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep and GOPROJECT_CMD=cmd/server 131 | iconClass: icon-go-gopher 132 | tags: builder,golang 133 | supports: golang:1.9.1,golang 134 | version: '1.9.1' 135 | sampleRepo: https://github.com/amsokol/openshift-golang-template.git 136 | from: 137 | kind: DockerImage 138 | name: docker.io/amsokol/golang-openshift:1.9.2-1 139 | - name: '1.9.2' 140 | annotations: 141 | openshift.io/display-name: Golang 1.9.2 142 | description: | 143 | Build and run Go 1.9.2 applications on CentOS 7. 144 | 145 | IMPORTANT: environment variables GOPROJECT_ROOT and GOPROJECT_CMD must be set by corresponding values in "Build Configuration" section, "Environment Variables (Build and Runtime)" list. 146 | For example, git URL is "https://github.com/amsokol/openshift-golang-template.git", context directory is "/example-golang-dep" and "main()" function of "main" package is located in "/example-golang-dep/cmd/server". 147 | In this case set GOPROJECT_ROOT=github.com/amsokol/openshift-golang-template/example-golang-dep and GOPROJECT_CMD=cmd/server 148 | iconClass: icon-go-gopher 149 | tags: builder,golang 150 | supports: golang:1.9.2,golang 151 | version: '1.9.2' 152 | sampleRepo: https://github.com/amsokol/openshift-golang-template.git 153 | from: 154 | kind: DockerImage 155 | name: docker.io/amsokol/golang-openshift:1.9.2-1 156 | --------------------------------------------------------------------------------