terraform提供了kubernetes provider(相当于kubectl),但是却不兼容k8s api库的json输出名,对整体输出名都进行了重构;
此处例举了Deployment Service Job 等Kind的golang struct,希望可以帮到golang开发人员;

terraform-k8s-type.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package terraform

import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
)

type DeploymentTF struct {
Deployment
WaitForRollout bool `json:"wait_for_rollout"`
}
type Deployment struct {
ObjectMeta ObjectMeta `json:"metadata,omitempty"`
Spec DeploymentSpec `json:"spec,omitempty"`
}

type DeploymentSpec struct {
Replicas *int32 `json:"replicas,omitempty"`

Selector *LabelSelector `json:"selector"`

Template PodTemplateSpec `json:"template"`

Strategy DeploymentStrategy `json:"strategy,omitempty"`
}

type LabelSelector struct {
MatchLabels map[string]string `json:"match_labels,omitempty"`
}

type DeploymentStrategy struct {
Type appsv1.DeploymentStrategyType `json:"type,omitempty"`

RollingUpdate *RollingUpdateDeployment `json:"rolling_update,omitempty"`
}

type RollingUpdateDeployment struct {
MaxUnavailable *intstr.IntOrString `json:"max_unavailable,omitempty"`
MaxSurge *intstr.IntOrString `json:"max_surge,omitempty"`
}

type JobTF struct {
Job
WaitForCompletion bool `json:"wait_for_completion"`
Timeouts map[string]string `json:"timeouts,omitempty"`
}
type Job struct {
ObjectMeta ObjectMeta `json:"metadata,omitempty"`
Spec JobSpec `json:"spec,omitempty"`
}

type ObjectMeta struct {
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
}

type JobSpec struct {
Completions *int32 `json:"completions,omitempty"`
Template PodTemplateSpec `json:"template"`
}

type ServiceTF struct {
Service
WaitForLoadBalancer bool `json:"wait_for_load_balancer"`
}
type Service struct {
ObjectMeta ObjectMeta `json:"metadata,omitempty"`
Spec ServiceSpec `json:"spec,omitempty"`
}
type ServiceSpec struct {
ClusterIP string `json:"cluster_ip,omitempty"`
Ports []ServicePort `json:"port,omitempty"`
Selector map[string]string `json:"selector,omitempty"`
Type corev1.ServiceType `json:"type,omitempty"`
}

type ServicePort struct {
Name string `json:"name,omitempty"`

Protocol corev1.Protocol `json:"protocol,omitempty"`

AppProtocol *string `json:"app_protocol,omitempty"`

Port int32 `json:"port" `

TargetPort intstr.IntOrString `json:"target_port,omitempty"`

NodePort int32 `json:"node_port,omitempty"`
}

type PodTemplateSpec struct {
ObjectMeta ObjectMeta `json:"metadata,omitempty"`
Spec PodSpec `json:"spec,omitempty"`
}

type PodSpec struct {
Volumes []Volume `json:"volume,omitempty"`

InitContainers []Container `json:"init_container,omitempty"`

Containers []Container `json:"container"`

RestartPolicy corev1.RestartPolicy `json:"restart_policy,omitempty"`

NodeSelector map[string]string `json:"node_selector,omitempty"`

ServiceAccountName string `json:"serviceAccountName,omitempty"`

NodeName string `json:"node_name,omitempty"`

ImagePullSecrets []corev1.LocalObjectReference `json:"image_pull_secrets,omitempty"`

Affinity *Affinity `json:"affinity,omitempty"`

EnableServiceLinks *bool `json:"enable_service_links,omitempty"`
}

type Affinity struct {
NodeAffinity *NodeAffinity `json:"node_affinity,omitempty"`

PodAffinity *PodAffinity `json:"pod_affinity,omitempty"`

PodAntiAffinity *PodAntiAffinity `json:"pod_anti_affinity,omitempty"`
}

type PodAffinity struct {
RequiredDuringSchedulingIgnoredDuringExecution []PodAffinityTerm `json:"required_during_scheduling_ignored_during_execution,omitempty"`

PreferredDuringSchedulingIgnoredDuringExecution []WeightedPodAffinityTerm `json:"preferred_during_scheduling_ignored_during_execution,omitempty"`
}

type NodeAffinity struct {
RequiredDuringSchedulingIgnoredDuringExecution *NodeSelector `json:"required_during_scheduling_ignored_during_execution,omitempty"`

PreferredDuringSchedulingIgnoredDuringExecution []PreferredSchedulingTerm `json:"preferred_during_scheduling_ignored_during_execution,omitempty"`
}

type PodAntiAffinity struct {
RequiredDuringSchedulingIgnoredDuringExecution []PodAffinityTerm `json:"required_during_scheduling_ignored_during_execution,omitempty"`

PreferredDuringSchedulingIgnoredDuringExecution []WeightedPodAffinityTerm `json:"preferred_during_scheduling_ignored_during_execution,omitempty"`
}

type PreferredSchedulingTerm struct {
Weight int32 `json:"weight" protobuf:"varint,1,opt,name=weight"`
Preference NodeSelectorTerm `json:"preference"`
}

type WeightedPodAffinityTerm struct {
Weight int32 `json:"weight" protobuf:"varint,1,opt,name=weight"`

PodAffinityTerm PodAffinityTerm `json:"pod_affinity_term"`
}

type PodAffinityTerm struct {
LabelSelector *LabelSelector `json:"label_selector,omitempty"`

Namespaces []string `json:"namespaces,omitempty"`

TopologyKey string `json:"topology_key"`
}
type NodeSelector struct {
NodeSelectorTerms []NodeSelectorTerm `json:"node_selector_term"`
}
type NodeSelectorTerm struct {
MatchExpressions []corev1.NodeSelectorRequirement `json:"match_expressions,omitempty"`

MatchFields []corev1.NodeSelectorRequirement `json:"match_fields,omitempty"`
}

type Volume struct {
Name string `json:"name"`
VolumeSource `json:",inline"`
}

type VolumeSource struct {
PersistentVolumeClaim *PersistentVolumeClaimVolumeSource `json:"persistent_volume_claim,omitempty"`
EmptyDir *EmptyDirVolumeSource `json:"empty_dir,omitempty"`
}
type EmptyDirVolumeSource struct {
Medium corev1.StorageMedium `json:"medium,omitempty"`
SizeLimit *resource.Quantity `json:"size_limit,omitempty"`
}

type PersistentVolumeClaimVolumeSource struct {
ClaimName string `json:"claim_name"`
ReadOnly bool `json:"readOnly,omitempty"`
}

type Container struct {
Name string `json:"name"`

Image string `json:"image,omitempty"`

Command []string `json:"command,omitempty"`

Args []string `json:"args,omitempty"`

WorkingDir string `json:"working_dir,omitempty"`

Ports []ContainerPort `json:"port,omitempty"`

Env []EnvVar `json:"env,omitempty"`

Resources corev1.ResourceRequirements `json:"resources,omitempty"`

VolumeMounts []VolumeMount `json:"volume_mount,omitempty"`

LivenessProbe *Probe `json:"liveness_probe,omitempty"`

ReadinessProbe *Probe `json:"readiness_probe,omitempty"`

ImagePullPolicy corev1.PullPolicy `json:"image_pull_policy,omitempty"`
}

type ContainerPort struct {
Name string `json:"name,omitempty"`

HostPort int32 `json:"host_port,omitempty"`

ContainerPort int32 `json:"container_port"`

Protocol corev1.Protocol `json:"protocol,omitempty"`

HostIP string `json:"host_ip,omitempty"`
}
type EnvVar struct {
Name string `json:"name"`

Value string `json:"value,omitempty"`
}
type VolumeMount struct {
Name string `json:"name"`

ReadOnly bool `json:"readOnly,omitempty"`

MountPath string `json:"mount_path"`

SubPath string `json:"sub_path,omitempty"`
}

type Probe struct {
ProbeHandler

InitialDelaySeconds int32 `json:"initial_delay_seconds,omitempty"`

TimeoutSeconds int32 `json:"timeout_seconds,omitempty"`

PeriodSeconds int32 `json:"period_seconds,omitempty"`

SuccessThreshold int32 `json:"success_threshold,omitempty"`

FailureThreshold int32 `json:"failure_threshold,omitempty"`
}
type ProbeHandler struct {
Exec *corev1.ExecAction `json:"exec,omitempty"`

HTTPGet *HTTPGetAction `json:"http_get,omitempty"`

TCPSocket *corev1.TCPSocketAction `json:"tcp_socket,omitempty"`
}

type HTTPGetAction struct {
Path string `json:"path,omitempty"`

Port intstr.IntOrString `json:"port"`

Host string `json:"host,omitempty"`

Scheme corev1.URIScheme `json:"scheme,omitempty"`

HTTPHeaders []HTTPHeader `json:"http_header,omitempty"`
}
type HTTPHeader struct {
Name string `json:"name"`
Value string `json:"value"`
}