Skip to content

Commit

Permalink
Merge pull request #1611 from estroz/feature/project-name-config
Browse files Browse the repository at this point in the history
pkg/plugin/v2: use config project name if project version is v3+
  • Loading branch information
k8s-ci-robot authored Jul 24, 2020
2 parents ad31c6c + b04bdde commit 21f9343
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
18 changes: 14 additions & 4 deletions pkg/plugin/v2/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func (p *initPlugin) BindFlags(fs *pflag.FlagSet) {
fs.StringVar(&p.config.Repo, "repo", "", "name to use for go module (e.g., github.com/user/repo), "+
"defaults to the go package of the current working directory.")
fs.StringVar(&p.config.Domain, "domain", "my.domain", "domain for groups")
if p.config.IsV3() {
fs.StringVar(&p.config.ProjectName, "project-name", "", "name of this project")
}
}

func (p *initPlugin) InjectConfig(c *config.Config) {
Expand All @@ -113,13 +116,20 @@ func (p *initPlugin) Validate() error {
}
}

// Check if the project name is a valid namespace according to k8s
// Check if the project name is a valid k8s namespace (DNS 1123 label).
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error to get the current path: %v", err)
return fmt.Errorf("error getting current directory: %v", err)
}
projectName := strings.ToLower(filepath.Base(dir))
if p.config.IsV3() {
if p.config.ProjectName == "" {
p.config.ProjectName = projectName
} else {
projectName = p.config.ProjectName
}
}
projectName := filepath.Base(dir)
if err := validation.IsDNS1123Label(strings.ToLower(projectName)); err != nil {
if err := validation.IsDNS1123Label(projectName); err != nil {
return fmt.Errorf("project name (%s) is invalid: %v", projectName, err)
}

Expand Down
20 changes: 9 additions & 11 deletions pkg/plugin/v2/scaffolds/internal/templates/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ var _ file.Template = &Kustomize{}
// Kustomize scaffolds the Kustomization file for the default overlay
type Kustomize struct {
file.TemplateMixin

// Prefix to use for name prefix customization
Prefix string
file.ProjectNameMixin
}

// SetTemplateDefaults implements input.Template
Expand All @@ -44,27 +42,27 @@ func (f *Kustomize) SetTemplateDefaults() error {

f.IfExistsAction = file.Error

if f.Prefix == "" {
// use directory name as prefix
if f.ProjectName == "" {
// Use directory name as project name, which will be empty if the project version is < v3.
dir, err := os.Getwd()
if err != nil {
return err
}
f.Prefix = strings.ToLower(filepath.Base(dir))
f.ProjectName = strings.ToLower(filepath.Base(dir))
}

return nil
}

const kustomizeTemplate = `# Adds namespace to all resources.
namespace: {{ .Prefix }}-system
namespace: {{ .ProjectName }}-system
# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "alices-wordpress".
# Note that it should also match with the prefix (text before '-') of the namespace
# field above.
namePrefix: {{ .Prefix }}-
namePrefix: {{ .ProjectName }}-
# Labels to add to all resources and selectors.
#commonLabels:
Expand All @@ -74,12 +72,12 @@ bases:
- ../crd
- ../rbac
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus
patchesStrategicMerge:
Expand All @@ -88,7 +86,7 @@ patchesStrategicMerge:
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- manager_webhook_patch.yaml
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/v3/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package v3
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -117,7 +117,7 @@ func (p *initPlugin) Validate() error {
if err != nil {
return fmt.Errorf("error getting current directory: %v", err)
}
p.config.ProjectName = strings.ToLower(path.Base(dir))
p.config.ProjectName = strings.ToLower(filepath.Base(dir))
}
if err := validation.IsDNS1123Label(p.config.ProjectName); err != nil {
return fmt.Errorf("project name (%s) is invalid: %v", p.config.ProjectName, err)
Expand Down
6 changes: 3 additions & 3 deletions testdata/project-v2-addon/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ bases:
- ../crd
- ../rbac
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus

patchesStrategicMerge:
Expand All @@ -30,7 +30,7 @@ patchesStrategicMerge:
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- manager_webhook_patch.yaml

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ bases:
- ../crd
- ../rbac
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus

patchesStrategicMerge:
Expand All @@ -30,7 +30,7 @@ patchesStrategicMerge:
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- manager_webhook_patch.yaml

Expand Down
6 changes: 3 additions & 3 deletions testdata/project-v2/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ bases:
- ../crd
- ../rbac
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus

patchesStrategicMerge:
Expand All @@ -30,7 +30,7 @@ patchesStrategicMerge:
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- manager_webhook_patch.yaml

Expand Down

0 comments on commit 21f9343

Please sign in to comment.