mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
Merge pull request #244 from brendandburns/minion
Add a minion registry that is backed by a cloud provider.
This commit is contained in:
commit
de06869d30
@ -38,4 +38,6 @@ type TCPLoadBalancer interface {
|
|||||||
|
|
||||||
type Instances interface {
|
type Instances interface {
|
||||||
IPAddress(name string) (net.IP, error)
|
IPAddress(name string) (net.IP, error)
|
||||||
|
// Lists instances that match 'filter' which is a regular expression which must match the entire instance name
|
||||||
|
List(filter string) ([]string, error)
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package cloudprovider
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakeCloud struct {
|
type FakeCloud struct {
|
||||||
@ -25,6 +26,7 @@ type FakeCloud struct {
|
|||||||
Err error
|
Err error
|
||||||
Calls []string
|
Calls []string
|
||||||
IP net.IP
|
IP net.IP
|
||||||
|
Machines []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeCloud) addCall(desc string) {
|
func (f *FakeCloud) addCall(desc string) {
|
||||||
@ -66,3 +68,14 @@ func (f *FakeCloud) IPAddress(instance string) (net.IP, error) {
|
|||||||
f.addCall("ip-address")
|
f.addCall("ip-address")
|
||||||
return f.IP, f.Err
|
return f.IP, f.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *FakeCloud) List(filter string) ([]string, error) {
|
||||||
|
f.addCall("list")
|
||||||
|
result := []string{}
|
||||||
|
for _, machine := range f.Machines {
|
||||||
|
if match, _ := regexp.MatchString(filter, machine); match {
|
||||||
|
result = append(result, machine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, f.Err
|
||||||
|
}
|
||||||
|
@ -33,6 +33,7 @@ type GCECloud struct {
|
|||||||
service *compute.Service
|
service *compute.Service
|
||||||
projectID string
|
projectID string
|
||||||
zone string
|
zone string
|
||||||
|
instanceRE string
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProjectAndZone() (string, string, error) {
|
func getProjectAndZone() (string, string, error) {
|
||||||
@ -179,3 +180,19 @@ func (gce *GCECloud) IPAddress(instance string) (net.IP, error) {
|
|||||||
}
|
}
|
||||||
return ip, nil
|
return ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gce *GCECloud) List(filter string) ([]string, error) {
|
||||||
|
listCall := gce.service.Instances.List(gce.projectID, gce.zone)
|
||||||
|
if len(filter) > 0 {
|
||||||
|
listCall = listCall.Filter("name eq " + filter)
|
||||||
|
}
|
||||||
|
res, err := listCall.Do()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var instances []string
|
||||||
|
for _, instance := range res.Items {
|
||||||
|
instances = append(instances, instance.Name)
|
||||||
|
}
|
||||||
|
return instances, nil
|
||||||
|
}
|
||||||
|
65
pkg/registry/cloud_minion_registry.go
Normal file
65
pkg/registry/cloud_minion_registry.go
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
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"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CloudMinionRegistry struct {
|
||||||
|
cloud cloudprovider.Interface
|
||||||
|
matchRE string
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeCloudMinionRegistry(cloud cloudprovider.Interface, matchRE string) (*CloudMinionRegistry, error) {
|
||||||
|
return &CloudMinionRegistry{
|
||||||
|
cloud: cloud,
|
||||||
|
matchRE: matchRE,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CloudMinionRegistry) List() ([]string, error) {
|
||||||
|
instances, ok := c.cloud.Instances()
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("cloud doesn't support instances")
|
||||||
|
}
|
||||||
|
|
||||||
|
return instances.List(c.matchRE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CloudMinionRegistry) Insert(minion string) error {
|
||||||
|
return fmt.Errorf("unsupported")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CloudMinionRegistry) Delete(minion string) error {
|
||||||
|
return fmt.Errorf("unsupported")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CloudMinionRegistry) Contains(minion string) (bool, error) {
|
||||||
|
instances, err := c.List()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
for _, name := range instances {
|
||||||
|
if name == minion {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
76
pkg/registry/cloud_minion_registry_test.go
Normal file
76
pkg/registry/cloud_minion_registry_test.go
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
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"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCloudList(t *testing.T) {
|
||||||
|
instances := []string{"m1", "m2"}
|
||||||
|
fakeCloud := cloudprovider.FakeCloud{
|
||||||
|
Machines: instances,
|
||||||
|
}
|
||||||
|
registry, err := MakeCloudMinionRegistry(&fakeCloud, ".*")
|
||||||
|
expectNoError(t, err)
|
||||||
|
|
||||||
|
list, err := registry.List()
|
||||||
|
expectNoError(t, 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 := MakeCloudMinionRegistry(&fakeCloud, ".*")
|
||||||
|
expectNoError(t, err)
|
||||||
|
|
||||||
|
contains, err := registry.Contains("m1")
|
||||||
|
expectNoError(t, err)
|
||||||
|
if !contains {
|
||||||
|
t.Errorf("Unexpected !contains")
|
||||||
|
}
|
||||||
|
|
||||||
|
contains, err = registry.Contains("m100")
|
||||||
|
expectNoError(t, 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 := MakeCloudMinionRegistry(&fakeCloud, "m[0-9]+")
|
||||||
|
expectNoError(t, err)
|
||||||
|
|
||||||
|
list, err := registry.List()
|
||||||
|
expectNoError(t, err)
|
||||||
|
expectedList := []string{"m1", "m2"}
|
||||||
|
if !reflect.DeepEqual(list, expectedList) {
|
||||||
|
t.Errorf("Unexpected inequality: %#v, %#v", list, expectedList)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user