mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-16 22:53:22 +00:00
Breakup the registry package into separate packages.
Currently all registry implementations live in a single package, which makes it bit harder to maintain. The different registry implementations do not follow the same coding style and naming conventions, which makes the code harder to read. Breakup the registry package into smaller packages based on the registry implementation. Refactor the registry packages to follow a similar coding style and naming convention. This patch does not introduce any changes in behavior.
This commit is contained in:
117
pkg/registry/minion/caching_registry.go
Normal file
117
pkg/registry/minion/caching_registry.go
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
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 minion
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Clock interface {
|
||||
Now() time.Time
|
||||
}
|
||||
|
||||
type SystemClock struct{}
|
||||
|
||||
func (SystemClock) Now() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
type CachingRegistry struct {
|
||||
delegate Registry
|
||||
ttl time.Duration
|
||||
minions []string
|
||||
lastUpdate int64
|
||||
lock sync.RWMutex
|
||||
clock Clock
|
||||
}
|
||||
|
||||
func NewCachingRegistry(delegate Registry, ttl time.Duration) (Registry, error) {
|
||||
list, err := delegate.List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &CachingRegistry{
|
||||
delegate: delegate,
|
||||
ttl: ttl,
|
||||
minions: list,
|
||||
lastUpdate: time.Now().Unix(),
|
||||
clock: SystemClock{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *CachingRegistry) Contains(minion string) (bool, error) {
|
||||
if r.expired() {
|
||||
if err := r.refresh(false); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
// block updates in the middle of a contains.
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
for _, name := range r.minions {
|
||||
if name == minion {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (r *CachingRegistry) Delete(minion string) error {
|
||||
if err := r.delegate.Delete(minion); err != nil {
|
||||
return err
|
||||
}
|
||||
return r.refresh(true)
|
||||
}
|
||||
|
||||
func (r *CachingRegistry) Insert(minion string) error {
|
||||
if err := r.delegate.Insert(minion); err != nil {
|
||||
return err
|
||||
}
|
||||
return r.refresh(true)
|
||||
}
|
||||
|
||||
func (r *CachingRegistry) List() ([]string, error) {
|
||||
if r.expired() {
|
||||
if err := r.refresh(false); err != nil {
|
||||
return r.minions, err
|
||||
}
|
||||
}
|
||||
return r.minions, nil
|
||||
}
|
||||
|
||||
func (r *CachingRegistry) expired() bool {
|
||||
var unix int64
|
||||
atomic.SwapInt64(&unix, r.lastUpdate)
|
||||
return r.clock.Now().Sub(time.Unix(r.lastUpdate, 0)) > r.ttl
|
||||
}
|
||||
|
||||
// refresh updates the current store. It double checks expired under lock with the assumption
|
||||
// of optimistic concurrency with the other functions.
|
||||
func (r *CachingRegistry) refresh(force bool) error {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
if force || r.expired() {
|
||||
var err error
|
||||
r.minions, err = r.delegate.List()
|
||||
time := r.clock.Now()
|
||||
atomic.SwapInt64(&r.lastUpdate, time.Unix())
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
130
pkg/registry/minion/caching_registry_test.go
Normal file
130
pkg/registry/minion/caching_registry_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
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 minion
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
||||
)
|
||||
|
||||
type fakeClock struct {
|
||||
now time.Time
|
||||
}
|
||||
|
||||
func (f *fakeClock) Now() time.Time {
|
||||
return f.now
|
||||
}
|
||||
|
||||
func TestCachingHit(t *testing.T) {
|
||||
fakeClock := fakeClock{
|
||||
now: time.Unix(0, 0),
|
||||
}
|
||||
fakeRegistry := registrytest.NewMinionRegistry([]string{"m1", "m2"})
|
||||
expected := []string{"m1", "m2", "m3"}
|
||||
cache := CachingRegistry{
|
||||
delegate: fakeRegistry,
|
||||
ttl: 1 * time.Second,
|
||||
clock: &fakeClock,
|
||||
lastUpdate: fakeClock.Now().Unix(),
|
||||
minions: expected,
|
||||
}
|
||||
list, err := cache.List()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(list, expected) {
|
||||
t.Errorf("expected: %v, got %v", expected, list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachingMiss(t *testing.T) {
|
||||
fakeClock := fakeClock{
|
||||
now: time.Unix(0, 0),
|
||||
}
|
||||
fakeRegistry := registrytest.NewMinionRegistry([]string{"m1", "m2"})
|
||||
expected := []string{"m1", "m2", "m3"}
|
||||
cache := CachingRegistry{
|
||||
delegate: fakeRegistry,
|
||||
ttl: 1 * time.Second,
|
||||
clock: &fakeClock,
|
||||
lastUpdate: fakeClock.Now().Unix(),
|
||||
minions: expected,
|
||||
}
|
||||
fakeClock.now = time.Unix(3, 0)
|
||||
list, err := cache.List()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(list, fakeRegistry.Minions) {
|
||||
t.Errorf("expected: %v, got %v", fakeRegistry.Minions, list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachingInsert(t *testing.T) {
|
||||
fakeClock := fakeClock{
|
||||
now: time.Unix(0, 0),
|
||||
}
|
||||
fakeRegistry := registrytest.NewMinionRegistry([]string{"m1", "m2"})
|
||||
expected := []string{"m1", "m2", "m3"}
|
||||
cache := CachingRegistry{
|
||||
delegate: fakeRegistry,
|
||||
ttl: 1 * time.Second,
|
||||
clock: &fakeClock,
|
||||
lastUpdate: fakeClock.Now().Unix(),
|
||||
minions: expected,
|
||||
}
|
||||
err := cache.Insert("foo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
list, err := cache.List()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(list, fakeRegistry.Minions) {
|
||||
t.Errorf("expected: %v, got %v", fakeRegistry.Minions, list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachingDelete(t *testing.T) {
|
||||
fakeClock := fakeClock{
|
||||
now: time.Unix(0, 0),
|
||||
}
|
||||
fakeRegistry := registrytest.NewMinionRegistry([]string{"m1", "m2"})
|
||||
expected := []string{"m1", "m2", "m3"}
|
||||
cache := CachingRegistry{
|
||||
delegate: fakeRegistry,
|
||||
ttl: 1 * time.Second,
|
||||
clock: &fakeClock,
|
||||
lastUpdate: fakeClock.Now().Unix(),
|
||||
minions: expected,
|
||||
}
|
||||
err := cache.Delete("m2")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
list, err := cache.List()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(list, fakeRegistry.Minions) {
|
||||
t.Errorf("expected: %v, got %v", fakeRegistry.Minions, list)
|
||||
}
|
||||
}
|
64
pkg/registry/minion/cloud_registry.go
Normal file
64
pkg/registry/minion/cloud_registry.go
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
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 minion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
||||
)
|
||||
|
||||
type CloudRegistry struct {
|
||||
cloud cloudprovider.Interface
|
||||
matchRE string
|
||||
}
|
||||
|
||||
func NewCloudRegistry(cloud cloudprovider.Interface, matchRE string) (*CloudRegistry, error) {
|
||||
return &CloudRegistry{
|
||||
cloud: cloud,
|
||||
matchRE: matchRE,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *CloudRegistry) Contains(minion string) (bool, error) {
|
||||
instances, err := r.List()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, name := range instances {
|
||||
if name == minion {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (r CloudRegistry) Delete(minion string) error {
|
||||
return fmt.Errorf("unsupported")
|
||||
}
|
||||
|
||||
func (r CloudRegistry) Insert(minion string) error {
|
||||
return fmt.Errorf("unsupported")
|
||||
}
|
||||
|
||||
func (r *CloudRegistry) List() ([]string, error) {
|
||||
instances, ok := r.cloud.Instances()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cloud doesn't support instances")
|
||||
}
|
||||
return instances.List(r.matchRE)
|
||||
}
|
94
pkg/registry/minion/cloud_registry_test.go
Normal file
94
pkg/registry/minion/cloud_registry_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
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 minion
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
||||
)
|
||||
|
||||
func TestCloudList(t *testing.T) {
|
||||
instances := []string{"m1", "m2"}
|
||||
fakeCloud := cloudprovider.FakeCloud{
|
||||
Machines: instances,
|
||||
}
|
||||
registry, err := NewCloudRegistry(&fakeCloud, ".*")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
list, err := registry.List()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(list, instances) {
|
||||
t.Errorf("Unexpected inequality: %#v, %#v", list, instances)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloudContains(t *testing.T) {
|
||||
instances := []string{"m1", "m2"}
|
||||
fakeCloud := cloudprovider.FakeCloud{
|
||||
Machines: instances,
|
||||
}
|
||||
registry, err := NewCloudRegistry(&fakeCloud, ".*")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
contains, err := registry.Contains("m1")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if !contains {
|
||||
t.Errorf("Unexpected !contains")
|
||||
}
|
||||
|
||||
contains, err = registry.Contains("m100")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if contains {
|
||||
t.Errorf("Unexpected contains")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloudListRegexp(t *testing.T) {
|
||||
instances := []string{"m1", "m2", "n1", "n2"}
|
||||
fakeCloud := cloudprovider.FakeCloud{
|
||||
Machines: instances,
|
||||
}
|
||||
registry, err := NewCloudRegistry(&fakeCloud, "m[0-9]+")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
list, err := registry.List()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
expectedList := []string{"m1", "m2"}
|
||||
if !reflect.DeepEqual(list, expectedList) {
|
||||
t.Errorf("Unexpected inequality: %#v, %#v", list, expectedList)
|
||||
}
|
||||
}
|
89
pkg/registry/minion/healthy_registry.go
Normal file
89
pkg/registry/minion/healthy_registry.go
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
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 minion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/health"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
type HealthyRegistry struct {
|
||||
delegate Registry
|
||||
client health.HTTPGetInterface
|
||||
port int
|
||||
}
|
||||
|
||||
func NewHealthyRegistry(delegate Registry, client *http.Client) Registry {
|
||||
return &HealthyRegistry{
|
||||
delegate: delegate,
|
||||
client: client,
|
||||
port: 10250,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *HealthyRegistry) Contains(minion string) (bool, error) {
|
||||
contains, err := r.delegate.Contains(minion)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !contains {
|
||||
return false, nil
|
||||
}
|
||||
status, err := health.DoHTTPCheck(r.makeMinionURL(minion), r.client)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if status == health.Unhealthy {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (r *HealthyRegistry) Delete(minion string) error {
|
||||
return r.delegate.Delete(minion)
|
||||
}
|
||||
|
||||
func (r *HealthyRegistry) Insert(minion string) error {
|
||||
return r.delegate.Insert(minion)
|
||||
}
|
||||
|
||||
func (r *HealthyRegistry) List() (currentMinions []string, err error) {
|
||||
var result []string
|
||||
list, err := r.delegate.List()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
for _, minion := range list {
|
||||
status, err := health.DoHTTPCheck(r.makeMinionURL(minion), r.client)
|
||||
if err != nil {
|
||||
glog.Errorf("%s failed health check with error: %s", minion, err)
|
||||
continue
|
||||
}
|
||||
if status == health.Healthy {
|
||||
result = append(result, minion)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *HealthyRegistry) makeMinionURL(minion string) string {
|
||||
return fmt.Sprintf("http://%s:%d/healthz", minion, r.port)
|
||||
}
|
109
pkg/registry/minion/healthy_registry_test.go
Normal file
109
pkg/registry/minion/healthy_registry_test.go
Normal 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 minion
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
||||
)
|
||||
|
||||
type alwaysYes struct{}
|
||||
|
||||
func fakeHTTPResponse(status int) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: status,
|
||||
Body: ioutil.NopCloser(&bytes.Buffer{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (alwaysYes) Get(url string) (*http.Response, error) {
|
||||
return fakeHTTPResponse(http.StatusOK), nil
|
||||
}
|
||||
|
||||
func TestBasicDelegation(t *testing.T) {
|
||||
mockMinionRegistry := registrytest.NewMinionRegistry([]string{"m1", "m2", "m3"})
|
||||
healthy := HealthyRegistry{
|
||||
delegate: mockMinionRegistry,
|
||||
client: alwaysYes{},
|
||||
}
|
||||
list, err := healthy.List()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(list, mockMinionRegistry.Minions) {
|
||||
t.Errorf("Expected %v, Got %v", mockMinionRegistry.Minions, list)
|
||||
}
|
||||
err = healthy.Insert("foo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
ok, err := healthy.Contains("m1")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Errorf("Unexpected absence of 'm1'")
|
||||
}
|
||||
ok, err = healthy.Contains("m5")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if ok {
|
||||
t.Errorf("Unexpected presence of 'm5'")
|
||||
}
|
||||
}
|
||||
|
||||
type notMinion struct {
|
||||
minion string
|
||||
}
|
||||
|
||||
func (n *notMinion) Get(url string) (*http.Response, error) {
|
||||
if url != "http://"+n.minion+":10250/healthz" {
|
||||
return fakeHTTPResponse(http.StatusOK), nil
|
||||
} else {
|
||||
return fakeHTTPResponse(http.StatusInternalServerError), nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestFiltering(t *testing.T) {
|
||||
mockMinionRegistry := registrytest.NewMinionRegistry([]string{"m1", "m2", "m3"})
|
||||
healthy := HealthyRegistry{
|
||||
delegate: mockMinionRegistry,
|
||||
client: ¬Minion{minion: "m1"},
|
||||
port: 10250,
|
||||
}
|
||||
expected := []string{"m2", "m3"}
|
||||
list, err := healthy.List()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(list, expected) {
|
||||
t.Errorf("Expected %v, Got %v", expected, list)
|
||||
}
|
||||
ok, err := healthy.Contains("m1")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if ok {
|
||||
t.Errorf("Unexpected presence of 'm1'")
|
||||
}
|
||||
}
|
81
pkg/registry/minion/minion.go
Normal file
81
pkg/registry/minion/minion.go
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
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 minion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
var ErrDoesNotExist = fmt.Errorf("The requested resource does not exist.")
|
||||
|
||||
// Keep track of a set of minions. Safe for concurrent reading/writing.
|
||||
type Registry interface {
|
||||
List() (currentMinions []string, err error)
|
||||
Insert(minion string) error
|
||||
Delete(minion string) error
|
||||
Contains(minion string) (bool, error)
|
||||
}
|
||||
|
||||
// Initialize a minion registry with a list of minions.
|
||||
func NewRegistry(minions []string) Registry {
|
||||
m := &minionList{
|
||||
minions: util.StringSet{},
|
||||
}
|
||||
for _, minion := range minions {
|
||||
m.minions.Insert(minion)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
type minionList struct {
|
||||
minions util.StringSet
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (m *minionList) Contains(minion string) (bool, error) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
return m.minions.Has(minion), nil
|
||||
}
|
||||
|
||||
func (m *minionList) Delete(minion string) error {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
m.minions.Delete(minion)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *minionList) Insert(newMinion string) error {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
m.minions.Insert(newMinion)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *minionList) List() (currentMinions []string, err error) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
for minion := range m.minions {
|
||||
currentMinions = append(currentMinions, minion)
|
||||
}
|
||||
sort.StringSlice(currentMinions).Sort()
|
||||
return
|
||||
}
|
54
pkg/registry/minion/minion_test.go
Normal file
54
pkg/registry/minion/minion_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
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 minion
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRegistry(t *testing.T) {
|
||||
m := NewRegistry([]string{"foo", "bar"})
|
||||
if has, err := m.Contains("foo"); !has || err != nil {
|
||||
t.Errorf("missing expected object")
|
||||
}
|
||||
if has, err := m.Contains("bar"); !has || err != nil {
|
||||
t.Errorf("missing expected object")
|
||||
}
|
||||
if has, err := m.Contains("baz"); has || err != nil {
|
||||
t.Errorf("has unexpected object")
|
||||
}
|
||||
if err := m.Insert("baz"); err != nil {
|
||||
t.Errorf("insert failed")
|
||||
}
|
||||
if has, err := m.Contains("baz"); !has || err != nil {
|
||||
t.Errorf("insert didn't actually insert")
|
||||
}
|
||||
if err := m.Delete("bar"); err != nil {
|
||||
t.Errorf("delete failed")
|
||||
}
|
||||
if has, err := m.Contains("bar"); has || err != nil {
|
||||
t.Errorf("delete didn't actually delete")
|
||||
}
|
||||
list, err := m.List()
|
||||
if err != nil {
|
||||
t.Errorf("got error calling List")
|
||||
}
|
||||
if !reflect.DeepEqual(list, []string{"baz", "foo"}) {
|
||||
t.Errorf("Unexpected list value: %#v", list)
|
||||
}
|
||||
}
|
106
pkg/registry/minion/storage.go
Normal file
106
pkg/registry/minion/storage.go
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
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 minion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
)
|
||||
|
||||
// RegistryStorage implements the RESTStorage interface, backed by a MinionRegistry.
|
||||
type RegistryStorage struct {
|
||||
registry Registry
|
||||
}
|
||||
|
||||
// NewRegistryStorage returns a new RegistryStorage.
|
||||
func NewRegistryStorage(m Registry) apiserver.RESTStorage {
|
||||
return &RegistryStorage{
|
||||
registry: m,
|
||||
}
|
||||
}
|
||||
|
||||
func (rs *RegistryStorage) Create(obj interface{}) (<-chan interface{}, error) {
|
||||
minion, ok := obj.(*api.Minion)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("not a minion: %#v", obj)
|
||||
}
|
||||
if minion.ID == "" {
|
||||
return nil, fmt.Errorf("ID should not be empty: %#v", minion)
|
||||
}
|
||||
return apiserver.MakeAsync(func() (interface{}, error) {
|
||||
err := rs.registry.Insert(minion.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
contains, err := rs.registry.Contains(minion.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if contains {
|
||||
return rs.toApiMinion(minion.ID), nil
|
||||
}
|
||||
return nil, fmt.Errorf("unable to add minion %#v", minion)
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (rs *RegistryStorage) Delete(id string) (<-chan interface{}, error) {
|
||||
exists, err := rs.registry.Contains(id)
|
||||
if !exists {
|
||||
return nil, ErrDoesNotExist
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return apiserver.MakeAsync(func() (interface{}, error) {
|
||||
return &api.Status{Status: api.StatusSuccess}, rs.registry.Delete(id)
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (rs *RegistryStorage) Get(id string) (interface{}, error) {
|
||||
exists, err := rs.registry.Contains(id)
|
||||
if !exists {
|
||||
return nil, ErrDoesNotExist
|
||||
}
|
||||
return rs.toApiMinion(id), err
|
||||
}
|
||||
|
||||
func (rs *RegistryStorage) List(selector labels.Selector) (interface{}, error) {
|
||||
nameList, err := rs.registry.List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var list api.MinionList
|
||||
for _, name := range nameList {
|
||||
list.Items = append(list.Items, rs.toApiMinion(name))
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (rs RegistryStorage) New() interface{} {
|
||||
return &api.Minion{}
|
||||
}
|
||||
|
||||
func (rs *RegistryStorage) Update(minion interface{}) (<-chan interface{}, error) {
|
||||
return nil, fmt.Errorf("Minions can only be created (inserted) and deleted.")
|
||||
}
|
||||
|
||||
func (rs *RegistryStorage) toApiMinion(name string) api.Minion {
|
||||
return api.Minion{JSONBase: api.JSONBase{ID: name}}
|
||||
}
|
84
pkg/registry/minion/storage_test.go
Normal file
84
pkg/registry/minion/storage_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
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 minion
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
)
|
||||
|
||||
func TestMinionRegistryStorage(t *testing.T) {
|
||||
m := NewRegistry([]string{"foo", "bar"})
|
||||
ms := NewRegistryStorage(m)
|
||||
|
||||
if obj, err := ms.Get("foo"); err != nil || obj.(api.Minion).ID != "foo" {
|
||||
t.Errorf("missing expected object")
|
||||
}
|
||||
if obj, err := ms.Get("bar"); err != nil || obj.(api.Minion).ID != "bar" {
|
||||
t.Errorf("missing expected object")
|
||||
}
|
||||
if _, err := ms.Get("baz"); err != ErrDoesNotExist {
|
||||
t.Errorf("has unexpected object")
|
||||
}
|
||||
|
||||
c, err := ms.Create(&api.Minion{JSONBase: api.JSONBase{ID: "baz"}})
|
||||
if err != nil {
|
||||
t.Errorf("insert failed")
|
||||
}
|
||||
obj := <-c
|
||||
if m, ok := obj.(api.Minion); !ok || m.ID != "baz" {
|
||||
t.Errorf("insert return value was weird: %#v", obj)
|
||||
}
|
||||
if obj, err := ms.Get("baz"); err != nil || obj.(api.Minion).ID != "baz" {
|
||||
t.Errorf("insert didn't actually insert")
|
||||
}
|
||||
|
||||
c, err = ms.Delete("bar")
|
||||
if err != nil {
|
||||
t.Errorf("delete failed")
|
||||
}
|
||||
obj = <-c
|
||||
if s, ok := obj.(*api.Status); !ok || s.Status != api.StatusSuccess {
|
||||
t.Errorf("delete return value was weird: %#v", obj)
|
||||
}
|
||||
if _, err := ms.Get("bar"); err != ErrDoesNotExist {
|
||||
t.Errorf("delete didn't actually delete")
|
||||
}
|
||||
|
||||
_, err = ms.Delete("bar")
|
||||
if err != ErrDoesNotExist {
|
||||
t.Errorf("delete returned wrong error")
|
||||
}
|
||||
|
||||
list, err := ms.List(labels.Everything())
|
||||
if err != nil {
|
||||
t.Errorf("got error calling List")
|
||||
}
|
||||
expect := []api.Minion{
|
||||
{
|
||||
JSONBase: api.JSONBase{ID: "baz"},
|
||||
}, {
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
},
|
||||
}
|
||||
if !reflect.DeepEqual(list.(api.MinionList).Items, expect) {
|
||||
t.Errorf("Unexpected list value: %#v", list)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user