Skip to content

Commit 11bbe25

Browse files
committed
node: Implement PodUIDLifecycleHandler for mockProvider
1 parent bb96588 commit 11bbe25

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

node/mock_test.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ import (
1010
"github.com/virtual-kubelet/virtual-kubelet/log"
1111
v1 "k8s.io/api/core/v1"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/apimachinery/pkg/types"
1314
)
1415

1516
const (
1617
mockProviderPodDeletedReason = "MockProviderPodDeleted"
1718
)
1819

1920
var (
20-
_ PodLifecycleHandler = (*mockProvider)(nil)
21+
_ PodLifecycleHandler = (*mockProvider)(nil)
22+
_ PodUIDLifecycleHandler = (*mockProvider)(nil)
2123
)
2224

2325
type mockProvider struct {
@@ -175,11 +177,15 @@ func (p *mockProvider) DeletePod(ctx context.Context, pod *v1.Pod) (err error) {
175177
return nil
176178
}
177179

178-
// GetPod returns a pod by name that is stored in memory.
179180
func (p *mockProvider) GetPod(ctx context.Context, namespace, name string) (pod *v1.Pod, err error) {
180-
log.G(ctx).Infof("receive GetPod %q", name)
181+
panic("GetPod not called when GetPodByUID implemented")
182+
}
183+
184+
// GetPodByUID returns a pod by name that is stored in memory.
185+
func (p *mockProvider) GetPodByUID(ctx context.Context, namespace, name string, uid types.UID) (pod *v1.Pod, err error) {
186+
log.G(ctx).Infof("receive GetPodByUID %q", name)
181187

182-
key, err := buildKeyFromNames(namespace, name)
188+
key, err := buildKeyFromNames(namespace, name, uid)
183189
if err != nil {
184190
return nil, err
185191
}
@@ -190,12 +196,16 @@ func (p *mockProvider) GetPod(ctx context.Context, namespace, name string) (pod
190196
return nil, errdefs.NotFoundf("pod \"%s/%s\" is not known to the provider", namespace, name)
191197
}
192198

193-
// GetPodStatus returns the status of a pod by name that is "running".
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+
203+
// GetPodStatusByUID returns the status of a pod by name that is "running".
194204
// returns nil if a pod by that name is not found.
195-
func (p *mockProvider) GetPodStatus(ctx context.Context, namespace, name string) (*v1.PodStatus, error) {
196-
log.G(ctx).Infof("receive GetPodStatus %q", name)
205+
func (p *mockProvider) GetPodStatusByUID(ctx context.Context, namespace, name string, uid types.UID) (*v1.PodStatus, error) {
206+
log.G(ctx).Infof("receive GetPodStatusByUID %q", name)
197207

198-
pod, err := p.GetPod(ctx, namespace, name)
208+
pod, err := p.GetPodByUID(ctx, namespace, name, uid)
199209
if err != nil {
200210
return nil, err
201211
}
@@ -237,8 +247,8 @@ func (p *mockProvider) getUpdates() *waitableInt {
237247
return p.updates
238248
}
239249

240-
func buildKeyFromNames(namespace string, name string) (string, error) {
241-
return fmt.Sprintf("%s-%s", namespace, name), nil
250+
func buildKeyFromNames(namespace string, name string, uid types.UID) (string, error) {
251+
return fmt.Sprintf("%s/%s/%s", namespace, name, uid), nil
242252
}
243253

244254
// buildKey is a helper for building the "key" for the providers pod store.
@@ -251,7 +261,11 @@ func buildKey(pod *v1.Pod) (string, error) {
251261
return "", fmt.Errorf("pod name not found")
252262
}
253263

254-
return buildKeyFromNames(pod.Namespace, pod.Name)
264+
if pod.UID == "" {
265+
return "", fmt.Errorf("pod UID not found")
266+
}
267+
268+
return buildKeyFromNames(pod.Namespace, pod.Name, pod.UID)
255269
}
256270

257271
type mockProviderAsync struct {

node/pod_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/apimachinery/pkg/runtime"
3131
"k8s.io/apimachinery/pkg/runtime/schema"
32+
"k8s.io/apimachinery/pkg/util/uuid"
3233
kubeinformers "k8s.io/client-go/informers"
3334
"k8s.io/client-go/kubernetes/fake"
3435
core "k8s.io/client-go/testing"
@@ -215,6 +216,7 @@ func TestPodCreateNewPod(t *testing.T) {
215216
pod.Namespace = "default" //nolint:goconst
216217
pod.Name = "nginx" //nolint:goconst
217218
pod.Spec = newPodSpec()
219+
pod.UID = uuid.NewUUID()
218220

219221
err := svr.createOrUpdatePod(context.Background(), pod.DeepCopy())
220222

@@ -242,6 +244,7 @@ func TestPodCreateNewPodWithNoDownwardAPIResolution(t *testing.T) {
242244
},
243245
},
244246
}
247+
pod.UID = uuid.NewUUID()
245248

246249
err := svr.createOrUpdatePod(context.Background(), pod.DeepCopy())
247250
assert.Check(t, is.Nil(err))
@@ -267,6 +270,7 @@ func TestPodUpdateExisting(t *testing.T) {
267270
pod.Namespace = "default"
268271
pod.Name = "nginx"
269272
pod.Spec = newPodSpec()
273+
pod.UID = uuid.NewUUID()
270274

271275
err := svr.createOrUpdatePod(context.Background(), pod.DeepCopy())
272276
assert.Check(t, is.Nil(err))
@@ -291,6 +295,7 @@ func TestPodNoSpecChange(t *testing.T) {
291295
pod.Namespace = "default"
292296
pod.Name = "nginx"
293297
pod.Spec = newPodSpec()
298+
pod.UID = uuid.NewUUID()
294299

295300
err := svr.createOrUpdatePod(context.Background(), pod.DeepCopy())
296301
assert.Check(t, is.Nil(err))
@@ -311,6 +316,7 @@ func TestPodStatusDelete(t *testing.T) {
311316
pod := &corev1.Pod{}
312317
pod.Namespace = "default"
313318
pod.Name = "nginx"
319+
pod.UID = uuid.NewUUID()
314320
pod.Spec = newPodSpec()
315321
fk8s := fake.NewSimpleClientset(pod)
316322
c.client = fk8s
@@ -495,6 +501,7 @@ func TestUpdatePodStatusWithNilProviderStatus(t *testing.T) {
495501
pod.Namespace = "default"
496502
pod.Name = "nginx"
497503
pod.Spec = newPodSpec()
504+
pod.UID = uuid.NewUUID()
498505

499506
fk8s := fake.NewSimpleClientset(pod)
500507
c.client = fk8s

0 commit comments

Comments
 (0)