-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from previousnext/poddisruptionbudget
Adds k8s_policy_v1beta1_poddisruptionbudget
- Loading branch information
Showing
10 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
internal/kubernetes/policy/v1beta1/poddisruptionbudget/create.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
internal/kubernetes/policy/v1beta1/poddisruptionbudget/delete.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
internal/kubernetes/policy/v1beta1/poddisruptionbudget/generate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
internal/kubernetes/policy/v1beta1/poddisruptionbudget/read.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
internal/kubernetes/policy/v1beta1/poddisruptionbudget/resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
internal/kubernetes/policy/v1beta1/poddisruptionbudget/update.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters