|
7 | 7 |
|
8 | 8 | g "github.com/onsi/ginkgo/v2" |
9 | 9 | o "github.com/onsi/gomega" |
| 10 | + "github.com/openshift/origin/test/extended/util/image" |
| 11 | + corev1 "k8s.io/api/core/v1" |
10 | 12 |
|
11 | 13 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
12 | 14 | "k8s.io/apimachinery/pkg/util/wait" |
@@ -278,4 +280,62 @@ spec: |
278 | 280 | o.Expect(err).NotTo(o.HaveOccurred()) |
279 | 281 | o.Expect(out).To(o.ContainSubstring("image: quay.io/wildfly/wildfly-centos7")) |
280 | 282 | }) |
| 283 | + |
| 284 | + g.It("ensure that the label is set for node debug", func() { |
| 285 | + var err error |
| 286 | + |
| 287 | + ns := oc.Namespace() |
| 288 | + |
| 289 | + err = oc.AsAdmin().Run("label").Args("namespace", ns, "pod-security.kubernetes.io/enforce=privileged", "pod-security.kubernetes.io/audit=privileged", "--overwrite").Execute() |
| 290 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 291 | + |
| 292 | + err = oc.AsAdmin().WithoutNamespace().Run("auth").Args("can-i", "get", "nodes").Execute() |
| 293 | + o.Expect(err).NotTo(o.HaveOccurred(), "User should be able to get nodes") |
| 294 | + |
| 295 | + nodes, err := oc.AsAdmin().KubeClient().CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{LabelSelector: "node-role.kubernetes.io/worker"}) |
| 296 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 297 | + o.Expect(nodes.Items).NotTo(o.BeEmpty(), "No worker nodes found") |
| 298 | + |
| 299 | + var readyWorkerNode string |
| 300 | + |
| 301 | + for _, node := range nodes.Items { |
| 302 | + for _, nodeStatus := range node.Status.Conditions { |
| 303 | + if nodeStatus.Type == corev1.NodeReady && nodeStatus.Status == corev1.ConditionTrue { |
| 304 | + readyWorkerNode = node.Name |
| 305 | + break |
| 306 | + } |
| 307 | + } |
| 308 | + } |
| 309 | + o.Expect(readyWorkerNode).NotTo(o.BeEmpty(), "No ready worker node found") |
| 310 | + |
| 311 | + err = oc.AsAdmin().Run("debug").Args("node/"+readyWorkerNode, "--image="+image.LocationFor("registry.k8s.io/e2e-test-images/agnhost:2.53"), "--keep-labels=true", "--preserve-pod=true", "--", "sleep", "1").Execute() |
| 312 | + pods, err := oc.AdminKubeClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: "debug.openshift.io/managed-by=oc-debug"}) |
| 313 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 314 | + o.Expect(pods.Items).To(o.HaveLen(1)) |
| 315 | + o.Expect(pods.Items[0].Labels).To(o.HaveKeyWithValue("debug.openshift.io/managed-by", "oc-debug")) |
| 316 | + o.Expect(pods.Items[0].Labels).To(o.HaveLen(1)) |
| 317 | + |
| 318 | + oc.AsAdmin().Run("delete").Args("pod", pods.Items[0].Name, "-n", ns).Output() |
| 319 | + |
| 320 | + // Wait for the pod to be deleted with 2 minute timeout and 10 second interval |
| 321 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) |
| 322 | + defer cancel() |
| 323 | + err = wait.PollUntilContextTimeout(ctx, 10*time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) { |
| 324 | + pods, err := oc.AdminKubeClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: "debug.openshift.io/managed-by=oc-debug"}) |
| 325 | + if err != nil { |
| 326 | + return false, err |
| 327 | + } |
| 328 | + return len(pods.Items) == 0, nil |
| 329 | + }) |
| 330 | + o.Expect(err).NotTo(o.HaveOccurred(), "Expected debug pod to be deleted") |
| 331 | + |
| 332 | + err = oc.AsAdmin().Run("debug").Args("node/"+readyWorkerNode, "--image="+image.LocationFor("registry.k8s.io/e2e-test-images/agnhost:2.53"), "--preserve-pod=true", "--", "sleep", "1").Execute() |
| 333 | + |
| 334 | + // Tests the code fix in https://github.com/openshift/oc/pull/2074 |
| 335 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 336 | + pods, err = oc.AdminKubeClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: "debug.openshift.io/managed-by=oc-debug"}) |
| 337 | + o.Expect(pods.Items).To(o.HaveLen(1)) |
| 338 | + o.Expect(pods.Items[0].Labels).To(o.HaveKeyWithValue("debug.openshift.io/managed-by", "oc-debug")) |
| 339 | + o.Expect(pods.Items[0].Labels).To(o.HaveLen(1)) |
| 340 | + }) |
281 | 341 | }) |
0 commit comments