PV & PVC Client implementation

This commit is contained in:
markturansky
2015-03-26 15:50:36 -04:00
parent c048e6fcbf
commit 95bd170ca2
38 changed files with 2058 additions and 30 deletions

View File

@@ -0,0 +1,17 @@
/*
Copyright 2014 Google Inc. 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 persistentvolumeclaim

View File

@@ -0,0 +1,63 @@
/*
Copyright 2015 Google Inc. 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 (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
etcdgeneric "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic/etcd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/persistentvolumeclaim"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
)
// rest implements a RESTStorage for persistentvolumeclaims against etcd
type REST struct {
*etcdgeneric.Etcd
}
// NewREST returns a RESTStorage object that will work against PersistentVolumeClaim objects.
func NewStorage(h tools.EtcdHelper) *REST {
prefix := "/registry/persistentvolumeclaims"
store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.PersistentVolumeClaim{} },
NewListFunc: func() runtime.Object { return &api.PersistentVolumeClaimList{} },
KeyRootFunc: func(ctx api.Context) string {
return etcdgeneric.NamespaceKeyRootFunc(ctx, prefix)
},
KeyFunc: func(ctx api.Context, name string) (string, error) {
return etcdgeneric.NamespaceKeyFunc(ctx, prefix, name)
},
ObjectNameFunc: func(obj runtime.Object) (string, error) {
return obj.(*api.PersistentVolumeClaim).Name, nil
},
PredicateFunc: func(label labels.Selector, field fields.Selector) generic.Matcher {
return persistentvolumeclaim.MatchPersistentVolumeClaim(label, field)
},
EndpointName: "persistentvolumeclaims",
Helper: h,
}
store.CreateStrategy = persistentvolumeclaim.Strategy
store.UpdateStrategy = persistentvolumeclaim.Strategy
store.ReturnDeletedObject = true
return &REST{store}
}

View File

@@ -0,0 +1,334 @@
/*
Copyright 2015 Google Inc. 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"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest/resttest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/coreos/go-etcd/etcd"
)
type testRegistry struct {
*registrytest.GenericRegistry
}
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient, tools.EtcdHelper) {
fakeEtcdClient := tools.NewFakeEtcdClient(t)
fakeEtcdClient.TestIndex = true
helper := tools.NewEtcdHelper(fakeEtcdClient, latest.Codec)
storage := NewStorage(helper)
return storage, fakeEtcdClient, helper
}
func validNewPersistentVolumeClaim(name, ns string) *api.PersistentVolumeClaim {
pv := &api.PersistentVolumeClaim{
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: api.PersistentVolumeClaimSpec{
AccessModes: []api.AccessModeType{api.ReadWriteOnce},
Resources: api.ResourceRequirements{
Requests: api.ResourceList{
api.ResourceName(api.ResourceStorage): resource.MustParse("10G"),
},
},
},
}
return pv
}
func validChangedPersistentVolumeClaim() *api.PersistentVolumeClaim {
pv := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
pv.ResourceVersion = "1"
return pv
}
func TestCreate(t *testing.T) {
registry, fakeEtcdClient, _ := newStorage(t)
test := resttest.New(t, registry, fakeEtcdClient.SetError)
pv := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
pv.ObjectMeta = api.ObjectMeta{}
test.TestCreate(
// valid
pv,
// invalid
&api.PersistentVolumeClaim{
ObjectMeta: api.ObjectMeta{Name: "*BadName!"},
},
)
}
func TestDelete2(t *testing.T) {
ctx := api.NewDefaultContext()
storage, fakeEtcdClient, _ := newStorage(t)
test := resttest.New(t, storage, fakeEtcdClient.SetError)
pv := validChangedPersistentVolumeClaim()
key, _ := storage.KeyFunc(ctx, pv.Name)
createFn := func() runtime.Object {
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(latest.Codec, pv),
ModifiedIndex: 1,
},
},
}
return pv
}
gracefulSetFn := func() bool {
if fakeEtcdClient.Data[key].R.Node == nil {
return false
}
return fakeEtcdClient.Data[key].R.Node.TTL == 30
}
test.TestDeleteNoGraceful(createFn, gracefulSetFn)
}
func TestDelete(t *testing.T) {
ctx := api.NewDefaultContext()
registry, fakeEtcdClient, _ := newStorage(t)
test := resttest.New(t, registry, fakeEtcdClient.SetError)
pvc := validChangedPersistentVolumeClaim()
key, _ := registry.KeyFunc(ctx, pvc.Name)
createFn := func() runtime.Object {
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(latest.Codec, pvc),
ModifiedIndex: 1,
},
},
}
return pvc
}
gracefulSetFn := func() bool {
if fakeEtcdClient.Data[key].R.Node == nil {
return false
}
return fakeEtcdClient.Data[key].R.Node.TTL == 30
}
test.TestDeleteNoGraceful(createFn, gracefulSetFn)
}
func TestEtcdListPersistentVolumeClaims(t *testing.T) {
ctx := api.NewDefaultContext()
registry, fakeClient, _ := newStorage(t)
key := registry.KeyRootFunc(ctx)
fakeClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Nodes: []*etcd.Node{
{
Value: runtime.EncodeOrDie(latest.Codec, validNewPersistentVolumeClaim("foo", api.NamespaceDefault)),
},
{
Value: runtime.EncodeOrDie(latest.Codec, validNewPersistentVolumeClaim("bar", api.NamespaceDefault)),
},
},
},
},
E: nil,
}
pvObj, err := registry.List(ctx, labels.Everything(), fields.Everything())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
pvs := pvObj.(*api.PersistentVolumeClaimList)
if len(pvs.Items) != 2 || pvs.Items[0].Name != "foo" || pvs.Items[1].Name != "bar" {
t.Errorf("Unexpected persistentVolume list: %#v", pvs)
}
}
func TestEtcdGetPersistentVolumeClaims(t *testing.T) {
ctx := api.NewDefaultContext()
registry, fakeClient, _ := newStorage(t)
persistentVolume := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
name := persistentVolume.Name
key, _ := registry.KeyFunc(ctx, name)
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, persistentVolume), 0)
response, err := fakeClient.Get(key, false, false)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
var persistentVolumeOut api.PersistentVolumeClaim
err = latest.Codec.DecodeInto([]byte(response.Node.Value), &persistentVolumeOut)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
obj, err := registry.Get(ctx, name)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
got := obj.(*api.PersistentVolumeClaim)
persistentVolume.ObjectMeta.ResourceVersion = got.ObjectMeta.ResourceVersion
if e, a := persistentVolume, got; !api.Semantic.DeepEqual(*e, *a) {
t.Errorf("Unexpected persistentVolume: %#v, expected %#v", e, a)
}
}
func TestListEmptyPersistentVolumeClaimsList(t *testing.T) {
ctx := api.NewDefaultContext()
registry, fakeClient, _ := newStorage(t)
fakeClient.ChangeIndex = 1
key := registry.KeyRootFunc(ctx)
fakeClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{},
E: fakeClient.NewError(tools.EtcdErrorCodeNotFound),
}
persistentVolume, err := registry.List(ctx, labels.Everything(), fields.Everything())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(persistentVolume.(*api.PersistentVolumeClaimList).Items) != 0 {
t.Errorf("Unexpected non-zero pod list: %#v", persistentVolume)
}
if persistentVolume.(*api.PersistentVolumeClaimList).ResourceVersion != "1" {
t.Errorf("Unexpected resource version: %#v", persistentVolume)
}
}
func TestListPersistentVolumeClaimsList(t *testing.T) {
ctx := api.NewDefaultContext()
registry, fakeClient, _ := newStorage(t)
fakeClient.ChangeIndex = 1
key := registry.KeyRootFunc(ctx)
fakeClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Nodes: []*etcd.Node{
{
Value: runtime.EncodeOrDie(latest.Codec, &api.PersistentVolumeClaim{
ObjectMeta: api.ObjectMeta{Name: "foo"},
}),
},
{
Value: runtime.EncodeOrDie(latest.Codec, &api.PersistentVolumeClaim{
ObjectMeta: api.ObjectMeta{Name: "bar"},
}),
},
},
},
},
}
persistentVolumeObj, err := registry.List(ctx, labels.Everything(), fields.Everything())
persistentVolumeList := persistentVolumeObj.(*api.PersistentVolumeClaimList)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(persistentVolumeList.Items) != 2 {
t.Errorf("Unexpected persistentVolume list: %#v", persistentVolumeList)
}
if persistentVolumeList.Items[0].Name != "foo" {
t.Errorf("Unexpected persistentVolume: %#v", persistentVolumeList.Items[0])
}
if persistentVolumeList.Items[1].Name != "bar" {
t.Errorf("Unexpected persistentVolume: %#v", persistentVolumeList.Items[1])
}
}
func TestPersistentVolumeClaimsDecode(t *testing.T) {
registry, _, _ := newStorage(t)
expected := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
body, err := latest.Codec.Encode(expected)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
actual := registry.New()
if err := latest.Codec.DecodeInto(body, actual); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !api.Semantic.DeepEqual(expected, actual) {
t.Errorf("mismatch: %s", util.ObjectDiff(expected, actual))
}
}
func TestEtcdUpdatePersistentVolumeClaims(t *testing.T) {
ctx := api.NewDefaultContext()
registry, fakeClient, _ := newStorage(t)
persistentVolume := validChangedPersistentVolumeClaim()
key, _ := registry.KeyFunc(ctx, "foo")
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, validNewPersistentVolumeClaim("foo", api.NamespaceDefault)), 0)
_, _, err := registry.Update(ctx, persistentVolume)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
response, err := fakeClient.Get(key, false, false)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
var persistentVolumeOut api.PersistentVolumeClaim
err = latest.Codec.DecodeInto([]byte(response.Node.Value), &persistentVolumeOut)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
persistentVolume.ObjectMeta.ResourceVersion = persistentVolumeOut.ObjectMeta.ResourceVersion
if !api.Semantic.DeepEqual(persistentVolume, &persistentVolumeOut) {
t.Errorf("Unexpected persistentVolume: %#v, expected %#v", &persistentVolumeOut, persistentVolume)
}
}
func TestDeletePersistentVolumeClaims(t *testing.T) {
ctx := api.NewDefaultContext()
registry, fakeClient, _ := newStorage(t)
persistentVolume := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
name := persistentVolume.Name
key, _ := registry.KeyFunc(ctx, name)
fakeClient.ChangeIndex = 1
fakeClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(latest.Codec, persistentVolume),
ModifiedIndex: 1,
CreatedIndex: 1,
},
},
}
_, err := registry.Delete(ctx, name, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

View File

@@ -0,0 +1,87 @@
/*
Copyright 2015 Google Inc. 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 persistentvolumeclaim
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
// Registry is an interface implemented by things that know how to store PersistentVolumeClaim objects.
type Registry interface {
// ListPersistentVolumeClaims obtains a list of PVCs having labels which match selector.
ListPersistentVolumeClaims(ctx api.Context, selector labels.Selector) (*api.PersistentVolumeClaimList, error)
// Watch for new/changed/deleted PVCs
WatchPersistentVolumeClaims(ctx api.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error)
// Get a specific PVC
GetPersistentVolumeClaim(ctx api.Context, pvcID string) (*api.PersistentVolumeClaim, error)
// Create a PVC based on a specification.
CreatePersistentVolumeClaim(ctx api.Context, pvc *api.PersistentVolumeClaim) error
// Update an existing PVC
UpdatePersistentVolumeClaim(ctx api.Context, pvc *api.PersistentVolumeClaim) error
// Delete an existing PVC
DeletePersistentVolumeClaim(ctx api.Context, pvcID 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) ListPersistentVolumeClaims(ctx api.Context, label labels.Selector) (*api.PersistentVolumeClaimList, error) {
obj, err := s.List(ctx, label, fields.Everything())
if err != nil {
return nil, err
}
return obj.(*api.PersistentVolumeClaimList), nil
}
func (s *storage) WatchPersistentVolumeClaims(ctx api.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
return s.Watch(ctx, label, field, resourceVersion)
}
func (s *storage) GetPersistentVolumeClaim(ctx api.Context, podID string) (*api.PersistentVolumeClaim, error) {
obj, err := s.Get(ctx, podID)
if err != nil {
return nil, err
}
return obj.(*api.PersistentVolumeClaim), nil
}
func (s *storage) CreatePersistentVolumeClaim(ctx api.Context, pod *api.PersistentVolumeClaim) error {
_, err := s.Create(ctx, pod)
return err
}
func (s *storage) UpdatePersistentVolumeClaim(ctx api.Context, pod *api.PersistentVolumeClaim) error {
_, _, err := s.Update(ctx, pod)
return err
}
func (s *storage) DeletePersistentVolumeClaim(ctx api.Context, podID string) error {
_, err := s.Delete(ctx, podID, nil)
return err
}

View File

@@ -0,0 +1,109 @@
/*
Copyright 2014 Google Inc. 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 persistentvolumeclaim
import (
"fmt"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
)
// persistentvolumeclaimStrategy implements behavior for PersistentVolumeClaim objects
type persistentvolumeclaimStrategy struct {
runtime.ObjectTyper
api.NameGenerator
}
// Strategy is the default logic that applies when creating and updating PersistentVolumeClaim
// objects via the REST API.
var Strategy = persistentvolumeclaimStrategy{api.Scheme, api.SimpleNameGenerator}
// NamespaceScoped is true for persistentvolumeclaims.
func (persistentvolumeclaimStrategy) NamespaceScoped() bool {
return true
}
// ResetBeforeCreate clears fields that are not allowed to be set by end users on creation.
func (persistentvolumeclaimStrategy) PrepareForCreate(obj runtime.Object) {
pv := obj.(*api.PersistentVolumeClaim)
pv.Status = api.PersistentVolumeClaimStatus{}
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (persistentvolumeclaimStrategy) PrepareForUpdate(obj, old runtime.Object) {
newPvc := obj.(*api.PersistentVolumeClaim)
oldPvc := obj.(*api.PersistentVolumeClaim)
newPvc.Status = oldPvc.Status
}
// Validate validates a new persistentvolumeclaim.
func (persistentvolumeclaimStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
pvc := obj.(*api.PersistentVolumeClaim)
return validation.ValidatePersistentVolumeClaim(pvc)
}
// AllowCreateOnUpdate is false for persistentvolumeclaims.
func (persistentvolumeclaimStrategy) AllowCreateOnUpdate() bool {
return false
}
// ValidateUpdate is the default update validation for an end user.
func (persistentvolumeclaimStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList {
return validation.ValidatePersistentVolumeClaimUpdate(obj.(*api.PersistentVolumeClaim), old.(*api.PersistentVolumeClaim))
}
type persistentvolumeclaimStatusStrategy struct {
persistentvolumeclaimStrategy
}
var StatusStrategy = persistentvolumeclaimStatusStrategy{Strategy}
func (persistentvolumeclaimStatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
newPvc := obj.(*api.PersistentVolumeClaim)
oldPvc := obj.(*api.PersistentVolumeClaim)
newPvc.Spec = oldPvc.Spec
}
func (persistentvolumeclaimStatusStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList {
return validation.ValidatePersistentVolumeClaimStatusUpdate(obj.(*api.PersistentVolumeClaim), old.(*api.PersistentVolumeClaim))
}
// MatchPersistentVolumeClaim returns a generic matcher for a given label and field selector.
func MatchPersistentVolumeClaim(label labels.Selector, field fields.Selector) generic.Matcher {
return generic.MatcherFunc(func(obj runtime.Object) (bool, error) {
persistentvolumeclaimObj, ok := obj.(*api.PersistentVolumeClaim)
if !ok {
return false, fmt.Errorf("not a persistentvolumeclaim")
}
fields := PersistentVolumeClaimToSelectableFields(persistentvolumeclaimObj)
return label.Matches(labels.Set(persistentvolumeclaimObj.Labels)) && field.Matches(fields), nil
})
}
// PersistentVolumeClaimToSelectableFields returns a label set that represents the object
// TODO: fields are not labels, and the validation rules for them do not apply.
func PersistentVolumeClaimToSelectableFields(persistentvolumeclaim *api.PersistentVolumeClaim) labels.Set {
return labels.Set{
"name": persistentvolumeclaim.Name,
}
}