Skip to content

Commit

Permalink
Adds hostaliases
Browse files Browse the repository at this point in the history
  • Loading branch information
nickschuch committed Jan 30, 2018
1 parent 5982487 commit 95f9340
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 5 deletions.
8 changes: 6 additions & 2 deletions cronjob/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/container"
"github.com/previousnext/terraform-provider-k8s/hostaliases"
"github.com/previousnext/terraform-provider-k8s/label"
"github.com/previousnext/terraform-provider-k8s/volume"
)
Expand Down Expand Up @@ -43,8 +44,9 @@ func Resource() *schema.Resource {
Description: "ServiceAccount to associate with this CronJob.",
Optional: true,
},
"container": container.Fields(),
"volume": volume.Fields(),
"hostaliases": hostaliases.Fields(),
"container": container.Fields(),
"volume": volume.Fields(),
},
}
}
Expand All @@ -57,6 +59,7 @@ func generateCronJob(d *schema.ResourceData) (batchv1beta1.CronJob, error) {
labels = d.Get("labels").(map[string]interface{})
schedule = d.Get("schedule").(string)
serviceaccount = d.Get("service_account").(string)
aliases = d.Get("hostaliases").([]interface{})
containers = d.Get("container").([]interface{})
volumes = d.Get("volume").([]interface{})
)
Expand Down Expand Up @@ -90,6 +93,7 @@ func generateCronJob(d *schema.ResourceData) (batchv1beta1.CronJob, error) {
Containers: containerList,
Volumes: volumeList,
ServiceAccountName: serviceaccount,
HostAliases: hostaliases.Expand(aliases),
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions cronjob/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"k8s.io/client-go/kubernetes"

"github.com/previousnext/terraform-provider-k8s/container"
"github.com/previousnext/terraform-provider-k8s/hostaliases"
"github.com/previousnext/terraform-provider-k8s/utils/id"
"github.com/previousnext/terraform-provider-k8s/volume"
)
Expand Down Expand Up @@ -34,6 +35,7 @@ func resourceRead(d *schema.ResourceData, m interface{}) error {
d.Set("schedule", cronJob.Spec.Schedule)
d.Set("service_account", cronJob.Spec.JobTemplate.Spec.Template.Spec.ServiceAccountName)
d.Set("container", container.Flatten(cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers))
d.Set("hostaliases", hostaliases.Flatten(cronJob.Spec.JobTemplate.Spec.Template.Spec.HostAliases))
d.Set("volume", volume.Flatten(cronJob.Spec.JobTemplate.Spec.Template.Spec.Volumes))

return nil
Expand Down
4 changes: 4 additions & 0 deletions daemonset/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/container"
"github.com/previousnext/terraform-provider-k8s/hostaliases"
"github.com/previousnext/terraform-provider-k8s/label"
"github.com/previousnext/terraform-provider-k8s/volume"
)
Expand Down Expand Up @@ -44,6 +45,7 @@ func Resource() *schema.Resource {
Optional: true,
},
"init_container": container.Fields(),
"hostaliases": hostaliases.Fields(),
"container": container.Fields(),
"volume": volume.Fields(),
},
Expand All @@ -59,6 +61,7 @@ func generateDaemonSet(d *schema.ResourceData) (appsv1beta2.DaemonSet, error) {
hostNetwork = d.Get("host_network").(bool)
serviceAccount = d.Get("service_account").(string)
initContainer = d.Get("init_container").([]interface{})
aliases = d.Get("hostaliases").([]interface{})
containers = d.Get("container").([]interface{})
volumes = d.Get("volume").([]interface{})
)
Expand Down Expand Up @@ -98,6 +101,7 @@ func generateDaemonSet(d *schema.ResourceData) (appsv1beta2.DaemonSet, error) {
InitContainers: initContainerList,
Containers: containerList,
Volumes: volumeList,
HostAliases: hostaliases.Expand(aliases),
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions daemonset/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"k8s.io/client-go/kubernetes"

"github.com/previousnext/terraform-provider-k8s/container"
"github.com/previousnext/terraform-provider-k8s/hostaliases"
"github.com/previousnext/terraform-provider-k8s/utils/id"
"github.com/previousnext/terraform-provider-k8s/volume"
)
Expand All @@ -32,6 +33,7 @@ func resourceRead(d *schema.ResourceData, m interface{}) error {
d.Set("namespace", daemonset.ObjectMeta.Namespace)
d.Set("service_account", daemonset.Spec.Template.Spec.ServiceAccountName)
d.Set("labels", daemonset.ObjectMeta.Labels)
d.Set("hostaliases", hostaliases.Flatten(daemonset.Spec.Template.Spec.HostAliases))
d.Set("container", container.Flatten(daemonset.Spec.Template.Spec.Containers))
d.Set("volume", volume.Flatten(daemonset.Spec.Template.Spec.Volumes))

Expand Down
10 changes: 7 additions & 3 deletions deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/container"
"github.com/previousnext/terraform-provider-k8s/hostaliases"
"github.com/previousnext/terraform-provider-k8s/label"
"github.com/previousnext/terraform-provider-k8s/volume"
)
Expand Down Expand Up @@ -36,9 +37,10 @@ func Resource() *schema.Resource {
Description: "ServiceAccount to associate with this Deployment.",
Optional: true,
},
"labels": label.Fields(),
"container": container.Fields(),
"volume": volume.Fields(),
"labels": label.Fields(),
"container": container.Fields(),
"hostaliases": hostaliases.Fields(),
"volume": volume.Fields(),
},
}
}
Expand All @@ -49,6 +51,7 @@ func generateDeployment(d *schema.ResourceData) (appsv1beta2.Deployment, error)
namespace = d.Get("namespace").(string)
serviceaccount = d.Get("service_account").(string)
labels = d.Get("labels").(map[string]interface{})
aliases = d.Get("hostaliases").([]interface{})
containers = d.Get("container").([]interface{})
volumes = d.Get("volume").([]interface{})
)
Expand Down Expand Up @@ -81,6 +84,7 @@ func generateDeployment(d *schema.ResourceData) (appsv1beta2.Deployment, error)
ServiceAccountName: serviceaccount,
Containers: containerList,
Volumes: volumeList,
HostAliases: hostaliases.Expand(aliases),
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions deployment/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/client-go/kubernetes"

"github.com/previousnext/terraform-provider-k8s/container"
"github.com/previousnext/terraform-provider-k8s/hostaliases"
"github.com/previousnext/terraform-provider-k8s/utils/id"
"github.com/previousnext/terraform-provider-k8s/volume"
)
Expand Down Expand Up @@ -39,6 +40,7 @@ func resourceRead(d *schema.ResourceData, m interface{}) error {
d.Set("service_account", deployment.Spec.Template.Spec.ServiceAccountName)
d.Set("labels", deployment.ObjectMeta.Labels)
d.Set("image", deployment.Spec.Template.Spec.Containers[0].Image)
d.Set("hostaliases", hostaliases.Flatten(deployment.Spec.Template.Spec.HostAliases))
d.Set("container", container.Flatten(deployment.Spec.Template.Spec.Containers))
d.Set("volume", volume.Flatten(deployment.Spec.Template.Spec.Volumes))

Expand Down
70 changes: 70 additions & 0 deletions hostaliases/hostaliases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package hostaliases

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/previousnext/terraform-provider-k8s/hostaliases/hostnames"
corev1 "k8s.io/api/core/v1"
)

// Fields returns the fields for this package.
func Fields() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Description: "Environment variables which can be set for a container",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Required: true,
Description: "IP Address of the host.",
},
"hostnames": hostnames.Fields(),
},
},
}
}

// Expand will return a structured object.
func Expand(in []interface{}) []corev1.HostAlias {
if len(in) == 0 {
return []corev1.HostAlias{}
}

aliases := make([]corev1.HostAlias, len(in))

for key, v := range in {
value := v.(map[string]interface{})

if ip, ok := value["ip"]; ok && ip != "" {
aliases[key].IP = ip.(string)
}

if hosts, ok := value["hostnames"]; ok {
aliases[key].Hostnames = hostnames.Expand(hosts.([]interface{}))
}
}

return aliases
}

// Flatten structured object into unstructured.
func Flatten(in []corev1.HostAlias) []interface{} {
flattened := make([]interface{}, len(in))

for key, value := range in {
row := map[string]interface{}{}

if value.IP != "" {
row["ip"] = value.IP
}

if len(value.Hostnames) > 0 {
row["hostnames"] = value.Hostnames
}

flattened[key] = row
}

return flattened
}
26 changes: 26 additions & 0 deletions hostaliases/hostnames/hostnames.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hostnames

import (
"github.com/hashicorp/terraform/helper/schema"
)

// Fields returns the fields for this package.
func Fields() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Description: "Hostnames to assign to an IP.",
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
}
}

// Expand will return a structured object.
func Expand(s []interface{}) []string {
result := make([]string, len(s), len(s))

for k, v := range s {
result[k] = v.(string)
}

return result
}

0 comments on commit 95f9340

Please sign in to comment.