mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-06 07:57:35 +00:00
Address comments.
This commit is contained in:
19
pkg/client/unversioned/cache/listers.go
vendored
19
pkg/client/unversioned/cache/listers.go
vendored
@@ -248,10 +248,10 @@ func (s *StoreToDaemonLister) List() (daemons []api.Daemon, err error) {
|
||||
return daemons, nil
|
||||
}
|
||||
|
||||
// GetPodDaemon returns a list of daemon daemons managing a pod. Returns an error iff no matching daemons are found.
|
||||
func (s *StoreToDaemonLister) GetPodDaemon(pod *api.Pod) (daemons []api.Daemon, err error) {
|
||||
// GetPodDaemons returns a list of daemons managing a pod. Returns an error iff no matching daemons are found.
|
||||
func (s *StoreToDaemonLister) GetPodDaemons(pod *api.Pod) (daemons []api.Daemon, err error) {
|
||||
var selector labels.Selector
|
||||
var dc api.Daemon
|
||||
var daemonController api.Daemon
|
||||
|
||||
if len(pod.Labels) == 0 {
|
||||
err = fmt.Errorf("No daemons found for pod %v because it has no labels", pod.Name)
|
||||
@@ -259,18 +259,17 @@ func (s *StoreToDaemonLister) GetPodDaemon(pod *api.Pod) (daemons []api.Daemon,
|
||||
}
|
||||
|
||||
for _, m := range s.Store.List() {
|
||||
dc = *m.(*api.Daemon)
|
||||
if dc.Namespace != pod.Namespace {
|
||||
daemonController = *m.(*api.Daemon)
|
||||
if daemonController.Namespace != pod.Namespace {
|
||||
continue
|
||||
}
|
||||
labelSet := labels.Set(dc.Spec.Selector)
|
||||
selector = labels.Set(dc.Spec.Selector).AsSelector()
|
||||
selector = labels.Set(daemonController.Spec.Selector).AsSelector()
|
||||
|
||||
// If an dc with a nil or empty selector creeps in, it should match nothing, not everything.
|
||||
if labelSet.AsSelector().Empty() || !selector.Matches(labels.Set(pod.Labels)) {
|
||||
// If a daemonController with a nil or empty selector creeps in, it should match nothing, not everything.
|
||||
if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
|
||||
continue
|
||||
}
|
||||
daemons = append(daemons, dc)
|
||||
daemons = append(daemons, daemonController)
|
||||
}
|
||||
if len(daemons) == 0 {
|
||||
err = fmt.Errorf("Could not find daemons for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
|
||||
|
||||
10
pkg/client/unversioned/cache/listers_test.go
vendored
10
pkg/client/unversioned/cache/listers_test.go
vendored
@@ -64,7 +64,7 @@ func TestStoreToReplicationControllerLister(t *testing.T) {
|
||||
},
|
||||
outRCNames: util.NewStringSet("basic"),
|
||||
},
|
||||
// No pod lables
|
||||
// No pod labels
|
||||
{
|
||||
inRCs: []*api.ReplicationController{
|
||||
{
|
||||
@@ -186,7 +186,7 @@ func TestStoreToDaemonLister(t *testing.T) {
|
||||
},
|
||||
outDCNames: util.NewStringSet("basic", "complex", "complex2"),
|
||||
},
|
||||
// No pod lables
|
||||
// No pod labels
|
||||
{
|
||||
inDCs: []*api.Daemon{
|
||||
{
|
||||
@@ -200,7 +200,7 @@ func TestStoreToDaemonLister(t *testing.T) {
|
||||
pod := &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{Name: "pod1", Namespace: "ns"},
|
||||
}
|
||||
return lister.GetPodDaemon(pod)
|
||||
return lister.GetPodDaemons(pod)
|
||||
},
|
||||
outDCNames: util.NewStringSet(),
|
||||
expectErr: true,
|
||||
@@ -220,7 +220,7 @@ func TestStoreToDaemonLister(t *testing.T) {
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
}
|
||||
return lister.GetPodDaemon(pod)
|
||||
return lister.GetPodDaemons(pod)
|
||||
},
|
||||
outDCNames: util.NewStringSet(),
|
||||
expectErr: true,
|
||||
@@ -249,7 +249,7 @@ func TestStoreToDaemonLister(t *testing.T) {
|
||||
Namespace: "ns",
|
||||
},
|
||||
}
|
||||
return lister.GetPodDaemon(pod)
|
||||
return lister.GetPodDaemons(pod)
|
||||
},
|
||||
outDCNames: util.NewStringSet("bar"),
|
||||
},
|
||||
|
||||
95
pkg/client/unversioned/daemon.go
Normal file
95
pkg/client/unversioned/daemon.go
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 unversioned
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/fields"
|
||||
"k8s.io/kubernetes/pkg/labels"
|
||||
"k8s.io/kubernetes/pkg/watch"
|
||||
)
|
||||
|
||||
// DaemonsNamespacer has methods to work with Daemon resources in a namespace
|
||||
type DaemonsNamespacer interface {
|
||||
Daemons(namespace string) DaemonInterface
|
||||
}
|
||||
|
||||
type DaemonInterface interface {
|
||||
List(selector labels.Selector) (*api.DaemonList, error)
|
||||
Get(name string) (*api.Daemon, error)
|
||||
Create(ctrl *api.Daemon) (*api.Daemon, error)
|
||||
Update(ctrl *api.Daemon) (*api.Daemon, error)
|
||||
Delete(name string) error
|
||||
Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error)
|
||||
}
|
||||
|
||||
// daemons implements DaemonsNamespacer interface
|
||||
type daemons struct {
|
||||
r *Client
|
||||
ns string
|
||||
}
|
||||
|
||||
func newDaemons(c *Client, namespace string) *daemons {
|
||||
return &daemons{c, namespace}
|
||||
}
|
||||
|
||||
// Ensure statically that daemons implements DaemonInterface.
|
||||
var _ DaemonInterface = &daemons{}
|
||||
|
||||
func (c *daemons) List(selector labels.Selector) (result *api.DaemonList, err error) {
|
||||
result = &api.DaemonList{}
|
||||
err = c.r.Get().Namespace(c.ns).Resource("daemons").LabelsSelectorParam(selector).Do().Into(result)
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns information about a particular daemon.
|
||||
func (c *daemons) Get(name string) (result *api.Daemon, err error) {
|
||||
result = &api.Daemon{}
|
||||
err = c.r.Get().Namespace(c.ns).Resource("daemons").Name(name).Do().Into(result)
|
||||
return
|
||||
}
|
||||
|
||||
// Create creates a new daemon.
|
||||
func (c *daemons) Create(daemon *api.Daemon) (result *api.Daemon, err error) {
|
||||
result = &api.Daemon{}
|
||||
err = c.r.Post().Namespace(c.ns).Resource("daemons").Body(daemon).Do().Into(result)
|
||||
return
|
||||
}
|
||||
|
||||
// Update updates an existing daemon.
|
||||
func (c *daemons) Update(daemon *api.Daemon) (result *api.Daemon, err error) {
|
||||
result = &api.Daemon{}
|
||||
err = c.r.Put().Namespace(c.ns).Resource("daemons").Name(daemon.Name).Body(daemon).Do().Into(result)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete deletes an existing daemon.
|
||||
func (c *daemons) Delete(name string) error {
|
||||
return c.r.Delete().Namespace(c.ns).Resource("daemons").Name(name).Do().Error()
|
||||
}
|
||||
|
||||
// Watch returns a watch.Interface that watches the requested daemons.
|
||||
func (c *daemons) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
return c.r.Get().
|
||||
Prefix("watch").
|
||||
Namespace(c.ns).
|
||||
Resource("daemons").
|
||||
Param("resourceVersion", resourceVersion).
|
||||
LabelsSelectorParam(label).
|
||||
FieldsSelectorParam(field).
|
||||
Watch()
|
||||
}
|
||||
159
pkg/client/unversioned/daemon_test.go
Normal file
159
pkg/client/unversioned/daemon_test.go
Normal file
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 unversioned
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/labels"
|
||||
)
|
||||
|
||||
func getDCResourceName() string {
|
||||
return "daemons"
|
||||
}
|
||||
|
||||
func TestListDaemons(t *testing.T) {
|
||||
ns := api.NamespaceAll
|
||||
c := &testClient{
|
||||
Request: testRequest{
|
||||
Method: "GET",
|
||||
Path: testapi.ResourcePath(getDCResourceName(), ns, ""),
|
||||
},
|
||||
Response: Response{StatusCode: 200,
|
||||
Body: &api.DaemonList{
|
||||
Items: []api.Daemon{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
Labels: map[string]string{
|
||||
"foo": "bar",
|
||||
"name": "baz",
|
||||
},
|
||||
},
|
||||
Spec: api.DaemonSpec{
|
||||
Template: &api.PodTemplateSpec{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
receivedControllerList, err := c.Setup().Daemons(ns).List(labels.Everything())
|
||||
c.Validate(t, receivedControllerList, err)
|
||||
|
||||
}
|
||||
|
||||
func TestGetDaemon(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
c := &testClient{
|
||||
Request: testRequest{Method: "GET", Path: testapi.ResourcePath(getDCResourceName(), ns, "foo"), Query: buildQueryValues(nil)},
|
||||
Response: Response{
|
||||
StatusCode: 200,
|
||||
Body: &api.Daemon{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
Labels: map[string]string{
|
||||
"foo": "bar",
|
||||
"name": "baz",
|
||||
},
|
||||
},
|
||||
Spec: api.DaemonSpec{
|
||||
Template: &api.PodTemplateSpec{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
receivedController, err := c.Setup().Daemons(ns).Get("foo")
|
||||
c.Validate(t, receivedController, err)
|
||||
}
|
||||
|
||||
func TestGetDaemonWithNoName(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
c := &testClient{Error: true}
|
||||
receivedPod, err := c.Setup().Daemons(ns).Get("")
|
||||
if (err != nil) && (err.Error() != nameRequiredError) {
|
||||
t.Errorf("Expected error: %v, but got %v", nameRequiredError, err)
|
||||
}
|
||||
|
||||
c.Validate(t, receivedPod, err)
|
||||
}
|
||||
|
||||
func TestUpdateDaemon(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
requestController := &api.Daemon{
|
||||
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"},
|
||||
}
|
||||
c := &testClient{
|
||||
Request: testRequest{Method: "PUT", Path: testapi.ResourcePath(getDCResourceName(), ns, "foo"), Query: buildQueryValues(nil)},
|
||||
Response: Response{
|
||||
StatusCode: 200,
|
||||
Body: &api.Daemon{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
Labels: map[string]string{
|
||||
"foo": "bar",
|
||||
"name": "baz",
|
||||
},
|
||||
},
|
||||
Spec: api.DaemonSpec{
|
||||
Template: &api.PodTemplateSpec{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
receivedController, err := c.Setup().Daemons(ns).Update(requestController)
|
||||
c.Validate(t, receivedController, err)
|
||||
}
|
||||
|
||||
func TestDeleteDaemon(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
c := &testClient{
|
||||
Request: testRequest{Method: "DELETE", Path: testapi.ResourcePath(getDCResourceName(), ns, "foo"), Query: buildQueryValues(nil)},
|
||||
Response: Response{StatusCode: 200},
|
||||
}
|
||||
err := c.Setup().Daemons(ns).Delete("foo")
|
||||
c.Validate(t, nil, err)
|
||||
}
|
||||
|
||||
func TestCreateDaemon(t *testing.T) {
|
||||
ns := api.NamespaceDefault
|
||||
requestController := &api.Daemon{
|
||||
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
||||
}
|
||||
c := &testClient{
|
||||
Request: testRequest{Method: "POST", Path: testapi.ResourcePath(getDCResourceName(), ns, ""), Body: requestController, Query: buildQueryValues(nil)},
|
||||
Response: Response{
|
||||
StatusCode: 200,
|
||||
Body: &api.Daemon{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
Labels: map[string]string{
|
||||
"foo": "bar",
|
||||
"name": "baz",
|
||||
},
|
||||
},
|
||||
Spec: api.DaemonSpec{
|
||||
Template: &api.PodTemplateSpec{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
receivedController, err := c.Setup().Daemons(ns).Create(requestController)
|
||||
c.Validate(t, receivedController, err)
|
||||
}
|
||||
74
pkg/client/unversioned/testclient/fake_daemons.go
Normal file
74
pkg/client/unversioned/testclient/fake_daemons.go
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
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 testclient
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
kClientLib "k8s.io/kubernetes/pkg/client/unversioned"
|
||||
"k8s.io/kubernetes/pkg/fields"
|
||||
"k8s.io/kubernetes/pkg/labels"
|
||||
"k8s.io/kubernetes/pkg/watch"
|
||||
)
|
||||
|
||||
// FakeDaemons implements DaemonInterface. Meant to be embedded into a struct to get a default
|
||||
// implementation. This makes faking out just the method you want to test easier.
|
||||
type FakeDaemons struct {
|
||||
Fake *Fake
|
||||
Namespace string
|
||||
}
|
||||
|
||||
const (
|
||||
GetDaemonAction = "get-daemon"
|
||||
UpdateDaemonAction = "update-daemon"
|
||||
WatchDaemonAction = "watch-daemon"
|
||||
DeleteDaemonAction = "delete-daemon"
|
||||
ListDaemonAction = "list-daemons"
|
||||
CreateDaemonAction = "create-daemon"
|
||||
)
|
||||
|
||||
// Ensure statically that FakeDaemons implements DaemonInterface.
|
||||
var _ kClientLib.DaemonInterface = &FakeDaemons{}
|
||||
|
||||
func (c *FakeDaemons) Get(name string) (*api.Daemon, error) {
|
||||
obj, err := c.Fake.Invokes(NewGetAction("daemons", c.Namespace, name), &api.Daemon{})
|
||||
return obj.(*api.Daemon), err
|
||||
}
|
||||
|
||||
func (c *FakeDaemons) List(label labels.Selector) (*api.DaemonList, error) {
|
||||
obj, err := c.Fake.Invokes(NewListAction("daemons", c.Namespace, label, nil), &api.DaemonList{})
|
||||
return obj.(*api.DaemonList), err
|
||||
}
|
||||
|
||||
func (c *FakeDaemons) Create(daemon *api.Daemon) (*api.Daemon, error) {
|
||||
obj, err := c.Fake.Invokes(NewCreateAction("daemons", c.Namespace, daemon), &api.Daemon{})
|
||||
return obj.(*api.Daemon), err
|
||||
}
|
||||
|
||||
func (c *FakeDaemons) Update(daemon *api.Daemon) (*api.Daemon, error) {
|
||||
obj, err := c.Fake.Invokes(NewUpdateAction("daemons", c.Namespace, daemon), &api.Daemon{})
|
||||
return obj.(*api.Daemon), err
|
||||
}
|
||||
|
||||
func (c *FakeDaemons) Delete(name string) error {
|
||||
_, err := c.Fake.Invokes(NewDeleteAction("daemons", c.Namespace, name), &api.Daemon{})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *FakeDaemons) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Invokes(NewWatchAction("daemons", c.Namespace, label, field, resourceVersion), nil)
|
||||
return c.Fake.Watch, nil
|
||||
}
|
||||
Reference in New Issue
Block a user