Skip to content

Commit e940c97

Browse files
authored
Merge branch 'main' into fix_nightly_version_step
2 parents af04d67 + 2b516d7 commit e940c97

File tree

14 files changed

+359
-55
lines changed

14 files changed

+359
-55
lines changed

site/src/content/docs/commands/zarf_tools_registry_prune.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ zarf tools registry prune [flags]
1717
### Options
1818

1919
```
20-
-c, --confirm Confirm the image prune action to prevent accidental deletions
21-
-h, --help help for prune
20+
-c, --confirm Confirm the image prune action to prevent accidental deletions
21+
-h, --help help for prune
22+
--insecure Allow image references to be fetched without TLS
2223
```
2324

2425
### Options inherited from parent commands
2526

2627
```
2728
--allow-nondistributable-artifacts Allow pushing non-distributable (foreign) layers
2829
--features stringToString [ALPHA] Provide a comma-separated list of feature names to bools to enable or disable. Ex. --features "foo=true,bar=false,baz=true" (default [])
29-
--insecure Allow image references to be fetched without TLS
3030
--insecure-skip-tls-verify Skip checking server's certificate for validity. This flag should only be used if you have a specific reason and accept the reduced security posture.
3131
--plain-http Force the connections over HTTP instead of HTTPS. This flag should only be used if you have a specific reason and accept the reduced security posture.
3232
--platform string Specifies the platform in the form os/arch[/variant][:osversion] (e.g. linux/amd64). (default "all")

src/cmd/crane.go

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ func newRegistryCommand() *cobra.Command {
8282

8383
cmd.AddCommand(newRegistryPruneCommand())
8484
cmd.AddCommand(newRegistryLoginCommand())
85-
cmd.AddCommand(newRegistryCopyCommand())
86-
cmd.AddCommand(newRegistryCatalogCommand())
85+
cmd.AddCommand(newRegistryCopyCommand(&craneOptions))
86+
cmd.AddCommand(newRegistryCatalogCommand(&craneOptions))
8787

8888
// TODO(soltysh): consider splitting craneOptions to be per command
8989
cmd.AddCommand(zarfCraneInternalWrapper(craneCmd.NewCmdList, &craneOptions, lang.CmdToolsRegistryListExample, 0))
@@ -115,25 +115,35 @@ func newRegistryLoginCommand() *cobra.Command {
115115
return cmd
116116
}
117117

118-
func newRegistryCopyCommand() *cobra.Command {
119-
// No package information is available so do not pass in a list of architectures
120-
craneOptions := []crane.Option{}
121-
cmd := craneCmd.NewCmdCopy(&craneOptions)
118+
func newRegistryCopyCommand(craneOpts *[]crane.Option) *cobra.Command {
119+
cmd := craneCmd.NewCmdCopy(craneOpts)
120+
// Store crane's original PreRunE if it exists
121+
originalPreRunE := cmd.PreRunE
122+
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
123+
// No package information is available so do not pass in a list of architectures
124+
*craneOpts = append(*craneOpts, crane.WithPlatform(nil))
125+
// return the original pre run
126+
if originalPreRunE != nil {
127+
return originalPreRunE(cmd, args)
128+
}
129+
return nil
130+
}
131+
122132
return cmd
123133
}
124134

125135
type registryCatalogOptions struct {
126-
craneOptions []crane.Option
136+
craneOptions *[]crane.Option
127137
originalRunFn func(cmd *cobra.Command, args []string) error
128138
}
129139

130-
func newRegistryCatalogCommand() *cobra.Command {
140+
func newRegistryCatalogCommand(craneOpts *[]crane.Option) *cobra.Command {
131141
o := registryCatalogOptions{
132142
// No package information is available so do not pass in a list of architectures
133-
craneOptions: []crane.Option{},
143+
craneOptions: craneOpts,
134144
}
135145

136-
cmd := craneCmd.NewCmdCatalog(&o.craneOptions)
146+
cmd := craneCmd.NewCmdCatalog(o.craneOptions)
137147
cmd.Example = lang.CmdToolsRegistryCatalogExample
138148
cmd.Args = nil
139149

@@ -169,7 +179,7 @@ func (o *registryCatalogOptions) run(cmd *cobra.Command, args []string) error {
169179

170180
// Add the correct authentication to the crane command options
171181
authOption := images.WithPullAuth(zarfState.RegistryInfo)
172-
o.craneOptions = append(o.craneOptions, authOption)
182+
*o.craneOptions = append(*o.craneOptions, authOption)
173183

174184
if tunnel != nil {
175185
defer tunnel.Close()
@@ -179,7 +189,9 @@ func (o *registryCatalogOptions) run(cmd *cobra.Command, args []string) error {
179189
return o.originalRunFn(cmd, []string{registryEndpoint})
180190
}
181191

182-
type registryPruneOptions struct{}
192+
type registryPruneOptions struct {
193+
insecure bool
194+
}
183195

184196
func newRegistryPruneCommand() *cobra.Command {
185197
o := registryPruneOptions{}
@@ -193,6 +205,7 @@ func newRegistryPruneCommand() *cobra.Command {
193205

194206
// Always require confirm flag (no viper)
195207
cmd.Flags().BoolVarP(&config.CommonOptions.Confirm, "confirm", "c", false, lang.CmdToolsRegistryPruneFlagConfirm)
208+
cmd.PersistentFlags().BoolVar(&o.insecure, "insecure", false, lang.CmdToolsRegistryFlagInsecure)
196209

197210
return cmd
198211
}
@@ -203,6 +216,10 @@ func (o *registryPruneOptions) run(cmd *cobra.Command, _ []string) error {
203216
if err != nil {
204217
return err
205218
}
219+
options := []crane.Option{}
220+
if o.insecure {
221+
options = append(options, crane.Insecure)
222+
}
206223

207224
ctx := cmd.Context()
208225
l := logger.From(ctx)
@@ -226,15 +243,17 @@ func (o *registryPruneOptions) run(cmd *cobra.Command, _ []string) error {
226243
if tunnel != nil {
227244
l.Info("opening a tunnel to the Zarf registry", "local-endpoint", registryEndpoint, "cluster-address", zarfState.RegistryInfo.Address)
228245
defer tunnel.Close()
229-
return tunnel.Wrap(func() error { return doPruneImagesForPackages(ctx, zarfState, zarfPackages, registryEndpoint) })
246+
return tunnel.Wrap(func() error {
247+
return doPruneImagesForPackages(ctx, options, zarfState, zarfPackages, registryEndpoint)
248+
})
230249
}
231250

232-
return doPruneImagesForPackages(ctx, zarfState, zarfPackages, registryEndpoint)
251+
return doPruneImagesForPackages(ctx, options, zarfState, zarfPackages, registryEndpoint)
233252
}
234253

235-
func doPruneImagesForPackages(ctx context.Context, s *state.State, zarfPackages []state.DeployedPackage, registryEndpoint string) error {
254+
func doPruneImagesForPackages(ctx context.Context, options []crane.Option, s *state.State, zarfPackages []state.DeployedPackage, registryEndpoint string) error {
236255
l := logger.From(ctx)
237-
authOption := images.WithPushAuth(s.RegistryInfo)
256+
options = append(options, images.WithPushAuth(s.RegistryInfo))
238257

239258
l.Info("finding images to prune")
240259

@@ -255,7 +274,7 @@ func doPruneImagesForPackages(ctx context.Context, s *state.State, zarfPackages
255274
return err
256275
}
257276

258-
digest, err := crane.Digest(transformedImageNoCheck, authOption)
277+
digest, err := crane.Digest(transformedImageNoCheck, options...)
259278
if err != nil {
260279
return err
261280
}
@@ -266,20 +285,20 @@ func doPruneImagesForPackages(ctx context.Context, s *state.State, zarfPackages
266285
}
267286

268287
// Find which images and tags are in the registry currently
269-
imageCatalog, err := crane.Catalog(registryEndpoint, authOption)
288+
imageCatalog, err := crane.Catalog(registryEndpoint, options...)
270289
if err != nil {
271290
return err
272291
}
273292
referenceToDigest := map[string]string{}
274293
for _, image := range imageCatalog {
275294
imageRef := fmt.Sprintf("%s/%s", registryEndpoint, image)
276-
tags, err := crane.ListTags(imageRef, authOption)
295+
tags, err := crane.ListTags(imageRef, options...)
277296
if err != nil {
278297
return err
279298
}
280299
for _, tag := range tags {
281300
taggedImageRef := fmt.Sprintf("%s:%s", imageRef, tag)
282-
digest, err := crane.Digest(taggedImageRef, authOption)
301+
digest, err := crane.Digest(taggedImageRef, options...)
283302
if err != nil {
284303
return err
285304
}
@@ -324,7 +343,7 @@ func doPruneImagesForPackages(ctx context.Context, s *state.State, zarfPackages
324343

325344
// Delete the digest references that are to be pruned
326345
for digestRef := range imageDigestsToPrune {
327-
err = crane.Delete(digestRef, authOption)
346+
err = crane.Delete(digestRef, options...)
328347
if err != nil {
329348
return err
330349
}

src/internal/packager/helm/chart.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ func InstallOrUpgradeChart(ctx context.Context, zarfChart v1alpha1.ZarfChart, ch
7777
// Setup K8s connection.
7878
actionConfig, err := createActionConfig(ctx, zarfChart.Namespace)
7979
if err != nil {
80-
return nil, "", fmt.Errorf("unable to initialize the K8s client: %w", err)
80+
return nil, zarfChart.ReleaseName, fmt.Errorf("unable to initialize the K8s client: %w", err)
8181
}
8282

8383
postRender, err := newRenderer(ctx, zarfChart, opts.AdoptExistingResources, opts.Cluster, opts.AirgapMode, opts.State, actionConfig, opts.VariableConfig)
8484
if err != nil {
85-
return nil, "", fmt.Errorf("unable to create helm renderer: %w", err)
85+
return nil, zarfChart.ReleaseName, fmt.Errorf("unable to create helm renderer: %w", err)
8686
}
8787

8888
histClient := action.NewHistory(actionConfig)
@@ -126,7 +126,7 @@ func InstallOrUpgradeChart(ctx context.Context, zarfChart v1alpha1.ZarfChart, ch
126126

127127
releases, err := histClient.Run(zarfChart.ReleaseName)
128128
if err != nil {
129-
return nil, "", errors.Join(err, installErr)
129+
return nil, zarfChart.ReleaseName, errors.Join(err, installErr)
130130
}
131131
previouslyDeployedVersion := 0
132132

@@ -139,21 +139,21 @@ func InstallOrUpgradeChart(ctx context.Context, zarfChart v1alpha1.ZarfChart, ch
139139

140140
// No prior releases means this was an initial install.
141141
if previouslyDeployedVersion == 0 {
142-
return nil, "", installErr
142+
return nil, zarfChart.ReleaseName, installErr
143143
}
144144

145145
// Attempt to rollback on a failed upgrade.
146146
l.Info("performing Helm rollback", "chart", zarfChart.Name)
147147
err = rollbackChart(zarfChart.ReleaseName, previouslyDeployedVersion, actionConfig, opts.Timeout)
148148
if err != nil {
149-
return nil, "", fmt.Errorf("%w: unable to rollback: %w", installErr, err)
149+
return nil, zarfChart.ReleaseName, fmt.Errorf("%w: unable to rollback: %w", installErr, err)
150150
}
151-
return nil, "", installErr
151+
return nil, zarfChart.ReleaseName, installErr
152152
}
153153

154154
resourceList, err := actionConfig.KubeClient.Build(bytes.NewBufferString(release.Manifest), true)
155155
if err != nil {
156-
return nil, "", fmt.Errorf("unable to build the resource list: %w", err)
156+
return nil, zarfChart.ReleaseName, fmt.Errorf("unable to build the resource list: %w", err)
157157
}
158158

159159
runtimeObjs := []runtime.Object{}
@@ -164,7 +164,7 @@ func InstallOrUpgradeChart(ctx context.Context, zarfChart v1alpha1.ZarfChart, ch
164164
// Ensure we don't go past the timeout by using a context initialized with the helm timeout
165165
l.Info("running health checks", "chart", zarfChart.Name)
166166
if err := healthchecks.WaitForReadyRuntime(helmCtx, opts.Cluster.Watcher, runtimeObjs); err != nil {
167-
return nil, "", err
167+
return nil, zarfChart.ReleaseName, err
168168
}
169169
}
170170
l.Debug("done processing Helm chart", "name", zarfChart.Name, "duration", time.Since(start))

src/pkg/packager/deploy.go

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,30 @@ func (d *deployer) deployComponents(ctx context.Context, pkgLayout *layout.Packa
221221
}
222222

223223
if deployErr != nil {
224-
onFailure()
225-
deployedComponents[idx].Status = state.ComponentStatusFailed
226-
if d.isConnectedToCluster() {
227-
if _, err := d.c.RecordPackageDeployment(ctx, pkgLayout.Pkg, deployedComponents, packageGeneration, state.WithPackageNamespaceOverride(opts.NamespaceOverride)); err != nil {
228-
l.Debug("unable to record package deployment", "component", component.Name, "error", err.Error())
224+
cleanup := func(ctx context.Context) {
225+
onFailure()
226+
l.Debug("component deployment failed", "component", component.Name, "error", deployErr.Error())
227+
deployedComponents[idx].Status = state.ComponentStatusFailed
228+
deployedComponents[idx].InstalledCharts = state.MergeInstalledChartsForComponent(deployedComponents[idx].InstalledCharts, charts, true)
229+
if d.isConnectedToCluster() {
230+
if _, err := d.c.RecordPackageDeployment(ctx, pkgLayout.Pkg, deployedComponents, packageGeneration, state.WithPackageNamespaceOverride(opts.NamespaceOverride)); err != nil {
231+
l.Debug("unable to record package deployment", "component", component.Name, "error", err.Error())
232+
}
229233
}
230234
}
231-
return nil, fmt.Errorf("unable to deploy component %q: %w", component.Name, deployErr)
235+
select {
236+
case <-ctx.Done():
237+
// Use background context here in order to ensure the cleanup logic can run when the context is cancelled
238+
cleanup(context.Background())
239+
return nil, fmt.Errorf("context cancelled while deploying component %q: %w", component.Name, deployErr)
240+
default:
241+
cleanup(ctx)
242+
return nil, fmt.Errorf("unable to deploy component %q: %w", component.Name, deployErr)
243+
}
232244
}
233245

234246
// Update the package secret to indicate that we successfully deployed this component
235-
deployedComponents[idx].InstalledCharts = charts
247+
deployedComponents[idx].InstalledCharts = state.MergeInstalledChartsForComponent(deployedComponents[idx].InstalledCharts, charts, false)
236248
deployedComponents[idx].Status = state.ComponentStatusSucceeded
237249
if d.isConnectedToCluster() {
238250
if _, err := d.c.RecordPackageDeployment(ctx, pkgLayout.Pkg, deployedComponents, packageGeneration, state.WithPackageNamespaceOverride(opts.NamespaceOverride)); err != nil {
@@ -421,35 +433,35 @@ func (d *deployer) deployComponent(ctx context.Context, pkgLayout *layout.Packag
421433
charts := []state.InstalledChart{}
422434
if hasCharts {
423435
helmCharts, err := d.installCharts(ctx, pkgLayout, component, opts)
436+
charts = append(charts, helmCharts...)
424437
if err != nil {
425-
return nil, err
438+
return charts, err
426439
}
427-
charts = append(charts, helmCharts...)
428440
}
429441

430442
if hasManifests {
431443
chartsFromManifests, err := d.installManifests(ctx, pkgLayout, component, opts)
444+
charts = append(charts, chartsFromManifests...)
432445
if err != nil {
433-
return nil, err
446+
return charts, err
434447
}
435-
charts = append(charts, chartsFromManifests...)
436448
}
437449

438450
if err := actions.Run(ctx, cwd, onDeploy.Defaults, onDeploy.After, d.vc); err != nil {
439-
return nil, fmt.Errorf("unable to run component after action: %w", err)
451+
return charts, fmt.Errorf("unable to run component after action: %w", err)
440452
}
441453

442454
if len(component.HealthChecks) > 0 {
443455
healthCheckContext, cancel := context.WithTimeout(ctx, opts.Timeout)
444456
defer cancel()
445457
l.Info("running health checks")
446458
if err := healthchecks.Run(healthCheckContext, d.c.Watcher, component.HealthChecks); err != nil {
447-
return nil, fmt.Errorf("health checks failed: %w", err)
459+
return charts, fmt.Errorf("health checks failed: %w", err)
448460
}
449461
}
450462

451463
if err := g.Wait(); err != nil {
452-
return nil, err
464+
return charts, err
453465
}
454466
l.Debug("done deploying component", "name", component.Name, "duration", time.Since(start))
455467
return charts, nil
@@ -485,15 +497,15 @@ func (d *deployer) installCharts(ctx context.Context, pkgLayout *layout.PackageL
485497
for idx := range chart.ValuesFiles {
486498
valueFilePath := helm.StandardValuesName(valuesDir, chart, idx)
487499
if err := d.vc.ReplaceTextTemplate(valueFilePath); err != nil {
488-
return nil, err
500+
return installedCharts, err
489501
}
490502
}
491503

492504
// Create a Helm values overrides map from set Zarf `variables` and DeployOpts library inputs
493505
// Values overrides are to be applied in order of Helm Chart Defaults -> Zarf `valuesFiles` -> Zarf `variables` -> DeployOpts overrides
494506
valuesOverrides, err := generateValuesOverrides(chart, component.Name, d.vc, opts.ValuesOverridesMap)
495507
if err != nil {
496-
return nil, err
508+
return installedCharts, err
497509
}
498510

499511
helmOpts := helm.InstallUpgradeOptions{
@@ -507,14 +519,15 @@ func (d *deployer) installCharts(ctx context.Context, pkgLayout *layout.PackageL
507519
}
508520
helmChart, values, err := helm.LoadChartData(chart, chartDir, valuesDir, valuesOverrides)
509521
if err != nil {
510-
return nil, fmt.Errorf("failed to load chart data: %w", err)
522+
return installedCharts, fmt.Errorf("failed to load chart data: %w", err)
511523
}
512524

513525
connectStrings, installedChartName, err := helm.InstallOrUpgradeChart(ctx, chart, helmChart, values, helmOpts)
514526
if err != nil {
515-
return nil, err
527+
installedCharts = append(installedCharts, state.InstalledChart{Namespace: chart.Namespace, ChartName: installedChartName, ConnectStrings: connectStrings, Status: state.ChartStatusFailed})
528+
return installedCharts, err
516529
}
517-
installedCharts = append(installedCharts, state.InstalledChart{Namespace: chart.Namespace, ChartName: installedChartName, ConnectStrings: connectStrings})
530+
installedCharts = append(installedCharts, state.InstalledChart{Namespace: chart.Namespace, ChartName: installedChartName, ConnectStrings: connectStrings, Status: state.ChartStatusSucceeded})
518531
}
519532

520533
return installedCharts, nil
@@ -540,7 +553,7 @@ func (d *deployer) installManifests(ctx context.Context, pkgLayout *layout.Packa
540553
// The path is likely invalid because of how we compose OCI components, add an index suffix to the filename
541554
manifest.Files[idx] = fmt.Sprintf("%s-%d.yaml", manifest.Name, idx)
542555
if helpers.InvalidPath(filepath.Join(manifestDir, manifest.Files[idx])) {
543-
return nil, fmt.Errorf("unable to find manifest file %s", manifest.Files[idx])
556+
return installedCharts, fmt.Errorf("unable to find manifest file %s", manifest.Files[idx])
544557
}
545558
}
546559
}
@@ -558,7 +571,7 @@ func (d *deployer) installManifests(ctx context.Context, pkgLayout *layout.Packa
558571
// Create a helmChart and helm cfg from a given Zarf Manifest.
559572
chart, helmChart, err := helm.ChartFromZarfManifest(manifest, manifestDir, pkgLayout.Pkg.Metadata.Name, component.Name)
560573
if err != nil {
561-
return nil, err
574+
return installedCharts, err
562575
}
563576
helmOpts := helm.InstallUpgradeOptions{
564577
AdoptExistingResources: opts.AdoptExistingResources,
@@ -573,9 +586,10 @@ func (d *deployer) installManifests(ctx context.Context, pkgLayout *layout.Packa
573586
// Install the chart.
574587
connectStrings, installedChartName, err := helm.InstallOrUpgradeChart(ctx, chart, helmChart, nil, helmOpts)
575588
if err != nil {
576-
return nil, err
589+
installedCharts = append(installedCharts, state.InstalledChart{Namespace: manifest.Namespace, ChartName: installedChartName, ConnectStrings: connectStrings, Status: state.ChartStatusFailed})
590+
return installedCharts, err
577591
}
578-
installedCharts = append(installedCharts, state.InstalledChart{Namespace: manifest.Namespace, ChartName: installedChartName, ConnectStrings: connectStrings})
592+
installedCharts = append(installedCharts, state.InstalledChart{Namespace: manifest.Namespace, ChartName: installedChartName, ConnectStrings: connectStrings, Status: state.ChartStatusSucceeded})
579593
}
580594

581595
return installedCharts, nil

src/pkg/packager/load/import.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ func overrideResources(comp v1alpha1.ZarfComponent, override v1alpha1.ZarfCompon
381381
if overrideChart.ReleaseName != "" {
382382
comp.Charts[idx].ReleaseName = overrideChart.ReleaseName
383383
}
384+
if overrideChart.Version != "" {
385+
comp.Charts[idx].Version = overrideChart.Version
386+
}
387+
if overrideChart.URL != "" {
388+
comp.Charts[idx].URL = overrideChart.URL
389+
}
384390
comp.Charts[idx].ValuesFiles = append(comp.Charts[idx].ValuesFiles, overrideChart.ValuesFiles...)
385391
comp.Charts[idx].Variables = append(comp.Charts[idx].Variables, overrideChart.Variables...)
386392
existing = true

0 commit comments

Comments
 (0)