Skip to content

Commit

Permalink
✨ (kustomize/v1) add validation to let users know when the architectu…
Browse files Browse the repository at this point in the history
…re used to run the plugin is not supported
  • Loading branch information
Camila Macedo committed May 24, 2022
1 parent a2b2be2 commit 4b96e1d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
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 use [kustomize/v2-alpha](./kustomize-v2-alpha.md) if you are looking for use it to scaffold projects in
others architecture environments. (i.e. if you be looking to scaffold projects within Apple Silicon/M1 (`darwin/arm64`) this plugin
will not work on it, more info: [kubernetes-sigs/kustomize/issues/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
61 changes: 59 additions & 2 deletions pkg/plugins/common/kustomize/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package v1
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"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,16 @@ 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 use its v3 release (%s).
Therefore, darwin/arm64 is not supported by it since the project does not provides
binaries for this architecture. The current support architetcures 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 +107,50 @@ func (p *initSubcommand) InjectConfig(c config.Config) error {
return nil
}

func (p *initSubcommand) PreScaffold(machinery.Filesystem) error {

// Check what is the SO
cmd := exec.Command("uname", "-s")
os, err := cmd.CombinedOutput()
osFound := strings.ToLower(string(os))
if err != nil {
log.Warnf("unable to check the Operational System to verify if kustomize/v1 can be supported "+
"on this environment: %s", err)
return nil
}
// Check what is the ARCH
cmd = exec.Command("uname", "-m")
arch, err := cmd.CombinedOutput()
archFound := strings.ToLower(string(arch))
archFound = strings.Replace(archFound, "x86_64", "amd64", -1)
if err != nil {
log.Warnf("unable to check the architecture to verify if kustomize/v1 can be supported on "+
"this environment: %s", err)
return nil
}
localPlatform := fmt.Sprintf("%s/%s", strings.TrimSpace(osFound),
strings.TrimSpace(archFound))

found := false
for _, value := range supportedArchs {
if value == localPlatform {
found = true
break
}
}

if !found {
log.Warnf("the architecture 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 this kustomize version supported "+
"and used by this plugin. The current supported architectures are: %q",
localPlatform,
KustomizeVersion,
supportedArchs)
}

return nil
}

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

0 comments on commit 4b96e1d

Please sign in to comment.