mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 12:15:52 +00:00
kubeadm: handle dry run GET actions from fake discovery
The kubeadm dry run client reactor code is flawed as it assumes all invoked "get" verb actions can be casted to GetAction. Apparently that is not the case when Discovery().ServerVersion() and other discovery calls are made. In such cases the action type is the bare ActionImpl. Catch if an action can be casted to ActionImpl and construct a GetAction from it. GetActionImpl only suppersets ActionImpl with a Name field (empty string in this case). Add unit test for Discovery().ServerVersion().
This commit is contained in:
parent
05a0ce83a6
commit
54b73deaca
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user