Skip to content

Commit

Permalink
This commit bumps sigs.k8s.io/kubebuilder to the latest commit so
Browse files Browse the repository at this point in the history
Config.ProjectName is available, and adds `--project-name` to Go
(indirectly) and Helm (directly) to set this field on `init`.

go.sum,go.mod: bump kubebuilder dep

internal/plugins/helm: add `--project-name` flag, and use Config.ProjectName
in scaffolds
  • Loading branch information
estroz committed Jul 23, 2020
1 parent d7ca558 commit 5f7c1b6
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion internal/plugins/helm/v1/chartutil/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
58 changes: 36 additions & 22 deletions internal/plugins/helm/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package v1
import (
"fmt"
"os"
"path/filepath"
"path"
"strings"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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"}
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions internal/plugins/helm/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const pluginName = "helm" + plugins.DefaultNameQualifier
var (
supportedProjectVersions = []string{config.Version3Alpha}
pluginVersion = plugin.Version{Number: 1}
pluginKey = plugin.KeyFor(Plugin{})
)

var (
Expand Down
16 changes: 13 additions & 3 deletions internal/plugins/helm/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/plugins/helm/v1/scaffolds/templates/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions internal/util/projutil/project_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5f7c1b6

Please sign in to comment.