Skip to content

Commit

Permalink
🏃 parameterize controller-runtime/tools version
Browse files Browse the repository at this point in the history
  • Loading branch information
droot committed Sep 23, 2019
1 parent 6236ac2 commit 8d73b15
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 9 deletions.
30 changes: 26 additions & 4 deletions pkg/scaffold/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ import (
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook"
)

const (
// controller runtime version to be used in the project
controllerRuntimeVersion = "v0.2.0"
// ControllerTools version to be used in the project
controllerToolsVersion = "v0.2.0"
)

type ProjectScaffolder interface {
EnsureDependencies() (bool, error)
Scaffold() error
Expand Down Expand Up @@ -147,11 +154,26 @@ func (p *V2Project) Validate() error {
}

func (p *V2Project) EnsureDependencies() (bool, error) {
c := exec.Command("go", "mod", "tidy") // #nosec
// ensure that we are pinning controller-runtime version
// xref: https://github.com/kubernetes-sigs/kubebuilder/issues/997
c := exec.Command("go", "get", "sigs.k8s.io/controller-runtime@"+controllerRuntimeVersion) // #nosec
c.Stderr = os.Stderr
c.Stdout = os.Stdout
fmt.Println(strings.Join(c.Args, " "))
return true, c.Run()
err := c.Run()
if err != nil {
return false, err
}

c = exec.Command("go", "mod", "tidy") // #nosec
c.Stderr = os.Stderr
c.Stdout = os.Stdout
fmt.Println(strings.Join(c.Args, " "))
err = c.Run()
if err != nil {
return false, err
}
return true, err
}

func (p *V2Project) buildUniverse() *model.Universe {
Expand Down Expand Up @@ -201,8 +223,8 @@ func (p *V2Project) Scaffold() error {
&project.AuthProxyRoleBinding{},
&managerv2.Config{Image: imgName},
&scaffoldv2.Main{},
&scaffoldv2.GoMod{},
&scaffoldv2.Makefile{Image: imgName},
&scaffoldv2.GoMod{ControllerRuntimeVersion: controllerRuntimeVersion},
&scaffoldv2.Makefile{Image: imgName, ControllerToolsVersion: controllerToolsVersion},
&scaffoldv2.Dockerfile{},
&scaffoldv2.Kustomize{},
&scaffoldv2.ManagerWebhookPatch{},
Expand Down
3 changes: 2 additions & 1 deletion pkg/scaffold/v2/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var _ input.File = &GoMod{}
// GoMod writes a templatefile for go.mod
type GoMod struct {
input.Input
ControllerRuntimeVersion string
}

// GetInput implements input.File
Expand All @@ -43,6 +44,6 @@ module {{ .Repo }}
go 1.12
require (
sigs.k8s.io/controller-runtime v0.2.0
sigs.k8s.io/controller-runtime {{ .ControllerRuntimeVersion }}
)
`
4 changes: 3 additions & 1 deletion pkg/scaffold/v2/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Makefile struct {
input.Input
// Image is controller manager image name
Image string
// Controller tools version to use in the project
ControllerToolsVersion string
}

// GetInput implements input.File
Expand Down Expand Up @@ -106,7 +108,7 @@ docker-push:
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.0
go get sigs.k8s.io/controller-tools/cmd/controller-gen@{{.ControllerToolsVersion}}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
Expand Down
4 changes: 3 additions & 1 deletion testdata/project-v2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:latest
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
USER nonroot:nonroot

ENTRYPOINT ["/manager"]
2 changes: 1 addition & 1 deletion testdata/project-v2/api/v1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ spec:
group: crew.testproject.org
names:
kind: Admiral
listKind: AdmiralList
plural: admirals
singular: admiral
scope: Cluster
validation:
openAPIV3Schema:
Expand All @@ -34,6 +36,7 @@ spec:
description: AdmiralStatus defines the observed state of Admiral
type: object
type: object
version: v1
versions:
- name: v1
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ spec:
group: crew.testproject.org
names:
kind: Captain
listKind: CaptainList
plural: captains
singular: captain
scope: ""
validation:
openAPIV3Schema:
Expand All @@ -34,6 +36,7 @@ spec:
description: CaptainStatus defines the observed state of Captain
type: object
type: object
version: v1
versions:
- name: v1
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ spec:
group: crew.testproject.org
names:
kind: FirstMate
listKind: FirstMateList
plural: firstmates
singular: firstmate
scope: ""
validation:
openAPIV3Schema:
Expand All @@ -34,6 +36,7 @@ spec:
description: FirstMateStatus defines the observed state of FirstMate
type: object
type: object
version: v1
versions:
- name: v1
served: true
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
Port: 9843,
Port: 9443,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down

0 comments on commit 8d73b15

Please sign in to comment.