Fix go lint error and add golint checks to a pre-commit hook

This commit is contained in:
Oleg Atamanenko
2018-06-09 23:05:26 -04:00
parent 2b05d39067
commit c994130005
18 changed files with 58 additions and 24 deletions

View File

@@ -35,6 +35,10 @@ function testGoImports {
diff -u <(echo -n) <(go_dirs | xargs -0 goimports -l) diff -u <(echo -n) <(go_dirs | xargs -0 goimports -l)
} }
function testGoLint {
diff -u <(echo -n) <(go_dirs | xargs -0 golint --min_confidence 0.85 )
}
function testGoVet { function testGoVet {
go vet -all ./... go vet -all ./...
} }
@@ -49,6 +53,7 @@ function testExamples {
runTest testGoFmt runTest testGoFmt
runTest testGoImports runTest testGoImports
runTest testGoLint
runTest testGoVet runTest testGoVet
runTest testGoTest runTest testGoTest
runTest testExamples runTest testExamples

View File

@@ -35,6 +35,7 @@ import (
"github.com/kubernetes-sigs/kustomize/pkg/types" "github.com/kubernetes-sigs/kustomize/pkg/types"
) )
// Application interface exposes methods to get resources of the application.
type Application interface { type Application interface {
// Resources computes and returns the resources for the app. // Resources computes and returns the resources for the app.
Resources() (resmap.ResMap, error) Resources() (resmap.ResMap, error)
@@ -57,7 +58,7 @@ type applicationImpl struct {
loader loader.Loader loader loader.Loader
} }
// NewApp parses the kustomization file at the path using the loader. // New parses the kustomization file at the path using the loader and returns application.
func New(loader loader.Loader) (Application, error) { func New(loader loader.Loader) (Application, error) {
content, err := loader.Load(constants.KustomizationFileName) content, err := loader.Load(constants.KustomizationFileName)
if err != nil { if err != nil {

View File

@@ -27,7 +27,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation"
) )
// handleConfigMapFromLiteralSources adds the specified literal source // HandleConfigMapFromLiteralSources adds the specified literal source
// information into the provided configMap. // information into the provided configMap.
func HandleConfigMapFromLiteralSources(configMap *v1.ConfigMap, literalSources []string) error { func HandleConfigMapFromLiteralSources(configMap *v1.ConfigMap, literalSources []string) error {
for _, literalSource := range literalSources { for _, literalSource := range literalSources {
@@ -43,7 +43,7 @@ func HandleConfigMapFromLiteralSources(configMap *v1.ConfigMap, literalSources [
return nil return nil
} }
// handleConfigMapFromFileSources adds the specified file source information // HandleConfigMapFromFileSources adds the specified file source information
// into the provided configMap // into the provided configMap
func HandleConfigMapFromFileSources(configMap *v1.ConfigMap, fileSources []string) error { func HandleConfigMapFromFileSources(configMap *v1.ConfigMap, fileSources []string) error {
for _, fileSource := range fileSources { for _, fileSource := range fileSources {
@@ -88,7 +88,7 @@ func HandleConfigMapFromFileSources(configMap *v1.ConfigMap, fileSources []strin
return nil return nil
} }
// handleConfigMapFromEnvFileSource adds the specified env file source information // HandleConfigMapFromEnvFileSource adds the specified env file source information
// into the provided configMap // into the provided configMap
func HandleConfigMapFromEnvFileSource(configMap *v1.ConfigMap, envFileSource string) error { func HandleConfigMapFromEnvFileSource(configMap *v1.ConfigMap, envFileSource string) error {
info, err := os.Stat(envFileSource) info, err := os.Stat(envFileSource)

View File

@@ -41,6 +41,7 @@ func ParseRFC3339(s string, nowFn func() metav1.Time) (metav1.Time, error) {
return metav1.Time{Time: t}, nil return metav1.Time{Time: t}, nil
} }
// HashObject encodes object using given codec and returns MD5 sum of the result.
func HashObject(obj runtime.Object, codec runtime.Codec) (string, error) { func HashObject(obj runtime.Object, codec runtime.Codec) (string, error) {
data, err := runtime.Encode(codec, obj) data, err := runtime.Encode(codec, obj)
if err != nil { if err != nil {

View File

@@ -19,6 +19,7 @@ package error
import "fmt" import "fmt"
// ConfigmapError represents error with a configmap.
type ConfigmapError struct { type ConfigmapError struct {
Path string Path string
ErrorMsg string ErrorMsg string

View File

@@ -20,7 +20,7 @@ import (
"fmt" "fmt"
) )
// First pass to encapsulate fields for more informative error messages. // KustomizationError represents an error with a kustomization.
type KustomizationError struct { type KustomizationError struct {
KustomizationPath string KustomizationPath string
ErrorMsg string ErrorMsg string
@@ -30,6 +30,7 @@ func (ke KustomizationError) Error() string {
return fmt.Sprintf("Kustomization File [%s]: %s\n", ke.KustomizationPath, ke.ErrorMsg) return fmt.Sprintf("Kustomization File [%s]: %s\n", ke.KustomizationPath, ke.ErrorMsg)
} }
// KustomizationErrors collects all errors.
type KustomizationErrors struct { type KustomizationErrors struct {
kErrors []error kErrors []error
} }
@@ -42,14 +43,17 @@ func (ke *KustomizationErrors) Error() string {
return errormsg return errormsg
} }
// Append adds error to a collection of errors.
func (ke *KustomizationErrors) Append(e error) { func (ke *KustomizationErrors) Append(e error) {
ke.kErrors = append(ke.kErrors, e) ke.kErrors = append(ke.kErrors, e)
} }
// Get returns all collected errors.
func (ke *KustomizationErrors) Get() []error { func (ke *KustomizationErrors) Get() []error {
return ke.kErrors return ke.kErrors
} }
// BatchAppend adds all errors from another KustomizationErrors
func (ke *KustomizationErrors) BatchAppend(e KustomizationErrors) { func (ke *KustomizationErrors) BatchAppend(e KustomizationErrors) {
for _, err := range e.Get() { for _, err := range e.Get() {
ke.kErrors = append(ke.kErrors, err) ke.kErrors = append(ke.kErrors, err)

View File

@@ -20,6 +20,7 @@ import (
"fmt" "fmt"
) )
// PatchError represents error during Patch.
type PatchError struct { type PatchError struct {
KustomizationPath string KustomizationPath string
PatchFilepath string PatchFilepath string

View File

@@ -18,7 +18,7 @@ package error
import "fmt" import "fmt"
// First pass to encapsulate fields for more informative error messages. // ResourceError represents error in a resource.
type ResourceError struct { type ResourceError struct {
KustomizationPath string KustomizationPath string
ResourceFilepath string ResourceFilepath string

View File

@@ -18,8 +18,10 @@ package error
import "fmt" import "fmt"
// SecretError represents error with a secret.
type SecretError struct { type SecretError struct {
KustomizationPath string KustomizationPath string
// ErrorMsg is an error message
ErrorMsg string ErrorMsg string
} }

View File

@@ -43,24 +43,27 @@ func NewFakeLoader(initialDir string) FakeLoader {
return FakeLoader{fs: fakefs, delegate: loader} return FakeLoader{fs: fakefs, delegate: loader}
} }
// Adds a fake file to the file system. // AddFile adds a fake file to the file system.
func (f FakeLoader) AddFile(fullFilePath string, content []byte) error { func (f FakeLoader) AddFile(fullFilePath string, content []byte) error {
return f.fs.WriteFile(fullFilePath, content) return f.fs.WriteFile(fullFilePath, content)
} }
// Adds a fake directory to the file system. // AddDirectory adds a fake directory to the file system.
func (f FakeLoader) AddDirectory(fullDirPath string, mode os.FileMode) error { func (f FakeLoader) AddDirectory(fullDirPath string, mode os.FileMode) error {
return f.fs.Mkdir(fullDirPath, mode) return f.fs.Mkdir(fullDirPath, mode)
} }
// Root returns root.
func (f FakeLoader) Root() string { func (f FakeLoader) Root() string {
return f.delegate.Root() return f.delegate.Root()
} }
// New creates a new loader from a new root.
func (f FakeLoader) New(newRoot string) (loader.Loader, error) { func (f FakeLoader) New(newRoot string) (loader.Loader, error) {
return f.delegate.New(newRoot) return f.delegate.New(newRoot)
} }
// Load performs load from a given location.
func (f FakeLoader) Load(location string) ([]byte, error) { func (f FakeLoader) Load(location string) ([]byte, error) {
return f.delegate.Load(location) return f.delegate.Load(location)
} }

View File

@@ -36,7 +36,7 @@ type loaderImpl struct {
schemes []SchemeLoader schemes []SchemeLoader
} }
// Interface for different types of loaders (e.g. fileLoader, httpLoader, etc.) // SchemeLoader is the interface for different types of loaders (e.g. fileLoader, httpLoader, etc.)
type SchemeLoader interface { type SchemeLoader interface {
// Does this location correspond to this scheme. // Does this location correspond to this scheme.
IsScheme(root string, location string) bool IsScheme(root string, location string) bool

View File

@@ -68,11 +68,12 @@ func (m ResMap) EncodeAsYaml() ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func (m1 ResMap) ErrorIfNotEqual(m2 ResMap) error { // ErrorIfNotEqual returns error if maps are not equal.
if len(m1) != len(m2) { func (m ResMap) ErrorIfNotEqual(m2 ResMap) error {
if len(m) != len(m2) {
keySet1 := []resource.ResId{} keySet1 := []resource.ResId{}
keySet2 := []resource.ResId{} keySet2 := []resource.ResId{}
for id := range m1 { for id := range m {
keySet1 = append(keySet1, id) keySet1 = append(keySet1, id)
} }
for id := range m2 { for id := range m2 {
@@ -80,7 +81,7 @@ func (m1 ResMap) ErrorIfNotEqual(m2 ResMap) error {
} }
return fmt.Errorf("maps has different number of entries: %#v doesn't equals %#v", keySet1, keySet2) return fmt.Errorf("maps has different number of entries: %#v doesn't equals %#v", keySet1, keySet2)
} }
for id, obj1 := range m1 { for id, obj1 := range m {
obj2, found := m2[id] obj2, found := m2[id]
if !found { if !found {
return fmt.Errorf("%#v doesn't exist in %#v", id, m2) return fmt.Errorf("%#v doesn't exist in %#v", id, m2)

View File

@@ -20,13 +20,13 @@ package resource
type GenerationBehavior int type GenerationBehavior int
const ( const (
// Unspecified behavior typically treated as a Create. // BehaviorUnspecified is an Unspecified behavior; typically treated as a Create.
BehaviorUnspecified GenerationBehavior = iota BehaviorUnspecified GenerationBehavior = iota
// Make a new resource. // BehaviorCreate makes a new resource.
BehaviorCreate BehaviorCreate
// Replace a resource. // BehaviorReplace replaces a resource.
BehaviorReplace BehaviorReplace
// Attempt to merge a new resource with an existing resource. // BehaviorMerge attempts to merge a new resource with an existing resource.
BehaviorMerge BehaviorMerge
) )

View File

@@ -30,6 +30,7 @@ type ResId struct {
name string name string
} }
// NewResId creates new resource identifier
func NewResId(g schema.GroupVersionKind, n string) ResId { func NewResId(g schema.GroupVersionKind, n string) ResId {
return ResId{gvk: g, name: n} return ResId{gvk: g, name: n}
} }
@@ -41,10 +42,12 @@ func (n ResId) String() string {
return strings.Join([]string{n.gvk.Group, n.gvk.Version, n.gvk.Kind, n.name}, "_") + ".yaml" return strings.Join([]string{n.gvk.Group, n.gvk.Version, n.gvk.Kind, n.name}, "_") + ".yaml"
} }
// Gvk returns Group/Version/Kind of the resource.
func (n ResId) Gvk() schema.GroupVersionKind { func (n ResId) Gvk() schema.GroupVersionKind {
return n.gvk return n.gvk
} }
// Name returns resource name.
func (n ResId) Name() string { func (n ResId) Name() string {
return n.name return n.name
} }

View File

@@ -68,11 +68,13 @@ func (r *Resource) Id() ResId {
return NewResId(r.GroupVersionKind(), r.GetName()) return NewResId(r.GroupVersionKind(), r.GetName())
} }
// Merge performs merge with other resource.
func (r *Resource) Merge(other *Resource) { func (r *Resource) Merge(other *Resource) {
r.Replace(other) r.Replace(other)
mergeConfigmap(r.Object, other.Object, r.Object) mergeConfigmap(r.Object, other.Object, r.Object)
} }
// Replace performs replace with other resource.
func (r *Resource) Replace(other *Resource) { func (r *Resource) Replace(other *Resource) {
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels())) r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
r.SetAnnotations(mergeStringMaps(other.GetAnnotations(), r.GetAnnotations())) r.SetAnnotations(mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
@@ -103,6 +105,7 @@ func mergeStringMaps(maps ...map[string]string) map[string]string {
return result return result
} }
// GetFieldValue returns value at the given fieldpath.
func (r *Resource) GetFieldValue(fieldPath string) (string, error) { func (r *Resource) GetFieldValue(fieldPath string) (string, error) {
return getFieldValue(r.UnstructuredContent(), strings.Split(fieldPath, ".")) return getFieldValue(r.UnstructuredContent(), strings.Split(fieldPath, "."))
} }

View File

@@ -67,7 +67,7 @@ type Kustomization struct {
Vars []Var `json:"vars,omitempty" yaml:"vars,omitempty"` Vars []Var `json:"vars,omitempty" yaml:"vars,omitempty"`
} }
// ConfigMapArg contains the metadata of how to generate a configmap. // ConfigMapArgs contains the metadata of how to generate a configmap.
type ConfigMapArgs struct { type ConfigMapArgs struct {
// Name of the configmap. // Name of the configmap.
// The full name should be Kustomization.NamePrefix + Configmap.Name + // The full name should be Kustomization.NamePrefix + Configmap.Name +
@@ -84,7 +84,7 @@ type ConfigMapArgs struct {
DataSources `json:",inline,omitempty" yaml:",inline,omitempty"` DataSources `json:",inline,omitempty" yaml:",inline,omitempty"`
} }
// SecretGenerator contains the metadata of how to generate a secret. // SecretArgs contains the metadata of how to generate a secret.
type SecretArgs struct { type SecretArgs struct {
// Name of the secret. // Name of the secret.
// The full name should be Kustomization.NamePrefix + SecretGenerator.Name + // The full name should be Kustomization.NamePrefix + SecretGenerator.Name +

View File

@@ -40,6 +40,7 @@ type Var struct {
FieldRef corev1.ObjectFieldSelector `json:"fieldref,omitempty" yaml:"objref,omitempty"` FieldRef corev1.ObjectFieldSelector `json:"fieldref,omitempty" yaml:"objref,omitempty"`
} }
// Defaulting sets reference to field used by default.
func (v *Var) Defaulting() { func (v *Var) Defaulting() {
if (corev1.ObjectFieldSelector{}) == v.FieldRef { if (corev1.ObjectFieldSelector{}) == v.FieldRef {
v.FieldRef = corev1.ObjectFieldSelector{FieldPath: "metadata.name"} v.FieldRef = corev1.ObjectFieldSelector{FieldPath: "metadata.name"}

View File

@@ -32,14 +32,21 @@ var (
buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
) )
// Version represens kustomize version.
type Version struct { type Version struct {
// KustomizeVersion is a kustomize binary version.
KustomizeVersion string `json:"kustomizeVersion"` KustomizeVersion string `json:"kustomizeVersion"`
// GitCommit is a git commit
GitCommit string `json:"gitCommit"` GitCommit string `json:"gitCommit"`
// BuildDate is a build date of the binary.
BuildDate string `json:"buildDate"` BuildDate string `json:"buildDate"`
// GoOs holds OS name.
GoOs string `json:"goOs"` GoOs string `json:"goOs"`
// GoArch holds architecture name.
GoArch string `json:"goArch"` GoArch string `json:"goArch"`
} }
// GetVersion returns version.
func GetVersion() Version { func GetVersion() Version {
return Version{ return Version{
kustomizeVersion, kustomizeVersion,
@@ -50,6 +57,7 @@ func GetVersion() Version {
} }
} }
// Print prints version.
func (v Version) Print(w io.Writer) { func (v Version) Print(w io.Writer) {
fmt.Fprintf(w, "Version: %+v\n", v) fmt.Fprintf(w, "Version: %+v\n", v)
} }