mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 15:13:08 +00:00
Create MinionController to sync minions from cloudprovider (pkg cloudprovider/controller).
This commit is contained in:
19
pkg/cloudprovider/controller/doc.go
Normal file
19
pkg/cloudprovider/controller/doc.go
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
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 controller contains code for syncing cloud instances with
|
||||
// minion registry
|
||||
package controller
|
109
pkg/cloudprovider/controller/minioncontroller.go
Normal file
109
pkg/cloudprovider/controller/minioncontroller.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 controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
type MinionController struct {
|
||||
cloud cloudprovider.Interface
|
||||
matchRE string
|
||||
staticResources *api.NodeResources
|
||||
registry minion.Registry
|
||||
}
|
||||
|
||||
// NewMinionController returns a new minion controller to sync instances from cloudprovider.
|
||||
func NewMinionController(cloud cloudprovider.Interface, matchRE string, staticResources *api.NodeResources, registry minion.Registry) (*MinionController, error) {
|
||||
return &MinionController{
|
||||
cloud: cloud,
|
||||
matchRE: matchRE,
|
||||
staticResources: staticResources,
|
||||
registry: registry,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Sync syncs list of instances from cloudprovider to master etcd registry.
|
||||
func (s *MinionController) Sync() error {
|
||||
matches, err := s.cloudMinions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
minions, err := s.registry.ListMinions(nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
minionMap := make(map[string]*api.Minion)
|
||||
for _, minion := range minions.Items {
|
||||
minionMap[minion.ID] = &minion
|
||||
}
|
||||
|
||||
// Create or delete minions from registry.
|
||||
for _, match := range matches.Items {
|
||||
if _, ok := minionMap[match.ID]; !ok {
|
||||
glog.Infof("Create minion in registry: %s", match.ID)
|
||||
err = s.registry.CreateMinion(nil, &match)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
delete(minionMap, match.ID)
|
||||
}
|
||||
|
||||
for minionID := range minionMap {
|
||||
glog.Infof("Delete minion from registry: %s", minionID)
|
||||
err = s.registry.DeleteMinion(nil, minionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// cloudMinions constructs and returns api.MinionList from cloudprovider.
|
||||
func (s *MinionController) cloudMinions() (*api.MinionList, error) {
|
||||
instances, ok := s.cloud.Instances()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cloud doesn't support instances")
|
||||
}
|
||||
matches, err := instances.List(s.matchRE)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &api.MinionList{
|
||||
Items: make([]api.Minion, len(matches)),
|
||||
}
|
||||
for i := range matches {
|
||||
result.Items[i].ID = matches[i]
|
||||
resources, err := instances.GetNodeResources(matches[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resources == nil {
|
||||
resources = s.staticResources
|
||||
}
|
||||
if resources != nil {
|
||||
result.Items[i].NodeResources = *resources
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
183
pkg/cloudprovider/controller/minioncontroller_test.go
Normal file
183
pkg/cloudprovider/controller/minioncontroller_test.go
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
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 controller
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
||||
fake_cloud "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider/fake"
|
||||
etcdregistry "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/etcd"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/pod"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
||||
|
||||
etcd "github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
func NewTestEtcdRegistry(client tools.EtcdClient) *etcdregistry.Registry {
|
||||
registry := etcdregistry.NewRegistry(tools.EtcdHelper{client, latest.Codec, tools.RuntimeVersionAdapter{latest.ResourceVersioner}},
|
||||
&pod.BasicManifestFactory{
|
||||
ServiceRegistry: ®istrytest.ServiceRegistry{},
|
||||
})
|
||||
return registry
|
||||
}
|
||||
|
||||
func TestSyncCreateMinion(t *testing.T) {
|
||||
ctx := api.NewContext()
|
||||
fakeClient := tools.NewFakeEtcdClient(t)
|
||||
m1 := runtime.EncodeOrDie(latest.Codec, &api.Minion{TypeMeta: api.TypeMeta{ID: "m1"}})
|
||||
m2 := runtime.EncodeOrDie(latest.Codec, &api.Minion{TypeMeta: api.TypeMeta{ID: "m2"}})
|
||||
fakeClient.Set("/registry/minions/m1", m1, 0)
|
||||
fakeClient.Set("/registry/minions/m2", m2, 0)
|
||||
fakeClient.ExpectNotFoundGet("/registry/minions/m3")
|
||||
fakeClient.Data["/registry/minions"] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Nodes: []*etcd.Node{
|
||||
{Value: m1},
|
||||
{Value: m2},
|
||||
},
|
||||
},
|
||||
},
|
||||
E: nil,
|
||||
}
|
||||
|
||||
registry := NewTestEtcdRegistry(fakeClient)
|
||||
instances := []string{"m1", "m2", "m3"}
|
||||
fakeCloud := fake_cloud.FakeCloud{
|
||||
Machines: instances,
|
||||
}
|
||||
minionController, err := NewMinionController(&fakeCloud, ".*", nil, registry)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error")
|
||||
}
|
||||
|
||||
minion, err := registry.GetMinion(ctx, "m3")
|
||||
if minion != nil {
|
||||
t.Errorf("Unexpected contains")
|
||||
}
|
||||
|
||||
err = minionController.Sync()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
minion, err = registry.GetMinion(ctx, "m3")
|
||||
if minion == nil {
|
||||
t.Errorf("Unexpected !contains")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncDeleteMinion(t *testing.T) {
|
||||
ctx := api.NewContext()
|
||||
fakeClient := tools.NewFakeEtcdClient(t)
|
||||
m1 := runtime.EncodeOrDie(latest.Codec, &api.Minion{TypeMeta: api.TypeMeta{ID: "m1"}})
|
||||
m2 := runtime.EncodeOrDie(latest.Codec, &api.Minion{TypeMeta: api.TypeMeta{ID: "m2"}})
|
||||
m3 := runtime.EncodeOrDie(latest.Codec, &api.Minion{TypeMeta: api.TypeMeta{ID: "m3"}})
|
||||
fakeClient.Set("/registry/minions/m1", m1, 0)
|
||||
fakeClient.Set("/registry/minions/m2", m2, 0)
|
||||
fakeClient.Set("/registry/minions/m3", m3, 0)
|
||||
fakeClient.Data["/registry/minions"] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Nodes: []*etcd.Node{
|
||||
{Value: m1},
|
||||
{Value: m2},
|
||||
{Value: m3},
|
||||
},
|
||||
},
|
||||
},
|
||||
E: nil,
|
||||
}
|
||||
|
||||
registry := NewTestEtcdRegistry(fakeClient)
|
||||
instances := []string{"m1", "m2"}
|
||||
fakeCloud := fake_cloud.FakeCloud{
|
||||
Machines: instances,
|
||||
}
|
||||
minionController, err := NewMinionController(&fakeCloud, ".*", nil, registry)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error")
|
||||
}
|
||||
|
||||
minion, err := registry.GetMinion(ctx, "m3")
|
||||
if minion == nil {
|
||||
t.Errorf("Unexpected !contains")
|
||||
}
|
||||
|
||||
err = minionController.Sync()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
minion, err = registry.GetMinion(ctx, "m3")
|
||||
if minion != nil {
|
||||
t.Errorf("Unexpected contains")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncMinionRegexp(t *testing.T) {
|
||||
ctx := api.NewContext()
|
||||
fakeClient := tools.NewFakeEtcdClient(t)
|
||||
fakeClient.Data["/registry/minions"] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Nodes: []*etcd.Node{},
|
||||
},
|
||||
},
|
||||
E: nil,
|
||||
}
|
||||
|
||||
registry := NewTestEtcdRegistry(fakeClient)
|
||||
instances := []string{"m1", "m2", "n1", "n2"}
|
||||
fakeCloud := fake_cloud.FakeCloud{
|
||||
Machines: instances,
|
||||
}
|
||||
minionController, err := NewMinionController(&fakeCloud, "m[0-9]+", nil, registry)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error")
|
||||
}
|
||||
|
||||
err = minionController.Sync()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
var minion *api.Minion
|
||||
fakeClient.ExpectNotFoundGet("/registry/minions/n1")
|
||||
fakeClient.ExpectNotFoundGet("/registry/minions/n2")
|
||||
|
||||
minion, err = registry.GetMinion(ctx, "m1")
|
||||
if minion == nil {
|
||||
t.Errorf("Unexpected !contains")
|
||||
}
|
||||
minion, err = registry.GetMinion(ctx, "m2")
|
||||
if minion == nil {
|
||||
t.Errorf("Unexpected !contains")
|
||||
}
|
||||
minion, err = registry.GetMinion(ctx, "n1")
|
||||
if minion != nil {
|
||||
t.Errorf("Unexpected !contains")
|
||||
}
|
||||
minion, err = registry.GetMinion(ctx, "n2")
|
||||
if minion != nil {
|
||||
t.Errorf("Unexpected !contains")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user