Support node label update.

This commit is contained in:
Deyuan Deng 2014-11-15 16:32:59 -05:00
parent 37d2bab7ed
commit cc310e0e71
7 changed files with 85 additions and 3 deletions

View File

@ -581,6 +581,12 @@ func (r *Registry) CreateMinion(ctx api.Context, minion *api.Minion) error {
return etcderr.InterpretCreateError(err, "minion", minion.Name)
}
func (r *Registry) UpdateMinion(ctx api.Context, minion *api.Minion) error {
// TODO: Add some validations.
err := r.SetObj(makeMinionKey(minion.Name), minion)
return etcderr.InterpretUpdateError(err, "minion", minion.Name)
}
func (r *Registry) GetMinion(ctx api.Context, minionID string) (*api.Minion, error) {
var minion api.Minion
key := makeMinionKey(minionID)

View File

@ -62,6 +62,10 @@ func (r *HealthyRegistry) CreateMinion(ctx api.Context, minion *api.Minion) erro
return r.delegate.CreateMinion(ctx, minion)
}
func (r *HealthyRegistry) UpdateMinion(ctx api.Context, minion *api.Minion) error {
return r.delegate.UpdateMinion(ctx, minion)
}
func (r *HealthyRegistry) ListMinions(ctx api.Context) (currentMinions *api.MinionList, err error) {
result := &api.MinionList{}
list, err := r.delegate.ListMinions(ctx)

View File

@ -22,6 +22,7 @@ import "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
type Registry interface {
ListMinions(ctx api.Context) (*api.MinionList, error)
CreateMinion(ctx api.Context, minion *api.Minion) error
UpdateMinion(ctx api.Context, minion *api.Minion) error
GetMinion(ctx api.Context, minionID string) (*api.Minion, error)
DeleteMinion(ctx api.Context, minionID string) error
}

View File

@ -100,8 +100,23 @@ func (rs *REST) New() runtime.Object {
return &api.Minion{}
}
func (rs *REST) Update(ctx api.Context, minion runtime.Object) (<-chan apiserver.RESTResult, error) {
return nil, fmt.Errorf("Minions can only be created (inserted) and deleted.")
func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan apiserver.RESTResult, error) {
minion, ok := obj.(*api.Minion)
if !ok {
return nil, fmt.Errorf("not a minion: %#v", obj)
}
if errs := validation.ValidateMinion(minion); len(errs) > 0 {
return nil, kerrors.NewInvalid("minion", minion.Name, errs)
}
return apiserver.MakeAsync(func() (runtime.Object, error) {
err := rs.registry.UpdateMinion(ctx, minion)
if err != nil {
return nil, err
}
// TODO: GetMinion will health check the minion, but we shouldn't require the minion to be
// running for updating labels.
return rs.registry.GetMinion(ctx, minion.Name)
}), nil
}
func (rs *REST) toApiMinion(name string) *api.Minion {

View File

@ -17,6 +17,7 @@ limitations under the License.
package minion
import (
"reflect"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@ -119,6 +120,49 @@ func contains(nodes *api.MinionList, nodeID string) bool {
return false
}
func TestMinionRegistryUpdate(t *testing.T) {
minionRegistry := registrytest.NewMinionRegistry([]string{}, api.NodeResources{})
ms := NewREST(minionRegistry)
ctx := api.NewContext()
expected_labels := map[string]string{"foo": "bar"}
c, err := ms.Create(ctx, &api.Minion{
ObjectMeta: api.ObjectMeta{Name: "m1"},
Labels: expected_labels,
})
if err != nil {
t.Errorf("insert failed")
}
result := <-c
created, ok := result.Object.(*api.Minion)
if !ok || created.Name != "m1" {
t.Errorf("insert return value was weird: %#v", result)
}
if !reflect.DeepEqual(expected_labels, created.Labels) {
t.Errorf("unexpected labels: %#v", created.Labels)
}
update := new(api.Minion)
*update = *created
update_labels := map[string]string{"bar": "foo"}
update.Labels = update_labels
c, err = ms.Update(ctx, update)
if err != nil {
t.Errorf("update failed")
}
result = <-c
updated, ok := result.Object.(*api.Minion)
if !ok || updated.Name != "m1" {
t.Errorf("update return value was weird: %#v", result)
}
if !reflect.DeepEqual(update_labels, updated.Labels) {
t.Errorf("unexpected labels: %#v", updated.Labels)
}
}
func TestMinionStorageValidatesCreate(t *testing.T) {
storage := NewREST(registrytest.NewMinionRegistry([]string{"foo", "bar"}, api.NodeResources{}))
ctx := api.NewContext()

View File

@ -60,6 +60,18 @@ func (r *MinionRegistry) CreateMinion(ctx api.Context, minion *api.Minion) error
return r.Err
}
func (r *MinionRegistry) UpdateMinion(ctx api.Context, minion *api.Minion) error {
r.Lock()
defer r.Unlock()
for i, node := range r.Minions.Items {
if node.Name == minion.Name {
r.Minions.Items[i] = *minion
return r.Err
}
}
return r.Err
}
func (r *MinionRegistry) GetMinion(ctx api.Context, minionID string) (*api.Minion, error) {
r.Lock()
defer r.Unlock()

View File

@ -306,7 +306,7 @@ func getTestRequests() []struct {
// Normal methods on minions
{"GET", "/api/v1beta1/minions", "", code200},
{"POST", "/api/v1beta1/minions" + syncFlags, aMinion, code200},
{"PUT", "/api/v1beta1/minions/a" + syncFlags, aMinion, code500}, // See #2114 about why 500
{"PUT", "/api/v1beta1/minions/a" + syncFlags, aMinion, code409}, // See #2115 about why 409
{"GET", "/api/v1beta1/minions", "", code200},
{"GET", "/api/v1beta1/minions/a", "", code200},
{"DELETE", "/api/v1beta1/minions/a" + syncFlags, "", code200},