Skip to content

Latest commit

 

History

History
72 lines (52 loc) · 3.08 KB

STYLEGUIDE.md

File metadata and controls

72 lines (52 loc) · 3.08 KB

Style Guide

In an effort to encourage clear, clean, uniform and readable code it is useful to have a style guide to establish standards and expectations for code and artifact files.

KUDO is a Kubernetes (K8S) project written primarily in Go, both of which have some differences in their style and expectation of code and APIs. In addition the founders of KUDO have some of their own preferences. This guide captures the preferred standards of this project.

Golang

When writing Go code, We favor Go idioms over Kubernetes style. Worth reading:

Here are some common cases / deviations:

Lint and Code Checks

All code should pass the linter. For cases of intentional lint deviation, it is expected that:

  • The linter is configured with the new rule.
  • The linter is configured to ignore the case.
  • The case is documented in code.

make lint

import

The general Go approach is to have a line of separation between Go libraries and external packages. We prefer to have an additional line of separation grouping kudo packages separately at the end. Example:

import (
	// standard library packages
	"context"
	"fmt"

	// third-party library packages
	"github.com/onsi/gomega"
	"github.com/sirupsen/logrus"
	appsv1 "k8s.io/api/apps/v1"
	"k8s.io/client-go/tools/record"
	"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
	"sigs.k8s.io/kustomize/pkg/target"
	ktypes "sigs.k8s.io/kustomize/pkg/types"

	// kudo packages
	"github.com/kudobuilder/kudo/pkg/util/kudo"
	"github.com/kudobuilder/kudo/pkg/version"
)

Executing make lint uses goimports which is configured to ensure this structure and will error in most situations that differ. Unfortunately not all pattern mismatches are captured.

make imports is also a useful tool in order to help create this structure. If all imports are in 1 import block (no lines of separation), make imports will modify the file in the appropriate order and structure.

If you use Goland or Intellij IDEA, setting the following under "Editor > Code Style > Go" in the "Imports" tab will match the above requirements:

[x] Add parentheses for a single import
Sorting type [gofmt]
[x] Group stdlib imports
   [x] Move all stdlib imports in a single group
[x] Group current project imports
[x] Move all import in a single declaration

Naming

In general, naming should follow Go conventions over Kubernetes code conventions.