Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/plugin/v3: use repo instead of directory basename as project name #1596

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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