mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
Add client code
This commit is contained in:
committed by
Vishnu Kannan
parent
b0679f18bc
commit
4a148f99d6
53
pkg/client/unversioned/cache/listers.go
vendored
53
pkg/client/unversioned/cache/listers.go
vendored
@@ -225,6 +225,59 @@ func (s *StoreToReplicationControllerLister) GetPodControllers(pod *api.Pod) (co
|
||||
return
|
||||
}
|
||||
|
||||
// StoreToDaemonLister gives a store List and Exists methods. The store must contain only Daemons.
|
||||
type StoreToDaemonLister struct {
|
||||
Store
|
||||
}
|
||||
|
||||
// Exists checks if the given dc exists in the store.
|
||||
func (s *StoreToDaemonLister) Exists(daemon *api.Daemon) (bool, error) {
|
||||
_, exists, err := s.Store.Get(daemon)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// StoreToDaemonLister lists all daemons in the store.
|
||||
// TODO: converge on the interface in pkg/client
|
||||
func (s *StoreToDaemonLister) List() (daemons []api.Daemon, err error) {
|
||||
for _, c := range s.Store.List() {
|
||||
daemons = append(daemons, *(c.(*api.Daemon)))
|
||||
}
|
||||
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) {
|
||||
var selector labels.Selector
|
||||
var dc api.Daemon
|
||||
|
||||
if len(pod.Labels) == 0 {
|
||||
err = fmt.Errorf("No daemons found for pod %v because it has no labels", pod.Name)
|
||||
return
|
||||
}
|
||||
|
||||
for _, m := range s.Store.List() {
|
||||
dc = *m.(*api.Daemon)
|
||||
if dc.Namespace != pod.Namespace {
|
||||
continue
|
||||
}
|
||||
labelSet := labels.Set(dc.Spec.Selector)
|
||||
selector = labels.Set(dc.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)) {
|
||||
continue
|
||||
}
|
||||
daemons = append(daemons, dc)
|
||||
}
|
||||
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)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// StoreToServiceLister makes a Store that has the List method of the client.ServiceInterface
|
||||
// The Store must contain (only) Services.
|
||||
type StoreToServiceLister struct {
|
||||
|
||||
122
pkg/client/unversioned/cache/listers_test.go
vendored
122
pkg/client/unversioned/cache/listers_test.go
vendored
@@ -155,6 +155,128 @@ func TestStoreToReplicationControllerLister(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreToDaemonLister(t *testing.T) {
|
||||
store := NewStore(MetaNamespaceKeyFunc)
|
||||
lister := StoreToDaemonLister{store}
|
||||
testCases := []struct {
|
||||
inDCs []*api.Daemon
|
||||
list func() ([]api.Daemon, error)
|
||||
outDCNames util.StringSet
|
||||
expectErr bool
|
||||
}{
|
||||
// Basic listing
|
||||
{
|
||||
inDCs: []*api.Daemon{
|
||||
{ObjectMeta: api.ObjectMeta{Name: "basic"}},
|
||||
},
|
||||
list: func() ([]api.Daemon, error) {
|
||||
return lister.List()
|
||||
},
|
||||
outDCNames: util.NewStringSet("basic"),
|
||||
},
|
||||
// Listing multiple controllers
|
||||
{
|
||||
inDCs: []*api.Daemon{
|
||||
{ObjectMeta: api.ObjectMeta{Name: "basic"}},
|
||||
{ObjectMeta: api.ObjectMeta{Name: "complex"}},
|
||||
{ObjectMeta: api.ObjectMeta{Name: "complex2"}},
|
||||
},
|
||||
list: func() ([]api.Daemon, error) {
|
||||
return lister.List()
|
||||
},
|
||||
outDCNames: util.NewStringSet("basic", "complex", "complex2"),
|
||||
},
|
||||
// No pod lables
|
||||
{
|
||||
inDCs: []*api.Daemon{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{Name: "basic", Namespace: "ns"},
|
||||
Spec: api.DaemonSpec{
|
||||
Selector: map[string]string{"foo": "baz"},
|
||||
},
|
||||
},
|
||||
},
|
||||
list: func() ([]api.Daemon, error) {
|
||||
pod := &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{Name: "pod1", Namespace: "ns"},
|
||||
}
|
||||
return lister.GetPodDaemon(pod)
|
||||
},
|
||||
outDCNames: util.NewStringSet(),
|
||||
expectErr: true,
|
||||
},
|
||||
// No RC selectors
|
||||
{
|
||||
inDCs: []*api.Daemon{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{Name: "basic", Namespace: "ns"},
|
||||
},
|
||||
},
|
||||
list: func() ([]api.Daemon, error) {
|
||||
pod := &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "pod1",
|
||||
Namespace: "ns",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
}
|
||||
return lister.GetPodDaemon(pod)
|
||||
},
|
||||
outDCNames: util.NewStringSet(),
|
||||
expectErr: true,
|
||||
},
|
||||
// Matching labels to selectors and namespace
|
||||
{
|
||||
inDCs: []*api.Daemon{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
||||
Spec: api.DaemonSpec{
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{Name: "bar", Namespace: "ns"},
|
||||
Spec: api.DaemonSpec{
|
||||
Selector: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
list: func() ([]api.Daemon, error) {
|
||||
pod := &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "pod1",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
Namespace: "ns",
|
||||
},
|
||||
}
|
||||
return lister.GetPodDaemon(pod)
|
||||
},
|
||||
outDCNames: util.NewStringSet("bar"),
|
||||
},
|
||||
}
|
||||
for _, c := range testCases {
|
||||
for _, r := range c.inDCs {
|
||||
store.Add(r)
|
||||
}
|
||||
|
||||
gotControllers, err := c.list()
|
||||
if err != nil && c.expectErr {
|
||||
continue
|
||||
} else if c.expectErr {
|
||||
t.Fatalf("Expected error, got none")
|
||||
} else if err != nil {
|
||||
t.Fatalf("Unexpected error %#v", err)
|
||||
}
|
||||
gotNames := make([]string, len(gotControllers))
|
||||
for ix := range gotControllers {
|
||||
gotNames[ix] = gotControllers[ix].Name
|
||||
}
|
||||
if !c.outDCNames.HasAll(gotNames...) || len(gotNames) != len(c.outDCNames) {
|
||||
t.Errorf("Unexpected got controllers %+v expected %+v", gotNames, c.outDCNames)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreToPodLister(t *testing.T) {
|
||||
store := NewStore(MetaNamespaceKeyFunc)
|
||||
ids := []string{"foo", "bar", "baz"}
|
||||
|
||||
@@ -33,6 +33,7 @@ type Interface interface {
|
||||
PodsNamespacer
|
||||
PodTemplatesNamespacer
|
||||
ReplicationControllersNamespacer
|
||||
DaemonsNamespacer
|
||||
ServicesNamespacer
|
||||
EndpointsNamespacer
|
||||
VersionInterface
|
||||
@@ -52,6 +53,10 @@ func (c *Client) ReplicationControllers(namespace string) ReplicationControllerI
|
||||
return newReplicationControllers(c, namespace)
|
||||
}
|
||||
|
||||
func (c *Client) Daemons(namespace string) DaemonInterface {
|
||||
return newDaemons(c, namespace)
|
||||
}
|
||||
|
||||
func (c *Client) Nodes() NodeInterface {
|
||||
return newNodes(c)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ limitations under the License.
|
||||
/*
|
||||
Package client contains the implementation of the client side communication with the
|
||||
Kubernetes master. The Client class provides methods for reading, creating, updating,
|
||||
and deleting pods, replication controllers, services, and minions.
|
||||
and deleting pods, replication controllers, daemons, services, and minions.
|
||||
|
||||
Most consumers should use the Config object to create a Client:
|
||||
|
||||
|
||||
@@ -127,7 +127,7 @@ type TLSClientConfig struct {
|
||||
}
|
||||
|
||||
// New creates a Kubernetes client for the given config. This client works with pods,
|
||||
// replication controllers and services. It allows operations such as list, get, update
|
||||
// replication controllers, daemons, and services. It allows operations such as list, get, update
|
||||
// and delete on these objects. An error is returned if the provided configuration
|
||||
// is not valid.
|
||||
func New(c *Config) (*Client, error) {
|
||||
|
||||
@@ -45,6 +45,7 @@ func TestResourceQuotaCreate(t *testing.T) {
|
||||
api.ResourcePods: resource.MustParse("10"),
|
||||
api.ResourceServices: resource.MustParse("10"),
|
||||
api.ResourceReplicationControllers: resource.MustParse("10"),
|
||||
api.ResourceDaemon: resource.MustParse("10"),
|
||||
api.ResourceQuotas: resource.MustParse("10"),
|
||||
},
|
||||
},
|
||||
@@ -77,6 +78,7 @@ func TestResourceQuotaGet(t *testing.T) {
|
||||
api.ResourcePods: resource.MustParse("10"),
|
||||
api.ResourceServices: resource.MustParse("10"),
|
||||
api.ResourceReplicationControllers: resource.MustParse("10"),
|
||||
api.ResourceDaemon: resource.MustParse("10"),
|
||||
api.ResourceQuotas: resource.MustParse("10"),
|
||||
},
|
||||
},
|
||||
@@ -133,6 +135,7 @@ func TestResourceQuotaUpdate(t *testing.T) {
|
||||
api.ResourcePods: resource.MustParse("10"),
|
||||
api.ResourceServices: resource.MustParse("10"),
|
||||
api.ResourceReplicationControllers: resource.MustParse("10"),
|
||||
api.ResourceDaemon: resource.MustParse("10"),
|
||||
api.ResourceQuotas: resource.MustParse("10"),
|
||||
},
|
||||
},
|
||||
@@ -160,6 +163,7 @@ func TestResourceQuotaStatusUpdate(t *testing.T) {
|
||||
api.ResourcePods: resource.MustParse("10"),
|
||||
api.ResourceServices: resource.MustParse("10"),
|
||||
api.ResourceReplicationControllers: resource.MustParse("10"),
|
||||
api.ResourceDaemon: resource.MustParse("10"),
|
||||
api.ResourceQuotas: resource.MustParse("10"),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -114,6 +114,10 @@ func (c *Fake) ReplicationControllers(namespace string) client.ReplicationContro
|
||||
return &FakeReplicationControllers{Fake: c, Namespace: namespace}
|
||||
}
|
||||
|
||||
func (c *Fake) Daemons(namespace string) client.DaemonInterface {
|
||||
return &FakeDaemons{Fake: c, Namespace: namespace}
|
||||
}
|
||||
|
||||
func (c *Fake) Nodes() client.NodeInterface {
|
||||
return &FakeNodes{Fake: c}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user