Skip to content

Commit

Permalink
feat: support helm dryrun=server
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Wang (holyspectral) <[email protected]>
  • Loading branch information
holyspectral committed Mar 19, 2024
1 parent 1fd7f4d commit 9fd7159
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions internal/cmd/helm-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func run(cmd *cobra.Command, f *flags.Flags) {
SuppressOverrideValues: f.SuppressOverrideValues,
MaxConcurrentReconciles: f.MaxConcurrentReconciles,
Selector: w.Selector,
DryRunOption: w.DryRunOption,
})
if err != nil {
log.Error(err, "Failed to add manager factory to controller.")
Expand Down
2 changes: 2 additions & 0 deletions internal/helm/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type WatchOptions struct {
SuppressOverrideValues bool
MaxConcurrentReconciles int
Selector metav1.LabelSelector
DryRunOption string
}

// Add creates a new helm operator controller and adds it to the manager
Expand All @@ -66,6 +67,7 @@ func Add(mgr manager.Manager, options WatchOptions) error {
ReconcilePeriod: options.ReconcilePeriod,
OverrideValues: options.OverrideValues,
SuppressOverrideValues: options.SuppressOverrideValues,
DryRunOption: options.DryRunOption,
}

c, err := controller.New(controllerName, mgr, controller.Options{
Expand Down
3 changes: 2 additions & 1 deletion internal/helm/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type HelmOperatorReconciler struct {
OverrideValues map[string]string
SuppressOverrideValues bool
releaseHook ReleaseHookFunc
DryRunOption string
}

const (
Expand Down Expand Up @@ -96,7 +97,7 @@ func (r HelmOperatorReconciler) Reconcile(ctx context.Context, request reconcile
return reconcile.Result{}, err
}

manager, err := r.ManagerFactory.NewManager(o, r.OverrideValues)
manager, err := r.ManagerFactory.NewManager(o, r.OverrideValues, r.DryRunOption)
if err != nil {
log.Error(err, "Failed to get release manager")
return reconcile.Result{}, err
Expand Down
3 changes: 3 additions & 0 deletions internal/helm/release/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type manager struct {
isUpgradeRequired bool
deployedRelease *rpb.Release
chart *cpb.Chart

dryRunOption string
}

type InstallOption func(*action.Install) error
Expand Down Expand Up @@ -159,6 +161,7 @@ func (m manager) getCandidateRelease(namespace, name string, chart *cpb.Chart,
upgrade := action.NewUpgrade(m.actionConfig)
upgrade.Namespace = namespace
upgrade.DryRun = true
upgrade.DryRunOption = m.dryRunOption
return upgrade.Run(name, chart, values)
}

Expand Down
11 changes: 6 additions & 5 deletions internal/helm/release/manager_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
// improves decoupling between reconciliation logic and the Helm backend
// components used to manage releases.
type ManagerFactory interface {
NewManager(r *unstructured.Unstructured, overrideValues map[string]string) (Manager, error)
NewManager(r *unstructured.Unstructured, overrideValues map[string]string, dryRunOption string) (Manager, error)
}

type managerFactory struct {
Expand All @@ -47,7 +47,7 @@ func NewManagerFactory(mgr crmanager.Manager, acg client.ActionConfigGetter, cha
return &managerFactory{mgr, acg, chartDir}
}

func (f managerFactory) NewManager(cr *unstructured.Unstructured, overrideValues map[string]string) (Manager, error) {
func (f managerFactory) NewManager(cr *unstructured.Unstructured, overrideValues map[string]string, dryRunOption string) (Manager, error) {
actionConfig, err := f.acg.ActionConfigFor(cr)
if err != nil {
return nil, fmt.Errorf("failed to get helm action config: %w", err)
Expand Down Expand Up @@ -86,9 +86,10 @@ func (f managerFactory) NewManager(cr *unstructured.Unstructured, overrideValues
releaseName: releaseName,
namespace: cr.GetNamespace(),

chart: crChart,
values: values,
status: types.StatusFor(cr),
chart: crChart,
values: values,
status: types.StatusFor(cr),
dryRunOption: dryRunOption,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/helm/watches/watches.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Watch struct {
OverrideValues map[string]string `json:"overrideValues,omitempty"`
Selector metav1.LabelSelector `json:"selector"`
ReconcilePeriod metav1.Duration `json:"reconcilePeriod,omitempty"`
DryRunOption string `json:"dryRunOption,omitempty"`
}

// UnmarshalYAML unmarshals an individual watch from the Helm watches.yaml file
Expand Down
25 changes: 24 additions & 1 deletion internal/helm/watches/watches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,30 @@ func TestLoadReader(t *testing.T) {
},
expectErr: false,
},

{
name: "valid with dry run option",
data: `---
- group: mygroup
version: v1alpha1
kind: MyKind
chart: ../../../internal/plugins/helm/v1/chartutil/testdata/test-chart
watchDependentResources: false
overrideValues:
key: $MY_VALUE
dryRunOption: server
`,
env: map[string]string{"MY_VALUE": "value"},
expectWatches: []Watch{
{
GroupVersionKind: schema.GroupVersionKind{Group: "mygroup", Version: "v1alpha1", Kind: "MyKind"},
ChartDir: "../../../internal/plugins/helm/v1/chartutil/testdata/test-chart",
WatchDependentResources: &falseVal,
OverrideValues: map[string]string{"key": "value"},
DryRunOption: "server",
},
},
expectErr: false,
},
{
name: "invalid with override template expansion",
data: `---
Expand Down

0 comments on commit 9fd7159

Please sign in to comment.