Skip to content

Commit

Permalink
Merge pull request #1187 from Adirio/import-order
Browse files Browse the repository at this point in the history
Imports ordering
  • Loading branch information
k8s-ci-robot authored Jan 7, 2020
2 parents 71e8a0b + 4e7ec4b commit 86dc861
Show file tree
Hide file tree
Showing 64 changed files with 110 additions and 107 deletions.
4 changes: 3 additions & 1 deletion cmd/go_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.

package main

import "testing"
import (
"testing"
)

func TestCheckGoVersion(t *testing.T) {

Expand Down
1 change: 1 addition & 0 deletions cmd/vendor_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"

"github.com/spf13/cobra"

"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/scaffold"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/cronjob-tutorial/testdata/emptycontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ package controllers

import (
"context"
"k8s.io/apimachinery/pkg/runtime"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/cronjob-tutorial/testdata/emptymain.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import (
"flag"
"fmt"
"os"
"sigs.k8s.io/controller-runtime/pkg/cache"

"k8s.io/apimachinery/pkg/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ package v1

import (
"github.com/robfig/cron"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
validationutils "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"

ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down
1 change: 1 addition & 0 deletions docs/book/src/cronjob-tutorial/testdata/project/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

batchv1 "tutorial.kubebuilder.io/project/api/v1"
"tutorial.kubebuilder.io/project/controllers"
// +kubebuilder:scaffold:imports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
"flag"
"os"

"project/pkg/apis"
"project/pkg/controller"
"project/pkg/webhook"

_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"

"project/pkg/apis"
"project/pkg/controller"
"project/pkg/webhook"
)

func main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import (
"time"

"github.com/robfig/cron"

batchv1 "project/pkg/apis/batch/v1"

kbatch "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -37,6 +34,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"

batchv1 "project/pkg/apis/batch/v1"
)

var log = logf.Log.WithName("controller")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"sync"
"testing"

"project/pkg/apis"

"github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"project/pkg/apis"
)

var cfg *rest.Config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package mutating

import (
batchv1 "project/pkg/apis/batch/v1"

admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"

batchv1 "project/pkg/apis/batch/v1"
)

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"context"
"net/http"

batchv1 "project/pkg/apis/batch/v1"

"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"

batchv1 "project/pkg/apis/batch/v1"
)

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package validating

import (
batchv1 "project/pkg/apis/batch/v1"

admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"

batchv1 "project/pkg/apis/batch/v1"
)

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ import (
"net/http"

"github.com/robfig/cron"

batchv1 "project/pkg/apis/batch/v1"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
validationutils "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"

batchv1 "project/pkg/apis/batch/v1"
)

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"context"
"net/http"

batchv1 "project/pkg/apis/batch/v1"

"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"

batchv1 "project/pkg/apis/batch/v1"
)

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package validating

import (
batchv1 "project/pkg/apis/batch/v1"

admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"

batchv1 "project/pkg/apis/batch/v1"
)

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ package v1

import (
"github.com/robfig/cron"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
validationutils "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"

ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

batchv1 "tutorial.kubebuilder.io/project/api/v1"
batchv2 "tutorial.kubebuilder.io/project/api/v2"
"tutorial.kubebuilder.io/project/controllers"
Expand Down
1 change: 1 addition & 0 deletions pkg/scaffold/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/scaffold"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
Expand Down
14 changes: 13 additions & 1 deletion pkg/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ import (
"text/template"

"golang.org/x/tools/imports"

"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/project"
"sigs.k8s.io/yaml"
)

var options = imports.Options{
Comments: true,
TabIndent: true,
TabWidth: 8,
FormatOnly: true,
}

// Scaffold writes Templates to scaffold new files
type Scaffold struct {
// BoilerplatePath is the path to the boilerplate file
Expand Down Expand Up @@ -181,6 +189,10 @@ func (s *Scaffold) Execute(u *model.Universe, options input.Options, files ...in
if err := s.defaultOptions(&options); err != nil {
return err
}

// Set the repo as the local prefix so that it knows how to group imports
imports.LocalPrefix = s.Project.Repo

for _, f := range files {
m, err := s.buildFileModel(f)
if err != nil {
Expand Down Expand Up @@ -289,7 +301,7 @@ func (s *Scaffold) doTemplate(i input.Input, e input.File) ([]byte, error) {

// gofmt the imports
if filepath.Ext(i.Path) == ".go" {
b, err = imports.Process(i.Path, b, nil)
b, err = imports.Process(i.Path, b, &options)
if err != nil {
fmt.Printf("%s\n", out.Bytes())
return nil, err
Expand Down
6 changes: 2 additions & 4 deletions pkg/scaffold/v1/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

"github.com/gobuffalo/flect"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/resource"
)
Expand Down Expand Up @@ -103,14 +104,11 @@ package {{ lower .Resource.Kind }}
import (
{{ if .Resource.CreateExampleReconcileBody }} "context"
"log"
"reflect"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -126,9 +124,9 @@ import (
var log = logf.Log.WithName("{{ lower .Resource.Kind }}-controller")
{{ else }} "context"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down
1 change: 0 additions & 1 deletion pkg/scaffold/v1/controller/controllersuitetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import (
"path/filepath"
"sync"
"testing"
"github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
Expand Down
1 change: 0 additions & 1 deletion pkg/scaffold/v1/controller/controllertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ package {{ lower .Resource.Kind }}
import (
"testing"
"time"
"github.com/onsi/gomega"
"golang.org/x/net/context"
{{ if .Resource.CreateExampleReconcileBody -}}
Expand Down
1 change: 0 additions & 1 deletion pkg/scaffold/v1/crd/typestest.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ package {{ .Resource.Version }}
import (
"testing"
"github.com/onsi/gomega"
"golang.org/x/net/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
1 change: 0 additions & 1 deletion pkg/scaffold/v1/crd/version_suitetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import (
"os"
"path/filepath"
"testing"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down
1 change: 0 additions & 1 deletion pkg/scaffold/v1/manager/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ package main
import (
"flag"
"os"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down
1 change: 1 addition & 0 deletions pkg/scaffold/v1/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/kubebuilder/pkg/model"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
"sigs.k8s.io/kubebuilder/pkg/scaffold/scaffoldtest"
Expand Down
1 change: 0 additions & 1 deletion pkg/scaffold/v1/webhook/add_admissionbuilder_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ package {{ .Server }}server
import (
"fmt"
"{{ .Repo }}/pkg/webhook/{{ .Server }}_server/{{ lower .Resource.Kind }}/{{ .Type }}"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/v1/webhook/admissionbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ const admissionWebhookBuilderTemplate = `{{ .Boilerplate }}
package {{ .Type }}
import (
{{ .Resource.Group}}{{ .Resource.Version }} "{{ .ResourcePackage }}/{{ .Resource.Group}}/{{ .Resource.Version }}"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"
{{ .Resource.Group}}{{ .Resource.Version }} "{{ .ResourcePackage }}/{{ .Resource.Group}}/{{ .Resource.Version }}"
)
func init() {
Expand Down
4 changes: 1 addition & 3 deletions pkg/scaffold/v1/webhook/admissionhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ package {{ .Type }}
import (
"context"
"net/http"
{{ .Resource.Group}}{{ .Resource.Version }} "{{ .ResourcePackage }}/{{ .Resource.Group}}/{{ .Resource.Version }}"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
{{ .Resource.Group}}{{ .Resource.Version }} "{{ .ResourcePackage }}/{{ .Resource.Group}}/{{ .Resource.Version }}"
)
func init() {
Expand Down
Loading

0 comments on commit 86dc861

Please sign in to comment.