Skip to content

Commit 6b90fda

Browse files
chore(helm/v2-alpha): add --input-dir flag for flexible project path resolution
Add --input-dir flag to the helm v2-alpha plugin to align with the alpha generate command pattern. This provides a consistent user experience across commands and makes the plugin more flexible. Changes: - Add --input-dir flag to specify the directory containing PROJECT file and manifests - Resolve manifests path relative to input-dir when not absolute - Add documentation and examples for the new flag Co-Authored-By: porridge@redhat.com Generated-by: Claude
1 parent 1ad3260 commit 6b90fda

3 files changed

Lines changed: 75 additions & 16 deletions

File tree

docs/book/src/plugins/available/helm-v2-alpha.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ kubebuilder edit --plugins=helm/v2-alpha --output-dir=charts
7272
kubebuilder edit --plugins=helm/v2-alpha \
7373
--manifests=manifests/install.yaml \
7474
--output-dir=helm-charts
75+
76+
# Generate chart from a different project directory
77+
kubebuilder edit --plugins=helm/v2-alpha --input-dir=./path/to/project
78+
79+
# Use custom manifests file (resolved relative to input-dir)
80+
kubebuilder edit --plugins=helm/v2-alpha \
81+
--input-dir=./path/to/project \
82+
--manifests=custom/install.yaml
7583
```
7684

7785
## Chart Structure
@@ -353,9 +361,10 @@ The Makefile targets use sensible defaults extracted from your project configura
353361

354362
| Flag | Description |
355363
|---------------------|-----------------------------------------------------------------------------|
356-
| **--manifests** | Path to YAML file containing Kubernetes manifests (default: `dist/install.yaml`) |
357-
| **--output-dir** string | Output directory for chart (default: `dist`) |
358-
| **--force** | Regenerates preserved files except `Chart.yaml` (`values.yaml`, `NOTES.txt`, `_helpers.tpl`, `.helmignore`, `test-chart.yml`) |
364+
| **--input-dir** | Path to the directory containing the PROJECT file and manifests. Defaults to current directory. |
365+
| **--manifests** | Path to YAML file containing Kubernetes manifests from kustomize output. Default: `dist/install.yaml`. Resolved relative to input-dir if not absolute. |
366+
| **--output-dir** | Output directory for the generated Helm chart. Default: `dist`. |
367+
| **--force** | If true, regenerates all preserved files except `Chart.yaml` (includes `values.yaml`, `NOTES.txt`, `_helpers.tpl`, `.helmignore`, `test-chart.yml`). |
359368

360369
<aside class="note" role="note">
361370
<p class="note-title"> Examples </p>

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

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var _ plugin.EditSubcommand = &editSubcommand{}
5050
type editSubcommand struct {
5151
config config.Config
5252
force bool
53+
inputDir string
5354
manifestsFile string
5455
outputDir string
5556
}
@@ -76,6 +77,12 @@ distribution of your project. When enabled, adds Helm helpers targets to Makefil
7677
# Generate from custom manifests to custom output directory
7778
%[1]s edit --plugins=%[2]s --manifests=manifests/install.yaml --output-dir=helm-charts
7879
80+
# Generate chart from a different project directory
81+
%[1]s edit --plugins=%[2]s --input-dir=./path/to/project
82+
83+
# Use custom manifests file (resolved relative to input-dir)
84+
%[1]s edit --plugins=%[2]s --input-dir=./path/to/project --manifests=custom/install.yaml
85+
7986
# Typical workflow:
8087
make build-installer # Generate dist/install.yaml with latest changes
8188
%[1]s edit --plugins=%[2]s # Generate/update Helm chart in dist/chart/
@@ -103,8 +110,11 @@ The generated chart structure mirrors your config/ directory:
103110

104111
func (p *editSubcommand) BindFlags(fs *pflag.FlagSet) {
105112
fs.BoolVar(&p.force, "force", false, "if true, regenerates all the files")
113+
fs.StringVar(&p.inputDir, "input-dir", "",
114+
"path to the directory containing the PROJECT file and manifests (defaults to current directory)")
106115
fs.StringVar(&p.manifestsFile, "manifests", DefaultManifestsFile,
107-
"path to the YAML file containing Kubernetes manifests from kustomize output")
116+
"path to the YAML file containing Kubernetes manifests from kustomize output "+
117+
"(resolved relative to input-dir if not absolute)")
108118
fs.StringVar(&p.outputDir, "output-dir", DefaultOutputDir, "output directory for the generated Helm chart")
109119
}
110120

@@ -114,14 +124,38 @@ func (p *editSubcommand) InjectConfig(c config.Config) error {
114124
}
115125

116126
func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
127+
// Resolve input directory (defaults to current working directory)
128+
if p.inputDir == "" {
129+
cwd, err := os.Getwd()
130+
if err != nil {
131+
return fmt.Errorf("failed to get current working directory: %w", err)
132+
}
133+
p.inputDir = cwd
134+
}
135+
136+
// Convert input directory to absolute path if needed
137+
if !filepath.IsAbs(p.inputDir) {
138+
absInputDir, err := filepath.Abs(p.inputDir)
139+
if err != nil {
140+
return fmt.Errorf("failed to resolve input directory %q: %w", p.inputDir, err)
141+
}
142+
p.inputDir = absInputDir
143+
}
144+
145+
// Resolve manifests path relative to input directory if not absolute
146+
manifestsPath := p.manifestsFile
147+
if !filepath.IsAbs(manifestsPath) {
148+
manifestsPath = filepath.Join(p.inputDir, manifestsPath)
149+
}
150+
117151
// If using default manifests file, ensure it exists by running make build-installer
118152
if p.manifestsFile == DefaultManifestsFile {
119-
if err := p.ensureManifestsExist(); err != nil {
120-
slog.Warn("Failed to generate default manifests file", "error", err, "file", p.manifestsFile)
153+
if err := p.ensureManifestsExist(manifestsPath); err != nil {
154+
slog.Warn("Failed to generate default manifests file", "error", err, "file", manifestsPath)
121155
}
122156
}
123157

124-
scaffolder := scaffolds.NewKustomizeHelmScaffolder(p.config, p.force, p.manifestsFile, p.outputDir)
158+
scaffolder := scaffolds.NewKustomizeHelmScaffolder(p.config, p.force, manifestsPath, p.outputDir)
125159
scaffolder.InjectFS(fs)
126160
err := scaffolder.Scaffold()
127161
if err != nil {
@@ -175,7 +209,7 @@ func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
175209
if isFirstRun {
176210
slog.Info("adding Helm deployment targets to Makefile...")
177211
// Extract namespace from manifests for accurate Makefile generation
178-
namespace := p.extractNamespaceFromManifests()
212+
namespace := p.extractNamespaceFromManifests(manifestsPath)
179213
if err := p.addHelmMakefileTargets(namespace); err != nil {
180214
slog.Warn("failed to add Helm targets to Makefile", "error", err)
181215
}
@@ -185,8 +219,20 @@ func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
185219
}
186220

187221
// ensureManifestsExist runs make build-installer to generate the default manifests file
188-
func (p *editSubcommand) ensureManifestsExist() error {
189-
slog.Info("Generating default manifests file", "file", p.manifestsFile)
222+
func (p *editSubcommand) ensureManifestsExist(manifestsPath string) error {
223+
slog.Info("Generating default manifests file", "file", manifestsPath)
224+
225+
// Change to input directory to run make commands
226+
originalDir, err := os.Getwd()
227+
if err != nil {
228+
return fmt.Errorf("failed to get current directory: %w", err)
229+
}
230+
if err := os.Chdir(p.inputDir); err != nil {
231+
return fmt.Errorf("failed to change to input directory %q: %w", p.inputDir, err)
232+
}
233+
defer func() {
234+
_ = os.Chdir(originalDir)
235+
}()
190236

191237
// Run the required make targets to generate the manifests file
192238
targets := []string{"manifests", "generate", "build-installer"}
@@ -197,11 +243,11 @@ func (p *editSubcommand) ensureManifestsExist() error {
197243
}
198244

199245
// Verify the file was created
200-
if _, err := os.Stat(p.manifestsFile); err != nil {
201-
return fmt.Errorf("manifests file %s was not created: %w", p.manifestsFile, err)
246+
if _, err := os.Stat(manifestsPath); err != nil {
247+
return fmt.Errorf("manifests file %s was not created: %w", manifestsPath, err)
202248
}
203249

204-
slog.Info("Successfully generated manifests file", "file", p.manifestsFile)
250+
slog.Info("Successfully generated manifests file", "file", manifestsPath)
205251
return nil
206252
}
207253

@@ -265,17 +311,17 @@ func (p *editSubcommand) addHelmMakefileTargets(namespace string) error {
265311

266312
// extractNamespaceFromManifests parses the manifests file to extract the manager namespace.
267313
// Returns projectName-system if manifests don't exist or namespace not found.
268-
func (p *editSubcommand) extractNamespaceFromManifests() string {
314+
func (p *editSubcommand) extractNamespaceFromManifests(manifestsPath string) string {
269315
// Default to project-name-system pattern
270316
defaultNamespace := p.config.GetProjectName() + "-system"
271317

272318
// If manifests file doesn't exist, use default
273-
if _, err := os.Stat(p.manifestsFile); os.IsNotExist(err) {
319+
if _, err := os.Stat(manifestsPath); os.IsNotExist(err) {
274320
return defaultNamespace
275321
}
276322

277323
// Parse the manifests to get the namespace
278-
file, err := os.Open(p.manifestsFile)
324+
file, err := os.Open(manifestsPath)
279325
if err != nil {
280326
return defaultNamespace
281327
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ version: "3"
8484
editCmd.BindFlags(flagSet)
8585

8686
// Check that flags were added
87+
inputDirFlag := flagSet.Lookup("input-dir")
88+
Expect(inputDirFlag).NotTo(BeNil())
89+
Expect(inputDirFlag.DefValue).To(Equal(""))
90+
8791
manifestsFlag := flagSet.Lookup("manifests")
8892
Expect(manifestsFlag).NotTo(BeNil())
8993
Expect(manifestsFlag.DefValue).To(Equal(DefaultManifestsFile))

0 commit comments

Comments
 (0)