Copyright 2015 The Kubernetes Authors.
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 application
import (
"time"
"strings"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"k8s.io/kubernetes/pkg/client/record"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller/informers"
"k8s.io/kubernetes/pkg/util/metrics"
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/util/workqueue"
)
type ApplicationController struct {
client clientset.Interface
eventRecorder record.EventRecorder
syncHandler func(dKey string) error
// Applications that need to be synced
queue workqueue.RateLimitingInterface
}
// NewApplicationController creates a new ApplicationController.
func NewApplicationController(aInformer informers.ApplicationInformer, client clientset.Interface) *ApplicationController {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&unversionedcore.EventSinkImpl{Interface: client.Core().Events("")})
if client != nil && client.Core().RESTClient().GetRateLimiter() != nil {
metrics.RegisterMetricAndTrackRateLimiterUsage("application_controller", client.Core().RESTClient().GetRateLimiter())
}
ac := &ApplicationController{
client: client,
eventRecorder: eventBroadcaster.NewRecorder(api.EventSource{Component: "application-controller"}),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "application"),
}
aInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ac.addApplicationNotification,
UpdateFunc: ac.updateApplicationNotification,
DeleteFunc: ac.deleteApplicationNotification,
})
ac.syncHandler = ac.syncApplication
return ac
}
func (ac *ApplicationController) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
glog.Infof("Starting application controller")
go wait.Until(ac.worker, time.Second, stopCh)
<-stopCh
glog.Infof("Shutting down application controller")
}
func (ac *ApplicationController) worker() {
work := func() bool {
key, quit := ac.queue.Get()
if quit {
return true
}
defer ac.queue.Done(key)
ac.syncHandler(key.(string))
return false
}
for {
if quit := work(); quit {
return
}
}
}
func (ac *ApplicationController) enqueueApplication(application *extensions.Application) {
key, err := controller.KeyFunc(application)
if err != nil {
glog.Errorf("Couldn't get key for object %#v: %v", application, err)
return
}
ac.queue.Add(key)
}
func (ac *ApplicationController) addApplicationNotification(obj interface{}) {
d := obj.(*extensions.Application)
glog.V(4).Infof("Adding application %s", d.Name)
ac.enqueueApplication(d)
}
func (ac *ApplicationController) updateApplicationNotification(old, cur interface{}) {
oldD := old.(*extensions.Application)
glog.V(4).Infof("Updating application %s", oldD.Name)
ac.enqueueApplication(cur.(*extensions.Application))
}
func (ac *ApplicationController) deleteApplicationNotification(obj interface{}) {
d, ok := obj.(*extensions.Application)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
glog.Errorf("Couldn't get object from tombstone %#v", obj)
return
}
d, ok = tombstone.Obj.(*extensions.Application)
if !ok {
glog.Errorf("Tombstone contained object that is not a Application %#v", obj)
return
}
}
glog.V(4).Infof("Deleting application %s", d.Name)
ac.enqueueApplication(d)
}
func (ac *ApplicationController) syncApplication(key string) error {
segs := strings.Split(key, "/")
namespace := segs[0]
name := segs[1]
application, err := ac.client.Extensions().Applications(namespace).Get(name)
if err != nil {
return err
}
glog.V(0).Infof("This is from syncApplication: %s/%s", application.Namespace, application.Name)
return nil
}