Skip to content

Commit

Permalink
use github.com/pkg/errors (#210)
Browse files Browse the repository at this point in the history
* upgrade github.com/pkg/errors

* pkg/errors.Wrap existing errors

* new global --debug flag

* formal code style
  • Loading branch information
kron4eg authored and kubermatic-bot committed Feb 22, 2019
1 parent ba3aae4 commit fc17bba
Show file tree
Hide file tree
Showing 38 changed files with 381 additions and 294 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
name = "github.com/sirupsen/logrus"
version = "1.2.0"

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.1"

[[constraint]]
branch = "master"
name = "github.com/tmc/scp"
Expand Down
108 changes: 108 additions & 0 deletions docs/code-style.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Code style

## Linting

Our existing linting tries to enforce common rules, but cannot cover everything.
Linting can be executed via `make lint`

## Guidelines

*This is mostly copied from
[Kubernetes](https://github.com/kubernetes/community/blob/master/contributors/guide/coding-conventions.md#code-conventions)*

- Bash
- https://google.github.io/styleguide/shell.xml
- Ensure that build, release, test, and cluster-management scripts run on
macOS
- Go
- [Go Code Review
Comments](https://github.com/golang/go/wiki/CodeReviewComments)
- [Effective Go](https://golang.org/doc/effective_go.html)
- Know and avoid [Go
landmines](https://gist.github.com/lavalamp/4bd23295a9f32706a48f)
- Comment your code.
- [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.
- Command-line flags should use dashes, not underscores
- Naming
- 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.
- Please consider parent directory name when choosing a package name.
- 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.

## Special cases

### Imports

We group imports the following way:
- Go SDK
- external packages which do not apply to other rules (Like
`github.com/golang/glog`, etc.)
- github.com/kubermatic
- k8s.io/*

For example:
```go
package main

import (
"context"
cryptorand "crypto/rand"
"crypto/rsa"
"path"
"strings"
"sync"
"time"

"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"

"github.com/kubermatic/kubeone/pkg/config"
"github.com/kubermatic/kubeone/pkg/installer/util"
"github.com/kubermatic/kubeone/pkg/ssh"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
```

Depending of the number of packages we import from a individual repository,
those packages can be grouped as well.

Like:
```go
package aws

import (
"errors"
"fmt"

kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
kuberneteshelper "github.com/kubermatic/kubermatic/api/pkg/kubernetes"
"github.com/kubermatic/kubermatic/api/pkg/provider"

"github.com/golang/glog"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/iam"
)
```
8 changes: 4 additions & 4 deletions pkg/archive/targz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package archive
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"os"

"github.com/pkg/errors"
)

type tarGzip struct {
Expand Down Expand Up @@ -43,11 +43,11 @@ func (tgz tarGzip) Add(file string, content string) error {
}

if err := tgz.arch.WriteHeader(hdr); err != nil {
return fmt.Errorf("failed to write tar file header: %v", err)
return errors.Wrap(err, "failed to write tar file header")
}

if _, err := tgz.arch.Write([]byte(content)); err != nil {
return fmt.Errorf("failed to write tar file data: %v", err)
return errors.Wrap(err, "failed to write tar file data")
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package certificate

import (
"crypto/rsa"
"errors"

"github.com/pkg/errors"

"github.com/kubermatic/kubeone/pkg/installer/util"

Expand Down
19 changes: 10 additions & 9 deletions pkg/cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package cmd

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/kubermatic/kubeone/pkg/config"
"github.com/kubermatic/kubeone/pkg/installer"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/kubermatic/kubeone/pkg/config"
"github.com/kubermatic/kubeone/pkg/installer"
)

type installOptions struct {
Expand All @@ -36,7 +37,7 @@ It's possible to source information about hosts from Terraform output, using the
RunE: func(_ *cobra.Command, args []string) error {
gopts, err := persistentGlobalOptions(rootFlags)
if err != nil {
return err
return errors.Wrap(err, "unable to get global flags")
}

logger := initLogger(gopts.Verbose)
Expand All @@ -61,16 +62,16 @@ It's possible to source information about hosts from Terraform output, using the
func runInstall(logger *logrus.Logger, installOptions *installOptions) error {
cluster, err := loadClusterConfig(installOptions.Manifest)
if err != nil {
return fmt.Errorf("failed to load cluster: %v", err)
return errors.Wrap(err, "failed to load cluster")
}

options, err := createInstallerOptions(installOptions.Manifest, cluster, installOptions)
if err != nil {
return fmt.Errorf("failed to create installer options: %v", err)
return errors.Wrap(err, "failed to create installer options")
}

if err = applyTerraform(installOptions.TerraformState, cluster); err != nil {
return fmt.Errorf("failed to setup PKI backup: %v", err)
return errors.Wrap(err, "failed to setup PKI backup")
}

if err = cluster.DefaultAndValidate(); err != nil {
Expand All @@ -95,13 +96,13 @@ func createInstallerOptions(clusterFile string, cluster *config.Cluster, options
// existing, zero byte backup)
stat, err := os.Stat(options.BackupFile)
if err != nil && stat != nil && stat.Size() > 0 {
return nil, fmt.Errorf("backup %s already exists, refusing to overwrite", options.BackupFile)
return nil, errors.Errorf("backup %s already exists, refusing to overwrite", options.BackupFile)
}

// try to write to the file before doing anything else
f, err := os.OpenFile(options.BackupFile, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return nil, fmt.Errorf("cannot open %s for writing", options.BackupFile)
return nil, errors.Errorf("cannot open %s for writing", options.BackupFile)
}
defer f.Close()

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/kubeconfig.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cmd

import (
"errors"
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand All @@ -30,7 +30,7 @@ It's possible to source information about hosts from Terraform output, using the
RunE: func(_ *cobra.Command, args []string) error {
gopts, err := persistentGlobalOptions(rootFlags)
if err != nil {
return err
return errors.Wrap(err, "unable to get global flags")
}

kopts.TerraformState = gopts.TerraformState
Expand All @@ -56,7 +56,7 @@ func runKubeconfig(kubeconfigOptions *kubeconfigOptions) error {

cluster, err := loadClusterConfig(kubeconfigOptions.Manifest)
if err != nil {
return fmt.Errorf("failed to load cluster: %v", err)
return errors.Wrap(err, "failed to load cluster")
}

// apply terraform
Expand Down
11 changes: 5 additions & 6 deletions pkg/cmd/reset.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package cmd

import (
"errors"
"fmt"

"github.com/kubermatic/kubeone/pkg/installer"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/kubermatic/kubeone/pkg/installer"
)

type resetOptions struct {
Expand All @@ -31,7 +30,7 @@ It's possible to source information about hosts from Terraform output, using the
RunE: func(_ *cobra.Command, args []string) error {
gopts, err := persistentGlobalOptions(rootFlags)
if err != nil {
return err
return errors.Wrap(err, "unable to get global flags")
}

logger := initLogger(gopts.Verbose)
Expand Down Expand Up @@ -60,7 +59,7 @@ func runReset(logger *logrus.Logger, resetOptions *resetOptions) error {

cluster, err := loadClusterConfig(resetOptions.Manifest)
if err != nil {
return fmt.Errorf("failed to load cluster: %v", err)
return errors.Wrap(err, "failed to load cluster")
}

if err = applyTerraform(resetOptions.TerraformState, cluster); err != nil {
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ func Execute() {
rootCmd := newRoot()

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
debug, _ := rootCmd.PersistentFlags().GetBool(globalDebugFlagName)
if debug {
fmt.Printf("%+v\n", err)
} else {
fmt.Println(err)
}
os.Exit(-1)
}
}
Expand All @@ -34,6 +39,7 @@ func newRoot() *cobra.Command {

fs.StringVarP(&opts.TerraformState, globalTerraformFlagName, "t", "", "path to terraform output JSON or - for stdin")
fs.BoolVarP(&opts.Verbose, globalVerboseFlagName, "v", false, "verbose")
fs.BoolVarP(&opts.Debug, globalDebugFlagName, "d", false, "debug")

rootCmd.AddCommand(
installCmd(fs),
Expand Down
Loading

0 comments on commit fc17bba

Please sign in to comment.