|
| 1 | +package singlecluster_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + |
| 9 | + "github.com/rancher/fleet/e2e/testenv/kubectl" |
| 10 | +) |
| 11 | + |
| 12 | +type containerLastTerminatedState struct { |
| 13 | + ExitCode int `json:"exitCode"` |
| 14 | + Reason string `json:"reason"` |
| 15 | +} |
| 16 | + |
| 17 | +var _ = Describe("Shuts down gracefully", func() { |
| 18 | + var ( |
| 19 | + k kubectl.Command |
| 20 | + ns string |
| 21 | + labels string |
| 22 | + ) |
| 23 | + |
| 24 | + When("the agent receives SIGTERM", func() { |
| 25 | + BeforeEach(func() { |
| 26 | + ns = "cattle-fleet-local-system" |
| 27 | + k = env.Kubectl.Namespace(ns) |
| 28 | + }) |
| 29 | + |
| 30 | + JustBeforeEach(func() { |
| 31 | + out, err := k.Run("exec", "deploy/fleet-agent", "--", "kill", "1") |
| 32 | + Expect(err).ToNot(HaveOccurred(), out) |
| 33 | + }) |
| 34 | + |
| 35 | + It("exits gracefully", func() { |
| 36 | + Eventually(func(g Gomega) { |
| 37 | + out, err := k.Get("pod", "-l", "app=fleet-agent", "-o", "jsonpath={.items[0].status.containerStatuses[0].lastState.terminated}") |
| 38 | + g.Expect(err).ToNot(HaveOccurred()) |
| 39 | + |
| 40 | + var state containerLastTerminatedState |
| 41 | + err = json.Unmarshal([]byte(out), &state) |
| 42 | + g.Expect(err).ToNot(HaveOccurred()) |
| 43 | + |
| 44 | + g.Expect(state.ExitCode).To(Equal(0)) |
| 45 | + g.Expect(state.Reason).To(Equal("Completed")) |
| 46 | + }).Should(Succeed()) |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + // Note: when dealing with sharded deployments, running `kubectl exec <deployment_name>` does not resolve to the |
| 51 | + // right deployment, as if name resolution were prefix-based. This causes checks against pods' container states |
| 52 | + // to fail, because changes would have been applied to a different deployment. |
| 53 | + // Therefore, we explicitly compute pod names based on labels. |
| 54 | + When("the fleet-controller deployment receives SIGTERM", func() { |
| 55 | + BeforeEach(func() { |
| 56 | + ns = "cattle-fleet-system" |
| 57 | + k = env.Kubectl.Namespace(ns) |
| 58 | + labels = "app=fleet-controller,fleet.cattle.io/shard-default=true" |
| 59 | + }) |
| 60 | + |
| 61 | + JustBeforeEach(func() { |
| 62 | + pod, err := k.Get("pod", "-l", labels, "-o", "jsonpath={.items[0].metadata.name}") |
| 63 | + Expect(err).ToNot(HaveOccurred(), pod) |
| 64 | + |
| 65 | + out, err := k.Run("exec", pod, "--", "kill", "1") |
| 66 | + Expect(err).ToNot(HaveOccurred(), out) |
| 67 | + }) |
| 68 | + |
| 69 | + It("exits gracefully", func() { |
| 70 | + Eventually(func(g Gomega) { |
| 71 | + out, err := k.Get( |
| 72 | + "pod", |
| 73 | + "-l", labels, |
| 74 | + "-o", `jsonpath={.items[0].status.containerStatuses[?(@.name=="fleet-controller")].lastState.terminated}`, |
| 75 | + ) |
| 76 | + g.Expect(err).ToNot(HaveOccurred()) |
| 77 | + |
| 78 | + var state containerLastTerminatedState |
| 79 | + err = json.Unmarshal([]byte(out), &state) |
| 80 | + g.Expect(err).ToNot(HaveOccurred(), out) |
| 81 | + |
| 82 | + g.Expect(state.ExitCode).To(Equal(0)) |
| 83 | + g.Expect(state.Reason).To(Equal("Completed")) |
| 84 | + }).Should(Succeed()) |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + When("the gitjob deployment receives SIGTERM", func() { |
| 89 | + BeforeEach(func() { |
| 90 | + ns = "cattle-fleet-system" |
| 91 | + k = env.Kubectl.Namespace(ns) |
| 92 | + labels = "app=gitjob,fleet.cattle.io/shard-default=true" |
| 93 | + }) |
| 94 | + |
| 95 | + JustBeforeEach(func() { |
| 96 | + pod, err := k.Get("pod", "-l", labels, "-o", "jsonpath={.items[0].metadata.name}") |
| 97 | + Expect(err).ToNot(HaveOccurred(), pod) |
| 98 | + |
| 99 | + out, err := k.Run("exec", pod, "--", "kill", "1") |
| 100 | + Expect(err).ToNot(HaveOccurred(), out) |
| 101 | + }) |
| 102 | + |
| 103 | + It("exits gracefully", func() { |
| 104 | + Eventually(func(g Gomega) { |
| 105 | + out, err := k.Get( |
| 106 | + "pod", |
| 107 | + "-l", labels, |
| 108 | + "-o", "jsonpath={.items[0].status.containerStatuses[0].lastState.terminated}", |
| 109 | + ) |
| 110 | + g.Expect(err).ToNot(HaveOccurred()) |
| 111 | + |
| 112 | + var state containerLastTerminatedState |
| 113 | + err = json.Unmarshal([]byte(out), &state) |
| 114 | + g.Expect(err).ToNot(HaveOccurred()) |
| 115 | + |
| 116 | + g.Expect(state.ExitCode).To(Equal(0)) |
| 117 | + g.Expect(state.Reason).To(Equal("Completed")) |
| 118 | + }).Should(Succeed()) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + When("the helmops deployment receives SIGTERM", func() { |
| 123 | + BeforeEach(func() { |
| 124 | + ns = "cattle-fleet-system" |
| 125 | + k = env.Kubectl.Namespace(ns) |
| 126 | + labels = "app=helmops,fleet.cattle.io/shard-default=true" |
| 127 | + }) |
| 128 | + |
| 129 | + JustBeforeEach(func() { |
| 130 | + pod, err := k.Get("pod", "-l", labels, "-o", "jsonpath={.items[0].metadata.name}") |
| 131 | + Expect(err).ToNot(HaveOccurred(), pod) |
| 132 | + |
| 133 | + out, err := k.Run("exec", pod, "--", "kill", "1") |
| 134 | + Expect(err).ToNot(HaveOccurred(), out) |
| 135 | + }) |
| 136 | + |
| 137 | + It("exits gracefully", func() { |
| 138 | + Eventually(func(g Gomega) { |
| 139 | + out, err := k.Get( |
| 140 | + "pod", |
| 141 | + "-l", labels, |
| 142 | + "-o", "jsonpath={.items[0].status.containerStatuses[0].lastState.terminated}", |
| 143 | + ) |
| 144 | + g.Expect(err).ToNot(HaveOccurred()) |
| 145 | + |
| 146 | + var state containerLastTerminatedState |
| 147 | + err = json.Unmarshal([]byte(out), &state) |
| 148 | + g.Expect(err).ToNot(HaveOccurred()) |
| 149 | + |
| 150 | + g.Expect(state.ExitCode).To(Equal(0)) |
| 151 | + g.Expect(state.Reason).To(Equal("Completed")) |
| 152 | + }).Should(Succeed()) |
| 153 | + }) |
| 154 | + }) |
| 155 | +}) |
0 commit comments