-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.go
129 lines (111 loc) · 3.61 KB
/
deployment.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
package validators
import (
"fmt"
"sort"
"github.com/btoll/validator/lib"
v1 "k8s.io/api/core/v1"
)
type DeploymentManifest struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata `json:"metadata"`
Spec DeploymentSpec `json:"spec"`
}
type DeploymentSpec struct {
Replicas int `json:"replicas,omitempty"`
Selector LabelSelector `json:"selector,omitempty"`
Template Template `json:"template,omitempty"`
}
type LabelSelector struct {
MatchLabels Data `json:"matchLabels,omitempty"`
}
type Template struct {
Metadata Metadata `json:"metadata,omitempty"`
Spec PodSpec `json:"spec,omitempty"`
}
type PodSpec struct {
Containers []Container `json:"containers,omitempty"`
Volumes []Volume `json:"volumes,omitempty"`
NodeSelector Data `json:"nodeSelector,omitempty"`
}
type Volume struct {
Name string `json:"name,omitempty"`
NFS *NFSVolumeSource `json:"nfs,omitempty"`
}
type NFSVolumeSource struct {
Server string `json:"server,omitempty"`
Path string `json:"path,omitempty"`
ReadOnly bool `json:"read_only,omitempty"`
}
type Container struct {
Name string `json:"name,omitempty"`
Image string `json:"image,omitempty"`
ImagePullPolicy string `json:"imagePullPolicy,omitempty"`
Env []EnvVar `json:"env,omitempty"`
EnvFrom []EnvFrom `json:"envFrom,omitempty"`
Ports []v1.ContainerPort `json:"ports,omitempty"`
Resources Resources `json:"resources,omitempty"`
VolumeMounts []VolumeMount `json:"volumeMounts,omitempty"`
}
type EnvVar struct {
Name string `json:"name"`
Value string `json:"value"`
}
type EnvFrom struct {
ConfigMapRef Data `json:"configMapRef,omitempty"`
}
type Resources struct {
Limits v1.ResourceList `json:"limits,omitempty"`
Requests v1.ResourceList `json:"requests,omitempty"`
}
type VolumeMount struct {
Name string `json:"name,omitempty"`
MountPath string `json:"mountPath,omitempty"`
SubPath string `json:"subPath,omitempty"`
}
func sortPorts(s []v1.ContainerPort) {
sort.SliceStable(s, func(i, j int) bool {
return s[i].ContainerPort < s[j].ContainerPort
})
}
func (m DeploymentManifest) Write() {
fmt.Println(m.Name)
dir := fmt.Sprintf("build/%s/deployment", m.Name)
err := lib.CreateBuildDir(dir)
if err != nil {
fmt.Println("err", err)
}
// Recall, `ConfigMapEnvVars` has already been sorted.
// See the note in `validators/configmap` for more context.
m.Spec.Template.Spec.Containers[0].Env = ConfigMapEnvVars
sortPorts(m.Spec.Template.Spec.Containers[0].Ports)
localFile := fmt.Sprintf("%s/local", dir)
WriteTemplate(localFile, "deployment.tpl", m)
deployment, err := GetDeploymentClient(m.Name)
if err != nil {
fmt.Println("err", err)
}
// This values are empty in the returned struct.
deployment.APIVersion = "apps/v1"
deployment.Kind = "Deployment"
// There should always be an `.Env` property here. If there is not,
// then this will fail and we can investigate. This is a GOOD THING,
// since by design the Deployment should have the env vars embedded in it.
e := deployment.Spec.Template.Spec.Containers[0].Env
sort.SliceStable(e, func(i, j int) bool {
return e[i].Name < e[j].Name
})
sortPorts(deployment.Spec.Template.Spec.Containers[0].Ports)
remoteFile := fmt.Sprintf("%s/remote", dir)
WriteTemplate(remoteFile, "deployment.tpl", deployment)
b, err := Validate(localFile, remoteFile)
if err != nil {
fmt.Println("err", err)
}
if b {
err = RemoveDir(dir, fmt.Sprintf("build/%s", m.Name))
if err != nil {
fmt.Println(err)
}
}
}