-
Notifications
You must be signed in to change notification settings - Fork 968
/
schema_resources_migrate.go
162 lines (136 loc) · 5 KB
/
schema_resources_migrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package kubernetes
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// NOTE these functions are for patching the schemas for resources with containers
// to use the old schema for the resources block so we can migrate it to the new format
// without having to duplicate the entire schema
func patchJobTemplatePodSpecWithResourcesFieldV0(m map[string]*schema.Schema) map[string]*schema.Schema {
spec := m["spec"].Elem.(*schema.Resource)
jobTemplate := spec.Schema["job_template"].Elem.(*schema.Resource)
jobSpec := jobTemplate.Schema["spec"].Elem.(*schema.Resource)
template := jobSpec.Schema["template"].Elem.(*schema.Resource)
template.Schema = patchPodSpecWithResourcesFieldV0(template.Schema)
return m
}
func patchTemplatePodSpecWithResourcesFieldV0(m map[string]*schema.Schema) map[string]*schema.Schema {
spec := m["spec"].Elem.(*schema.Resource)
template := spec.Schema["template"].Elem.(*schema.Resource)
template.Schema = patchPodSpecWithResourcesFieldV0(template.Schema)
return m
}
func patchPodSpecWithResourcesFieldV0(m map[string]*schema.Schema) map[string]*schema.Schema {
spec := m["spec"].Elem.(*schema.Resource)
initContainer := spec.Schema["init_container"].Elem.(*schema.Resource)
initContainer.Schema["resources"].Elem = &schema.Resource{
Schema: resourcesFieldV0(),
}
container := spec.Schema["container"].Elem.(*schema.Resource)
container.Schema["resources"].Elem = &schema.Resource{
Schema: resourcesFieldV0(),
}
return m
}
func upgradeJobTemplatePodSpecWithResourcesFieldV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) map[string]interface{} {
s, ok := rawState["spec"].([]interface{})
if !ok || len(s) == 0 {
return rawState
}
spec := s[0].(map[string]interface{})
jt, ok := spec["job_template"].([]interface{})
if !ok || len(jt) == 0 {
return rawState
}
jobTemplate := jt[0].(map[string]interface{})
js, ok := jobTemplate["spec"].([]interface{})
if !ok || len(js) == 0 {
return rawState
}
jobSpec := js[0].(map[string]interface{})
t, ok := jobSpec["template"].([]interface{})
if !ok || len(t) == 0 {
return rawState
}
template := t[0].(map[string]interface{})
ps, ok := template["spec"].([]interface{})
if !ok || len(ps) == 0 {
return rawState
}
podSpec := ps[0].(map[string]interface{})
template["spec"] = []interface{}{upgradeContainers(podSpec)}
return rawState
}
func upgradeTemplatePodSpecWithResourcesFieldV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) map[string]interface{} {
s, ok := rawState["spec"].([]interface{})
if !ok || len(s) == 0 {
return rawState
}
spec := s[0].(map[string]interface{})
t, ok := spec["template"].([]interface{})
if !ok || len(t) == 0 {
return rawState
}
template := t[0].(map[string]interface{})
ps, ok := template["spec"].([]interface{})
if !ok || len(ps) == 0 {
return rawState
}
podSpec := ps[0].(map[string]interface{})
template["spec"] = []interface{}{upgradeContainers(podSpec)}
return rawState
}
func upgradePodSpecWithResourcesFieldV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) map[string]interface{} {
s, ok := rawState["spec"].([]interface{})
if !ok || len(s) == 0 {
return rawState
}
spec := s[0].(map[string]interface{})
rawState["spec"] = []interface{}{upgradeContainers(spec)}
return rawState
}
func upgradeContainers(rawState map[string]interface{}) map[string]interface{} {
if initContainers, ok := rawState["init_container"].([]interface{}); ok && len(initContainers) > 0 {
for _, c := range initContainers {
initContainer := c.(map[string]interface{})
if r, ok := initContainer["resources"].([]interface{}); ok && len(r) > 0 {
resources := r[0].(map[string]interface{})
if req, ok := resources["requests"].([]interface{}); ok && len(req) > 0 {
requests := req[0].(map[string]interface{})
resources["requests"] = requests
} else {
resources["requests"] = map[string]interface{}{}
}
if lim, ok := resources["limits"].([]interface{}); ok && len(lim) > 0 {
limits := lim[0].(map[string]interface{})
resources["limits"] = limits
} else {
resources["limits"] = map[string]interface{}{}
}
}
}
}
if containers, ok := rawState["container"].([]interface{}); ok && len(containers) > 0 {
for _, c := range containers {
container := c.(map[string]interface{})
if r, ok := container["resources"].([]interface{}); ok && len(r) > 0 {
resources := r[0].(map[string]interface{})
if req, ok := resources["requests"].([]interface{}); ok && len(req) > 0 {
requests := req[0].(map[string]interface{})
resources["requests"] = requests
} else {
resources["requests"] = map[string]interface{}{}
}
if lim, ok := resources["limits"].([]interface{}); ok && len(lim) > 0 {
limits := lim[0].(map[string]interface{})
resources["limits"] = limits
} else {
resources["limits"] = map[string]interface{}{}
}
}
}
}
return rawState
}