k8s NewListWatchFromClient与NewSimpleClientset崩溃

时间:2017-12-20 15:25:55

标签: go kubernetes

我正在为我的kubernetis控制器实现测试。 我正在使用NewSimpleClientset。 我想收到一个对象来查看对象是否通过informer正确传递。

我的测试功能如下所示

func Test_TestListInformer(t *testing.T) {
    dpl := GenerateHelloWorldDeployment()
    clientSet := fake.NewSimpleClientset(&dpl)
    watchlist := cache.NewListWatchFromClient(clientSet.ExtensionsV1beta1().RESTClient(), "deployments", api_v1.NamespaceAll, fields.Everything())
    var expectedObj interface{}

    resyncPeriod := 30 * time.Minute

    _, eController := cache.NewInformer(
        watchlist,
        &v1beta1.Deployment{},
        resyncPeriod,
        cache.ResourceEventHandlerFuncs{
            AddFunc: func(obj interface{}) {
                expectedObj = obj
            },
            DeleteFunc: func(obj interface{}) {
                expectedObj = obj
            },
            UpdateFunc: func(oldObj interface{}, newObj interface{}) {
                expectedObj = oldObj
            },
        },
    )
    stopChan := make(chan struct{})
    go eController.Run(stopChan)
    time.Sleep(time.Second)
    close(stopChan)


}

它崩溃了

E1220 17:06:06.304112   35412 runtime.go:66] Observed a panic: "invalid memory address or nil pointer dereference" (runtime error: invalid memory address or nil pointer dereference)
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:72
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:65
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:51
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/asm_amd64.s:509
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:491
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:63
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/signal_unix.go:367
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/rest/client.go:222
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/rest/client.go:247
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/listwatch.go:68
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/listwatch.go:101
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/reflector.go:249
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/reflector.go:204
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:133
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:134
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:88
/Users/myuser/dev/go/src/github.com//vendor/k8s.io/client-go/tools/cache/reflector.go:203
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/controller.go:122
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:54
/Users/myuser/dev/go/srmyrepo/myprojc/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:71
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/asm_amd64.s:2337

GenerateHelloWorldDeployment 返回的部署对象很好。 我可以使用clientSet.ExtensionsV1beta1().Deployments("").Get("hello-world-deployment",meta_v1.GetOptions{})

来检索它

为什么告密者会崩溃?

1 个答案:

答案 0 :(得分:0)

想出来。 k8s去lib很难掌握。许多自我复制的对象和接口。

你必须使用 NewFakeControllerSource来自" k8s.io/client-go/tools/cache/testing"

dpl := GenerateFakeDeployment()

watchlist := fcache.NewFakeControllerSource()
watchlist.Add(&dpl)

var expectedObj interface{}

resyncPeriod := 30 * time.Minute

_, eController := cache.NewInformer(
    watchlist,
    &v1beta1.Deployment{},
    resyncPeriod,
    cache.ResourceEventHandlerFuncs{
        AddFunc: func(obj interface{}) {
            expectedObj = obj
        },
        DeleteFunc: func(obj interface{}) {
            expectedObj = obj
        },
        UpdateFunc: func(oldObj interface{}, newObj interface{}) {
            expectedObj = oldObj
        },
    },
)
stopChan := make(chan struct{})
go eController.Run(stopChan)