From 1add28e12de56d32615ba33352d542961640bb16 Mon Sep 17 00:00:00 2001 From: Hugo Joubert Date: Mon, 30 Dec 2024 16:02:45 +0100 Subject: [PATCH 1/5] fix(app): #2645 Fixed data source label selection Fixed the missing implementation of selection with label in the datasource. Before it was only getting the pods with the name attribute, now it can select with labels --- kubernetes/data_source_kubernetes_pod_v1.go | 30 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/kubernetes/data_source_kubernetes_pod_v1.go b/kubernetes/data_source_kubernetes_pod_v1.go index a7878d9337..182b18a00d 100644 --- a/kubernetes/data_source_kubernetes_pod_v1.go +++ b/kubernetes/data_source_kubernetes_pod_v1.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -41,6 +42,7 @@ func dataSourceKubernetesPodV1() *schema.Resource { } func dataSourceKubernetesPodV1Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var pod v1.Pod conn, err := meta.(KubeClientsets).MainClientset() if err != nil { return diag.FromErr(err) @@ -54,8 +56,32 @@ func dataSourceKubernetesPodV1Read(ctx context.Context, d *schema.ResourceData, } d.SetId(buildId(om)) - log.Printf("[INFO] Reading pod %s", metadata.Name) - pod, err := conn.CoreV1().Pods(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{}) + pods := conn.CoreV1().Pods(metadata.Namespace) + + if metadata.Name != "" { + log.Printf("[INFO] Getting pod %s", metadata.Name) + podResult, getErr := pods.Get(ctx, metadata.Name, metav1.GetOptions{}) + if getErr != nil { + err = getErr + } else { + pod = *podResult + } + } else { + log.Printf("[INFO] Listing pods") + listOptions := metav1.ListOptions{ + LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{MatchLabels: metadata.Labels}), + } + podList, listErr := pods.List(ctx, listOptions) + if listErr != nil { + err = listErr + } else { + if len(podList.Items) == 0 { + return diag.Errorf("No pods found") + } + pod = podList.Items[0] + } + } + if err != nil { if apierrors.IsNotFound(err) { return nil From 022f6b474d384bd52ae1468dcd14ccd37e8a08e3 Mon Sep 17 00:00:00 2001 From: Hugo Joubert Date: Mon, 30 Dec 2024 16:11:14 +0100 Subject: [PATCH 2/5] added changelog --- .changelog/2645.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2645.txt diff --git a/.changelog/2645.txt b/.changelog/2645.txt new file mode 100644 index 0000000000..99cce540ae --- /dev/null +++ b/.changelog/2645.txt @@ -0,0 +1,3 @@ +```release-note:bug +`data_source_kubernetes_pod_v1` : Fixed name required problem and label selection issue +``` \ No newline at end of file From 5cac0da7dce78b85e5062cb24b630e89da04b344 Mon Sep 17 00:00:00 2001 From: Hugo Joubert Date: Tue, 31 Dec 2024 11:22:14 +0100 Subject: [PATCH 3/5] test(app): 2645 Added test for label selection Based on the issue description, the name in the metadata nested resource was mandatory, which is not the intended behaviour. Added test to verify if the label selection is working as intended and that the name attribute is not required every time. --- .../data_source_kubernetes_pod_v1_test.go | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/kubernetes/data_source_kubernetes_pod_v1_test.go b/kubernetes/data_source_kubernetes_pod_v1_test.go index b01d428f35..8aa7554eb8 100644 --- a/kubernetes/data_source_kubernetes_pod_v1_test.go +++ b/kubernetes/data_source_kubernetes_pod_v1_test.go @@ -59,6 +59,37 @@ func TestAccKubernetesDataSourcePodV1_not_found(t *testing.T) { }) } +func TestAccKubernetesDataSourcePodV1_readWithLabel(t *testing.T) { + resourceName := "kubernetes_pod_v1.test" + dataSourceName := "data.kubernetes_pod_v1.test" + name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + labelKey := "app" + labelValue := "test" + imageName := busyboxImage + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccKubernetesDataSourcePodV1_basicWithLabel(name, imageName, labelKey, labelValue), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name), + resource.TestCheckResourceAttr(resourceName, "metadata.0.labels."+labelKey, labelValue), + ), + }, + { + Config: testAccKubernetesDataSourcePodV1_basicWithLabel(name, imageName, labelKey, labelValue) + + testAccKubernetesDataSourcePodV1_readWithLabel(labelKey, labelValue), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "metadata.0.name", name), + resource.TestCheckResourceAttr(resourceName, "metadata.0.labels."+labelKey, labelValue), + ), + }, + }, + }) +} + func testAccKubernetesDataSourcePodV1_basic(name, imageName string) string { return fmt.Sprintf(`resource "kubernetes_pod_v1" "test" { metadata { @@ -74,6 +105,24 @@ func testAccKubernetesDataSourcePodV1_basic(name, imageName string) string { `, name, imageName) } +func testAccKubernetesDataSourcePodV1_basicWithLabel(name, imageName, labelKey, labelValue string) string { + return fmt.Sprintf(`resource "kubernetes_pod_v1" "test" { + metadata { + name = "%s" + labels = { + %s = "%s" + } + } + spec { + container { + image = "%s" + name = "containername" + } + } +} +`, name, labelKey, labelValue, imageName) +} + func testAccKubernetesDataSourcePodV1_read() string { return `data "kubernetes_pod_v1" "test" { metadata { @@ -91,3 +140,14 @@ func testAccKubernetesDataSourcePodV1_nonexistent(name string) string { } `, name) } + +func testAccKubernetesDataSourcePodV1_readWithLabel(labelKey, labelValue string) string { + return fmt.Sprintf(`data "kubernetes_pod_v1" "test" { + metadata { + labels = { + %s = "%s" + } + } +} +`, labelKey, labelValue) +} From 7bf6cbc8c782ac6578040adf32570b86ced55ff9 Mon Sep 17 00:00:00 2001 From: Hugo Joubert Date: Tue, 14 Jan 2025 10:13:23 +0100 Subject: [PATCH 4/5] fix(app): fixed .changelog number --- .changelog/{2645.txt => 2660.txt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changelog/{2645.txt => 2660.txt} (100%) diff --git a/.changelog/2645.txt b/.changelog/2660.txt similarity index 100% rename from .changelog/2645.txt rename to .changelog/2660.txt From 7f6ba55845486aa4478abc11ea2d7d4083606d86 Mon Sep 17 00:00:00 2001 From: Hugo Joubert Date: Tue, 14 Jan 2025 15:04:45 +0100 Subject: [PATCH 5/5] refactor: terrafmt --- .../data_source_kubernetes_pod_v1_test.go | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/kubernetes/data_source_kubernetes_pod_v1_test.go b/kubernetes/data_source_kubernetes_pod_v1_test.go index 8aa7554eb8..63b58eb12d 100644 --- a/kubernetes/data_source_kubernetes_pod_v1_test.go +++ b/kubernetes/data_source_kubernetes_pod_v1_test.go @@ -108,16 +108,16 @@ func testAccKubernetesDataSourcePodV1_basic(name, imageName string) string { func testAccKubernetesDataSourcePodV1_basicWithLabel(name, imageName, labelKey, labelValue string) string { return fmt.Sprintf(`resource "kubernetes_pod_v1" "test" { metadata { - name = "%s" - labels = { - %s = "%s" - } + name = "%s" + labels = { + %s = "%s" + } } spec { - container { - image = "%s" - name = "containername" - } + container { + image = "%s" + name = "containername" + } } } `, name, labelKey, labelValue, imageName) @@ -144,9 +144,9 @@ func testAccKubernetesDataSourcePodV1_nonexistent(name string) string { func testAccKubernetesDataSourcePodV1_readWithLabel(labelKey, labelValue string) string { return fmt.Sprintf(`data "kubernetes_pod_v1" "test" { metadata { - labels = { - %s = "%s" - } + labels = { + %s = "%s" + } } } `, labelKey, labelValue)