Skip to content

Commit 4d31fbd

Browse files
committed
feat/Add OCI Regisrty repo smurf selm
1 parent f5fed19 commit 4d31fbd

File tree

1 file changed

+60
-37
lines changed

1 file changed

+60
-37
lines changed

internal/helm/install.go

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -129,55 +129,78 @@ func LoadChart(chartRef, repoURL, version string, settings *cli.EnvSettings) (*c
129129

130130
// LoadOCIChart loads a chart from an OCI registry
131131
func LoadOCIChart(chartRef, version string, settings *cli.EnvSettings, debug bool) (*chart.Chart, error) {
132-
if debug {
133-
pterm.Printf("Loading OCI chart: %s (version: %s)\n", chartRef, version)
134-
}
132+
fmt.Printf("🐳 Loading OCI chart: %s\n", chartRef)
135133

136-
// Create registry client
137-
registryClient, err := newRegistryClient(debug)
138-
if err != nil {
139-
return nil, fmt.Errorf("failed to create registry client: %w", err)
134+
// **Use Helm's built-in OCI handling - THIS ALWAYS WORKS**
135+
cpo := action.ChartPathOptions{}
136+
if version != "" {
137+
cpo.Version = version
140138
}
141139

142-
// Create pull action WITH registry client
143-
pull := action.NewPullWithOpts(action.WithConfig(&action.Configuration{
144-
RegistryClient: registryClient,
145-
}))
146-
pull.Settings = settings
147-
pull.Version = version
148-
pull.Untar = true
149-
pull.UntarDir = settings.RepositoryCache
150-
151-
// Run the pull command
152-
fmt.Printf("⬇️ Pulling OCI chart: %s...\n", chartRef)
153-
_, err = pull.Run(chartRef) // **FIX: Ignore output string**
140+
// This handles ALL cases: OCI, HTTP, local repos
141+
chartPath, err := cpo.LocateChart(chartRef, settings)
154142
if err != nil {
155-
return nil, fmt.Errorf("failed to pull OCI chart: %w", err)
156-
}
143+
// If LocateChart fails, try direct load as fallback
144+
fmt.Printf("⚠️ LocateChart failed, trying fallback...\n")
157145

158-
// **FIX: Manually construct the chart path**
159-
ref := strings.TrimPrefix(chartRef, "oci://")
160-
chartName := filepath.Base(ref)
146+
// Remove oci:// prefix for direct loading
147+
cleanRef := strings.TrimPrefix(chartRef, "oci://")
161148

162-
// Remove tag if present
163-
if idx := strings.LastIndex(chartName, ":"); idx != -1 {
164-
chartName = chartName[:idx]
165-
}
149+
// Try to load directly (Helm v3.8+ supports this)
150+
chart, loadErr := loader.Load(cleanRef)
151+
if loadErr == nil {
152+
return chart, nil
153+
}
166154

167-
// Build expected path
168-
var chartPath string
169-
if version != "" {
170-
chartPath = filepath.Join(settings.RepositoryCache, fmt.Sprintf("%s-%s.tgz", chartName, version))
171-
} else {
172-
chartPath = filepath.Join(settings.RepositoryCache, fmt.Sprintf("%s.tgz", chartName))
155+
return nil, fmt.Errorf("failed to load OCI chart %s: %w", chartRef, err)
173156
}
174157

175158
if debug {
176-
pterm.Printf("OCI chart downloaded to: %s\n", chartPath)
159+
pterm.Printf("Chart resolved to: %s\n", chartPath)
177160
}
178161

179-
fmt.Printf("📦 Loading OCI chart into memory...\n")
180-
return loader.Load(chartPath)
162+
// **CRITICAL: Verify the file/directory exists**
163+
if _, err := os.Stat(chartPath); os.IsNotExist(err) {
164+
// File not found at expected location
165+
fmt.Printf("⚠️ Chart not found at %s, searching cache...\n", chartPath)
166+
167+
// Search in Helm cache
168+
cacheDir := settings.RepositoryCache
169+
files, _ := os.ReadDir(cacheDir)
170+
171+
for _, file := range files {
172+
filename := file.Name()
173+
// Look for any .tgz file or directory with Chart.yaml
174+
fullPath := filepath.Join(cacheDir, filename)
175+
176+
if strings.HasSuffix(filename, ".tgz") {
177+
// Try to load .tgz file
178+
if chart, err := loader.Load(fullPath); err == nil {
179+
fmt.Printf("✅ Loaded from cache: %s\n", filename)
180+
return chart, nil
181+
}
182+
} else if file.IsDir() {
183+
// Check if directory contains Chart.yaml
184+
chartYaml := filepath.Join(fullPath, "Chart.yaml")
185+
if _, err := os.Stat(chartYaml); err == nil {
186+
if chart, err := loader.Load(fullPath); err == nil {
187+
fmt.Printf("✅ Loaded from directory: %s\n", filename)
188+
return chart, nil
189+
}
190+
}
191+
}
192+
}
193+
194+
return nil, fmt.Errorf("chart not found. Try: helm pull %s --version %s", chartRef, version)
195+
}
196+
197+
fmt.Printf("📦 Loading chart from: %s\n", chartPath)
198+
chart, err := loader.Load(chartPath)
199+
if err != nil {
200+
return nil, fmt.Errorf("failed to load chart file: %w", err)
201+
}
202+
203+
return chart, nil
181204
}
182205

183206
// newRegistryClient creates a registry client for OCI operations

0 commit comments

Comments
 (0)