diff --git a/go.mod b/go.mod index 9f02117ce9..4cf9f73064 100644 --- a/go.mod +++ b/go.mod @@ -43,7 +43,7 @@ require ( rsc.io/letsencrypt v0.0.3 // indirect sigs.k8s.io/controller-runtime v0.6.0 sigs.k8s.io/controller-tools v0.3.0 - sigs.k8s.io/kubebuilder v1.0.9-0.20200618125005-36aa113dbe99 + sigs.k8s.io/kubebuilder v1.0.9-0.20200723162140-851e4f5b4163 sigs.k8s.io/yaml v1.2.0 ) diff --git a/go.sum b/go.sum index c856d5c4d3..5b4dd9b02f 100644 --- a/go.sum +++ b/go.sum @@ -1024,8 +1024,8 @@ sigs.k8s.io/controller-runtime v0.6.0 h1:Fzna3DY7c4BIP6KwfSlrfnj20DJ+SeMBK8HSFvO sigs.k8s.io/controller-runtime v0.6.0/go.mod h1:CpYf5pdNY/B352A1TFLAS2JVSlnGQ5O2cftPHndTroo= sigs.k8s.io/controller-tools v0.3.0 h1:y3YD99XOyWaXkiF1kd41uRvfp/64teWcrEZFuHxPhJ4= sigs.k8s.io/controller-tools v0.3.0/go.mod h1:enhtKGfxZD1GFEoMgP8Fdbu+uKQ/cq1/WGJhdVChfvI= -sigs.k8s.io/kubebuilder v1.0.9-0.20200618125005-36aa113dbe99 h1:wdt455ji+MywIGDGQVUQUEGHa8WiRy0sfr5YFn00HbA= -sigs.k8s.io/kubebuilder v1.0.9-0.20200618125005-36aa113dbe99/go.mod h1:FGPx0hvP73+bapzWoy5ePuhAJYgJjrFbPxgvWyortM0= +sigs.k8s.io/kubebuilder v1.0.9-0.20200723162140-851e4f5b4163 h1:rLvDKLL1bIK/NWrwFvQ/ywhTAjri/qHaIAEUaSo8ols= +sigs.k8s.io/kubebuilder v1.0.9-0.20200723162140-851e4f5b4163/go.mod h1:lkExAOdnNf9BGrvi4lWHCMo1fa6xtENt/QVwDhWpK+c= sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0= sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU= sigs.k8s.io/structured-merge-diff/v3 v3.0.0-20200116222232-67a7b8c61874/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw= diff --git a/internal/plugins/helm/v1/chartutil/chart.go b/internal/plugins/helm/v1/chartutil/chart.go index 0792b5585d..9a6e1fbca2 100644 --- a/internal/plugins/helm/v1/chartutil/chart.go +++ b/internal/plugins/helm/v1/chartutil/chart.go @@ -152,7 +152,7 @@ func CreateChart(projectDir string, opts CreateOptions) (*resource.Options, *cha return nil, nil, fmt.Errorf("failed to load chart: %v", err) } - log.Infof("Created %s", relChartPath) + fmt.Printf("Created %s\n", relChartPath) return r, c, nil } diff --git a/internal/plugins/helm/v1/init.go b/internal/plugins/helm/v1/init.go index eddd889ad4..ad3e58e1e5 100644 --- a/internal/plugins/helm/v1/init.go +++ b/internal/plugins/helm/v1/init.go @@ -17,7 +17,7 @@ package v1 import ( "fmt" "os" - "path/filepath" + "path" "strings" "github.com/spf13/pflag" @@ -61,12 +61,21 @@ Writes the following files: ` ctx.Examples = fmt.Sprintf(` $ %s init --plugins=%s \ --domain=example.com \ - --group=apps --version=v1alpha1 \ + --group=apps \ + --version=v1alpha1 \ --kind=AppService $ %s init --plugins=%s \ + --project-name=myapp --domain=example.com \ - --group=apps --version=v1alpha1 \ + --group=apps \ + --version=v1alpha1 \ + --kind=AppService + + $ %s init --plugins=%s \ + --domain=example.com \ + --group=apps \ + --version=v1alpha1 \ --kind=AppService \ --helm-chart=myrepo/app @@ -98,14 +107,15 @@ Writes the following files: --domain=example.com \ --helm-chart=/path/to/local/chart-archives/app-1.2.3.tgz `, - ctx.CommandName, plugin.KeyFor(Plugin{}), - ctx.CommandName, plugin.KeyFor(Plugin{}), - ctx.CommandName, plugin.KeyFor(Plugin{}), - ctx.CommandName, plugin.KeyFor(Plugin{}), - ctx.CommandName, plugin.KeyFor(Plugin{}), - ctx.CommandName, plugin.KeyFor(Plugin{}), - ctx.CommandName, plugin.KeyFor(Plugin{}), - ctx.CommandName, plugin.KeyFor(Plugin{}), + ctx.CommandName, pluginKey, + ctx.CommandName, pluginKey, + ctx.CommandName, pluginKey, + ctx.CommandName, pluginKey, + ctx.CommandName, pluginKey, + ctx.CommandName, pluginKey, + ctx.CommandName, pluginKey, + ctx.CommandName, pluginKey, + ctx.CommandName, pluginKey, ) p.commandName = ctx.CommandName @@ -115,13 +125,14 @@ Writes the following files: func (p *initPlugin) BindFlags(fs *pflag.FlagSet) { fs.SortFlags = false fs.StringVar(&p.config.Domain, "domain", "my.domain", "domain for groups") + fs.StringVar(&p.config.ProjectName, "project-name", "", "name of this project, the default being directory name") p.apiPlugin.BindFlags(fs) } // InjectConfig will inject the PROJECT file/config in the plugin func (p *initPlugin) InjectConfig(c *config.Config) { // v3 project configs get a 'layout' value. - c.Layout = plugin.KeyFor(Plugin{}) + c.Layout = pluginKey p.config = c p.apiPlugin.config = p.config } @@ -133,14 +144,17 @@ func (p *initPlugin) Run() error { // Validate perform the required validations for this plugin func (p *initPlugin) Validate() error { - // Check if the project name is a valid namespace according to k8s - dir, err := os.Getwd() - if err != nil { - return fmt.Errorf("error to get the current path: %v", err) + + // Check if the project name is a valid k8s namespace (DNS 1123 label). + if p.config.ProjectName == "" { + dir, err := os.Getwd() + if err != nil { + return fmt.Errorf("error getting current directory: %v", err) + } + p.config.ProjectName = path.Base(dir) } - projectName := filepath.Base(dir) - if err := validation.IsDNS1123Label(strings.ToLower(projectName)); err != nil { - return fmt.Errorf("project name (%s) is invalid: %v", projectName, err) + if err := validation.IsDNS1123Label(strings.ToLower(p.config.ProjectName)); err != nil { + return fmt.Errorf("project name (%s) is invalid: %v", p.config.ProjectName, err) } defaultOpts := chartutil.CreateOptions{CRDVersion: "v1"} @@ -174,10 +188,10 @@ func (p *initPlugin) PostScaffold() error { return err } - if !p.doAPIScaffold { - fmt.Printf("Next: define a resource with:\n$ %s create api\n", p.commandName) - } else { + if p.doAPIScaffold { return p.apiPlugin.PostScaffold() } + + fmt.Printf("Next: define a resource with:\n$ %s create api\n", p.commandName) return nil } diff --git a/internal/plugins/helm/v1/plugin.go b/internal/plugins/helm/v1/plugin.go index 74f5543221..e981fdd588 100644 --- a/internal/plugins/helm/v1/plugin.go +++ b/internal/plugins/helm/v1/plugin.go @@ -26,6 +26,7 @@ const pluginName = "helm" + plugins.DefaultNameQualifier var ( supportedProjectVersions = []string{config.Version3Alpha} pluginVersion = plugin.Version{Number: 1} + pluginKey = plugin.KeyFor(Plugin{}) ) var ( diff --git a/internal/plugins/helm/v1/scaffolds/init.go b/internal/plugins/helm/v1/scaffolds/init.go index c3742e7635..ed302c6cc6 100644 --- a/internal/plugins/helm/v1/scaffolds/init.go +++ b/internal/plugins/helm/v1/scaffolds/init.go @@ -79,15 +79,23 @@ func (s *initScaffolder) scaffold() error { if err := os.MkdirAll(chartutil.HelmChartsDir, 0755); err != nil { return err } + + projectName := strings.ToLower(s.config.ProjectName) + return machinery.NewScaffold().Execute( s.newUniverse(), &templates.GitIgnore{}, &templates.AuthProxyRole{}, &templates.AuthProxyRoleBinding{}, - &metricsauth.AuthProxyPatch{}, + &metricsauth.AuthProxyPatch{ + OperatorName: projectName, + }, &metricsauth.AuthProxyService{}, &metricsauth.ClientClusterRole{}, - &manager.Config{Image: imageName}, + &manager.Config{ + Image: imageName, + OperatorName: projectName, + }, &templates.Makefile{ Image: imageName, KustomizeVersion: KustomizeVersion, @@ -96,7 +104,9 @@ func (s *initScaffolder) scaffold() error { &templates.Dockerfile{ HelmOperatorVersion: HelmOperatorVersion, }, - &templates.Kustomize{}, + &templates.Kustomize{ + Prefix: projectName, + }, &templates.ManagerRole{}, &templates.ManagerRoleBinding{}, &templates.LeaderElectionRole{}, diff --git a/internal/plugins/helm/v1/scaffolds/templates/manager/config.go b/internal/plugins/helm/v1/scaffolds/templates/manager/config.go index 304492a7a9..4f7882afee 100644 --- a/internal/plugins/helm/v1/scaffolds/templates/manager/config.go +++ b/internal/plugins/helm/v1/scaffolds/templates/manager/config.go @@ -21,6 +21,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "sigs.k8s.io/kubebuilder/pkg/model/file" ) @@ -51,7 +52,7 @@ func (f *Config) SetTemplateDefaults() error { if err != nil { return fmt.Errorf("error getting working directory: %v", err) } - f.OperatorName = filepath.Base(dir) + f.OperatorName = strings.ToLower(filepath.Base(dir)) } return nil } diff --git a/internal/plugins/helm/v1/scaffolds/templates/metricsauth/auth_proxy_patch.go b/internal/plugins/helm/v1/scaffolds/templates/metricsauth/auth_proxy_patch.go index 4c3347e726..98cf445ca6 100644 --- a/internal/plugins/helm/v1/scaffolds/templates/metricsauth/auth_proxy_patch.go +++ b/internal/plugins/helm/v1/scaffolds/templates/metricsauth/auth_proxy_patch.go @@ -21,6 +21,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "sigs.k8s.io/kubebuilder/pkg/model/file" ) @@ -50,13 +51,13 @@ func (f *AuthProxyPatch) SetTemplateDefaults() error { if err != nil { return fmt.Errorf("error to get the current path: %v", err) } - f.OperatorName = filepath.Base(dir) + f.OperatorName = strings.ToLower(filepath.Base(dir)) } return nil } -const kustomizeAuthProxyPatchTemplate = `# This patch inject a sidecar container which is a HTTP proxy for the +const kustomizeAuthProxyPatchTemplate = `# This patch inject a sidecar container which is a HTTP proxy for the # controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews. apiVersion: apps/v1 kind: Deployment diff --git a/internal/plugins/helm/v1/scaffolds/templates/role.go b/internal/plugins/helm/v1/scaffolds/templates/role.go index c726bec50b..3565000e9f 100644 --- a/internal/plugins/helm/v1/scaffolds/templates/role.go +++ b/internal/plugins/helm/v1/scaffolds/templates/role.go @@ -248,7 +248,7 @@ type roleDiscoveryInterface interface { // discovery API to lookup each resource in the resulting manifest. // The role scaffold will have IsClusterScoped=true if the chart lists cluster scoped resources func (f *ManagerRoleUpdater) updateForChart(dc roleDiscoveryInterface) { - log.Info("Generating RBAC rules") + fmt.Println("Generating RBAC rules") clusterResourceRules, namespacedResourceRules, err := generateRoleRules(dc, f.Chart) if err != nil { diff --git a/internal/util/projutil/project_util.go b/internal/util/projutil/project_util.go index 0e87a51098..ce5409d571 100644 --- a/internal/util/projutil/project_util.go +++ b/internal/util/projutil/project_util.go @@ -103,6 +103,7 @@ func CheckProjectRoot() error { return nil } +// TODO: remove this (should use os.Getwd() or Config.ProjectName). func MustGetwd() string { wd, err := os.Getwd() if err != nil {