Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pendoragon committed May 21, 2020
0 parents commit df18cba
Show file tree
Hide file tree
Showing 25 changed files with 566 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Issue template; please answer the questions. -->

**Is this a BUG REPORT or FEATURE REQUEST?**:

> Uncomment only one, leave it on its own line:
>
> /kind bug
> /kind feature
**What happened**:

**What you expected to happen**:

**How to reproduce it (as minimally and precisely as possible)**:

**Anything else we need to know?**:
46 changes: 46 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!-- Thanks for sending a pull request! See below for tips! -->

**What this PR does / why we need it**:

Add your description

**Which issue(s) this PR is related to** *(optional, link to 3rd issue(s))*:

Fixes #

Reference to #
<!-- 填在 Fixes,PR 合并就会关 issue。填在 Reference to 会关联 issue,不会联动关闭。-->

**Special notes for your reviewer**:

/cc @your-reviewer

<!-- Please answer the following questions during the code freeze, and delete this line.
**Code freeze questions**
1. What causes this PR to not be merged before code freeze?
2. Why this PR is absolutely necessary for this version? Paste a screenshot of smoke testing docs if you could.
3. What's the effects after merging it?
4. Is there anyway we can skip this to not affect the overall process?
-->

**Release note**:
<!-- Write your release note:
1. Enter your extended release note in the below block. If the PR requires additional action from users switching to the new release, include the string "action required".
2. If no release note is required, just write "NONE".
-->

```release-note
NONE
```

<!-- Thanks for sending a pull request! Here are some tips:
1. https://github.com/caicloud/engineering/blob/master/guidelines/review_conventions.md <-- what is the review process looks like
2. https://github.com/caicloud/engineering/blob/master/guidelines/git_commit_conventions.md <-- how to structure your git commit
3. https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md <-- how to work with caicloud bot
Other tips:
If this is your first contribution, read our Getting Started guide https://github.com/caicloud/engineering/blob/master/guidelines/README.md
-->
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Output folder of build
bin

# Output file of unit test coverage
coverage.out
26 changes: 26 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
run:
# concurrency: 2
deadline: 5m

linter-settings:
goconst:
min-len: 2
min-occurrences: 2

linters:
enable:
- golint
- goconst
- gofmt
- goimports
- misspell
- unparam

issues:
exclude-use-default: false
exclude-rules:
- path: _test.go
linters:
- errcheck
exclude:
- (comment on exported (method|function|type|const)|should have( a package)? comment|comment should be of the form)
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- DOCTOC SKIP -->

# Change Log

## [v0.1.0]

* This is not a real project so we hand-write the note. Production project should auto-generate the changelog file.
148 changes: 148 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Copyright 2019 The Caicloud Authors.
#
# The old school Makefile, following are required targets. The Makefile is written
# to allow building multiple binaries. You are free to add more targets or change
# existing implementations, as long as the semantics are preserved.
#
# make - default to 'build' target
# make lint - code analysis
# make test - run unit test (or plus integration test)
# make build - alias to build-local target
# make build-local - build local binary targets
# make build-linux - build linux binary targets
# make container - build containers
# $ docker login registry -u username -p xxxxx
# make push - push containers
# make clean - clean up targets
#
# Not included but recommended targets:
# make e2e-test
#
# The makefile is also responsible to populate project version information.
#

#
# Tweak the variables based on your project.
#

# This repo's root import path (under GOPATH).
ROOT := github.com/caicloud/golang-template-project

# Target binaries. You can build multiple binaries for a single project.
TARGETS := admin controller

# Container image prefix and suffix added to targets.
# The final built images are:
# $[REGISTRY]/$[IMAGE_PREFIX]$[TARGET]$[IMAGE_SUFFIX]:$[VERSION]
# $[REGISTRY] is an item from $[REGISTRIES], $[TARGET] is an item from $[TARGETS].
IMAGE_PREFIX ?= $(strip template-)
IMAGE_SUFFIX ?= $(strip )

# Container registries.
REGISTRY ?= cargo.dev.caicloud.xyz/release

# Container registry for base images.
BASE_REGISTRY ?= cargo.caicloud.xyz/library

#
# These variables should not need tweaking.
#

# It's necessary to set this because some environments don't link sh -> bash.
export SHELL := /bin/bash

# It's necessary to set the errexit flags for the bash shell.
export SHELLOPTS := errexit

# Project main package location (can be multiple ones).
CMD_DIR := ./cmd

# Project output directory.
OUTPUT_DIR := ./bin

# Build direcotory.
BUILD_DIR := ./build

# Current version of the project.
VERSION ?= $(shell git describe --tags --always --dirty)

# Available cpus for compiling, please refer to https://github.com/caicloud/engineering/issues/8186#issuecomment-518656946 for more information.
CPUS ?= $(shell /bin/bash hack/read_cpus_available.sh)

# Track code version with Docker Label.
DOCKER_LABELS ?= git-describe="$(shell date -u +v%Y%m%d)-$(shell git describe --tags --always --dirty)"

# Golang standard bin directory.
GOPATH ?= $(shell go env GOPATH)
BIN_DIR := $(GOPATH)/bin
GOLANGCI_LINT := $(BIN_DIR)/golangci-lint

# Default golang flags used in build and test
# -mod=vendor: force go to use the vendor files instead of using the `$GOPATH/pkg/mod`
# -p: the number of programs that can be run in parallel
# -race: enable data race detection
# -count: run each test and benchmark 1 times. Set this flag to disable test cache
export GOFLAGS ?= -mod=vendor -p=$(CPUS) -race -count=1

#
# Define all targets. At least the following commands are required:
#

# All targets.
.PHONY: lint test build container push

build: build-local

# more info about `GOGC` env: https://github.com/golangci/golangci-lint#memory-usage-of-golangci-lint
lint: $(GOLANGCI_LINT)
@$(GOLANGCI_LINT) run

$(GOLANGCI_LINT):
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(BIN_DIR) v1.23.6

test:
@go test -coverprofile=coverage.out ./...
@go tool cover -func coverage.out | tail -n 1 | awk '{ print "Total coverage: " $$3 }'

build-local:
@for target in $(TARGETS); do \
go build -v -o $(OUTPUT_DIR)/$${target} \
-ldflags "-s -w -X $(ROOT)/pkg/version.VERSION=$(VERSION) \
-X $(ROOT)/pkg/version.REPOROOT=$(ROOT)" \
$(CMD_DIR)/$${target}; \
done

build-linux:
@docker run --rm -t \
-v $(PWD):/go/src/$(ROOT) \
-w /go/src/$(ROOT) \
-e GOOS=linux \
-e GOARCH=amd64 \
-e GOPATH=/go \
-e GOFLAGS="$(GOFLAGS)" \
-e SHELLOPTS="$(SHELLOPTS)" \
$(BASE_REGISTRY)/golang:1.13.9-stretch \
/bin/bash -c 'for target in $(TARGETS); do \
go build -v -o $(OUTPUT_DIR)/$${target} \
-ldflags "-s -w -X $(ROOT)/pkg/version.VERSION=$(VERSION) \
-X $(ROOT)/pkg/version.REPOROOT=$(ROOT)" \
$(CMD_DIR)/$${target}; \
done'

container: build-linux
@for target in $(TARGETS); do \
image=$(IMAGE_PREFIX)$${target}$(IMAGE_SUFFIX); \
docker build -t $(REGISTRY)/$${image}:$(VERSION) \
--label $(DOCKER_LABELS) \
-f $(BUILD_DIR)/$${target}/Dockerfile .; \
done

push: container
@for target in $(TARGETS); do \
image=$(IMAGE_PREFIX)$${target}$(IMAGE_SUFFIX); \
docker push $(REGISTRY)/$${image}:$(VERSION); \
done

.PHONY: clean
clean:
@-rm -vrf ${OUTPUT_DIR}
8 changes: 8 additions & 0 deletions OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
approvers:
- ddysher
- bbbmj
- supereagle
reviewers:
- ddysher
- bbbmj
- supereagle
125 changes: 125 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [Golang Template Project](#golang-template-project)
- [About the project](#about-the-project)
- [API docs](#api-docs)
- [Design](#design)
- [Status](#status)
- [See also](#see-also)
- [Getting started](#getting-started)
- [Layout](#layout)
- [Notes](#notes)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# Golang Template Project

## About the project

The template is used to create golang project. All golang projects must follow the conventions in the
template. Calling for exceptions must be brought up in the engineering team.

### API docs

The template doesn't have API docs. For web service, please include API docs here, whether it's
auto-generated or hand-written. For auto-generated API docs, you can also give instructions on the
build process.

### Design

The template follows project convention doc.

* [Repository Conventions](https://github.com/caicloud/engineering/blob/master/guidelines/repo_conventions.md)

### Status

The template project is in alpha status.

### See also

* [nirvana project template](https://github.com/caicloud/nirvana-template-project)
* [python project template](https://github.com/caicloud/python-template-project)
* [common project template](https://github.com/caicloud/common-template-project)

## Getting started

Below we describe the conventions or tools specific to golang project.

### Layout

```tree
├── .github
│   ├── ISSUE_TEMPLATE.md
│   └── PULL_REQUEST_TEMPLATE.md
├── .gitignore
├── .golangci.yml
├── CHANGELOG.md
├── Makefile
├── OWNERS
├── README.md
├── build
│   ├── admin
│   │   └── Dockerfile
│   └── controller
│   └── Dockerfile
├── cmd
│   ├── admin
│   │   └── admin.go
│   └── controller
│   └── controller.go
├── docs
│   └── README.md
├── hack
│   ├── README.md
│   ├── deployment.yaml
│   └── script.sh
├── pkg
│ ├── apis
│ │ └── v1
│ │ └── README.md
│   ├── utils
│   │   └── net
│   │   └── net.go
│   └── version
│   └── version.go
├── release
│   ├── template-admin.yaml
│   └── template-controller.yaml
├── test
│   ├── README.md
│   └── test_make.sh
├── third_party
│   └── README.md
└── vendor
└── README.md
```

A brief description of the layout:

* `.github` has two template files for creating PR and issue. Please see the files for more details.
* `.gitignore` varies per project, but all projects need to ignore `bin` directory.
* `.golangci.yml` is the golangci-lint config file.
* `Makefile` is used to build the project. **You need to tweak the variables based on your project**.
* `CHANGELOG.md` contains auto-generated changelog information.
* `OWNERS` contains owners of the project.
* `README.md` is a detailed description of the project.
* `bin` is to hold build outputs.
* `cmd` contains main packages, each subdirecoty of `cmd` is a main package.
* `build` contains scripts, yaml files, dockerfiles, etc, to build and package the project.
* `docs` for project documentations.
* `hack` contains scripts used to manage this repository, e.g. codegen, installation, verification, etc.
* `pkg` places most of project business logic and locate `api` package.
* `release` [chart](https://github.com/caicloud/charts) for production deployment.
* `test` holds all tests (except unit tests), e.g. integration, e2e tests.
* `third_party` for all third party libraries and tools, e.g. swagger ui, protocol buf, etc.
* `vendor` contains all vendored code.

## Notes

* Makefile **MUST NOT** change well-defined command semantics, see Makefile for details.
* Every project **MUST** use `dep` for vendor management and **MUST** checkin `vendor` direcotry.
* `cmd` and `build` **MUST** have the same set of subdirectories for main targets
* For example, `cmd/admin,cmd/controller` and `build/admin,build/controller`.
* Dockerfile **MUST** be put under `build` directory even if you have only one Dockerfile.
5 changes: 5 additions & 0 deletions build/admin/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM cargo.caicloud.xyz/library/debian:stretch

COPY bin/admin /usr/local/bin/template-admin

ENTRYPOINT ["template-admin"]
Loading

0 comments on commit df18cba

Please sign in to comment.