Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 46 additions & 0 deletions util/apiwarnings/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apiwarnings

import (
"fmt"
"regexp"

"github.com/go-logr/logr"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

// DomainQualifiedFinalizerWarning is a regular expression that matches a
// warning that the API server returns when a finalizer is not domain-qualified.
func DomainQualifiedFinalizerWarning(domain string) *regexp.Regexp {
return regexp.MustCompile(
fmt.Sprintf("^metadata.finalizers:.*%s.*prefer a domain-qualified finalizer name to avoid accidental conflicts with other finalizer writers$", domain),
)
}

// DefaultHandler is a handler that discards warnings that are the result of
// 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 &DiscardMatchingHandler{
Logger: l,
Expressions: []regexp.Regexp{
*DomainQualifiedFinalizerWarning(clusterv1.GroupVersion.Group),
},
}
}
61 changes: 61 additions & 0 deletions util/apiwarnings/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apiwarnings

import (
"testing"

"github.com/go-logr/logr/funcr"
. "github.com/onsi/gomega"
)

func TestDefaultHandler(t *testing.T) {
tests := []struct {
name string
code int
message string
wantLogged bool
}{
{
name: "log, if warning does not match any expression",
code: 299,
message: `metadata.finalizers: "foo.example.com": prefer a domain-qualified finalizer name to avoid accidental conflicts with other finalizer writers`,
wantLogged: true,
},
{
name: "do not log, if warning matches at least one expression",
code: 299,
message: `metadata.finalizers: "dockermachine.infrastructure.cluster.x-k8s.io": prefer a domain-qualified finalizer name to avoid accidental conflicts with other finalizer writers`,
wantLogged: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
logged := false
h := DefaultHandler(
funcr.New(func(_, _ string) {
logged = true
},
funcr.Options{},
),
)
h.HandleWarningHeader(tt.code, "", tt.message)
g.Expect(logged).To(Equal(tt.wantLogged))
})
}
}
Loading