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

View File

@@ -35,6 +35,7 @@ import (
"github.com/kubernetes-sigs/kustomize/pkg/types"
)
// Application interface exposes methods to get resources of the application.
type Application interface {
// Resources computes and returns the resources for the app.
Resources() (resmap.ResMap, error)
@@ -57,7 +58,7 @@ type applicationImpl struct {
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) {
content, err := loader.Load(constants.KustomizationFileName)
if err != nil {

View File

@@ -27,7 +27,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation"
)
// handleConfigMapFromLiteralSources adds the specified literal source
// HandleConfigMapFromLiteralSources adds the specified literal source
// information into the provided configMap.
func HandleConfigMapFromLiteralSources(configMap *v1.ConfigMap, literalSources []string) error {
for _, literalSource := range literalSources {
@@ -43,7 +43,7 @@ func HandleConfigMapFromLiteralSources(configMap *v1.ConfigMap, literalSources [
return nil
}
// handleConfigMapFromFileSources adds the specified file source information
// HandleConfigMapFromFileSources adds the specified file source information
// into the provided configMap
func HandleConfigMapFromFileSources(configMap *v1.ConfigMap, fileSources []string) error {
for _, fileSource := range fileSources {
@@ -88,7 +88,7 @@ func HandleConfigMapFromFileSources(configMap *v1.ConfigMap, fileSources []strin
return nil
}
// handleConfigMapFromEnvFileSource adds the specified env file source information
// HandleConfigMapFromEnvFileSource adds the specified env file source information
// into the provided configMap
func HandleConfigMapFromEnvFileSource(configMap *v1.ConfigMap, envFileSource string) error {
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
}
// HashObject encodes object using given codec and returns MD5 sum of the result.
func HashObject(obj runtime.Object, codec runtime.Codec) (string, error) {
data, err := runtime.Encode(codec, obj)
if err != nil {

View File

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

View File

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

View File

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

View File

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

View File

@@ -18,9 +18,11 @@ package error
import "fmt"
// SecretError represents error with a secret.
type SecretError struct {
KustomizationPath string
ErrorMsg string
// ErrorMsg is an error message
ErrorMsg string
}
func (e SecretError) Error() string {

View File

@@ -43,24 +43,27 @@ func NewFakeLoader(initialDir string) FakeLoader {
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 {
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 {
return f.fs.Mkdir(fullDirPath, mode)
}
// Root returns root.
func (f FakeLoader) Root() string {
return f.delegate.Root()
}
// New creates a new loader from a new root.
func (f FakeLoader) New(newRoot string) (loader.Loader, error) {
return f.delegate.New(newRoot)
}
// Load performs load from a given location.
func (f FakeLoader) Load(location string) ([]byte, error) {
return f.delegate.Load(location)
}

View File

@@ -36,7 +36,7 @@ type loaderImpl struct {
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 {
// Does this location correspond to this scheme.
IsScheme(root string, location string) bool

View File

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

View File

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

View File

@@ -30,6 +30,7 @@ type ResId struct {
name string
}
// NewResId creates new resource identifier
func NewResId(g schema.GroupVersionKind, n string) ResId {
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"
}
// Gvk returns Group/Version/Kind of the resource.
func (n ResId) Gvk() schema.GroupVersionKind {
return n.gvk
}
// Name returns resource name.
func (n ResId) Name() string {
return n.name
}

View File

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

View File

@@ -67,7 +67,7 @@ type Kustomization struct {
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 {
// Name of the configmap.
// The full name should be Kustomization.NamePrefix + Configmap.Name +
@@ -84,7 +84,7 @@ type ConfigMapArgs struct {
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 {
// Name of the secret.
// 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"`
}
// Defaulting sets reference to field used by default.
func (v *Var) Defaulting() {
if (corev1.ObjectFieldSelector{}) == v.FieldRef {
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')
)
// Version represens kustomize version.
type Version struct {
// KustomizeVersion is a kustomize binary version.
KustomizeVersion string `json:"kustomizeVersion"`
GitCommit string `json:"gitCommit"`
BuildDate string `json:"buildDate"`
GoOs string `json:"goOs"`
GoArch string `json:"goArch"`
// GitCommit is a git commit
GitCommit string `json:"gitCommit"`
// BuildDate is a build date of the binary.
BuildDate string `json:"buildDate"`
// GoOs holds OS name.
GoOs string `json:"goOs"`
// GoArch holds architecture name.
GoArch string `json:"goArch"`
}
// GetVersion returns version.
func GetVersion() Version {
return Version{
kustomizeVersion,
@@ -50,6 +57,7 @@ func GetVersion() Version {
}
}
// Print prints version.
func (v Version) Print(w io.Writer) {
fmt.Fprintf(w, "Version: %+v\n", v)
}