Remove Server object (#629)
This had some weird shared responsibility with the PodController. Instead just move the functionality to the PodController.
This commit is contained in:
@@ -22,21 +22,21 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
"github.com/cpuguy83/strongerrors/status/ocstatus"
|
||||
pkgerrors "github.com/pkg/errors"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/manager"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/trace"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
v1 "k8s.io/client-go/informers/core/v1"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
corev1informers "k8s.io/client-go/informers/core/v1"
|
||||
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
corev1listers "k8s.io/client-go/listers/core/v1"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/client-go/tools/record"
|
||||
"k8s.io/client-go/util/workqueue"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
)
|
||||
|
||||
// PodLifecycleHandler defines the interface used by the PodController to react
|
||||
@@ -74,42 +74,75 @@ type PodNotifier interface {
|
||||
|
||||
// PodController is the controller implementation for Pod resources.
|
||||
type PodController struct {
|
||||
// server is the instance to which this controller belongs.
|
||||
server *Server
|
||||
provider PodLifecycleHandler
|
||||
|
||||
// podsInformer is an informer for Pod resources.
|
||||
podsInformer v1.PodInformer
|
||||
podsInformer corev1informers.PodInformer
|
||||
// podsLister is able to list/get Pod resources from a shared informer's store.
|
||||
podsLister corev1listers.PodLister
|
||||
|
||||
// workqueue is a rate limited work queue.
|
||||
// This is used to queue work to be processed instead of performing it as soon as a change happens.
|
||||
// This means we can ensure we only process a fixed amount of resources at a time, and makes it easy to ensure we are never processing the same item simultaneously in two different workers.
|
||||
workqueue workqueue.RateLimitingInterface
|
||||
// recorder is an event recorder for recording Event resources to the Kubernetes API.
|
||||
recorder record.EventRecorder
|
||||
|
||||
// inSync is a channel which will be closed once the pod controller has become in-sync with apiserver
|
||||
// it will never close if startup fails, or if the run context is cancelled prior to initialization completing
|
||||
inSyncCh chan struct{}
|
||||
// ready is a channel which will be closed once the pod controller is fully up and running.
|
||||
// this channel will never be closed if there is an error on startup.
|
||||
ready chan struct{}
|
||||
|
||||
client corev1client.PodsGetter
|
||||
|
||||
resourceManager *manager.ResourceManager // TODO: can we eliminate this?
|
||||
}
|
||||
|
||||
// NewPodController returns a new instance of PodController.
|
||||
func NewPodController(server *Server) *PodController {
|
||||
// Create an event broadcaster.
|
||||
eventBroadcaster := record.NewBroadcaster()
|
||||
eventBroadcaster.StartLogging(log.L.Infof)
|
||||
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: server.k8sClient.CoreV1().Events("")})
|
||||
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: fmt.Sprintf("%s/pod-controller", server.nodeName)})
|
||||
// PodControllerConfig is used to configure a new PodController.
|
||||
type PodControllerConfig struct {
|
||||
// PodClient is used to perform actions on the k8s API, such as updating pod status
|
||||
// This field is required
|
||||
PodClient corev1client.PodsGetter
|
||||
|
||||
// Create an instance of PodController having a work queue that uses the rate limiter created above.
|
||||
pc := &PodController{
|
||||
server: server,
|
||||
podsInformer: server.podInformer,
|
||||
podsLister: server.podInformer.Lister(),
|
||||
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "pods"),
|
||||
recorder: recorder,
|
||||
inSyncCh: make(chan struct{}),
|
||||
// PodInformer is used as a local cache for pods
|
||||
// This should be configured to only look at pods scheduled to the node which the controller will be managing
|
||||
PodInformer corev1informers.PodInformer
|
||||
|
||||
EventRecorder record.EventRecorder
|
||||
|
||||
Provider PodLifecycleHandler
|
||||
|
||||
// TODO: get rid of this
|
||||
ResourceManager *manager.ResourceManager
|
||||
}
|
||||
|
||||
func NewPodController(cfg PodControllerConfig) (*PodController, error) {
|
||||
if cfg.PodClient == nil {
|
||||
return nil, strongerrors.InvalidArgument(pkgerrors.New("must set core client"))
|
||||
}
|
||||
if cfg.EventRecorder == nil {
|
||||
return nil, strongerrors.InvalidArgument(pkgerrors.New("must set event recorder"))
|
||||
}
|
||||
if cfg.PodInformer == nil {
|
||||
return nil, strongerrors.InvalidArgument(pkgerrors.New("must set informer"))
|
||||
}
|
||||
|
||||
return &PodController{
|
||||
client: cfg.PodClient,
|
||||
podsInformer: cfg.PodInformer,
|
||||
podsLister: cfg.PodInformer.Lister(),
|
||||
provider: cfg.Provider,
|
||||
resourceManager: cfg.ResourceManager,
|
||||
ready: make(chan struct{}),
|
||||
recorder: cfg.EventRecorder,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Run will set up the event handlers for types we are interested in, as well as syncing informer caches and starting workers.
|
||||
// It will block until the context is cancelled, at which point it will shutdown the work queue and wait for workers to finish processing their current work items.
|
||||
func (pc *PodController) Run(ctx context.Context, podSyncWorkers int) error {
|
||||
k8sQ := workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "syncPodsFromKubernetes")
|
||||
defer k8sQ.ShutDown()
|
||||
|
||||
podStatusQueue := workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "syncPodStatusFromProvider")
|
||||
pc.runProviderSyncWorkers(ctx, podStatusQueue, podSyncWorkers)
|
||||
pc.runSyncFromProvider(ctx, podStatusQueue)
|
||||
defer podStatusQueue.ShutDown()
|
||||
|
||||
// Set up event handlers for when Pod resources change.
|
||||
pc.podsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||
@@ -117,7 +150,7 @@ func NewPodController(server *Server) *PodController {
|
||||
if key, err := cache.MetaNamespaceKeyFunc(pod); err != nil {
|
||||
log.L.Error(err)
|
||||
} else {
|
||||
pc.workqueue.AddRateLimited(key)
|
||||
k8sQ.AddRateLimited(key)
|
||||
}
|
||||
},
|
||||
UpdateFunc: func(oldObj, newObj interface{}) {
|
||||
@@ -136,49 +169,39 @@ func NewPodController(server *Server) *PodController {
|
||||
if key, err := cache.MetaNamespaceKeyFunc(newPod); err != nil {
|
||||
log.L.Error(err)
|
||||
} else {
|
||||
pc.workqueue.AddRateLimited(key)
|
||||
k8sQ.AddRateLimited(key)
|
||||
}
|
||||
},
|
||||
DeleteFunc: func(pod interface{}) {
|
||||
if key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(pod); err != nil {
|
||||
log.L.Error(err)
|
||||
} else {
|
||||
pc.workqueue.AddRateLimited(key)
|
||||
k8sQ.AddRateLimited(key)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
// Return the instance of PodController back to the caller.
|
||||
return pc
|
||||
}
|
||||
|
||||
// Run will set up the event handlers for types we are interested in, as well as syncing informer caches and starting workers.
|
||||
// It will block until stopCh is closed, at which point it will shutdown the work queue and wait for workers to finish processing their current work items.
|
||||
func (pc *PodController) Run(ctx context.Context, threadiness int) error {
|
||||
defer pc.workqueue.ShutDown()
|
||||
|
||||
// Wait for the caches to be synced before starting workers.
|
||||
// Wait for the caches to be synced *before* starting workers.
|
||||
if ok := cache.WaitForCacheSync(ctx.Done(), pc.podsInformer.Informer().HasSynced); !ok {
|
||||
return pkgerrors.New("failed to wait for caches to sync")
|
||||
}
|
||||
log.G(ctx).Info("Pod cache in-sync")
|
||||
|
||||
close(pc.inSyncCh)
|
||||
|
||||
// Perform a reconciliation step that deletes any dangling pods from the provider.
|
||||
// This happens only when the virtual-kubelet is starting, and operates on a "best-effort" basis.
|
||||
// If by any reason the provider fails to delete a dangling pod, it will stay in the provider and deletion won't be retried.
|
||||
pc.deleteDanglingPods(ctx, threadiness)
|
||||
pc.deleteDanglingPods(ctx, podSyncWorkers)
|
||||
|
||||
// Launch "threadiness" workers to process Pod resources.
|
||||
log.G(ctx).Info("starting workers")
|
||||
for id := 0; id < threadiness; id++ {
|
||||
for id := 0; id < podSyncWorkers; id++ {
|
||||
go wait.Until(func() {
|
||||
// Use the worker's "index" as its ID so we can use it for tracing.
|
||||
pc.runWorker(ctx, strconv.Itoa(id))
|
||||
pc.runWorker(ctx, strconv.Itoa(id), k8sQ)
|
||||
}, time.Second, ctx.Done())
|
||||
}
|
||||
|
||||
close(pc.ready)
|
||||
|
||||
log.G(ctx).Info("started workers")
|
||||
<-ctx.Done()
|
||||
log.G(ctx).Info("shutting down workers")
|
||||
@@ -186,14 +209,21 @@ func (pc *PodController) Run(ctx context.Context, threadiness int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ready returns a channel which gets closed once the PodController is ready to handle scheduled pods.
|
||||
// This channel will never close if there is an error on startup.
|
||||
// The status of this channel after sthudown is indeterminate.
|
||||
func (pc *PodController) Ready() <-chan struct{} {
|
||||
return pc.ready
|
||||
}
|
||||
|
||||
// runWorker is a long-running function that will continually call the processNextWorkItem function in order to read and process an item on the work queue.
|
||||
func (pc *PodController) runWorker(ctx context.Context, workerId string) {
|
||||
for pc.processNextWorkItem(ctx, workerId) {
|
||||
func (pc *PodController) runWorker(ctx context.Context, workerId string, q workqueue.RateLimitingInterface) {
|
||||
for pc.processNextWorkItem(ctx, workerId, q) {
|
||||
}
|
||||
}
|
||||
|
||||
// processNextWorkItem will read a single work item off the work queue and attempt to process it,by calling the syncHandler.
|
||||
func (pc *PodController) processNextWorkItem(ctx context.Context, workerId string) bool {
|
||||
func (pc *PodController) processNextWorkItem(ctx context.Context, workerId string, q workqueue.RateLimitingInterface) bool {
|
||||
|
||||
// We create a span only after popping from the queue so that we can get an adequate picture of how long it took to process the item.
|
||||
ctx, span := trace.StartSpan(ctx, "processNextWorkItem")
|
||||
@@ -201,7 +231,7 @@ func (pc *PodController) processNextWorkItem(ctx context.Context, workerId strin
|
||||
|
||||
// Add the ID of the current worker as an attribute to the current span.
|
||||
ctx = span.WithField(ctx, "workerId", workerId)
|
||||
return handleQueueItem(ctx, pc.workqueue, pc.syncHandler)
|
||||
return handleQueueItem(ctx, q, pc.syncHandler)
|
||||
}
|
||||
|
||||
// syncHandler compares the actual state with the desired, and attempts to converge the two.
|
||||
@@ -232,7 +262,7 @@ func (pc *PodController) syncHandler(ctx context.Context, key string) error {
|
||||
}
|
||||
// At this point we know the Pod resource doesn't exist, which most probably means it was deleted.
|
||||
// Hence, we must delete it from the provider if it still exists there.
|
||||
if err := pc.server.deletePod(ctx, namespace, name); err != nil {
|
||||
if err := pc.deletePod(ctx, namespace, name); err != nil {
|
||||
err := pkgerrors.Wrapf(err, "failed to delete pod %q in the provider", loggablePodNameFromCoordinates(namespace, name))
|
||||
span.SetStatus(ocstatus.FromError(err))
|
||||
return err
|
||||
@@ -254,7 +284,7 @@ func (pc *PodController) syncPodInProvider(ctx context.Context, pod *corev1.Pod)
|
||||
// Check whether the pod has been marked for deletion.
|
||||
// If it does, guarantee it is deleted in the provider and Kubernetes.
|
||||
if pod.DeletionTimestamp != nil {
|
||||
if err := pc.server.deletePod(ctx, pod.Namespace, pod.Name); err != nil {
|
||||
if err := pc.deletePod(ctx, pod.Namespace, pod.Name); err != nil {
|
||||
err := pkgerrors.Wrapf(err, "failed to delete pod %q in the provider", loggablePodName(pod))
|
||||
span.SetStatus(ocstatus.FromError(err))
|
||||
return err
|
||||
@@ -269,7 +299,7 @@ func (pc *PodController) syncPodInProvider(ctx context.Context, pod *corev1.Pod)
|
||||
}
|
||||
|
||||
// Create or update the pod in the provider.
|
||||
if err := pc.server.createOrUpdatePod(ctx, pod, pc.recorder); err != nil {
|
||||
if err := pc.createOrUpdatePod(ctx, pod); err != nil {
|
||||
err := pkgerrors.Wrapf(err, "failed to sync pod %q in the provider", loggablePodName(pod))
|
||||
span.SetStatus(ocstatus.FromError(err))
|
||||
return err
|
||||
@@ -283,7 +313,7 @@ func (pc *PodController) deleteDanglingPods(ctx context.Context, threadiness int
|
||||
defer span.End()
|
||||
|
||||
// Grab the list of pods known to the provider.
|
||||
pps, err := pc.server.provider.GetPods(ctx)
|
||||
pps, err := pc.provider.GetPods(ctx)
|
||||
if err != nil {
|
||||
err := pkgerrors.Wrap(err, "failed to fetch the list of pods from the provider")
|
||||
span.SetStatus(ocstatus.FromError(err))
|
||||
@@ -332,7 +362,7 @@ func (pc *PodController) deleteDanglingPods(ctx context.Context, threadiness int
|
||||
// Add the pod's attributes to the current span.
|
||||
ctx = addPodAttributes(ctx, span, pod)
|
||||
// Actually delete the pod.
|
||||
if err := pc.server.deletePod(ctx, pod.Namespace, pod.Name); err != nil {
|
||||
if err := pc.deletePod(ctx, pod.Namespace, pod.Name); err != nil {
|
||||
span.SetStatus(ocstatus.FromError(err))
|
||||
log.G(ctx).Errorf("failed to delete pod %q in provider", loggablePodName(pod))
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user