-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathdata_source_kubernetes_all_namespaces.go
More file actions
66 lines (59 loc) · 1.96 KB
/
data_source_kubernetes_all_namespaces.go
File metadata and controls
66 lines (59 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright IBM Corp. 2017, 2025
// SPDX-License-Identifier: MPL-2.0
package kubernetes
import (
"context"
"crypto/sha256"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func dataSourceKubernetesAllNamespaces() *schema.Resource {
return &schema.Resource{
Description: "This data source provides a mechanism for listing the names of all available namespaces in a Kubernetes cluster. It can be used to check for existence of a specific namespaces or to apply another resource to all or a subset of existing namespaces in a cluster.In Kubernetes, namespaces provide a scope for names and are intended as a way to divide cluster resources between multiple users.",
ReadContext: dataSourceKubernetesAllNamespacesRead,
Schema: map[string]*schema.Schema{
"namespaces": {
Type: schema.TypeList,
Description: "List of all namespaces in a cluster.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceKubernetesAllNamespacesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := meta.(KubeClientsets).MainClientset()
if err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] Listing namespaces")
nsRaw, err := conn.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return diag.FromErr(err)
}
namespaces := make([]string, len(nsRaw.Items))
for i, v := range nsRaw.Items {
namespaces[i] = v.Name
}
log.Printf("[INFO] Received namespaces: %#v", namespaces)
err = d.Set("namespaces", namespaces)
if err != nil {
return diag.FromErr(err)
}
idsum := sha256.New()
for _, v := range namespaces {
_, err := idsum.Write([]byte(v))
if err != nil {
return diag.FromErr(err)
}
}
id := fmt.Sprintf("%x", idsum.Sum(nil))
d.SetId(id)
return nil
}