Skip to content

Commit 1ea8db5

Browse files
committed
ocicni: pass a Pod UID down to CNI plugins as K8S_POD_UID
If a pod is deleted from the Kube API while a SetUpPod() call is ongoing it would be nice if the CNI plugin could easily figure that out and exit early. Plugins can watch the Kube API for pod events, but there is a race where the pod could have been deleted + recreated before the plugin is executed and sets up the watches. Since each new pod object will have a different UID, pass the UID we get from the runtime down to the CNI plugins so they can compare the UID they receive from ocicni with one they read from the Kube API. If the two UIDs are different, that means the pod was deleted + recreated before or during the plugin execution, and the plugin may wish to exit early since any information it read from the Kube API and used to configure sandbox resources may be out-of-date. Signed-off-by: Dan Williams <[email protected]>
1 parent 4901c67 commit 1ea8db5

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

pkg/ocicni/ocicni.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ func buildCNIRuntimeConf(podNetwork *PodNetwork, ifName string, runtimeConfig Ru
795795
{"K8S_POD_NAMESPACE", podNetwork.Namespace},
796796
{"K8S_POD_NAME", podNetwork.Name},
797797
{"K8S_POD_INFRA_CONTAINER_ID", podNetwork.ID},
798+
{"K8S_POD_UID", podNetwork.UID},
798799
},
799800
CapabilityArgs: map[string]interface{}{},
800801
}

pkg/ocicni/ocicni_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ var _ = Describe("ocicni operations", func() {
381381
runtimeConfig = RuntimeConfig{IP: "172.16.0.1"}
382382
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
383383
Expect(err).NotTo(HaveOccurred())
384-
Expect(len(rt.Args)).To(Equal(5))
385-
Expect(rt.Args[4][1]).To(Equal("172.16.0.1"))
384+
Expect(len(rt.Args)).To(Equal(6))
385+
Expect(rt.Args[5][1]).To(Equal("172.16.0.1"))
386386

387387
// runtimeConfig with invalid MAC
388388
runtimeConfig = RuntimeConfig{MAC: "f0:a6"}
@@ -393,16 +393,16 @@ var _ = Describe("ocicni operations", func() {
393393
runtimeConfig = RuntimeConfig{MAC: "9e:0c:d9:b2:f0:a6"}
394394
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
395395
Expect(err).NotTo(HaveOccurred())
396-
Expect(len(rt.Args)).To(Equal(5))
397-
Expect(rt.Args[4][1]).To(Equal("9e:0c:d9:b2:f0:a6"))
396+
Expect(len(rt.Args)).To(Equal(6))
397+
Expect(rt.Args[5][1]).To(Equal("9e:0c:d9:b2:f0:a6"))
398398

399399
// runtimeConfig with valid IP and valid MAC
400400
runtimeConfig = RuntimeConfig{IP: "172.16.0.1", MAC: "9e:0c:d9:b2:f0:a6"}
401401
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
402402
Expect(err).NotTo(HaveOccurred())
403-
Expect(len(rt.Args)).To(Equal(6))
404-
Expect(rt.Args[4][1]).To(Equal("172.16.0.1"))
405-
Expect(rt.Args[5][1]).To(Equal("9e:0c:d9:b2:f0:a6"))
403+
Expect(len(rt.Args)).To(Equal(7))
404+
Expect(rt.Args[5][1]).To(Equal("172.16.0.1"))
405+
Expect(rt.Args[6][1]).To(Equal("9e:0c:d9:b2:f0:a6"))
406406

407407
// runtimeConfig with portMappings is nil
408408
runtimeConfig = RuntimeConfig{PortMappings: nil}
@@ -499,6 +499,7 @@ var _ = Describe("ocicni operations", func() {
499499
Name: "pod1",
500500
Namespace: "namespace1",
501501
ID: "1234567890",
502+
UID: "9414bd03-b3d3-453e-9d9f-47dcee07958c",
502503
NetNS: networkNS.Path(),
503504
}
504505
results, err := ocicni.SetUpPod(podNet)
@@ -579,6 +580,7 @@ var _ = Describe("ocicni operations", func() {
579580
Name: "pod1",
580581
Namespace: "namespace1",
581582
ID: "1234567890",
583+
UID: "9414bd03-b3d3-453e-9d9f-47dcee07958c",
582584
NetNS: networkNS.Path(),
583585
Networks: []NetAttachment{
584586
{Name: "network3"},
@@ -656,6 +658,7 @@ var _ = Describe("ocicni operations", func() {
656658
Name: "pod1",
657659
Namespace: "namespace1",
658660
ID: "1234567890",
661+
UID: "9414bd03-b3d3-453e-9d9f-47dcee07958c",
659662
NetNS: networkNS.Path(),
660663
Networks: []NetAttachment{
661664
{Name: "network3"},
@@ -731,6 +734,7 @@ var _ = Describe("ocicni operations", func() {
731734
Name: "pod1",
732735
Namespace: "namespace1",
733736
ID: containerID,
737+
UID: "9414bd03-b3d3-453e-9d9f-47dcee07958c",
734738
NetNS: networkNS.Path(),
735739
}
736740
})
@@ -797,6 +801,7 @@ var _ = Describe("ocicni operations", func() {
797801
Name: "pod1",
798802
Namespace: "namespace1",
799803
ID: containerID,
804+
UID: "9414bd03-b3d3-453e-9d9f-47dcee07958c",
800805
NetNS: networkNS.Path(),
801806
Networks: []NetAttachment{
802807
{Name: netName1},

pkg/ocicni/types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ type BandwidthConfig struct {
7171

7272
// PodNetwork configures the network of a pod sandbox.
7373
type PodNetwork struct {
74-
// Name is the name of the sandbox.
74+
// Name is the name of the pod.
7575
Name string
76-
// Namespace is the namespace of the sandbox.
76+
// Namespace is the namespace of the pod.
7777
Namespace string
7878
// ID is the id of the sandbox container.
7979
ID string
80+
// UID is the UID of the pod that owns the sandbox.
81+
UID string
8082
// NetNS is the network namespace path of the sandbox.
8183
NetNS string
8284

0 commit comments

Comments
 (0)