Skip to content

Commit b4d682d

Browse files
authored
Add StateUpgraders for resources field change (#1082)
* Add StateUpgrader for kubernetes_deployment * Add StateUpgraders for pod, job, cronjob, daemonset, statefulset
1 parent b3dd8b8 commit b4d682d

15 files changed

+766
-279
lines changed

kubernetes/resource_kubernetes_cron_job.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package kubernetes
33
import (
44
"context"
55
"fmt"
6-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
76
"log"
87
"time"
98

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213
"k8s.io/api/batch/v1beta1"
@@ -23,21 +24,31 @@ func resourceKubernetesCronJob() *schema.Resource {
2324
Importer: &schema.ResourceImporter{
2425
StateContext: schema.ImportStatePassthroughContext,
2526
},
26-
2727
Timeouts: &schema.ResourceTimeout{
2828
Delete: schema.DefaultTimeout(1 * time.Minute),
2929
},
30+
StateUpgraders: []schema.StateUpgrader{
31+
{
32+
Version: 0,
33+
Type: resourceKubernetesCronJobV0().CoreConfigSchema().ImpliedType(),
34+
Upgrade: resourceKubernetesCronJobUpgradeV0,
35+
},
36+
},
37+
SchemaVersion: 1,
38+
Schema: resourceKubernetesCronJobSchemaV1(),
39+
}
40+
}
3041

31-
Schema: map[string]*schema.Schema{
32-
"metadata": namespacedMetadataSchema("cronjob", true),
33-
"spec": {
34-
Type: schema.TypeList,
35-
Description: "Spec of the cron job owned by the cluster",
36-
Required: true,
37-
MaxItems: 1,
38-
Elem: &schema.Resource{
39-
Schema: cronJobSpecFields(),
40-
},
42+
func resourceKubernetesCronJobSchemaV1() map[string]*schema.Schema {
43+
return map[string]*schema.Schema{
44+
"metadata": namespacedMetadataSchema("cronjob", true),
45+
"spec": {
46+
Type: schema.TypeList,
47+
Description: "Spec of the cron job owned by the cluster",
48+
Required: true,
49+
MaxItems: 1,
50+
Elem: &schema.Resource{
51+
Schema: cronJobSpecFields(),
4152
},
4253
},
4354
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package kubernetes
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func resourceKubernetesCronJobV0() *schema.Resource {
10+
schemaV1 := resourceKubernetesCronJobSchemaV1()
11+
schemaV0 := patchJobTemplatePodSpecWithResourcesFieldV0(schemaV1)
12+
return &schema.Resource{Schema: schemaV0}
13+
}
14+
15+
func resourceKubernetesCronJobUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
16+
return upgradeJobTemplatePodSpecWithResourcesFieldV0(ctx, rawState, meta)
17+
}

kubernetes/resource_kubernetes_daemonset.go

Lines changed: 85 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -28,98 +28,108 @@ func resourceKubernetesDaemonSet() *schema.Resource {
2828
Importer: &schema.ResourceImporter{
2929
StateContext: schema.ImportStatePassthroughContext,
3030
},
31-
31+
StateUpgraders: []schema.StateUpgrader{
32+
{
33+
Version: 0,
34+
Type: resourceKubernetesDaemonSetV0().CoreConfigSchema().ImpliedType(),
35+
Upgrade: resourceKubernetesDaemonSetUpgradeV0,
36+
},
37+
},
38+
SchemaVersion: 1,
3239
Timeouts: &schema.ResourceTimeout{
3340
Create: schema.DefaultTimeout(10 * time.Minute),
3441
Update: schema.DefaultTimeout(10 * time.Minute),
3542
Delete: schema.DefaultTimeout(10 * time.Minute),
3643
},
44+
Schema: resourceKubernetesDaemonSetSchemaV1(),
45+
}
46+
}
3747

38-
Schema: map[string]*schema.Schema{
39-
"metadata": namespacedMetadataSchema("daemonset", true),
40-
"spec": {
41-
Type: schema.TypeList,
42-
Description: "Spec defines the specification of the desired behavior of the daemonset. More info: https://v1-9.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/#daemonset-v1-apps",
43-
Required: true,
44-
MaxItems: 1,
45-
Elem: &schema.Resource{
46-
Schema: map[string]*schema.Schema{
47-
"min_ready_seconds": {
48-
Type: schema.TypeInt,
49-
Description: "Minimum number of seconds for which a newly created pod should be ready without any of its container crashing, for it to be considered available. Defaults to 0 (pod will be considered available as soon as it is ready)",
50-
Optional: true,
51-
Default: 0,
52-
},
53-
"revision_history_limit": {
54-
Type: schema.TypeInt,
55-
Description: "The number of old ReplicaSets to retain to allow rollback. This is a pointer to distinguish between explicit zero and not specified. Defaults to 10.",
56-
Optional: true,
57-
Default: 10,
58-
},
59-
"selector": {
60-
Type: schema.TypeList,
61-
Description: "A label query over pods that are managed by the DaemonSet.",
62-
Optional: true,
63-
ForceNew: true,
64-
MaxItems: 1,
65-
Elem: &schema.Resource{
66-
Schema: labelSelectorFields(true),
67-
},
48+
func resourceKubernetesDaemonSetSchemaV1() map[string]*schema.Schema {
49+
return map[string]*schema.Schema{
50+
"metadata": namespacedMetadataSchema("daemonset", true),
51+
"spec": {
52+
Type: schema.TypeList,
53+
Description: "Spec defines the specification of the desired behavior of the daemonset. More info: https://v1-9.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/#daemonset-v1-apps",
54+
Required: true,
55+
MaxItems: 1,
56+
Elem: &schema.Resource{
57+
Schema: map[string]*schema.Schema{
58+
"min_ready_seconds": {
59+
Type: schema.TypeInt,
60+
Description: "Minimum number of seconds for which a newly created pod should be ready without any of its container crashing, for it to be considered available. Defaults to 0 (pod will be considered available as soon as it is ready)",
61+
Optional: true,
62+
Default: 0,
63+
},
64+
"revision_history_limit": {
65+
Type: schema.TypeInt,
66+
Description: "The number of old ReplicaSets to retain to allow rollback. This is a pointer to distinguish between explicit zero and not specified. Defaults to 10.",
67+
Optional: true,
68+
Default: 10,
69+
},
70+
"selector": {
71+
Type: schema.TypeList,
72+
Description: "A label query over pods that are managed by the DaemonSet.",
73+
Optional: true,
74+
ForceNew: true,
75+
MaxItems: 1,
76+
Elem: &schema.Resource{
77+
Schema: labelSelectorFields(true),
6878
},
69-
"strategy": {
70-
Type: schema.TypeList,
71-
Optional: true,
72-
Computed: true,
73-
Description: "The deployment strategy used to replace existing pods with new ones.",
74-
MaxItems: 1,
75-
Elem: &schema.Resource{
76-
Schema: map[string]*schema.Schema{
77-
"type": {
78-
Type: schema.TypeString,
79-
Description: "Type of deployment. Can be 'RollingUpdate' or 'OnDelete'. Default is RollingUpdate.",
80-
Optional: true,
81-
Default: "RollingUpdate",
82-
ValidateFunc: validation.StringInSlice([]string{"RollingUpdate", "OnDelete"}, false),
83-
},
84-
"rolling_update": {
85-
Type: schema.TypeList,
86-
Description: "Rolling update config params. Present only if type = 'RollingUpdate'.",
87-
Optional: true,
88-
Computed: true,
89-
MaxItems: 1,
90-
Elem: &schema.Resource{
91-
Schema: map[string]*schema.Schema{
92-
"max_unavailable": {
93-
Type: schema.TypeString,
94-
Description: "The maximum number of DaemonSet pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of total number of DaemonSet pods at the start of the update (ex: 10%). Absolute number is calculated from percentage by rounding up. This cannot be 0. Default value is 1. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their pods stopped for an update at any given time. The update starts by stopping at most 30% of those DaemonSet pods and then brings up new DaemonSet pods in their place. Once the new pods are available, it then proceeds onto other DaemonSet pods, thus ensuring that at least 70% of original number of DaemonSet pods are available at all times during the update.",
95-
Optional: true,
96-
Default: 1,
97-
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^([1-9][0-9]*|[1-9][0-9]%|[1-9]%|100%)$`), ""),
98-
},
79+
},
80+
"strategy": {
81+
Type: schema.TypeList,
82+
Optional: true,
83+
Computed: true,
84+
Description: "The deployment strategy used to replace existing pods with new ones.",
85+
MaxItems: 1,
86+
Elem: &schema.Resource{
87+
Schema: map[string]*schema.Schema{
88+
"type": {
89+
Type: schema.TypeString,
90+
Description: "Type of deployment. Can be 'RollingUpdate' or 'OnDelete'. Default is RollingUpdate.",
91+
Optional: true,
92+
Default: "RollingUpdate",
93+
ValidateFunc: validation.StringInSlice([]string{"RollingUpdate", "OnDelete"}, false),
94+
},
95+
"rolling_update": {
96+
Type: schema.TypeList,
97+
Description: "Rolling update config params. Present only if type = 'RollingUpdate'.",
98+
Optional: true,
99+
Computed: true,
100+
MaxItems: 1,
101+
Elem: &schema.Resource{
102+
Schema: map[string]*schema.Schema{
103+
"max_unavailable": {
104+
Type: schema.TypeString,
105+
Description: "The maximum number of DaemonSet pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of total number of DaemonSet pods at the start of the update (ex: 10%). Absolute number is calculated from percentage by rounding up. This cannot be 0. Default value is 1. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their pods stopped for an update at any given time. The update starts by stopping at most 30% of those DaemonSet pods and then brings up new DaemonSet pods in their place. Once the new pods are available, it then proceeds onto other DaemonSet pods, thus ensuring that at least 70% of original number of DaemonSet pods are available at all times during the update.",
106+
Optional: true,
107+
Default: 1,
108+
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^([1-9][0-9]*|[1-9][0-9]%|[1-9]%|100%)$`), ""),
99109
},
100110
},
101111
},
102112
},
103113
},
104114
},
105-
"template": {
106-
Type: schema.TypeList,
107-
Description: "An object that describes the pod that will be created. The DaemonSet will create exactly one copy of this pod on every node that matches the template's node selector (or on every node if no node selector is specified). More info: https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/#pod-template",
108-
Required: true,
109-
MaxItems: 1,
110-
Elem: &schema.Resource{
111-
Schema: podTemplateFields("daemon set"),
112-
},
115+
},
116+
"template": {
117+
Type: schema.TypeList,
118+
Description: "An object that describes the pod that will be created. The DaemonSet will create exactly one copy of this pod on every node that matches the template's node selector (or on every node if no node selector is specified). More info: https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/#pod-template",
119+
Required: true,
120+
MaxItems: 1,
121+
Elem: &schema.Resource{
122+
Schema: podTemplateFields("daemon set"),
113123
},
114124
},
115125
},
116126
},
117-
"wait_for_rollout": {
118-
Type: schema.TypeBool,
119-
Description: "Wait for the rollout of the deployment to complete. Defaults to true.",
120-
Default: true,
121-
Optional: true,
122-
},
127+
},
128+
"wait_for_rollout": {
129+
Type: schema.TypeBool,
130+
Description: "Wait for the rollout of the deployment to complete. Defaults to true.",
131+
Default: true,
132+
Optional: true,
123133
},
124134
}
125135
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package kubernetes
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func resourceKubernetesDaemonSetV0() *schema.Resource {
10+
schemaV1 := resourceKubernetesDaemonSetSchemaV1()
11+
schemaV0 := patchTemplatePodSpecWithResourcesFieldV0(schemaV1)
12+
return &schema.Resource{Schema: schemaV0}
13+
}
14+
15+
func resourceKubernetesDaemonSetUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
16+
return upgradeTemplatePodSpecWithResourcesFieldV0(ctx, rawState, meta)
17+
}

0 commit comments

Comments
 (0)