forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes#730 from lavalamp/rename
Begin systemizing files in pkg/registry
- Loading branch information
Showing
12 changed files
with
147 additions
and
102 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
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 registry | ||
|
||
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 MinionRegistry 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 MakeMinionRegistry(minions []string) MinionRegistry { | ||
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) List() (currentMinions []string, err error) { | ||
m.lock.Lock() | ||
defer m.lock.Unlock() | ||
// Convert from map to []string | ||
for minion := range m.minions { | ||
currentMinions = append(currentMinions, minion) | ||
} | ||
sort.StringSlice(currentMinions).Sort() | ||
return | ||
} | ||
|
||
func (m *minionList) Insert(newMinion string) error { | ||
m.lock.Lock() | ||
defer m.lock.Unlock() | ||
m.minions.Insert(newMinion) | ||
return nil | ||
} | ||
|
||
func (m *minionList) Delete(minion string) error { | ||
m.lock.Lock() | ||
defer m.lock.Unlock() | ||
m.minions.Delete(minion) | ||
return nil | ||
} | ||
|
||
func (m *minionList) Contains(minion string) (bool, error) { | ||
m.lock.Lock() | ||
defer m.lock.Unlock() | ||
return m.minions.Has(minion), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
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 registry | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestMinionRegistry(t *testing.T) { | ||
m := MakeMinionRegistry([]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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters