2021-11-30 12:04:28 +01:00
|
|
|
package helm
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
2021-11-30 15:45:36 +01:00
|
|
|
// Deployment is a k8s deployment.
|
2021-11-30 12:04:28 +01:00
|
|
|
type Deployment struct {
|
|
|
|
*K8sBase `yaml:",inline"`
|
|
|
|
Spec *DepSpec `yaml:"spec"`
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:17:34 +01:00
|
|
|
func NewDeployment(name string) *Deployment {
|
2021-11-30 12:04:28 +01:00
|
|
|
d := &Deployment{K8sBase: NewBase(), Spec: NewDepSpec()}
|
2021-12-05 09:05:48 +01:00
|
|
|
d.K8sBase.Metadata.Name = RELEASE_NAME + "-" + name
|
2021-11-30 12:04:28 +01:00
|
|
|
d.K8sBase.ApiVersion = "apps/v1"
|
|
|
|
d.K8sBase.Kind = "Deployment"
|
2021-12-01 15:17:34 +01:00
|
|
|
d.K8sBase.Metadata.Labels[K+"/component"] = name
|
2021-11-30 12:04:28 +01:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
type DepSpec struct {
|
|
|
|
Replicas int `yaml:"replicas"`
|
|
|
|
Selector map[string]interface{} `yaml:"selector"`
|
|
|
|
Template PodTemplate `yaml:"template"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDepSpec() *DepSpec {
|
|
|
|
return &DepSpec{
|
|
|
|
Replicas: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Value struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Value interface{} `yaml:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerPort struct {
|
|
|
|
Name string
|
|
|
|
ContainerPort int `yaml:"containerPort"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Container struct {
|
2022-02-14 14:37:09 +01:00
|
|
|
Name string `yaml:"name,omitempty"`
|
|
|
|
Image string `yaml:"image"`
|
|
|
|
Ports []*ContainerPort `yaml:"ports,omitempty"`
|
|
|
|
Env []Value `yaml:"env,omitempty"`
|
|
|
|
EnvFrom []map[string]map[string]string `yaml:"envFrom,omitempty"`
|
|
|
|
Command []string `yaml:"command,omitempty"`
|
|
|
|
VolumeMounts []interface{} `yaml:"volumeMounts,omitempty"`
|
|
|
|
LivenessProbe *Probe `yaml:"livenessProbe,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type HttpGet struct {
|
|
|
|
Path string `yaml:"path"`
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Exec struct {
|
|
|
|
Command []string `yaml:"command"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TCP struct {
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Probe struct {
|
|
|
|
HttpGet *HttpGet `yaml:"httpGet,omitempty"`
|
|
|
|
Exec *Exec `yaml:"exec,omitempty"`
|
|
|
|
TCP *TCP `yaml:"tcp,omitempty"`
|
|
|
|
Period int `yaml:"periodSeconds"`
|
|
|
|
Success int `yaml:"successThreshold"`
|
|
|
|
Failure int `yaml:"failureThreshold"`
|
|
|
|
InitialDelay int `yaml:"initialDelaySeconds"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProbe(period, initialDelaySeconds, success, failure int) *Probe {
|
|
|
|
return &Probe{
|
|
|
|
Period: period,
|
|
|
|
Success: success,
|
|
|
|
Failure: failure,
|
|
|
|
InitialDelay: initialDelaySeconds,
|
|
|
|
}
|
2021-11-30 12:04:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewContainer(name, image string, environment, labels map[string]string) *Container {
|
|
|
|
container := &Container{
|
2021-12-01 11:53:10 +01:00
|
|
|
Image: image,
|
|
|
|
Name: name,
|
|
|
|
Env: make([]Value, len(environment)),
|
|
|
|
EnvFrom: make([]map[string]map[string]string, 0),
|
2021-11-30 12:04:28 +01:00
|
|
|
}
|
|
|
|
|
2021-12-02 14:56:51 +01:00
|
|
|
// find bound environment variable to a service
|
2021-11-30 12:04:28 +01:00
|
|
|
toServices := make([]string, 0)
|
2021-12-02 14:56:51 +01:00
|
|
|
if bound, ok := labels[LABEL_ENV_SERVICE]; ok {
|
2021-11-30 12:04:28 +01:00
|
|
|
toServices = strings.Split(bound, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := 0
|
|
|
|
for n, v := range environment {
|
|
|
|
for _, name := range toServices {
|
|
|
|
if name == n {
|
2021-12-05 09:05:48 +01:00
|
|
|
v = RELEASE_NAME + "-" + v
|
2021-11-30 12:04:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
container.Env[idx] = Value{Name: n, Value: v}
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
return container
|
|
|
|
}
|
|
|
|
|
|
|
|
type PodSpec struct {
|
2021-11-30 17:29:42 +01:00
|
|
|
InitContainers []*Container `yaml:"initContainers,omitempty"`
|
|
|
|
Containers []*Container `yaml:"containers"`
|
|
|
|
Volumes []map[string]interface{} `yaml:"volumes,omitempty"`
|
2021-11-30 12:04:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type PodTemplate struct {
|
|
|
|
Metadata Metadata `yaml:"metadata"`
|
|
|
|
Spec PodSpec `yaml:"spec"`
|
|
|
|
}
|