Skip to content

Commit

Permalink
✨ clusterctl: Suppress API warnings in "move" command
Browse files Browse the repository at this point in the history
  • Loading branch information
dlipovetsky committed Sep 23, 2024
1 parent 483d63d commit 1be7d4a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
40 changes: 39 additions & 1 deletion cmd/clusterctl/cmd/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"github.com/spf13/cobra"

"sigs.k8s.io/cluster-api/cmd/clusterctl/client"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
"sigs.k8s.io/cluster-api/util/apiwarnings"
)

type moveOptions struct {
Expand All @@ -34,6 +38,7 @@ type moveOptions struct {
fromDirectory string
toDirectory string
dryRun bool
hideAPIWarnings bool
}

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

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

c, err := client.New(ctx, cfgFile)
configClient, err := config.New(ctx, cfgFile)
if err != nil {
return err
}

clientOptions := []client.Option{}
if mo.hideAPIWarnings {
clientOptions = append(clientOptions,
client.InjectClusterClientFactory(
func(input client.ClusterClientFactoryInput) (cluster.Client, error) {
return cluster.New(
cluster.Kubeconfig(input.Kubeconfig),
configClient,
cluster.InjectYamlProcessor(input.Processor),
cluster.InjectProxy(
cluster.NewProxy(
cluster.Kubeconfig(input.Kubeconfig),
cluster.InjectWarningHandler(
apiwarnings.DefaultHandler(
logf.Log.WithName("API Server Warning"),
),
),
)),
), nil
},
),
// Ensure that the same configClient used by both the client constructor, and the cluster client factory.
client.InjectConfig(configClient),
)
}

c, err := client.New(ctx, cfgFile, clientOptions...)
if err != nil {
return err
}
Expand Down
13 changes: 5 additions & 8 deletions util/apiwarnings/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,10 @@ func DomainQualifiedFinalizerWarning(domain string) *regexp.Regexp {
// decisions made by the Cluster API project, but cannot be immediately
// addressed, and are therefore not helpful to the user.
func DefaultHandler(l logr.Logger) *DiscardMatchingHandler {
return NewDiscardMatchingHandler(
l,
DiscardMatchingHandlerOptions{
Deduplicate: true,
Expressions: []regexp.Regexp{
*DomainQualifiedFinalizerWarning(clusterv1.GroupVersion.Group),
},
return &DiscardMatchingHandler{
Logger: l,
Expressions: []regexp.Regexp{
*DomainQualifiedFinalizerWarning(clusterv1.GroupVersion.Group),
},
)
}
}

0 comments on commit 1be7d4a

Please sign in to comment.