Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
29d225e9
Commit
29d225e9
authored
Aug 12, 2018
by
Renaud Gaubert
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update pluginwatcher tests
parent
4d18aa63
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
141 additions
and
80 deletions
+141
-80
BUILD
pkg/kubelet/util/pluginwatcher/BUILD
+13
-18
example_handler.go
pkg/kubelet/util/pluginwatcher/example_handler.go
+85
-43
example_plugin.go
pkg/kubelet/util/pluginwatcher/example_plugin.go
+43
-19
plugin_watcher_test.go
pkg/kubelet/util/pluginwatcher/plugin_watcher_test.go
+0
-0
No files found.
pkg/kubelet/util/pluginwatcher/BUILD
View file @
29d225e9
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
...
...
@@ -12,8 +6,10 @@ go_library(
"example_handler.go",
"example_plugin.go",
"plugin_watcher.go",
"types.go",
],
importpath = "k8s.io/kubernetes/pkg/kubelet/util/pluginwatcher",
visibility = ["//visibility:public"],
deps = [
"//pkg/kubelet/apis/pluginregistration/v1alpha1:go_default_library",
"//pkg/kubelet/util/pluginwatcher/example_plugin_apis/v1beta1:go_default_library",
...
...
@@ -27,6 +23,16 @@ go_library(
],
)
go_test(
name = "go_default_test",
srcs = ["plugin_watcher_test.go"],
embed = [":go_default_library"],
deps = [
"//pkg/kubelet/apis/pluginregistration/v1alpha1:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
...
...
@@ -44,14 +50,3 @@ filegroup(
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["plugin_watcher_test.go"],
embed = [":go_default_library"],
deps = [
"//pkg/kubelet/apis/pluginregistration/v1alpha1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
],
)
pkg/kubelet/util/pluginwatcher/example_handler.go
View file @
29d225e9
...
...
@@ -23,6 +23,7 @@ import (
"sync"
"time"
"github.com/golang/glog"
"golang.org/x/net/context"
v1beta1
"k8s.io/kubernetes/pkg/kubelet/util/pluginwatcher/example_plugin_apis/v1beta1"
...
...
@@ -30,41 +31,61 @@ import (
)
type
exampleHandler
struct
{
registeredPlugins
map
[
string
]
struct
{}
mutex
sync
.
Mutex
chanForHandlerAckErrors
chan
error
// for testing
SupportedVersions
[]
string
ExpectedNames
map
[
string
]
int
eventChans
map
[
string
]
chan
examplePluginEvent
// map[pluginName]eventChan
m
sync
.
Mutex
count
int
}
type
examplePluginEvent
int
const
(
exampleEventValidate
examplePluginEvent
=
0
exampleEventRegister
examplePluginEvent
=
1
exampleEventDeRegister
examplePluginEvent
=
2
exampleEventError
examplePluginEvent
=
3
)
// NewExampleHandler provide a example handler
func
NewExampleHandler
()
*
exampleHandler
{
func
NewExampleHandler
(
supportedVersions
[]
string
)
*
exampleHandler
{
return
&
exampleHandler
{
chanForHandlerAckErrors
:
make
(
chan
error
),
registeredPlugins
:
make
(
map
[
string
]
struct
{}),
SupportedVersions
:
supportedVersions
,
ExpectedNames
:
make
(
map
[
string
]
int
),
eventChans
:
make
(
map
[
string
]
chan
examplePluginEvent
),
}
}
func
(
h
*
exampleHandler
)
Cleanup
()
error
{
h
.
mutex
.
Lock
()
defer
h
.
mutex
.
Unlock
()
h
.
registeredPlugins
=
make
(
map
[
string
]
struct
{})
return
nil
}
func
(
p
*
exampleHandler
)
ValidatePlugin
(
pluginName
string
,
endpoint
string
,
versions
[]
string
)
error
{
p
.
SendEvent
(
pluginName
,
exampleEventValidate
)
func
(
h
*
exampleHandler
)
Handler
(
pluginName
string
,
endpoint
string
,
versions
[]
string
,
sockPath
string
)
(
chan
bool
,
error
)
{
n
,
ok
:=
p
.
DecreasePluginCount
(
pluginName
)
if
!
ok
&&
n
>
0
{
return
fmt
.
Errorf
(
"pluginName('%s') wasn't expected (count is %d)"
,
pluginName
,
n
)
}
// check for supported versions
if
!
reflect
.
DeepEqual
([]
string
{
"v1beta1"
,
"v1beta2"
},
versions
)
{
return
nil
,
fmt
.
Errorf
(
"not the supported versions: %s"
,
versions
)
if
!
reflect
.
DeepEqual
(
versions
,
p
.
SupportedVersions
)
{
return
fmt
.
Errorf
(
"versions('%v') != supported versions('%v')"
,
versions
,
p
.
SupportedVersions
)
}
// this handler expects non-empty endpoint as an example
if
len
(
endpoint
)
==
0
{
return
nil
,
errors
.
New
(
"expecting non empty endpoint"
)
return
errors
.
New
(
"expecting non empty endpoint"
)
}
_
,
conn
,
err
:=
dial
(
sockPath
)
return
nil
}
func
(
p
*
exampleHandler
)
RegisterPlugin
(
pluginName
,
endpoint
string
)
error
{
p
.
SendEvent
(
pluginName
,
exampleEventRegister
)
// Verifies the grpcServer is ready to serve services.
_
,
conn
,
err
:=
dial
(
endpoint
,
time
.
Second
)
if
err
!=
nil
{
return
nil
,
err
return
fmt
.
Errorf
(
"Failed dialing endpoint (%s): %v"
,
endpoint
,
err
)
}
defer
conn
.
Close
()
...
...
@@ -73,33 +94,54 @@ func (h *exampleHandler) Handler(pluginName string, endpoint string, versions []
v1beta2Client
:=
v1beta2
.
NewExampleClient
(
conn
)
// Tests v1beta1 GetExampleInfo
if
_
,
err
=
v1beta1Client
.
GetExampleInfo
(
context
.
Background
(),
&
v1beta1
.
ExampleRequest
{});
err
!=
nil
{
return
nil
,
err
_
,
err
=
v1beta1Client
.
GetExampleInfo
(
context
.
Background
(),
&
v1beta1
.
ExampleRequest
{})
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Failed GetExampleInfo for v1beta2Client(%s): %v"
,
endpoint
,
err
)
}
// Tests v1beta1 GetExampleInfo
_
,
err
=
v1beta2Client
.
GetExampleInfo
(
context
.
Background
(),
&
v1beta2
.
ExampleRequest
{})
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Failed GetExampleInfo for v1beta2Client(%s): %v"
,
endpoint
,
err
)
}
// Tests v1beta2 GetExampleInfo
if
_
,
err
=
v1beta2Client
.
GetExampleInfo
(
context
.
Background
(),
&
v1beta2
.
ExampleRequest
{});
err
!=
nil
{
return
nil
,
err
return
nil
}
func
(
p
*
exampleHandler
)
DeRegisterPlugin
(
pluginName
string
)
{
p
.
SendEvent
(
pluginName
,
exampleEventDeRegister
)
}
func
(
p
*
exampleHandler
)
EventChan
(
pluginName
string
)
chan
examplePluginEvent
{
return
p
.
eventChans
[
pluginName
]
}
func
(
p
*
exampleHandler
)
SendEvent
(
pluginName
string
,
event
examplePluginEvent
)
{
glog
.
V
(
2
)
.
Infof
(
"Sending %v for plugin %s over chan %v"
,
event
,
pluginName
,
p
.
eventChans
[
pluginName
])
p
.
eventChans
[
pluginName
]
<-
event
}
func
(
p
*
exampleHandler
)
AddPluginName
(
pluginName
string
)
{
p
.
m
.
Lock
()
defer
p
.
m
.
Unlock
()
v
,
ok
:=
p
.
ExpectedNames
[
pluginName
]
if
!
ok
{
p
.
eventChans
[
pluginName
]
=
make
(
chan
examplePluginEvent
)
v
=
1
}
// handle registered plugin
h
.
mutex
.
Lock
()
if
_
,
exist
:=
h
.
registeredPlugins
[
pluginName
];
exist
{
h
.
mutex
.
Unlock
()
return
nil
,
fmt
.
Errorf
(
"plugin %s already registered"
,
pluginName
)
p
.
ExpectedNames
[
pluginName
]
=
v
}
func
(
p
*
exampleHandler
)
DecreasePluginCount
(
pluginName
string
)
(
old
int
,
ok
bool
)
{
p
.
m
.
Lock
()
defer
p
.
m
.
Unlock
()
v
,
ok
:=
p
.
ExpectedNames
[
pluginName
]
if
!
ok
{
v
=
-
1
}
h
.
registeredPlugins
[
pluginName
]
=
struct
{}{}
h
.
mutex
.
Unlock
()
chanForAckOfNotification
:=
make
(
chan
bool
)
go
func
()
{
select
{
case
<-
chanForAckOfNotification
:
// TODO: handle the negative scenario
close
(
chanForAckOfNotification
)
case
<-
time
.
After
(
time
.
Second
)
:
h
.
chanForHandlerAckErrors
<-
errors
.
New
(
"Timed out while waiting for notification ack"
)
}
}()
return
chanForAckOfNotification
,
nil
return
v
,
ok
}
pkg/kubelet/util/pluginwatcher/example_plugin.go
View file @
29d225e9
...
...
@@ -18,7 +18,9 @@ package pluginwatcher
import
(
"errors"
"fmt"
"net"
"os"
"sync"
"time"
...
...
@@ -39,6 +41,7 @@ type examplePlugin struct {
endpoint
string
// for testing
pluginName
string
pluginType
string
versions
[]
string
}
type
pluginServiceV1Beta1
struct
{
...
...
@@ -73,12 +76,13 @@ func NewExamplePlugin() *examplePlugin {
}
// NewTestExamplePlugin returns an initialized examplePlugin instance for testing
func
NewTestExamplePlugin
(
pluginName
string
,
pluginType
string
,
endpoint
string
)
*
examplePlugin
{
func
NewTestExamplePlugin
(
pluginName
string
,
pluginType
string
,
endpoint
string
,
advertisedVersions
...
string
)
*
examplePlugin
{
return
&
examplePlugin
{
pluginName
:
pluginName
,
pluginType
:
pluginType
,
registrationStatus
:
make
(
chan
registerapi
.
RegistrationStatus
),
endpoint
:
endpoint
,
versions
:
advertisedVersions
,
registrationStatus
:
make
(
chan
registerapi
.
RegistrationStatus
),
}
}
...
...
@@ -88,36 +92,48 @@ func (e *examplePlugin) GetInfo(ctx context.Context, req *registerapi.InfoReques
Type
:
e
.
pluginType
,
Name
:
e
.
pluginName
,
Endpoint
:
e
.
endpoint
,
SupportedVersions
:
[]
string
{
"v1beta1"
,
"v1beta2"
}
,
SupportedVersions
:
e
.
versions
,
},
nil
}
func
(
e
*
examplePlugin
)
NotifyRegistrationStatus
(
ctx
context
.
Context
,
status
*
registerapi
.
RegistrationStatus
)
(
*
registerapi
.
RegistrationStatusResponse
,
error
)
{
glog
.
Errorf
(
"Registration is: %v
\n
"
,
status
)
if
e
.
registrationStatus
!=
nil
{
e
.
registrationStatus
<-
*
status
}
if
!
status
.
PluginRegistered
{
glog
.
Errorf
(
"Registration failed: %s
\n
"
,
status
.
Error
)
}
return
&
registerapi
.
RegistrationStatusResponse
{},
nil
}
// Serve starts
example plugin grpc server
func
(
e
*
examplePlugin
)
Serve
(
s
ocketPath
string
)
error
{
glog
.
Infof
(
"starting example server at: %s
\n
"
,
socketPath
)
lis
,
err
:=
net
.
Listen
(
"unix"
,
socketPath
)
// Serve starts
a pluginwatcher server and one or more of the plugin services
func
(
e
*
examplePlugin
)
Serve
(
s
ervices
...
string
)
error
{
glog
.
Infof
(
"starting example server at: %s
\n
"
,
e
.
endpoint
)
lis
,
err
:=
net
.
Listen
(
"unix"
,
e
.
endpoint
)
if
err
!=
nil
{
return
err
}
glog
.
Infof
(
"example server started at: %s
\n
"
,
socketPath
)
glog
.
Infof
(
"example server started at: %s
\n
"
,
e
.
endpoint
)
e
.
grpcServer
=
grpc
.
NewServer
()
// Registers kubelet plugin watcher api.
registerapi
.
RegisterRegistrationServer
(
e
.
grpcServer
,
e
)
// Registers services for both v1beta1 and v1beta2 versions.
v1beta1
:=
&
pluginServiceV1Beta1
{
server
:
e
}
v1beta1
.
RegisterService
()
v1beta2
:=
&
pluginServiceV1Beta2
{
server
:
e
}
v1beta2
.
RegisterService
()
for
_
,
service
:=
range
services
{
switch
service
{
case
"v1beta1"
:
v1beta1
:=
&
pluginServiceV1Beta1
{
server
:
e
}
v1beta1
.
RegisterService
()
break
case
"v1beta2"
:
v1beta2
:=
&
pluginServiceV1Beta2
{
server
:
e
}
v1beta2
.
RegisterService
()
break
default
:
return
fmt
.
Errorf
(
"Unsupported service: '%s'"
,
service
)
}
}
// Starts service
e
.
wg
.
Add
(
1
)
...
...
@@ -128,22 +144,30 @@ func (e *examplePlugin) Serve(socketPath string) error {
glog
.
Errorf
(
"example server stopped serving: %v"
,
err
)
}
}()
return
nil
}
func
(
e
*
examplePlugin
)
Stop
()
error
{
glog
.
Infof
(
"Stopping example server
\n
"
)
glog
.
Infof
(
"Stopping example server at: %s
\n
"
,
e
.
endpoint
)
e
.
grpcServer
.
Stop
()
c
:=
make
(
chan
struct
{})
go
func
()
{
defer
close
(
c
)
e
.
wg
.
Wait
()
}()
select
{
case
<-
c
:
return
nil
break
case
<-
time
.
After
(
time
.
Second
)
:
glog
.
Errorf
(
"Timed out on waiting for stop completion"
)
return
errors
.
New
(
"Timed out on waiting for stop completion"
)
}
if
err
:=
os
.
Remove
(
e
.
endpoint
);
err
!=
nil
&&
!
os
.
IsNotExist
(
err
)
{
return
err
}
return
nil
}
pkg/kubelet/util/pluginwatcher/plugin_watcher_test.go
View file @
29d225e9
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment