diff --git a/.changelog/2660.txt b/.changelog/2660.txt new file mode 100644 index 0000000000..99cce540ae --- /dev/null +++ b/.changelog/2660.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 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 diff --git a/kubernetes/data_source_kubernetes_pod_v1_test.go b/kubernetes/data_source_kubernetes_pod_v1_test.go index b01d428f35..63b58eb12d 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) +}