Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): #2645 Fixed data source label selection #2660

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2660.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
`data_source_kubernetes_pod_v1` : Fixed name required problem and label selection issue
```
30 changes: 28 additions & 2 deletions kubernetes/data_source_kubernetes_pod_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
60 changes: 60 additions & 0 deletions kubernetes/data_source_kubernetes_pod_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
Loading