Skip to content

Commit

Permalink
add cluster registry
Browse files Browse the repository at this point in the history
  • Loading branch information
jianhuiz committed Apr 26, 2016
1 parent fcb241d commit 1c1ad1a
Show file tree
Hide file tree
Showing 7 changed files with 591 additions and 1 deletion.
82 changes: 82 additions & 0 deletions federation/registry/cluster/etcd/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2016 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 etcd

import (
"k8s.io/kubernetes/federation/apis/federation"
"k8s.io/kubernetes/federation/registry/cluster"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/registry/generic"
"k8s.io/kubernetes/pkg/registry/generic/registry"
"k8s.io/kubernetes/pkg/runtime"
)

type REST struct {
*registry.Store
}

type StatusREST struct {
store *registry.Store
}

func (r *StatusREST) New() runtime.Object {
return &federation.Cluster{}
}

// Update alters the status subset of an object.
func (r *StatusREST) Update(ctx api.Context, obj runtime.Object) (runtime.Object, bool, error) {
return r.store.Update(ctx, obj)
}

// NewREST returns a RESTStorage object that will work against clusters.
func NewREST(opts generic.RESTOptions) (*REST, *StatusREST) {
prefix := "/clusters"

newListFunc := func() runtime.Object { return &federation.ClusterList{} }
storageInterface := opts.Decorator(
opts.Storage, 100, &federation.Cluster{}, prefix, cluster.Strategy, newListFunc)

store := &registry.Store{
NewFunc: func() runtime.Object { return &federation.Cluster{} },
NewListFunc: newListFunc,
KeyRootFunc: func(ctx api.Context) string {
return prefix
},
KeyFunc: func(ctx api.Context, name string) (string, error) {
return registry.NoNamespaceKeyFunc(ctx, prefix, name)
},
ObjectNameFunc: func(obj runtime.Object) (string, error) {
return obj.(*federation.Cluster).Name, nil
},
PredicateFunc: cluster.MatchCluster,
QualifiedResource: federation.Resource("clusters"),
DeleteCollectionWorkers: opts.DeleteCollectionWorkers,

CreateStrategy: cluster.Strategy,
UpdateStrategy: cluster.Strategy,
DeleteStrategy: cluster.Strategy,

ReturnDeletedObject: true,

Storage: storageInterface,
}

statusStore := *store
statusStore.UpdateStrategy = cluster.StatusStrategy

return &REST{store}, &StatusREST{store: &statusStore}
}
140 changes: 140 additions & 0 deletions federation/registry/cluster/etcd/etcd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copyright 2016 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 etcd

import (
"testing"

"k8s.io/kubernetes/federation/apis/federation"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/registry/generic"
"k8s.io/kubernetes/pkg/registry/registrytest"
"k8s.io/kubernetes/pkg/runtime"
etcdtesting "k8s.io/kubernetes/pkg/storage/etcd/testing"
)

func newStorage(t *testing.T) (*REST, *etcdtesting.EtcdTestServer) {
etcdStorage, server := registrytest.NewEtcdStorage(t, federation.GroupName)
restOptions := generic.RESTOptions{
Storage: etcdStorage,
Decorator: generic.UndecoratedStorage,
DeleteCollectionWorkers: 1}
storage, _ := NewREST(restOptions)
return storage, server
}

func validNewCluster() *federation.Cluster {
return &federation.Cluster{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"name": "foo",
},
},
Spec: federation.ClusterSpec{
ServerAddressByClientCIDRs: []unversioned.ServerAddressByClientCIDR{
{
ClientCIDR: "0.0.0.0/0",
ServerAddress: "localhost:8888",
},
},
},
Status: federation.ClusterStatus{
Phase: federation.ClusterPending,
},
}
}

func TestCreate(t *testing.T) {
storage, server := newStorage(t)
defer server.Terminate(t)
test := registrytest.New(t, storage.Store).ClusterScope()
cluster := validNewCluster()
cluster.ObjectMeta = api.ObjectMeta{GenerateName: "foo"}
test.TestCreate(
cluster,
&federation.Cluster{
ObjectMeta: api.ObjectMeta{Name: "-a123-a_"},
},
)
}

func TestUpdate(t *testing.T) {
storage, server := newStorage(t)
defer server.Terminate(t)
test := registrytest.New(t, storage.Store).ClusterScope()
test.TestUpdate(
// valid
validNewCluster(),
// updateFunc
func(obj runtime.Object) runtime.Object {
object := obj.(*federation.Cluster)
object.Spec.Credential = "bar"
return object
},
)
}

func TestDelete(t *testing.T) {
storage, server := newStorage(t)
defer server.Terminate(t)
test := registrytest.New(t, storage.Store).ClusterScope().ReturnDeletedObject()
test.TestDelete(validNewCluster())
}

func TestGet(t *testing.T) {
storage, server := newStorage(t)
defer server.Terminate(t)
test := registrytest.New(t, storage.Store).ClusterScope()
test.TestGet(validNewCluster())
}

func TestList(t *testing.T) {
storage, server := newStorage(t)
defer server.Terminate(t)
test := registrytest.New(t, storage.Store).ClusterScope()
test.TestList(validNewCluster())
}

func TestWatch(t *testing.T) {
storage, server := newStorage(t)
defer server.Terminate(t)
test := registrytest.New(t, storage.Store).ClusterScope()
test.TestWatch(
validNewCluster(),
// matching labels
[]labels.Set{
{"name": "foo"},
},
// not matching labels
[]labels.Set{
{"name": "bar"},
{"foo": "bar"},
},
// matching fields
[]fields.Set{
{"metadata.name": "foo"},
},
// not matching fields
[]fields.Set{
{"metadata.name": "bar"},
},
)
}
81 changes: 81 additions & 0 deletions federation/registry/cluster/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2016 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 cluster

import (
"k8s.io/kubernetes/federation/apis/federation"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/watch"
)

// Registry is an interface implemented by things that know how to store Cluster objects.
type Registry interface {
ListClusters(ctx api.Context, options *api.ListOptions) (*federation.ClusterList, error)
WatchCluster(ctx api.Context, options *api.ListOptions) (watch.Interface, error)
GetCluster(ctx api.Context, name string) (*federation.Cluster, error)
CreateCluster(ctx api.Context, cluster *federation.Cluster) error
UpdateCluster(ctx api.Context, cluster *federation.Cluster) error
DeleteCluster(ctx api.Context, name string) error
}

// storage puts strong typing around storage calls

type storage struct {
rest.StandardStorage
}

// NewRegistry returns a new Registry interface for the given Storage. Any mismatched
// types will panic.
func NewRegistry(s rest.StandardStorage) Registry {
return &storage{s}
}

func (s *storage) ListClusters(ctx api.Context, options *api.ListOptions) (*federation.ClusterList, error) {
obj, err := s.List(ctx, options)
if err != nil {
return nil, err
}
return obj.(*federation.ClusterList), nil
}

func (s *storage) WatchCluster(ctx api.Context, options *api.ListOptions) (watch.Interface, error) {
return s.Watch(ctx, options)
}

func (s *storage) GetCluster(ctx api.Context, name string) (*federation.Cluster, error) {
obj, err := s.Get(ctx, name)
if err != nil {
return nil, err
}
return obj.(*federation.Cluster), nil
}

func (s *storage) CreateCluster(ctx api.Context, cluster *federation.Cluster) error {
_, err := s.Create(ctx, cluster)
return err
}

func (s *storage) UpdateCluster(ctx api.Context, cluster *federation.Cluster) error {
_, _, err := s.Update(ctx, cluster)
return err
}

func (s *storage) DeleteCluster(ctx api.Context, name string) error {
_, err := s.Delete(ctx, name, nil)
return err
}
Loading

0 comments on commit 1c1ad1a

Please sign in to comment.