Skip to content

Commit

Permalink
Merge pull request #15 from previousnext/poddisruptionbudget
Browse files Browse the repository at this point in the history
Adds k8s_policy_v1beta1_poddisruptionbudget
  • Loading branch information
nickschuch authored Jan 9, 2020
2 parents 0056b6c + 26e4b0f commit 8e10740
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func Generate(d *schema.ResourceData) (apiregistrationv1beta1.APIService, error)
serviceName = d.Get(FieldServiceName).(string)
serviceNamespace = d.Get(FieldServiceNamespace).(string)
insecure = d.Get(FieldInsecureSkipTLSVerify).(bool)
groupPriority = d.Get(FieldGroupPriorityMinimum).(int32)
versionPriority = d.Get(FieldVersionPriority).(int32)
groupPriority = d.Get(FieldGroupPriorityMinimum).(int)
versionPriority = d.Get(FieldVersionPriority).(int)
)

crd := apiregistrationv1beta1.APIService{
Expand All @@ -35,8 +35,8 @@ func Generate(d *schema.ResourceData) (apiregistrationv1beta1.APIService, error)
Group: group,
Version: version,
InsecureSkipTLSVerify: insecure,
GroupPriorityMinimum: groupPriority,
VersionPriority: versionPriority,
GroupPriorityMinimum: int32(groupPriority),
VersionPriority: int32(versionPriority),
},
}

Expand Down
6 changes: 3 additions & 3 deletions internal/kubernetes/core/v1/pod/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func Expand(in []interface{}) (corev1.PodTemplateSpec, error) {
}

if val, ok := raw[FieldPullSecret]; ok {
template.Spec.ImagePullSecrets = []corev1.LocalObjectReference{
{
if val != "" {
template.Spec.ImagePullSecrets = append(template.Spec.ImagePullSecrets, corev1.LocalObjectReference{
Name: val.(string),
},
})
}
}

Expand Down
28 changes: 28 additions & 0 deletions internal/kubernetes/policy/v1beta1/poddisruptionbudget/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package poddisruptionbudget

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"

"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/id"
)

// Create the PodDisruptionBudget.
func Create(d *schema.ResourceData, m interface{}) error {
conn := m.(*config.Client)

budget, err := Generate(d)
if err != nil {
return errors.Wrap(err, "failed to generate")
}

out, err := conn.Kubernetes().PolicyV1beta1().PodDisruptionBudgets(budget.ObjectMeta.Namespace).Create(&budget)
if err != nil {
return errors.Wrap(err, "failed to create")
}

d.SetId(id.Join(out.ObjectMeta))

return nil
}
22 changes: 22 additions & 0 deletions internal/kubernetes/policy/v1beta1/poddisruptionbudget/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package poddisruptionbudget

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/id"
)

// Delete the PodDisruptionBudget.
func Delete(d *schema.ResourceData, m interface{}) error {
conn := m.(*config.Client)

namespace, name, err := id.Split(d.Id())
if err != nil {
return errors.Wrap(err, "failed to get ID")
}

return conn.Kubernetes().PolicyV1beta1().PodDisruptionBudgets(namespace).Delete(name, &metav1.DeleteOptions{})
}
39 changes: 39 additions & 0 deletions internal/kubernetes/policy/v1beta1/poddisruptionbudget/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package poddisruptionbudget

import (
"github.com/hashicorp/terraform/helper/schema"
policyv1beta1 "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/previousnext/terraform-provider-k8s/internal/interfaceutils"
)

// Generate the Deployment.
func Generate(d *schema.ResourceData) (policyv1beta1.PodDisruptionBudget, error) {
var (
name = d.Get(FieldName).(string)
namespace = d.Get(FieldNamespace).(string)
rawLabels = d.Get(FieldLabels).(map[string]interface{})
rawMinAvailable = d.Get(FieldMinAvailable).(string)
rawMatchLabels = d.Get(FieldMatchLabels).(map[string]interface{})
)

minAvailable := intstr.Parse(rawMinAvailable)

budget := policyv1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: interfaceutils.ExpandMap(rawLabels),
},
Spec: policyv1beta1.PodDisruptionBudgetSpec{
MinAvailable: &minAvailable,
Selector: &metav1.LabelSelector{
MatchLabels: interfaceutils.ExpandMap(rawMatchLabels),
},
},
}

return budget, nil
}
38 changes: 38 additions & 0 deletions internal/kubernetes/policy/v1beta1/poddisruptionbudget/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package poddisruptionbudget

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
"github.com/previousnext/terraform-provider-k8s/internal/terraform/id"
)

// Read the PodDisruptionBudget.
func Read(d *schema.ResourceData, m interface{}) error {
conn := m.(*config.Client)

namespace, name, err := id.Split(d.Id())
if err != nil {
return errors.Wrap(err, "failed to get ID")
}

budget, err := conn.Kubernetes().PolicyV1beta1().PodDisruptionBudgets(namespace).Get(name, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
// This is how we tell Terraform that the resource does not exist.
d.SetId("")
return nil
} else if err != nil {
return errors.Wrap(err, "failed to get")
}

d.Set(FieldName, budget.ObjectMeta.Name)
d.Set(FieldNamespace, budget.ObjectMeta.Namespace)
d.Set(FieldLabels, budget.ObjectMeta.Labels)
d.Set(FieldMinAvailable, budget.Spec.MinAvailable.String())
d.Set(FieldMatchLabels, budget.Spec.Selector.MatchLabels)

return nil
}
47 changes: 47 additions & 0 deletions internal/kubernetes/policy/v1beta1/poddisruptionbudget/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package poddisruptionbudget

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

const (
// FieldName is a field identifier.
FieldName = "name"
// FieldNamespace is a field identifier.
FieldNamespace = "namespace"
// FieldLabels is a field identifier.
FieldLabels = "labels"
// FieldMinAvailable is a field identifier.
FieldMinAvailable = "min_available"
// FieldMatchLabels is a field identifier.
FieldMatchLabels = "data"
)

// Resource returns this packages resource.
func Resource() *schema.Resource {
return &schema.Resource{
Create: Create,
Read: Read,
Update: Update,
Delete: Delete,

Schema: map[string]*schema.Schema{
FieldName: {
Type: schema.TypeString,
Required: true,
},
FieldNamespace: {
Type: schema.TypeString,
Optional: true,
},
FieldLabels: {
Type: schema.TypeMap,
Optional: true,
},
FieldMatchLabels: {
Type: schema.TypeMap,
Optional: true,
},
},
}
}
25 changes: 25 additions & 0 deletions internal/kubernetes/policy/v1beta1/poddisruptionbudget/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package poddisruptionbudget

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"

"github.com/previousnext/terraform-provider-k8s/internal/terraform/config"
)

// Update the PodDisruptionBudget.
func Update(d *schema.ResourceData, m interface{}) error {
conn := m.(*config.Client)

budget, err := Generate(d)
if err != nil {
return errors.Wrap(err, "failed to generate")
}

_, err = conn.Kubernetes().PolicyV1beta1().PodDisruptionBudgets(budget.ObjectMeta.Namespace).Update(&budget)
if err != nil {
return errors.Wrap(err, "failed to update")
}

return nil
}
2 changes: 1 addition & 1 deletion internal/terraform/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Client) APIExtensions() *apiextensions.Clientset {
return c.apiextensions
}

// APIExtensions ClientSet.
// APIRegistration ClientSet.
func (c *Client) APIRegistration() *apiregistration.Clientset {
return c.apiregistration
}
5 changes: 5 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/secret"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/service"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/core/v1/serviceaccount"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/policy/v1beta1/poddisruptionbudget"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/rbac/v1/clusterrole"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/rbac/v1/clusterrolebinding"
"github.com/previousnext/terraform-provider-k8s/internal/kubernetes/rbac/v1/role"
Expand Down Expand Up @@ -57,6 +58,9 @@ const (

// ResourceAPIService identifier for the Kubernetes APIService.
ResourceAPIService = "k8s_apiregistration_v1beta1_apiservice"

// ResourcePodDisruptionBudget identifier for the Kubernetes PodDisruptionBudget.
ResourcePodDisruptionBudget = "k8s_policy_v1beta1_poddisruptionbudget"
)

// Provider returns this providers resources.
Expand All @@ -82,6 +86,7 @@ func Provider() *schema.Provider {
ResourceClusterRoleBinding: clusterrolebinding.Resource(),
ResourceCustomtResourceDefinition: crd.Resource(),
ResourceAPIService: apiservice.Resource(),
ResourcePodDisruptionBudget: poddisruptionbudget.Resource(),
},
ConfigureFunc: config.Func,
}
Expand Down

0 comments on commit 8e10740

Please sign in to comment.