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 11, 2024
1 parent a08e4b4 commit f5d8e10
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion cmd/clusterctl/cmd/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/rest"

"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"
)

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

var mo = &moveOptions{}
Expand Down Expand Up @@ -80,6 +84,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 +104,34 @@ 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(rest.NoWarnings{}),
)),
), 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

0 comments on commit f5d8e10

Please sign in to comment.