Skip to content

Commit 31ffa20

Browse files
committed
node: Add PodUIDLifecycleHandler
1 parent 40cc429 commit 31ffa20

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

node/podcontroller.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/virtual-kubelet/virtual-kubelet/trace"
3030
corev1 "k8s.io/api/core/v1"
3131
"k8s.io/apimachinery/pkg/api/errors"
32+
"k8s.io/apimachinery/pkg/types"
3233
"k8s.io/apimachinery/pkg/util/wait"
3334
corev1informers "k8s.io/client-go/informers/core/v1"
3435
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
@@ -75,6 +76,22 @@ type PodLifecycleHandler interface {
7576
GetPods(context.Context) ([]*corev1.Pod, error)
7677
}
7778

79+
type PodUIDLifecycleHandler interface {
80+
PodLifecycleHandler
81+
82+
// GetPodByUID retrieves a pod by name and UID from the provider (can be cached).
83+
// The Pod returned is expected to be immutable, and may be accessed
84+
// concurrently outside of the calling goroutine. Therefore it is recommended
85+
// to return a version after DeepCopy.
86+
GetPodByUID(ctx context.Context, namespace, name string, uid types.UID) (*corev1.Pod, error)
87+
88+
// GetPodStatusByUID retrieves the status of a pod by name and UID from the provider.
89+
// The PodStatus returned is expected to be immutable, and may be accessed
90+
// concurrently outside of the calling goroutine. Therefore it is recommended
91+
// to return a version after DeepCopy.
92+
GetPodStatusByUID(ctx context.Context, namespace, name string, uid types.UID) (*corev1.PodStatus, error)
93+
}
94+
7895
// PodNotifier is used as an extension to PodLifecycleHandler to support async updates of pod statuses.
7996
type PodNotifier interface {
8097
// NotifyPods instructs the notifier to call the passed in function when
@@ -97,7 +114,7 @@ type PodEventFilterFunc func(context.Context, *corev1.Pod) bool
97114

98115
// PodController is the controller implementation for Pod resources.
99116
type PodController struct {
100-
provider PodLifecycleHandler
117+
provider PodUIDLifecycleHandler
101118

102119
// podsInformer is an informer for Pod resources.
103120
podsInformer corev1informers.PodInformer
@@ -245,11 +262,18 @@ func NewPodController(cfg PodControllerConfig) (*PodController, error) {
245262
return nil, pkgerrors.Wrap(err, "could not create resource manager")
246263
}
247264

265+
var provider PodUIDLifecycleHandler
266+
if p, ok := cfg.Provider.(PodUIDLifecycleHandler); ok {
267+
provider = p
268+
} else {
269+
provider = &uidProviderWrapper{PodLifecycleHandler: cfg.Provider}
270+
}
271+
248272
pc := &PodController{
249273
client: cfg.PodClient,
250274
podsInformer: cfg.PodInformer,
251275
podsLister: cfg.PodInformer.Lister(),
252-
provider: cfg.Provider,
276+
provider: provider,
253277
resourceManager: rm,
254278
ready: make(chan struct{}),
255279
done: make(chan struct{}),
@@ -266,7 +290,7 @@ func NewPodController(cfg PodControllerConfig) (*PodController, error) {
266290
}
267291

268292
type asyncProvider interface {
269-
PodLifecycleHandler
293+
PodUIDLifecycleHandler
270294
PodNotifier
271295
}
272296

@@ -296,7 +320,7 @@ func (pc *PodController) Run(ctx context.Context, podSyncWorkers int) (retErr er
296320
if p, ok := pc.provider.(asyncProvider); ok {
297321
provider = p
298322
} else {
299-
wrapped := &syncProviderWrapper{PodLifecycleHandler: pc.provider, l: pc.podsLister}
323+
wrapped := &syncProviderWrapper{PodUIDLifecycleHandler: pc.provider, l: pc.podsLister}
300324
runProvider = wrapped.run
301325
provider = wrapped
302326
log.G(ctx).Debug("Wrapped non-async provider with async")

node/sync.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const (
2626
containerStatusTerminatedMessage = "Container was terminated. The exit code may not reflect the real exit code"
2727
)
2828

29-
// syncProviderWrapper wraps a PodLifecycleHandler to give it async-like pod status notification behavior.
29+
// syncProviderWrapper wraps a PodUIDLifecycleHandler to give it async-like pod status notification behavior.
3030
type syncProviderWrapper struct {
31-
PodLifecycleHandler
31+
PodUIDLifecycleHandler
3232
notify func(*corev1.Pod)
3333
l corev1listers.PodLister
3434

@@ -60,7 +60,7 @@ func (p *syncProviderWrapper) DeletePod(ctx context.Context, pod *corev1.Pod) er
6060
}
6161

6262
p.deletedPods.Store(key, pod)
63-
if err := p.PodLifecycleHandler.DeletePod(ctx, pod.DeepCopy()); err != nil {
63+
if err := p.PodUIDLifecycleHandler.DeletePod(ctx, pod.DeepCopy()); err != nil {
6464
log.G(ctx).WithField("key", key).WithError(err).Debug("Removed key from deleted pods cache")
6565
// We aren't going to actually delete the pod from the provider since there is an error so delete it from our cache,
6666
// otherwise we could end up leaking pods in our deletion cache.

node/uid.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package node
2+
3+
import (
4+
"context"
5+
6+
corev1 "k8s.io/api/core/v1"
7+
"k8s.io/apimachinery/pkg/types"
8+
)
9+
10+
type uidProviderWrapper struct {
11+
PodLifecycleHandler
12+
}
13+
14+
var _ PodUIDLifecycleHandler = (*uidProviderWrapper)(nil)
15+
16+
func (p *uidProviderWrapper) GetPodByUID(ctx context.Context, namespace, name string, uid types.UID) (*corev1.Pod, error) {
17+
return p.GetPod(ctx, namespace, name)
18+
}
19+
20+
func (p *uidProviderWrapper) GetPodStatusByUID(ctx context.Context, namespace, name string, uid types.UID) (*corev1.PodStatus, error) {
21+
return p.GetPodStatus(ctx, namespace, name)
22+
}

0 commit comments

Comments
 (0)