Commit 4d2f7a38 authored by Daniel Smith's avatar Daniel Smith

Merge pull request #6866 from bprashanth/rc_watch_pods

RcManager watches pods and RCs instead of polling every 10s
parents 2802b18b 7592dabe
......@@ -216,7 +216,7 @@ func startComponents(firstManifestURL, secondManifestURL, apiVersion string) (st
controllerManager := replicationControllerPkg.NewReplicationManager(cl)
// TODO: Write an integration test for the replication controllers watch.
controllerManager.Run(1 * time.Second)
go controllerManager.Run(3, util.NeverStop)
nodeResources := &api.NodeResources{
Capacity: api.ResourceList{
......
......@@ -55,6 +55,7 @@ type CMServer struct {
CloudProvider string
CloudConfigFile string
ConcurrentEndpointSyncs int
ConcurrentRCSyncs int
MinionRegexp string
NodeSyncPeriod time.Duration
ResourceQuotaSyncPeriod time.Duration
......@@ -90,6 +91,7 @@ func NewCMServer() *CMServer {
Port: ports.ControllerManagerPort,
Address: util.IP(net.ParseIP("127.0.0.1")),
ConcurrentEndpointSyncs: 5,
ConcurrentRCSyncs: 5,
NodeSyncPeriod: 10 * time.Second,
ResourceQuotaSyncPeriod: 10 * time.Second,
NamespaceSyncPeriod: 5 * time.Minute,
......@@ -111,6 +113,7 @@ func (s *CMServer) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&s.CloudProvider, "cloud-provider", s.CloudProvider, "The provider for cloud services. Empty string for no provider.")
fs.StringVar(&s.CloudConfigFile, "cloud-config", s.CloudConfigFile, "The path to the cloud provider configuration file. Empty string for no configuration file.")
fs.IntVar(&s.ConcurrentEndpointSyncs, "concurrent-endpoint-syncs", s.ConcurrentEndpointSyncs, "The number of endpoint syncing operations that will be done concurrently. Larger number = faster endpoint updating, but more CPU (and network) load")
fs.IntVar(&s.ConcurrentRCSyncs, "concurrent_rc_syncs", s.ConcurrentRCSyncs, "The number of replication controllers that are allowed to sync concurrently. Larger number = more reponsive replica management, but more CPU (and network) load")
fs.StringVar(&s.MinionRegexp, "minion-regexp", s.MinionRegexp, "If non empty, and --cloud-provider is specified, a regular expression for matching minion VMs.")
fs.DurationVar(&s.NodeSyncPeriod, "node-sync-period", s.NodeSyncPeriod, ""+
"The period for syncing nodes from cloudprovider. Longer periods will result in "+
......@@ -207,7 +210,7 @@ func (s *CMServer) Run(_ []string) error {
go endpoints.Run(s.ConcurrentEndpointSyncs, util.NeverStop)
controllerManager := replicationControllerPkg.NewReplicationManager(kubeClient)
controllerManager.Run(replicationControllerPkg.DefaultSyncPeriod)
go controllerManager.Run(s.ConcurrentRCSyncs, util.NeverStop)
cloud := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile)
nodeResources := &api.NodeResources{
......
......@@ -144,7 +144,7 @@ func runControllerManager(machineList []string, cl *client.Client, nodeMilliCPU,
go endpoints.Run(5, util.NeverStop)
controllerManager := controller.NewReplicationManager(cl)
controllerManager.Run(controller.DefaultSyncPeriod)
go controllerManager.Run(5, util.NeverStop)
}
func startComponents(etcdClient tools.EtcdClient, cl *client.Client, addr net.IP, port int) {
......
......@@ -123,6 +123,59 @@ func (s *StoreToNodeLister) GetNodeInfo(id string) (*api.Node, error) {
return minion.(*api.Node), nil
}
// StoreToControllerLister gives a store List and Exists methods. The store must contain only ReplicationControllers.
type StoreToControllerLister struct {
Store
}
// Exists checks if the given rc exists in the store.
func (s *StoreToControllerLister) Exists(controller *api.ReplicationController) (bool, error) {
_, exists, err := s.Store.Get(controller)
if err != nil {
return false, err
}
return exists, nil
}
// StoreToControllerLister lists all controllers in the store.
// TODO: converge on the interface in pkg/client
func (s *StoreToControllerLister) List() (controllers []api.ReplicationController, err error) {
for _, c := range s.Store.List() {
controllers = append(controllers, *(c.(*api.ReplicationController)))
}
return controllers, nil
}
// GetPodControllers returns a list of controllers managing a pod. Returns an error only if no matching controllers are found.
func (s *StoreToControllerLister) GetPodControllers(pod *api.Pod) (controllers []api.ReplicationController, err error) {
var selector labels.Selector
var rc api.ReplicationController
if len(pod.Labels) == 0 {
err = fmt.Errorf("No controllers found for pod %v because it has no labels", pod.Name)
return
}
for _, m := range s.Store.List() {
rc = *m.(*api.ReplicationController)
if rc.Namespace != pod.Namespace {
continue
}
labelSet := labels.Set(rc.Spec.Selector)
selector = labels.Set(rc.Spec.Selector).AsSelector()
// If an rc with a nil or empty selector creeps in, it should match nothing, not everything.
if labelSet.AsSelector().Empty() || !selector.Matches(labels.Set(pod.Labels)) {
continue
}
controllers = append(controllers, rc)
}
if len(controllers) == 0 {
err = fmt.Errorf("Could not find controllers for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
}
return
}
// StoreToServiceLister makes a Store that has the List method of the client.ServiceInterface
// The Store must contain (only) Services.
type StoreToServiceLister struct {
......
......@@ -45,6 +45,116 @@ func TestStoreToMinionLister(t *testing.T) {
}
}
func TestStoreToControllerLister(t *testing.T) {
store := NewStore(MetaNamespaceKeyFunc)
lister := StoreToControllerLister{store}
testCases := []struct {
inRCs []*api.ReplicationController
list func() ([]api.ReplicationController, error)
outRCNames util.StringSet
expectErr bool
}{
// Basic listing with all labels and no selectors
{
inRCs: []*api.ReplicationController{
{ObjectMeta: api.ObjectMeta{Name: "basic"}},
},
list: func() ([]api.ReplicationController, error) {
return lister.List()
},
outRCNames: util.NewStringSet("basic"),
},
// No pod lables
{
inRCs: []*api.ReplicationController{
{
ObjectMeta: api.ObjectMeta{Name: "basic", Namespace: "ns"},
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{"foo": "baz"},
},
},
},
list: func() ([]api.ReplicationController, error) {
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{Name: "pod1", Namespace: "ns"},
}
return lister.GetPodControllers(pod)
},
outRCNames: util.NewStringSet(),
expectErr: true,
},
// No RC selectors
{
inRCs: []*api.ReplicationController{
{
ObjectMeta: api.ObjectMeta{Name: "basic", Namespace: "ns"},
},
},
list: func() ([]api.ReplicationController, error) {
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "pod1",
Namespace: "ns",
Labels: map[string]string{"foo": "bar"},
},
}
return lister.GetPodControllers(pod)
},
outRCNames: util.NewStringSet(),
expectErr: true,
},
// Matching labels to selectors and namespace
{
inRCs: []*api.ReplicationController{
{
ObjectMeta: api.ObjectMeta{Name: "foo"},
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{"foo": "bar"},
},
},
{
ObjectMeta: api.ObjectMeta{Name: "bar", Namespace: "ns"},
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{"foo": "bar"},
},
},
},
list: func() ([]api.ReplicationController, error) {
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "pod1",
Labels: map[string]string{"foo": "bar"},
Namespace: "ns",
},
}
return lister.GetPodControllers(pod)
},
outRCNames: util.NewStringSet("bar"),
},
}
for _, c := range testCases {
for _, r := range c.inRCs {
store.Add(r)
}
gotControllers, err := c.list()
if err != nil && c.expectErr {
continue
} else if c.expectErr {
t.Fatalf("Expected error, got none")
} else if err != nil {
t.Fatalf("Unexpected error %#v", err)
}
gotNames := make([]string, len(gotControllers))
for ix := range gotControllers {
gotNames[ix] = gotControllers[ix].Name
}
if !c.outRCNames.HasAll(gotNames...) || len(gotNames) != len(c.outRCNames) {
t.Errorf("Unexpected got controllers %+v expected %+v", gotNames, c.outRCNames)
}
}
}
func TestStoreToPodLister(t *testing.T) {
store := NewStore(MetaNamespaceKeyFunc)
ids := []string{"foo", "bar", "baz"}
......
......@@ -23,7 +23,6 @@ import (
"github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/controller"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
......@@ -43,7 +42,7 @@ $ kubectl resize --replicas=3 replicationcontrollers foo
// If the replication controller named foo's current size is 2, resize foo to 3.
$ kubectl resize --current-replicas=2 --replicas=3 replicationcontrollers foo`
retryFrequency = controller.DefaultSyncPeriod / 100
retryFrequency = 100 * time.Millisecond
retryTimeout = 10 * time.Second
)
......
......@@ -665,7 +665,7 @@ func validateLabelValue(v string) error {
}
// SelectorFromSet returns a Selector which will match exactly the given Set. A
// nil Set is considered equivalent to Everything().
// nil and empty Sets are considered equivalent to Everything().
func SelectorFromSet(ls Set) Selector {
if ls == nil {
return LabelSelector{}
......
......@@ -99,6 +99,7 @@ func TestSelectorMatches(t *testing.T) {
expectMatch(t, "x=y,z=w", Set{"x": "y", "z": "w"})
expectMatch(t, "x!=y,z!=w", Set{"x": "z", "z": "a"})
expectMatch(t, "notin=in", Set{"notin": "in"}) // in and notin in exactMatch
expectNoMatch(t, "x=z", Set{})
expectNoMatch(t, "x=y", Set{"x": "z"})
expectNoMatch(t, "x=y,z=w", Set{"x": "w", "z": "w"})
expectNoMatch(t, "x!=y,z!=w", Set{"x": "z", "z": "w"})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment