Skip to content

Commit b62349a

Browse files
authored
Merge pull request #316 from Liujingfang1/preprocess
add preprocess function in commands
2 parents b76dc8d + dfd0419 commit b62349a

File tree

6 files changed

+111
-16
lines changed

6 files changed

+111
-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/flagutils/utils_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2021 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package flagutils
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"sigs.k8s.io/cli-utils/pkg/inventory"
11+
)
12+
13+
func TestConvertInventoryPolicy(t *testing.T) {
14+
testcases := []struct {
15+
value string
16+
policy inventory.InventoryPolicy
17+
err error
18+
}{
19+
{
20+
value: "strict",
21+
policy: inventory.InventoryPolicyMustMatch,
22+
},
23+
{
24+
value: "adopt",
25+
policy: inventory.AdoptIfNoInventory,
26+
},
27+
{
28+
value: "random",
29+
err: fmt.Errorf("inventory policy must be one of strict, adopt"),
30+
},
31+
}
32+
for _, tc := range testcases {
33+
t.Run(tc.value, func(t *testing.T) {
34+
policy, err := ConvertInventoryPolicy(tc.value)
35+
if tc.err == nil {
36+
if err != nil {
37+
t.Errorf("unexpected error %v", err)
38+
}
39+
if policy != tc.policy {
40+
t.Errorf("expected %v but got %v", policy, tc.policy)
41+
}
42+
}
43+
if err == nil && tc.err != nil {
44+
t.Errorf("expected an error, but not happened")
45+
}
46+
})
47+
}
48+
}

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() {

pkg/inventory/fake-inventory-client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ func (fic *FakeInventoryClient) ClearError() {
8585
func (fic *FakeInventoryClient) GetClusterInventoryInfo(inv InventoryInfo) (*unstructured.Unstructured, error) {
8686
return nil, nil
8787
}
88+
89+
func (fic *FakeInventoryClient) UpdateLabels(inv InventoryInfo, labels map[string]string) error {
90+
return nil
91+
}

pkg/inventory/inventory-client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type InventoryClient interface {
4444
ApplyInventoryNamespace(invNamespace *unstructured.Unstructured) error
4545
// GetClusterInventoryInfo returns the cluster inventory object.
4646
GetClusterInventoryInfo(inv InventoryInfo) (*unstructured.Unstructured, error)
47+
// UpdateLabels updates the labels of the cluster inventory object if it exists.
48+
UpdateLabels(InventoryInfo, map[string]string) error
4749
}
4850

4951
// ClusterInventoryClient is a concrete implementation of the
@@ -270,6 +272,18 @@ func (cic *ClusterInventoryClient) GetClusterInventoryInfo(inv InventoryInfo) (*
270272
return clusterInv, nil
271273
}
272274

275+
func (cic *ClusterInventoryClient) UpdateLabels(inv InventoryInfo, labels map[string]string) error {
276+
obj, err := cic.GetClusterInventoryInfo(inv)
277+
if err != nil {
278+
if apierrors.IsNotFound(err) {
279+
return nil
280+
}
281+
return err
282+
}
283+
obj.SetLabels(labels)
284+
return cic.applyInventoryObj(obj)
285+
}
286+
273287
// mergeClusterInventory merges the inventory of multiple inventory objects
274288
// into one inventory object, and applies it. Deletes the remaining unnecessary
275289
// inventory objects. There should be only one inventory object stored in the

0 commit comments

Comments
 (0)