Skip to content

Commit 4e1ecc7

Browse files
committed
Add preprocess function for apply/preview/destroy commands
1 parent 048e886 commit 4e1ecc7

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

cmd/apply/cmdapply.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"sigs.k8s.io/cli-utils/cmd/printers"
1919
"sigs.k8s.io/cli-utils/pkg/apply"
2020
"sigs.k8s.io/cli-utils/pkg/common"
21+
"sigs.k8s.io/cli-utils/pkg/inventory"
2122
"sigs.k8s.io/cli-utils/pkg/manifestreader"
2223
"sigs.k8s.io/cli-utils/pkg/provider"
2324
)
@@ -70,11 +71,12 @@ func ApplyCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cob
7071
}
7172

7273
type ApplyRunner struct {
73-
Command *cobra.Command
74-
ioStreams genericclioptions.IOStreams
75-
Applier *apply.Applier
76-
provider provider.Provider
77-
loader manifestreader.ManifestLoader
74+
Command *cobra.Command
75+
PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error)
76+
ioStreams genericclioptions.IOStreams
77+
Applier *apply.Applier
78+
provider provider.Provider
79+
loader manifestreader.ManifestLoader
7880

7981
serverSideOptions common.ServerSideOptions
8082
output string
@@ -125,6 +127,13 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
125127
return err
126128
}
127129

130+
if r.PreProcess != nil {
131+
inventoryPolicy, err = r.PreProcess(inv, common.DryRunNone)
132+
if err != nil {
133+
return err
134+
}
135+
}
136+
128137
// Run the applier. It will return a channel where we can receive updates
129138
// to keep track of progress and any issues.
130139
if err := r.Applier.Initialize(); err != nil {

cmd/destroy/cmddestroy.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"sigs.k8s.io/cli-utils/cmd/flagutils"
1515
"sigs.k8s.io/cli-utils/cmd/printers"
1616
"sigs.k8s.io/cli-utils/pkg/apply"
17+
"sigs.k8s.io/cli-utils/pkg/common"
18+
"sigs.k8s.io/cli-utils/pkg/inventory"
1719
"sigs.k8s.io/cli-utils/pkg/manifestreader"
1820
"sigs.k8s.io/cli-utils/pkg/provider"
1921
)
@@ -52,11 +54,12 @@ func DestroyCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *c
5254

5355
// DestroyRunner encapsulates data necessary to run the destroy command.
5456
type DestroyRunner struct {
55-
Command *cobra.Command
56-
ioStreams genericclioptions.IOStreams
57-
Destroyer *apply.Destroyer
58-
provider provider.Provider
59-
loader manifestreader.ManifestLoader
57+
Command *cobra.Command
58+
PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error)
59+
ioStreams genericclioptions.IOStreams
60+
Destroyer *apply.Destroyer
61+
provider provider.Provider
62+
loader manifestreader.ManifestLoader
6063

6164
output string
6265
inventoryPolicy string
@@ -82,6 +85,13 @@ func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
8285
return err
8386
}
8487

88+
if r.PreProcess != nil {
89+
inventoryPolicy, err = r.PreProcess(inv, r.Destroyer.DryRunStrategy)
90+
if err != nil {
91+
return err
92+
}
93+
}
94+
8595
// Run the destroyer. It will return a channel where we can receive updates
8696
// to keep track of progress and any issues.
8797
err = r.Destroyer.Initialize()

cmd/preview/cmdpreview.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212
"k8s.io/cli-runtime/pkg/genericclioptions"
1313
cmdutil "k8s.io/kubectl/pkg/cmd/util"
1414
"k8s.io/kubectl/pkg/util/i18n"
15+
1516
"sigs.k8s.io/cli-utils/cmd/flagutils"
1617
"sigs.k8s.io/cli-utils/cmd/printers"
1718
"sigs.k8s.io/cli-utils/pkg/apply"
1819
"sigs.k8s.io/cli-utils/pkg/apply/event"
1920
"sigs.k8s.io/cli-utils/pkg/common"
21+
"sigs.k8s.io/cli-utils/pkg/inventory"
2022
"sigs.k8s.io/cli-utils/pkg/manifestreader"
2123
"sigs.k8s.io/cli-utils/pkg/provider"
2224
)
@@ -70,12 +72,13 @@ func PreviewCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *c
7072

7173
// PreviewRunner encapsulates data necessary to run the preview command.
7274
type PreviewRunner struct {
73-
Command *cobra.Command
74-
ioStreams genericclioptions.IOStreams
75-
Applier *apply.Applier
76-
Destroyer *apply.Destroyer
77-
provider provider.Provider
78-
loader manifestreader.ManifestLoader
75+
Command *cobra.Command
76+
PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error)
77+
ioStreams genericclioptions.IOStreams
78+
Applier *apply.Applier
79+
Destroyer *apply.Destroyer
80+
provider provider.Provider
81+
loader manifestreader.ManifestLoader
7982

8083
serverSideOptions common.ServerSideOptions
8184
output string
@@ -114,6 +117,13 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
114117
return err
115118
}
116119

120+
if r.PreProcess != nil {
121+
inventoryPolicy, err = r.PreProcess(inv, drs)
122+
if err != nil {
123+
return err
124+
}
125+
}
126+
117127
// if destroy flag is set in preview, transmit it to destroyer DryRunStrategy flag
118128
// and pivot execution to destroy with dry-run
119129
if !r.Destroyer.DryRunStrategy.ClientOrServerDryRun() {

0 commit comments

Comments
 (0)