Skip to content

Commit 182aa72

Browse files
committed
Get data from kustomize output rather than kustomization.yaml
1 parent d2a79c6 commit 182aa72

File tree

3 files changed

+23
-37
lines changed

3 files changed

+23
-37
lines changed

pkg/plugins/optional/helm/v2alpha/edit.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ const (
3737
DefaultManifestsFile = "dist/install.yaml"
3838
// DefaultOutputDir is the default output directory for Helm charts
3939
DefaultOutputDir = "dist"
40-
// DefaultKustomizeFile is the default path for kustomization.yaml
41-
DefaultKustomizeFile = "config/default/kustomization.yaml"
4240
)
4341

4442
var _ plugin.EditSubcommand = &editSubcommand{}
@@ -48,7 +46,6 @@ type editSubcommand struct {
4846
force bool
4947
manifestsFile string
5048
outputDir string
51-
kustomizePath string
5249
}
5350

5451
//nolint:lll
@@ -101,8 +98,6 @@ func (p *editSubcommand) BindFlags(fs *pflag.FlagSet) {
10198
fs.BoolVar(&p.force, "force", false, "if true, regenerates all the files")
10299
fs.StringVar(&p.manifestsFile, "manifests", DefaultManifestsFile,
103100
"path to the YAML file containing Kubernetes manifests from kustomize output")
104-
fs.StringVar(&p.kustomizePath, "kustomize-path", DefaultKustomizeFile,
105-
"path to the kustomization.yaml file used to generate the manifests file")
106101
fs.StringVar(&p.outputDir, "output-dir", DefaultOutputDir, "output directory for the generated Helm chart")
107102
}
108103

@@ -119,7 +114,7 @@ func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
119114
}
120115
}
121116

122-
scaffolder := scaffolds.NewKustomizeHelmScaffolder(p.config, p.force, p.manifestsFile, p.outputDir, p.kustomizePath)
117+
scaffolder := scaffolds.NewKustomizeHelmScaffolder(p.config, p.force, p.manifestsFile, p.outputDir)
123118
scaffolder.InjectFS(fs)
124119
err := scaffolder.Scaffold()
125120
if err != nil {

pkg/plugins/optional/helm/v2alpha/scaffolds/edit_kustomize.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,15 @@ type editKustomizeScaffolder struct {
4545
force bool
4646
manifestsFile string
4747
outputDir string
48-
kustomizeFile string
4948
}
5049

5150
// NewKustomizeHelmScaffolder returns a new Scaffolder for HelmPlugin using kustomize output
52-
func NewKustomizeHelmScaffolder(
53-
cfg config.Config, force bool, manifestsFile, outputDir, kustomizeFile string,
54-
) plugins.Scaffolder {
51+
func NewKustomizeHelmScaffolder(cfg config.Config, force bool, manifestsFile, outputDir string) plugins.Scaffolder {
5552
return &editKustomizeScaffolder{
5653
config: cfg,
5754
force: force,
5855
manifestsFile: manifestsFile,
5956
outputDir: outputDir,
60-
kustomizeFile: kustomizeFile,
6157
}
6258
}
6359

@@ -113,17 +109,7 @@ func (s *editKustomizeScaffolder) Scaffold() error {
113109
slog.Warn("failed to remove stale generic ServiceMonitor", "path", staleSM, "error", rmErr)
114110
}
115111
}
116-
117-
// Read namePrefix from kustomization.yaml
118-
// falls back to project name if not found/configured
119-
namePrefix := kustomize.ReadNamePrefix(s.kustomizeFile)
120-
if namePrefix == "" {
121-
namePrefix = s.config.GetProjectName()
122-
} else {
123-
// Remove trailing dash if present
124-
namePrefix = strings.TrimSuffix(namePrefix, "-")
125-
}
126-
112+
namePrefix := resources.EstimatePrefix(s.config.GetProjectName())
127113
chartConverter := kustomize.NewChartConverter(resources, s.config.GetProjectName(), namePrefix, s.outputDir)
128114
deploymentConfig := chartConverter.ExtractDeploymentConfig()
129115

pkg/plugins/optional/helm/v2alpha/scaffolds/internal/kustomize/parser.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io"
2222
"os"
23+
"strings"
2324

2425
"gopkg.in/yaml.v3"
2526
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -152,21 +153,25 @@ func (p *Parser) categorizeResource(obj *unstructured.Unstructured, resources *P
152153
}
153154
}
154155

155-
// ReadNamePrefix reads the namePrefix from a kustomization.yaml file
156-
// Returns empty string if the file doesn't exist or has no namePrefix
157-
func ReadNamePrefix(kustomizationPath string) string {
158-
data, err := os.ReadFile(kustomizationPath)
159-
if err != nil {
160-
return ""
161-
}
162-
163-
var kustomization struct {
164-
NamePrefix string `yaml:"namePrefix"`
156+
func (pr *ParsedResources) EstimatePrefix(projectName string) string {
157+
prefix := projectName
158+
if pr.Deployment != nil {
159+
if name := pr.Deployment.GetName(); name != "" {
160+
deploymentPrefix, found := strings.CutSuffix(projectName, "-controller-manager")
161+
if found {
162+
prefix = deploymentPrefix
163+
}
164+
}
165165
}
166-
167-
if err := yaml.Unmarshal(data, &kustomization); err != nil {
168-
return ""
166+
// Double check that the prefix is also the prefix for the service names
167+
for _, svc := range pr.Services {
168+
if name := svc.GetName(); name != "" {
169+
if !strings.HasPrefix(name, prefix) {
170+
// If not, fallback to just project name
171+
prefix = projectName
172+
break
173+
}
174+
}
169175
}
170-
171-
return kustomization.NamePrefix
176+
return prefix
172177
}

0 commit comments

Comments
 (0)