Skip to content
Permalink
Browse files

Add contributor documentation and helper scripts (#201)

* Add script to run all code checks

* Add some instructions for krew contributors

* Add script to run krew in a docker sandbox

Add instruction for docker sandbox

* Add whitespace around markdown blocks

* Add install instructions for `goimports`

* Wrap lines at 80 characters

* Rename gofmt.sh -> verify-gofmt.sh

* Rename scripts

check-code-patterns.sh -> verify-code-patterns.sh
run_lint.sh -> run-lint.sh

This ensures a more consistent naming pattern

* Install kubectl in docker sandbox

* Clarify that the result must be immediately extracted

* Fail fast in run-in-docker.sh
  • Loading branch information...
corneliusweig authored and k8s-ci-robot committed Jun 5, 2019
1 parent c3c3e6c commit 38590764e7ca76d78d42704e537c5c462c4a9637
@@ -0,0 +1,10 @@
/.idea/
/hack/
/out/
/OWNERS
/OWNERS_ALIASES
/CONTRIBUTING.md
/SECURITY_CONTACTS
/.travis.yml
/code-of-conduct.md
/coverage.txt
@@ -9,10 +9,10 @@ notifications:
before_install:
- go get github.com/mitchellh/gox
script:
- hack/check-code-patterns.sh
- hack/verify-code-patterns.sh
- hack/verify-boilerplate.sh
- hack/gofmt.sh
- hack/run_lint.sh
- hack/verify-gofmt.sh
- hack/run-lint.sh
- go test -v -coverprofile=coverage.txt -covermode=atomic ./...
- hack/make-all.sh
after_success:
@@ -0,0 +1,75 @@
# Contributor Guide

This guide is intended for people who want to start working on `krew` itself.
If you intend to write a new plugin, see [Developer Guide](./DEVELOPER_GUIDE.md)
instead.

## Setting up the environment

Krew is built with go 1.10, but newer versions will do as well.
Most toolchains will expect that the krew repository is on the `GOPATH`.
To set it up correctly, do

```bash
mkdir -p $(go env GOPATH)/src/sigs.k8s.io/krew
cd $(go env GOPATH)/src/sigs.k8s.io/krew
git clone https://github.com/kubernetes-sigs/krew .
git remote set-url origin --push no_push # to avoid pushes
```

## Code style

Krew adheres to standard `golang` code formatting conventions, and also expects
imports sorted properly.
To automatically format code appropriately, install
[`goimports`](https://godoc.org/golang.org/x/tools/cmd/goimports) via

```bash
go get golang.org/x/tools/cmd/goimports
```

and run:

```bash
goimports -w cmd pkg
```

In addition, a boilerplate license header is expected in all source files.

_All new code should be covered by tests._


## Running tests

To run tests locally, the easiest way to get started is with

```bash
hack/run-tests.sh
```

To run a single tool independently of the other code checks, have a look at the
other scripts in [`hack/`](../hack).

## Testing `krew` in a sandbox

After making changes to krew, you should also check that it behaves as expected.
You can do this without messing up the krew installation on the host system by
setting the `KREW_ROOT` environment variable.
For example:

```bash
mkdir playground
KREW_ROOT="$PWD/playground" krew update
```

Any changes that krew is going to apply will then be applied in the
`playground/` folder, instead of the standard `~/.krew` folder.

### Testing in a docker sandbox

Alternatively, if the isolation provided by `KREW_ROOT` is not enough, there is
also a script to run krew in a docker sandbox:

```bash
hack/run-in-docker.sh
```
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

docker build -f "${SCRIPTDIR}/sandboxed.Dockerfile" -t krew:sandbox "${SCRIPTDIR}/.."

docker run --rm -ti krew:sandbox
File renamed without changes.
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

color_red="$(tput setaf 1)"
color_green="$(tput setaf 2)"
color_blue="$(tput setaf 4)"
color_reset="$(tput sgr0)"

print_with_color() {
echo "${1}${*:2}${color_reset}"
}

print_status() {
local result=$? # <- this must be the first action
if [[ $result == 0 ]]; then
print_with_color "$color_green" 'SUCCESS'
else
print_with_color "$color_red" 'FAILURE'
fi
}
trap print_status EXIT

print_with_color "$color_blue" 'Checking boilerplate'
"$SCRIPTDIR"/verify-boilerplate.sh

print_with_color "$color_blue" 'Running gofmt'
"$SCRIPTDIR"/verify-gofmt.sh

print_with_color "$color_blue" 'Running tests'
go test -v -race sigs.k8s.io/krew/...

print_with_color "$color_blue" 'Running linter'
"$SCRIPTDIR"/run-lint.sh

print_with_color "$color_blue" 'Check code patterns'
"$SCRIPTDIR"/verify-code-patterns.sh
@@ -0,0 +1,37 @@
# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:alpine as builder

WORKDIR /go/src/sigs.k8s.io/krew

ENV KUBECTL_VERSION v1.14.2
RUN apk add --no-cache curl && \
curl -Lo /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl && \
chmod +x /usr/bin/kubectl

# build binary
COPY . .
RUN go build -tags netgo -ldflags "-s -w" ./cmd/krew

# production image
FROM alpine
RUN apk --no-cache add git && \
ln -s /usr/bin/krew /usr/bin/kubectl-krew

# initialize index
RUN mkdir -p /root/.krew/index && \
git clone https://github.com/kubernetes-sigs/krew-index /root/.krew/index

COPY --from=builder /go/src/sigs.k8s.io/krew/krew /usr/bin/kubectl /usr/bin/
File renamed without changes.
File renamed without changes.

0 comments on commit 3859076

Please sign in to comment.
You can’t perform that action at this time.