Skip to content

Commit

Permalink
Merge pull request #1596 from estroz/bugfix/use-repo-project-name
Browse files Browse the repository at this point in the history
pkg/plugin/v3: use repo instead of directory basename as project name
  • Loading branch information
k8s-ci-robot committed Jul 14, 2020
2 parents a9e4f9f + 6879810 commit b335ded
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
21 changes: 8 additions & 13 deletions pkg/plugin/v3/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ package v3

import (
"fmt"
"os"
"path/filepath"
"path"
"strings"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -109,17 +108,6 @@ func (p *initPlugin) Validate() error {
return err
}
}

// 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)
}
projectName := filepath.Base(dir)
if err := validation.IsDNS1123Label(strings.ToLower(projectName)); err != nil {
return fmt.Errorf("project name (%s) is invalid: %v", projectName, err)
}

// Try to guess repository if flag is not set.
if p.config.Repo == "" {
repoPath, err := util.FindCurrentRepo()
Expand All @@ -129,6 +117,13 @@ func (p *initPlugin) Validate() error {
p.config.Repo = repoPath
}

// Use base repository path as project name, since a project should be able to exist in an
// arbitrarily-named directory.
projectName := path.Base(p.config.Repo)
if err := validation.IsDNS1123Label(strings.ToLower(projectName)); err != nil {
return fmt.Errorf("project name (%s) is invalid: %v", projectName, err)
}

return nil
}

Expand Down
22 changes: 14 additions & 8 deletions pkg/plugin/v3/scaffolds/internal/templates/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package templates

import (
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -29,6 +30,7 @@ var _ file.Template = &Kustomize{}
// Kustomize scaffolds the Kustomization file for the default overlay
type Kustomize struct {
file.TemplateMixin
file.RepositoryMixin

// Prefix to use for name prefix customization
Prefix string
Expand All @@ -45,12 +47,16 @@ func (f *Kustomize) SetTemplateDefaults() error {
f.IfExistsAction = file.Error

if f.Prefix == "" {
// use directory name as prefix
dir, err := os.Getwd()
if err != nil {
return err
if f.Repo != "" {
f.Prefix = strings.ToLower(path.Base(f.Repo))
} else {
// Use directory name as prefix if no repo is present.
dir, err := os.Getwd()
if err != nil {
return err
}
f.Prefix = strings.ToLower(filepath.Base(dir))
}
f.Prefix = strings.ToLower(filepath.Base(dir))
}

return nil
Expand All @@ -74,12 +80,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 +94,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-v3-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-v3/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 b335ded

Please sign in to comment.