Skip to content

Commit

Permalink
feat: generate Dockerfile from declarative plugin
Browse files Browse the repository at this point in the history
We need to copy the channels directory in the Dockerfile, particularly
now that we're running with non-root permissions and it is no longer a
one-liner.

Signed-off-by: Vivek Singh <svivekkumar@vmware.com>
  • Loading branch information
viveksyngh committed Feb 6, 2022
1 parent 958ac27 commit e5659b2
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 29 deletions.
31 changes: 5 additions & 26 deletions pkg/plugins/golang/declarative/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,20 @@ package v1
import (
"errors"
"fmt"
"path/filepath"

"github.com/spf13/afero"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1/internal/templates"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1/scaffolds"
goPluginV2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
)

const (
// kbDeclarativePattern is the sigs.k8s.io/kubebuilder-declarative-pattern version
kbDeclarativePatternForV2 = "v0.0.0-20200522144838-848d48e5b073"
kbDeclarativePatternForV3 = "fea7e5cc701290589ec20ef4d9c0629d08b5307d"

exampleManifestVersion = "0.0.1"
)

var _ plugin.CreateAPISubcommand = &createAPISubcommand{}
Expand Down Expand Up @@ -97,27 +92,11 @@ func (p *createAPISubcommand) InjectResource(res *resource.Resource) error {
func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
fmt.Println("updating scaffold with declarative pattern...")

// Load the boilerplate
bp, err := afero.ReadFile(fs.FS, filepath.Join("hack", "boilerplate.go.txt"))
scaffolder := scaffolds.NewAPIScaffolder(p.config, *p.resource)
scaffolder.InjectFS(fs)
err := scaffolder.Scaffold()
if err != nil {
return fmt.Errorf("error updating scaffold: unable to load boilerplate: %w", err)
}
boilerplate := string(bp)

// Initialize the machinery.Scaffold that will write the files to disk
scaffold := machinery.NewScaffold(fs,
machinery.WithConfig(p.config),
machinery.WithBoilerplate(boilerplate),
machinery.WithResource(p.resource),
)

if err := scaffold.Execute(
&templates.Types{},
&templates.Controller{},
&templates.Channel{ManifestVersion: exampleManifestVersion},
&templates.Manifest{ManifestVersion: exampleManifestVersion},
); err != nil {
return fmt.Errorf("error updating scaffold: %w", err)
return err
}

// Track the resources following a declarative approach
Expand Down
46 changes: 46 additions & 0 deletions pkg/plugins/golang/declarative/v1/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1/scaffolds"
)

var _ plugin.InitSubcommand = &initSubcommand{}

type initSubcommand struct {
config config.Config
}

func (p *initSubcommand) InjectConfig(c config.Config) error {
p.config = c

return nil
}

func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
scaffolder := scaffolds.NewInitScaffolder(p.config)
scaffolder.InjectFS(fs)
err := scaffolder.Scaffold()
if err != nil {
return err
}
return nil
}
76 changes: 76 additions & 0 deletions pkg/plugins/golang/declarative/v1/internal/templates/dockerfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package templates

import (
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
)

var _ machinery.Template = &Dockerfile{}

// Dockerfile scaffolds a file that defines the containerized build process
type Dockerfile struct {
machinery.TemplateMixin
}

// SetTemplateDefaults implements file.Template
func (f *Dockerfile) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = "Dockerfile"
}

f.TemplateBody = dockerfileTemplate

f.IfExistsAction = machinery.OverwriteFile

return nil
}

const dockerfileTemplate = `# Build the manager binary
FROM golang:1.16 as builder
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download
# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
# Stage channels and make readable
COPY channels/ /channels/
RUN chmod -R a+rx /channels/
# 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:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /channels /channels
USER 65532:65532
ENTRYPOINT ["/manager"]
`
4 changes: 4 additions & 0 deletions pkg/plugins/golang/declarative/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var _ plugin.CreateAPI = Plugin{}

// Plugin implements the plugin.Full interface
type Plugin struct {
initSubcommand
createAPISubcommand
}

Expand All @@ -49,6 +50,9 @@ func (Plugin) Version() plugin.Version { return pluginVersion }
// SupportedProjectVersions returns an array with all project versions supported by the plugin
func (Plugin) SupportedProjectVersions() []config.Version { return supportedProjectVersions }

// GetInitSubcommand will return the subcommand which is responsible for initializing and common scaffolding
func (p Plugin) GetInitSubcommand() plugin.InitSubcommand { return &p.initSubcommand }

// GetCreateAPISubcommand will return the subcommand which is responsible for scaffolding apis
func (p Plugin) GetCreateAPISubcommand() plugin.CreateAPISubcommand { return &p.createAPISubcommand }

Expand Down
84 changes: 84 additions & 0 deletions pkg/plugins/golang/declarative/v1/scaffolds/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scaffolds

import (
"fmt"
"path/filepath"

"github.com/spf13/afero"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1/internal/templates"
)

const (
exampleManifestVersion = "0.0.1"
)

var _ plugins.Scaffolder = &apiScaffolder{}

type apiScaffolder struct {
config config.Config
resource resource.Resource

// fs is the filesystem that will be used by the scaffolder
fs machinery.Filesystem
}

// NewAPIScaffolder returns a new Scaffolder for declarative
func NewAPIScaffolder(config config.Config, res resource.Resource) plugins.Scaffolder {
return &apiScaffolder{
config: config,
resource: res,
}
}

// InjectFS implements cmdutil.Scaffolder
func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) {
s.fs = fs
}

// Scaffold implements cmdutil.Scaffolder
func (s *apiScaffolder) Scaffold() error {
// Load the boilerplate
boilerplate, err := afero.ReadFile(s.fs.FS, filepath.Join("hack", "boilerplate.go.txt"))
if err != nil {
return fmt.Errorf("error updating scaffold: unable to load boilerplate: %w", err)
}

// Initialize the machinery.Scaffold that will write the files to disk
scaffold := machinery.NewScaffold(s.fs,
machinery.WithConfig(s.config),
machinery.WithBoilerplate(string(boilerplate)),
machinery.WithResource(&s.resource),
)

err = scaffold.Execute(
&templates.Types{},
&templates.Controller{},
&templates.Channel{ManifestVersion: exampleManifestVersion},
&templates.Manifest{ManifestVersion: exampleManifestVersion},
&templates.Dockerfile{},
)
if err != nil {
return fmt.Errorf("error updating scaffold: %w", err)
}
return nil
}
64 changes: 64 additions & 0 deletions pkg/plugins/golang/declarative/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scaffolds

import (
"fmt"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1/internal/templates"
)

var _ plugins.Scaffolder = &initScaffolder{}

type initScaffolder struct {
config config.Config

// fs is the filesystem that will be used by the scaffolder
fs machinery.Filesystem
}

// NewInitScaffolder returns a new Scaffolder for declarative
func NewInitScaffolder(config config.Config) plugins.Scaffolder {
return &apiScaffolder{
config: config,
}
}

// InjectFS implements cmdutil.Scaffolder
func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
s.fs = fs
}

// Scaffold implements cmdutil.Scaffolder
func (s *initScaffolder) Scaffold() error {

// Initialize the machinery.Scaffold that will write the files to disk
scaffold := machinery.NewScaffold(s.fs,
machinery.WithConfig(s.config),
)

err := scaffold.Execute(
&templates.Dockerfile{},
)
if err != nil {
return fmt.Errorf("error updating scaffold: %w", err)
}
return nil
}
12 changes: 9 additions & 3 deletions testdata/project-v2-addon/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM golang:1.13 as builder
FROM golang:1.16 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand All @@ -15,13 +15,19 @@ COPY api/ api/
COPY controllers/ controllers/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

# Stage channels and make readable
COPY channels/ /channels/
RUN chmod -R a+rx /channels/

# 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:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
USER nonroot:nonroot
COPY --from=builder /channels /channels

USER 65532:65532

ENTRYPOINT ["/manager"]
6 changes: 6 additions & 0 deletions testdata/project-v3-addon/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ COPY controllers/ controllers/
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

# Stage channels and make readable
COPY channels/ /channels/
RUN chmod -R a+rx /channels/

# 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:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /channels /channels

USER 65532:65532

ENTRYPOINT ["/manager"]

0 comments on commit e5659b2

Please sign in to comment.