Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (kustomize/v1) add validation to let users know when the architecture used to run the plugin is not supported #2698

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/book/src/plugins/kustomize-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ The kustomize plugin allows you to scaffold all kustomize manifests used to work
By using the kustomize plugin, you can create your own language plugins and ensure that you will have the same configurations
and features provided by it.

<aside class="note">
<h1>Supportability</h1>

This plugin uses [kubernetes-sigs/kustomize](https://github.com/kubernetes-sigs/kustomize) v3 and the architectures supported are:
- linux/amd64
- linux/arm64
- darwin/amd64

You might want to consider using [kustomize/v2-alpha](./kustomize-v2-alpha.md) if you are looking to scaffold projects in
other architecture environments. (i.e. if you are looking to scaffold projects with Apple Silicon/M1 (`darwin/arm64`) this plugin
will not work, more info: [kubernetes-sigs/kustomize#4612](https://github.com/kubernetes-sigs/kustomize/issues/4612)).
</aside>

Note that projects such as [Operator-sdk][sdk] consume the Kubebuilder project as a lib and provide options to work with other languages
like Ansible and Helm. The kustomize plugin allows them to easily keep a maintained configuration and ensure that all languages have
the same configuration. It is also helpful if you are looking to provide nice plugins which will perform changes on top of
Expand Down
49 changes: 47 additions & 2 deletions pkg/plugins/common/kustomize/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
Expand All @@ -33,6 +35,11 @@ import (

var _ plugin.InitSubcommand = &initSubcommand{}

// Verify if the local environment is supported by this plugin
var supportedArchs = []string{"linux/amd64",
"linux/arm64",
"darwin/amd64"}

type initSubcommand struct {
config config.Config

Expand All @@ -43,10 +50,17 @@ type initSubcommand struct {
}

func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
subcmdMeta.Description = `Initialize a common project including the following files:
subcmdMeta.Description = fmt.Sprintf(`Initialize a common project including the following files:
- a "PROJECT" file that stores project configuration
- several YAML files for project deployment under the "config" directory
`

NOTE: The kustomize/v1 plugin used to do this scaffold uses the v3 release (%s).
Therefore, darwin/arm64 is not supported since Kustomize does not provide v3
binaries for this architecture. The currently supported architectures are %q.
More info: https://github.com/kubernetes-sigs/kustomize/issues/4612.

`, KustomizeVersion, supportedArchs)

subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with your domain and name in copyright
%[1]s init --plugins common/v3 --domain example.org

Expand Down Expand Up @@ -94,6 +108,37 @@ func (p *initSubcommand) InjectConfig(c config.Config) error {
return nil
}

func (p *initSubcommand) PreScaffold(machinery.Filesystem) error {
arch := runtime.GOARCH
// It probably will never return x86_64. However, we are here checking the support for the binaries
// So that, x86_64 means getting the Linux/amd64 binary. Then, we just keep this line to ensure
// that it complies with the same code implementation that we have in the targets. In case someone
// call the command inform the GOARCH=x86_64 then, we will properly handle the scenario
// since it will work successfully and will instal the Linux/amd64 binary via the Makefile target.
arch = strings.Replace(arch, "x86_64", "amd64", -1)
camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
localPlatform := fmt.Sprintf("%s/%s", strings.TrimSpace(runtime.GOOS), strings.TrimSpace(arch))

camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
if !hasSupportFor(localPlatform) {
log.Warnf("the platform of this environment (%s) is not suppported by kustomize v3 (%s) which is "+
"used in this scaffold. You will be unable to download a binary for the kustomize version supported "+
"and used by this plugin. The currently supported platforms are: %q",
localPlatform,
KustomizeVersion,
supportedArchs)
}

return nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the error return of this function will always be nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because we will never return an error here so far.
See that it is an implementation of the interface:

// PreScaffold executes tasks before the main scaffolding.
	PreScaffold(machinery.Filesystem) error

}

func hasSupportFor(localPlatform string) bool {
for _, value := range supportedArchs {
if value == localPlatform {
return true
}
}
return false
}

func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
scaffolder := scaffolds.NewInitScaffolder(p.config)
scaffolder.InjectFS(fs)
Expand Down