diff --git a/cmd/kubeadm/app/util/apiclient/dryrunclient.go b/cmd/kubeadm/app/util/apiclient/dryrunclient.go index 6dccc5b8794..f3ac13343b3 100644 --- a/cmd/kubeadm/app/util/apiclient/dryrunclient.go +++ b/cmd/kubeadm/app/util/apiclient/dryrunclient.go @@ -119,8 +119,16 @@ func NewDryRunClientWithOpts(opts DryRunClientOptions) clientset.Interface { Reaction: func(action core.Action) (bool, runtime.Object, error) { getAction, ok := action.(core.GetAction) if !ok { - // something's wrong, we can't handle this event - return true, nil, errors.New("can't cast get reactor event action object to GetAction interface") + // If the GetAction cast fails, this could be an ActionImpl with a "get" verb. + // Such actions could be invoked from any of the fake discovery calls, such as ServerVersion(). + // Attempt the cast to ActionImpl and construct a GetActionImpl from it. + actionImpl, ok := action.(core.ActionImpl) + if ok { + getAction = core.GetActionImpl{ActionImpl: actionImpl} + } else { + // something's wrong, we can't handle this event + return true, nil, errors.New("can't cast get reactor event action object to GetAction interface") + } } handled, obj, err := opts.Getter.HandleGetAction(getAction) diff --git a/cmd/kubeadm/app/util/apiclient/dryrunclient_test.go b/cmd/kubeadm/app/util/apiclient/dryrunclient_test.go index 7ef41c17585..fc2b6412099 100644 --- a/cmd/kubeadm/app/util/apiclient/dryrunclient_test.go +++ b/cmd/kubeadm/app/util/apiclient/dryrunclient_test.go @@ -18,12 +18,15 @@ package apiclient import ( "bytes" + "io" "testing" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" rbac "k8s.io/api/rbac/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" + pkgversion "k8s.io/apimachinery/pkg/version" + fakediscovery "k8s.io/client-go/discovery/fake" core "k8s.io/client-go/testing" ) @@ -109,3 +112,24 @@ func TestLogDryRunAction(t *testing.T) { }) } } + +func TestDiscoveryServerVersion(t *testing.T) { + dryRunGetter := &InitDryRunGetter{ + controlPlaneName: "controlPlane", + serviceSubnet: "serviceSubnet", + } + c := NewDryRunClient(dryRunGetter, io.Discard) + fakeclientDiscovery, ok := c.Discovery().(*fakediscovery.FakeDiscovery) + if !ok { + t.Fatal("could not obtain FakeDiscovery from dry run client") + } + const gitVersion = "foo" + fakeclientDiscovery.FakedServerVersion = &pkgversion.Info{GitVersion: gitVersion} + ver, err := c.Discovery().ServerVersion() + if err != nil { + t.Fatalf("Get ServerVersion failed.: %v", err) + } + if ver.GitVersion != gitVersion { + t.Fatalf("GitVersion did not match, expected %s, got %s", gitVersion, ver.GitVersion) + } +}