Skip to content

Commit 1be7d4a

Browse files
committed
✨ clusterctl: Suppress API warnings in "move" command
1 parent 483d63d commit 1be7d4a

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

cmd/clusterctl/cmd/move.go

+39-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import (
2323
"github.com/spf13/cobra"
2424

2525
"sigs.k8s.io/cluster-api/cmd/clusterctl/client"
26+
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
27+
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
28+
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
29+
"sigs.k8s.io/cluster-api/util/apiwarnings"
2630
)
2731

2832
type moveOptions struct {
@@ -34,6 +38,7 @@ type moveOptions struct {
3438
fromDirectory string
3539
toDirectory string
3640
dryRun bool
41+
hideAPIWarnings bool
3742
}
3843

3944
var mo = &moveOptions{}
@@ -80,6 +85,8 @@ func init() {
8085
"Write Cluster API objects and all dependencies from a management cluster to directory.")
8186
moveCmd.Flags().StringVar(&mo.fromDirectory, "from-directory", "",
8287
"Read Cluster API objects and all dependencies from a directory into a management cluster.")
88+
moveCmd.Flags().BoolVar(&mo.hideAPIWarnings, "hide-api-warnings", true,
89+
"Hide warnings returned by the API server.")
8390

8491
moveCmd.MarkFlagsMutuallyExclusive("to-directory", "to-kubeconfig")
8592
moveCmd.MarkFlagsMutuallyExclusive("from-directory", "to-directory")
@@ -98,7 +105,38 @@ func runMove() error {
98105
return errors.New("please specify a target cluster using the --to-kubeconfig flag when not using --dry-run, --to-directory or --from-directory")
99106
}
100107

101-
c, err := client.New(ctx, cfgFile)
108+
configClient, err := config.New(ctx, cfgFile)
109+
if err != nil {
110+
return err
111+
}
112+
113+
clientOptions := []client.Option{}
114+
if mo.hideAPIWarnings {
115+
clientOptions = append(clientOptions,
116+
client.InjectClusterClientFactory(
117+
func(input client.ClusterClientFactoryInput) (cluster.Client, error) {
118+
return cluster.New(
119+
cluster.Kubeconfig(input.Kubeconfig),
120+
configClient,
121+
cluster.InjectYamlProcessor(input.Processor),
122+
cluster.InjectProxy(
123+
cluster.NewProxy(
124+
cluster.Kubeconfig(input.Kubeconfig),
125+
cluster.InjectWarningHandler(
126+
apiwarnings.DefaultHandler(
127+
logf.Log.WithName("API Server Warning"),
128+
),
129+
),
130+
)),
131+
), nil
132+
},
133+
),
134+
// Ensure that the same configClient used by both the client constructor, and the cluster client factory.
135+
client.InjectConfig(configClient),
136+
)
137+
}
138+
139+
c, err := client.New(ctx, cfgFile, clientOptions...)
102140
if err != nil {
103141
return err
104142
}

util/apiwarnings/default.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ func DomainQualifiedFinalizerWarning(domain string) *regexp.Regexp {
3737
// decisions made by the Cluster API project, but cannot be immediately
3838
// addressed, and are therefore not helpful to the user.
3939
func DefaultHandler(l logr.Logger) *DiscardMatchingHandler {
40-
return NewDiscardMatchingHandler(
41-
l,
42-
DiscardMatchingHandlerOptions{
43-
Deduplicate: true,
44-
Expressions: []regexp.Regexp{
45-
*DomainQualifiedFinalizerWarning(clusterv1.GroupVersion.Group),
46-
},
40+
return &DiscardMatchingHandler{
41+
Logger: l,
42+
Expressions: []regexp.Regexp{
43+
*DomainQualifiedFinalizerWarning(clusterv1.GroupVersion.Group),
4744
},
48-
)
45+
}
4946
}

0 commit comments

Comments
 (0)