2015-07-12 04:04:52 +00:00
|
|
|
<!-- BEGIN MUNGE: UNVERSIONED_WARNING -->
|
|
|
|
|
2016-06-10 23:46:46 +00:00
|
|
|
<!-- BEGIN STRIP_FOR_RELEASE -->
|
|
|
|
|
|
|
|
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
|
|
|
|
width="25" height="25">
|
|
|
|
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
|
|
|
|
width="25" height="25">
|
|
|
|
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
|
|
|
|
width="25" height="25">
|
|
|
|
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
|
|
|
|
width="25" height="25">
|
|
|
|
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
|
|
|
|
width="25" height="25">
|
|
|
|
|
|
|
|
<h2>PLEASE NOTE: This document applies to the HEAD of the source tree</h2>
|
|
|
|
|
|
|
|
If you are using a released version of Kubernetes, you should
|
|
|
|
refer to the docs that go with that version.
|
|
|
|
|
|
|
|
<!-- TAG RELEASE_LINK, added by the munger automatically -->
|
|
|
|
<strong>
|
|
|
|
The latest release of this document can be found
|
2016-06-13 19:24:34 +00:00
|
|
|
[here](http://releases.k8s.io/release-1.3/docs/devel/coding-conventions.md).
|
2016-06-10 23:46:46 +00:00
|
|
|
|
|
|
|
Documentation for other releases can be found at
|
|
|
|
[releases.k8s.io](http://releases.k8s.io).
|
|
|
|
</strong>
|
|
|
|
--
|
|
|
|
|
|
|
|
<!-- END STRIP_FOR_RELEASE -->
|
2015-07-12 04:04:52 +00:00
|
|
|
|
|
|
|
<!-- END MUNGE: UNVERSIONED_WARNING -->
|
2016-05-03 17:23:25 +00:00
|
|
|
|
|
|
|
# Coding Conventions
|
|
|
|
|
|
|
|
Updated: 5/3/2016
|
|
|
|
|
|
|
|
**Table of Contents**
|
|
|
|
<!-- BEGIN MUNGE: GENERATED_TOC -->
|
|
|
|
|
|
|
|
- [Coding Conventions](#coding-conventions)
|
|
|
|
- [Code conventions](#code-conventions)
|
|
|
|
- [Testing conventions](#testing-conventions)
|
|
|
|
- [Directory and file conventions](#directory-and-file-conventions)
|
|
|
|
- [Coding advice](#coding-advice)
|
|
|
|
|
|
|
|
<!-- END MUNGE: GENERATED_TOC -->
|
|
|
|
|
|
|
|
## Code conventions
|
|
|
|
|
2015-03-03 22:29:39 +00:00
|
|
|
- Bash
|
2016-04-29 20:04:03 +00:00
|
|
|
|
2015-03-03 22:29:39 +00:00
|
|
|
- https://google-styleguide.googlecode.com/svn/trunk/shell.xml
|
2016-04-29 20:04:03 +00:00
|
|
|
|
|
|
|
- Ensure that build, release, test, and cluster-management scripts run on
|
|
|
|
OS X
|
|
|
|
|
2015-03-03 22:29:39 +00:00
|
|
|
- Go
|
2016-04-29 20:04:03 +00:00
|
|
|
|
2015-08-27 21:12:06 +00:00
|
|
|
- Ensure your code passes the [presubmit checks](development.md#hooks)
|
2016-04-29 20:04:03 +00:00
|
|
|
|
|
|
|
- [Go Code Review
|
|
|
|
Comments](https://github.com/golang/go/wiki/CodeReviewComments)
|
|
|
|
|
2015-08-27 21:12:06 +00:00
|
|
|
- [Effective Go](https://golang.org/doc/effective_go.html)
|
2016-04-29 20:04:03 +00:00
|
|
|
|
2015-08-27 21:12:06 +00:00
|
|
|
- Comment your code.
|
2016-04-29 20:04:03 +00:00
|
|
|
- [Go's commenting
|
|
|
|
conventions](http://blog.golang.org/godoc-documenting-go-code)
|
|
|
|
- If reviewers ask questions about why the code is the way it is, that's a
|
|
|
|
sign that comments might be helpful.
|
|
|
|
|
|
|
|
|
2015-08-27 21:12:06 +00:00
|
|
|
- Command-line flags should use dashes, not underscores
|
2016-04-29 20:04:03 +00:00
|
|
|
|
|
|
|
|
2015-08-27 21:12:06 +00:00
|
|
|
- Naming
|
2016-04-29 20:04:03 +00:00
|
|
|
- Please consider package name when selecting an interface name, and avoid
|
|
|
|
redundancy.
|
|
|
|
|
|
|
|
- e.g.: `storage.Interface` is better than `storage.StorageInterface`.
|
|
|
|
|
|
|
|
- Do not use uppercase characters, underscores, or dashes in package
|
|
|
|
names.
|
2015-08-27 21:12:06 +00:00
|
|
|
- Please consider parent directory name when choosing a package name.
|
2016-04-29 20:04:03 +00:00
|
|
|
|
|
|
|
- so pkg/controllers/autoscaler/foo.go should say `package autoscaler`
|
|
|
|
not `package autoscalercontroller`.
|
|
|
|
- Unless there's a good reason, the `package foo` line should match
|
|
|
|
the name of the directory in which the .go file exists.
|
|
|
|
- Importers can use a different name if they need to disambiguate.
|
|
|
|
|
|
|
|
- Locks should be called `lock` and should never be embedded (always `lock
|
|
|
|
sync.Mutex`). When multiple locks are present, give each lock a distinct name
|
|
|
|
following Go conventions - `stateLock`, `mapLock` etc.
|
|
|
|
|
|
|
|
- [API changes](api_changes.md)
|
|
|
|
|
|
|
|
- [API conventions](api-conventions.md)
|
|
|
|
|
2015-08-27 21:12:06 +00:00
|
|
|
- [Kubectl conventions](kubectl-conventions.md)
|
2016-04-29 20:04:03 +00:00
|
|
|
|
2015-08-27 21:12:06 +00:00
|
|
|
- [Logging conventions](logging.md)
|
|
|
|
|
2016-05-03 17:23:25 +00:00
|
|
|
## Testing conventions
|
|
|
|
|
2016-04-29 20:04:03 +00:00
|
|
|
- All new packages and most new significant functionality must come with unit
|
|
|
|
tests
|
|
|
|
|
|
|
|
- Table-driven tests are preferred for testing multiple scenarios/inputs; for
|
2016-06-08 21:49:33 +00:00
|
|
|
example, see [TestNamespaceAuthorization](../../test/integration/auth/auth_test.go)
|
2016-04-29 20:04:03 +00:00
|
|
|
|
|
|
|
- Significant features should come with integration (test/integration) and/or
|
|
|
|
[end-to-end (test/e2e) tests](e2e-tests.md)
|
2015-08-27 21:12:06 +00:00
|
|
|
- Including new kubectl commands and major features of existing commands
|
2016-04-29 20:04:03 +00:00
|
|
|
|
|
|
|
- Unit tests must pass on OS X and Windows platforms - if you use Linux
|
|
|
|
specific features, your test case must either be skipped on windows or compiled
|
|
|
|
out (skipped is better when running Linux specific commands, compiled out is
|
|
|
|
required when your code does not compile on Windows).
|
|
|
|
|
2016-05-03 17:23:25 +00:00
|
|
|
- Avoid relying on Docker hub (e.g. pull from Docker hub). Use gcr.io instead.
|
2016-04-29 20:04:03 +00:00
|
|
|
|
|
|
|
- Avoid waiting for a short amount of time (or without waiting) and expect an
|
|
|
|
asynchronous thing to happen (e.g. wait for 1 seconds and expect a Pod to be
|
|
|
|
running). Wait and retry instead.
|
|
|
|
|
2016-03-07 03:50:31 +00:00
|
|
|
- See the [testing guide](testing.md) for additional testing advice.
|
2015-08-27 21:12:06 +00:00
|
|
|
|
2016-05-03 17:23:25 +00:00
|
|
|
## Directory and file conventions
|
|
|
|
|
2016-04-29 20:04:03 +00:00
|
|
|
- Avoid package sprawl. Find an appropriate subdirectory for new packages.
|
|
|
|
(See [#4851](http://issues.k8s.io/4851) for discussion.)
|
|
|
|
- Libraries with no more appropriate home belong in new package
|
|
|
|
subdirectories of pkg/util
|
|
|
|
|
|
|
|
- Avoid general utility packages. Packages called "util" are suspect. Instead,
|
|
|
|
derive a name that describes your desired function. For example, the utility
|
|
|
|
functions dealing with waiting for operations are in the "wait" package and
|
|
|
|
include functionality like Poll. So the full name is wait.Poll
|
|
|
|
|
2015-12-20 19:38:34 +00:00
|
|
|
- All filenames should be lowercase
|
2016-04-29 20:04:03 +00:00
|
|
|
|
2015-09-18 04:34:25 +00:00
|
|
|
- Go source files and directories use underscores, not dashes
|
2016-04-29 20:04:03 +00:00
|
|
|
- Package directories should generally avoid using separators as much as
|
|
|
|
possible (when packages are multiple words, they usually should be in nested
|
|
|
|
subdirectories).
|
|
|
|
|
2015-09-18 04:34:25 +00:00
|
|
|
- Document directories and filenames should use dashes rather than underscores
|
2016-04-29 20:04:03 +00:00
|
|
|
|
|
|
|
- Contrived examples that illustrate system features belong in
|
|
|
|
/docs/user-guide or /docs/admin, depending on whether it is a feature primarily
|
|
|
|
intended for users that deploy applications or cluster administrators,
|
|
|
|
respectively. Actual application examples belong in /examples.
|
|
|
|
- Examples should also illustrate [best practices for configuration and
|
|
|
|
using the system](../user-guide/config-best-practices.md)
|
|
|
|
|
2015-09-18 04:34:25 +00:00
|
|
|
- Third-party code
|
2016-04-29 20:04:03 +00:00
|
|
|
|
|
|
|
- Go code for normal third-party dependencies is managed using
|
|
|
|
[Godeps](https://github.com/tools/godep)
|
|
|
|
|
2016-01-29 05:00:34 +00:00
|
|
|
- Other third-party code belongs in `/third_party`
|
|
|
|
- forked third party Go code goes in `/third_party/forked`
|
|
|
|
- forked _golang stdlib_ code goes in `/third_party/golang`
|
2016-04-29 20:04:03 +00:00
|
|
|
|
2015-09-18 04:34:25 +00:00
|
|
|
- Third-party code must include licenses
|
2016-04-29 20:04:03 +00:00
|
|
|
|
2015-09-18 04:34:25 +00:00
|
|
|
- This includes modified third-party code and excerpts, as well
|
2015-08-27 21:12:06 +00:00
|
|
|
|
2016-05-03 17:23:25 +00:00
|
|
|
## Coding advice
|
|
|
|
|
2015-08-27 21:12:06 +00:00
|
|
|
- Go
|
2016-04-29 20:04:03 +00:00
|
|
|
|
2015-08-27 21:12:06 +00:00
|
|
|
- [Go landmines](https://gist.github.com/lavalamp/4bd23295a9f32706a48f)
|
2015-03-03 22:29:39 +00:00
|
|
|
|
2015-05-14 22:12:45 +00:00
|
|
|
|
2015-07-14 00:13:09 +00:00
|
|
|
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
|
2015-05-14 22:12:45 +00:00
|
|
|
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/devel/coding-conventions.md?pixel)]()
|
2015-07-14 00:13:09 +00:00
|
|
|
<!-- END MUNGE: GENERATED_ANALYTICS -->
|