This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.go
179 lines (154 loc) · 5.53 KB
/
generator.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
package keepalived
import (
"fmt"
"strconv"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kapi "k8s.io/kubernetes/pkg/apis/core"
dapi "github.com/openshift/origin/pkg/apps/apis/apps"
"github.com/openshift/origin/pkg/oc/experimental/ipfailover/ipfailover"
"github.com/openshift/origin/pkg/oc/generate/app"
)
const defaultInterface = "eth0"
const libModulesVolumeName = "lib-modules"
const libModulesPath = "/lib/modules"
// Generate the IP failover monitor (keepalived) container environment entries.
func generateEnvEntries(name string, options *ipfailover.IPFailoverConfigCmdOptions) app.Environment {
watchPort := strconv.Itoa(options.WatchPort)
replicas := strconv.FormatInt(int64(options.Replicas), 10)
interval := strconv.Itoa(options.CheckInterval)
VRRPIDOffset := strconv.Itoa(options.VRRPIDOffset)
VIPGroups := strconv.FormatUint(uint64(options.VIPGroups), 10)
env := app.Environment{}
env.Add(app.Environment{
"OPENSHIFT_HA_CONFIG_NAME": name,
"OPENSHIFT_HA_VIRTUAL_IPS": options.VirtualIPs,
"OPENSHIFT_HA_VIP_GROUPS": VIPGroups,
"OPENSHIFT_HA_NETWORK_INTERFACE": options.NetworkInterface,
"OPENSHIFT_HA_MONITOR_PORT": watchPort,
"OPENSHIFT_HA_VRRP_ID_OFFSET": VRRPIDOffset,
"OPENSHIFT_HA_REPLICA_COUNT": replicas,
"OPENSHIFT_HA_USE_UNICAST": "false",
"OPENSHIFT_HA_IPTABLES_CHAIN": options.IptablesChain,
"OPENSHIFT_HA_NOTIFY_SCRIPT": options.NotifyScript,
"OPENSHIFT_HA_CHECK_SCRIPT": options.CheckScript,
"OPENSHIFT_HA_PREEMPTION": options.Preemption,
"OPENSHIFT_HA_CHECK_INTERVAL": interval,
// "OPENSHIFT_HA_UNICAST_PEERS": "127.0.0.1",
})
return env
}
// Generate the IP failover monitor (keepalived) container configuration.
func generateFailoverMonitorContainerConfig(name string, options *ipfailover.IPFailoverConfigCmdOptions, env app.Environment) *kapi.Container {
containerName := fmt.Sprintf("%s-%s", name, options.Type)
imageName := fmt.Sprintf("%s-%s", options.Type, ipfailover.DefaultName)
image := options.ImageTemplate.ExpandOrDie(imageName)
// Container port to expose the service interconnects between keepaliveds.
ports := make([]kapi.ContainerPort, 1)
ports[0] = kapi.ContainerPort{
ContainerPort: int32(options.ServicePort),
HostPort: int32(options.ServicePort),
}
mounts := make([]kapi.VolumeMount, 1)
mounts[0] = kapi.VolumeMount{
Name: libModulesVolumeName,
ReadOnly: true,
MountPath: libModulesPath,
}
livenessProbe := &kapi.Probe{
InitialDelaySeconds: 10,
Handler: kapi.Handler{
Exec: &kapi.ExecAction{
Command: []string{"pgrep", "keepalived"},
},
},
}
privileged := true
return &kapi.Container{
Name: containerName,
Image: image,
Ports: ports,
SecurityContext: &kapi.SecurityContext{
Privileged: &privileged,
},
ImagePullPolicy: kapi.PullIfNotPresent,
VolumeMounts: mounts,
Env: env.List(),
LivenessProbe: livenessProbe,
}
}
// Generate the IP failover monitor (keepalived) container configuration.
func generateContainerConfig(name string, options *ipfailover.IPFailoverConfigCmdOptions) ([]kapi.Container, error) {
containers := make([]kapi.Container, 0)
if len(options.VirtualIPs) < 1 {
return containers, nil
}
env := generateEnvEntries(name, options)
c := generateFailoverMonitorContainerConfig(name, options, env)
if c != nil {
containers = append(containers, *c)
}
return containers, nil
}
// Generate the IP failover monitor (keepalived) container volume config.
func generateVolumeConfig() []kapi.Volume {
// The keepalived container needs access to the kernel modules
// directory in order to load the module.
hostPath := &kapi.HostPathVolumeSource{Path: libModulesPath}
src := kapi.VolumeSource{HostPath: hostPath}
vol := kapi.Volume{Name: libModulesVolumeName, VolumeSource: src}
return []kapi.Volume{vol}
}
// Generates the node selector (if any) to use.
func generateNodeSelector(name string, selector map[string]string) map[string]string {
// Check if the selector is default.
selectorValue, ok := selector[ipfailover.DefaultName]
if ok && len(selector) == 1 && selectorValue == name {
return map[string]string{}
}
return selector
}
// GenerateDeploymentConfig generates an IP Failover deployment configuration.
func GenerateDeploymentConfig(name string, options *ipfailover.IPFailoverConfigCmdOptions, selector map[string]string) (*dapi.DeploymentConfig, error) {
containers, err := generateContainerConfig(name, options)
if err != nil {
return nil, err
}
labels := map[string]string{
"ipfailover": name,
}
podTemplate := &kapi.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: kapi.PodSpec{
SecurityContext: &kapi.PodSecurityContext{
HostNetwork: true,
},
NodeSelector: generateNodeSelector(name, selector),
Containers: containers,
Volumes: generateVolumeConfig(),
ServiceAccountName: options.ServiceAccount,
},
}
return &dapi.DeploymentConfig{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
},
Spec: dapi.DeploymentConfigSpec{
Strategy: dapi.DeploymentStrategy{
Type: dapi.DeploymentStrategyTypeRecreate,
},
// TODO: v0.1 requires a manual resize of the
// replicas to match current cluster state.
// In the future, the PerNodeController in
// kubernetes would remove the need for this
// manual intervention.
Replicas: options.Replicas,
Template: podTemplate,
Triggers: []dapi.DeploymentTriggerPolicy{
{Type: dapi.DeploymentTriggerOnConfigChange},
},
},
}, nil
}