|
| 1 | +package kubernetes |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | +) |
| 9 | + |
| 10 | +func dataSourceKubernetesServiceAccount() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Read: dataSourceKubernetesServiceAccountRead, |
| 13 | + |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "metadata": namespacedMetadataSchema("service account", false), |
| 16 | + "image_pull_secret": { |
| 17 | + Type: schema.TypeList, |
| 18 | + Description: "A list of references to secrets in the same namespace to use for pulling any images in pods that reference this Service Account. More info: http://kubernetes.io/docs/user-guide/secrets#manually-specifying-an-imagepullsecret", |
| 19 | + Computed: true, |
| 20 | + Elem: &schema.Resource{ |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "name": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Description: "Name of the referent. More info: http://kubernetes.io/docs/user-guide/identifiers#names", |
| 25 | + Computed: true, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + "secret": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Description: "A list of secrets allowed to be used by pods running using this Service Account. More info: http://kubernetes.io/docs/user-guide/secrets", |
| 33 | + Computed: true, |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "name": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Description: "Name of the referent. More info: http://kubernetes.io/docs/user-guide/identifiers#names", |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + "automount_service_account_token": { |
| 45 | + Type: schema.TypeBool, |
| 46 | + Description: "True to enable automatic mounting of the service account token", |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + "default_secret_name": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func dataSourceKubernetesServiceAccountRead(d *schema.ResourceData, meta interface{}) error { |
| 58 | + conn, err := meta.(KubeClientsets).MainClientset() |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + metadata := expandMetadata(d.Get("metadata").([]interface{})) |
| 63 | + |
| 64 | + sa, err := conn.CoreV1().ServiceAccounts(metadata.Namespace).Get(metadata.Name, metav1.GetOptions{}) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("Unable to fetch service account from Kubernetes: %s", err) |
| 67 | + } |
| 68 | + |
| 69 | + defaultSecret, err := findDefaultServiceAccount(sa, conn) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("Failed to discover the default service account token: %s", err) |
| 72 | + } |
| 73 | + |
| 74 | + err = d.Set("default_secret_name", defaultSecret) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("Unable to set default_secret_name: %s", err) |
| 77 | + } |
| 78 | + |
| 79 | + d.SetId(buildId(sa.ObjectMeta)) |
| 80 | + |
| 81 | + return resourceKubernetesServiceAccountRead(d, meta) |
| 82 | +} |
0 commit comments