Skip to content

Commit 7be255c

Browse files
committed
node: Add UIDBasedProvider wrapper for legacy PodLifecycleHandler methods
1 parent dfe615f commit 7be255c

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

node/lifecycle_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (s *system) start(ctx context.Context) error {
242242
return s.pc.Err()
243243
}
244244

245-
func wireUpSystem(ctx context.Context, provider PodLifecycleHandler, f testFunction) error {
245+
func wireUpSystem(ctx context.Context, provider PodUIDLifecycleHandler, f testFunction) error {
246246
ctx, cancel := context.WithCancel(ctx)
247247
defer cancel()
248248

@@ -266,7 +266,7 @@ func wireUpSystem(ctx context.Context, provider PodLifecycleHandler, f testFunct
266266
return wireUpSystemWithClient(ctx, provider, client, f)
267267
}
268268

269-
func wireUpSystemWithClient(ctx context.Context, provider PodLifecycleHandler, client kubernetes.Interface, f testFunction) error {
269+
func wireUpSystemWithClient(ctx context.Context, provider PodUIDLifecycleHandler, client kubernetes.Interface, f testFunction) error {
270270
ctx, cancel := context.WithCancel(ctx)
271271
defer cancel()
272272

@@ -293,7 +293,7 @@ func wireUpSystemWithClient(ctx context.Context, provider PodLifecycleHandler, c
293293
PodClient: client.CoreV1(),
294294
PodInformer: podInformer,
295295
EventRecorder: fakeRecorder,
296-
Provider: provider,
296+
Provider: UIDBasedProvider(provider),
297297
ConfigMapInformer: configMapInformer,
298298
SecretInformer: secretInformer,
299299
ServiceInformer: serviceInformer,

node/mock_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const (
1818
)
1919

2020
var (
21-
_ PodLifecycleHandler = (*mockProvider)(nil)
2221
_ PodUIDLifecycleHandler = (*mockProvider)(nil)
2322
)
2423

@@ -177,10 +176,6 @@ func (p *mockProvider) DeletePod(ctx context.Context, pod *v1.Pod) (err error) {
177176
return nil
178177
}
179178

180-
func (p *mockProvider) GetPod(ctx context.Context, namespace, name string) (pod *v1.Pod, err error) {
181-
panic("GetPod not called when GetPodByUID implemented")
182-
}
183-
184179
// GetPodByUID returns a pod by name that is stored in memory.
185180
func (p *mockProvider) GetPodByUID(ctx context.Context, namespace, name string, uid types.UID) (pod *v1.Pod, err error) {
186181
log.G(ctx).Infof("receive GetPodByUID %q", name)
@@ -196,10 +191,6 @@ func (p *mockProvider) GetPodByUID(ctx context.Context, namespace, name string,
196191
return nil, errdefs.NotFoundf("pod \"%s/%s\" is not known to the provider", namespace, name)
197192
}
198193

199-
func (p *mockProvider) GetPodStatus(ctx context.Context, namespace, name string) (pod *v1.PodStatus, err error) {
200-
panic("GetPodStatus not called when GetPodStatusByUID implemented")
201-
}
202-
203194
// GetPodStatusByUID returns the status of a pod by name that is "running".
204195
// returns nil if a pod by that name is not found.
205196
func (p *mockProvider) GetPodStatusByUID(ctx context.Context, namespace, name string, uid types.UID) (*v1.PodStatus, error) {
@@ -279,7 +270,7 @@ func (p *mockProviderAsync) NotifyPods(ctx context.Context, notifier func(*v1.Po
279270
}
280271

281272
type testingProvider interface {
282-
PodLifecycleHandler
273+
PodUIDLifecycleHandler
283274
setErrorOnDelete(error)
284275
getAttemptedDeletes() *waitableInt
285276
getDeletes() *waitableInt

node/pod_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func newTestController() *TestController {
5454
PodClient: fk8s.CoreV1(),
5555
PodInformer: iFactory.Core().V1().Pods(),
5656
EventRecorder: testutil.FakeEventRecorder(5),
57-
Provider: p,
57+
Provider: UIDBasedProvider(p),
5858
ConfigMapInformer: iFactory.Core().V1().ConfigMaps(),
5959
SecretInformer: iFactory.Core().V1().Secrets(),
6060
ServiceInformer: iFactory.Core().V1().Services(),

node/podcontroller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ import (
3838
"k8s.io/client-go/util/workqueue"
3939
)
4040

41+
// podLifecycleHandler contains the methods shared between PodUIDLifecycleHandler and the legacy
42+
// PodLifecycleHandler. In the future, when PodLifecycleHandler is replaced with PodUIDLifecycleHandler,
43+
// this common interface can be incorporated directly into PodUIDLifecycleHandler.
4144
type podLifecycleHandler interface {
4245
// CreatePod takes a Kubernetes Pod and deploys it within the provider.
4346
CreatePod(ctx context.Context, pod *corev1.Pod) error

node/uid.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
corev1 "k8s.io/api/core/v1"
7+
v1 "k8s.io/api/core/v1"
78
"k8s.io/apimachinery/pkg/types"
89
)
910

@@ -37,7 +38,7 @@ func (k *podUIDKey) String() string {
3738
}
3839

3940
// uidProviderWrapper wraps a legacy PodLifecycleHandler to handle GetPodByUID/GetPodStatusByUID,
40-
// by ignoring the pod UID and using only the pod namespace/name.
41+
// by ignoring the pod UID and calling GetPod/GetPodStatus using only the pod namespace/name.
4142
type uidProviderWrapper struct {
4243
PodLifecycleHandler
4344
}
@@ -51,3 +52,26 @@ func (p *uidProviderWrapper) GetPodByUID(ctx context.Context, namespace, name st
5152
func (p *uidProviderWrapper) GetPodStatusByUID(ctx context.Context, namespace, name string, uid types.UID) (*corev1.PodStatus, error) {
5253
return p.GetPodStatus(ctx, namespace, name)
5354
}
55+
56+
// uidBasedProvider wraps a PodUIDLifecycleHandler to implement PodLifecycleHandler, by stubbing
57+
// the legacy GetPod/GetPodStatus methods. These methods are never called if the provider implements
58+
// PodUIDLifecycleHandler, but are required to be implemented as PodControllerConfig takes a
59+
// PodLifecycleHandler. In the future, PodControllerConfig will take an asyncProvider, and this will
60+
// no longer be necessary.
61+
type uidBasedProvider struct {
62+
PodUIDLifecycleHandler
63+
}
64+
65+
// UIDBasedProvider wraps a PodUIDLifecycleHandler into a PodLifecycleHandler that can be used in
66+
// PodControllerConfig.
67+
func UIDBasedProvider(p PodUIDLifecycleHandler) PodLifecycleHandler {
68+
return &uidBasedProvider{PodUIDLifecycleHandler: p}
69+
}
70+
71+
func (p *uidBasedProvider) GetPod(ctx context.Context, namespace, name string) (*v1.Pod, error) {
72+
panic("GetPod should never be called when GetPodByUID is implemented")
73+
}
74+
75+
func (p *uidBasedProvider) GetPodStatus(ctx context.Context, namespace, name string) (*v1.PodStatus, error) {
76+
panic("GetPodStatus should never be called when GetPodStatusByUID is implemented")
77+
}

0 commit comments

Comments
 (0)