forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
132 lines (102 loc) · 4.04 KB
/
plugin.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
package keepalived
import (
"fmt"
"strings"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/runtime"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/openshift/origin/pkg/generate/app"
"github.com/openshift/origin/pkg/ipfailover"
)
// KeepalivedPlugin is an IP Failover configurator plugin for keepalived sidecar.
type KeepalivedPlugin struct {
Name string
Factory *clientcmd.Factory
Options *ipfailover.IPFailoverConfigCmdOptions
}
// NewIPFailoverConfiguratorPlugin creates a new IPFailoverConfigurator (keepalived) plugin instance.
func NewIPFailoverConfiguratorPlugin(name string, f *clientcmd.Factory, options *ipfailover.IPFailoverConfigCmdOptions) (*KeepalivedPlugin, error) {
glog.V(4).Infof("Creating new KeepAlived plugin: %q", name)
p := &KeepalivedPlugin{
Name: name,
Factory: f,
Options: options,
}
return p, nil
}
// GetWatchPort gets the port to monitor for the IP Failover configuration.
func (p *KeepalivedPlugin) GetWatchPort() (int, error) {
port := p.Options.WatchPort
if port < 1 || port > 65535 {
glog.V(4).Infof("Warning: KeepAlived IP Failover config: %q - WatchPort: %d invalid, will default to %d", p.Name, port, ipfailover.DefaultWatchPort)
port = ipfailover.DefaultWatchPort
}
glog.V(4).Infof("KeepAlived IP Failover config: %q - WatchPort: %d", p.Name, port)
return port, nil
}
// GetSelector gets the selector associated with this IP Failover configurator plugin.
func (p *KeepalivedPlugin) GetSelector() (map[string]string, error) {
labels := make(map[string]string, 0)
if p.Options.Selector == ipfailover.DefaultSelector {
return map[string]string{ipfailover.DefaultName: p.Name}, nil
}
labels, remove, err := app.LabelsFromSpec(strings.Split(p.Options.Selector, ","))
if err != nil {
return labels, err
}
if len(remove) > 0 {
return labels, fmt.Errorf("you may not pass negative labels in %q", p.Options.Selector)
}
glog.V(4).Infof("KeepAlived IP Failover config: %q - selector: %+v", p.Name, labels)
return labels, nil
}
// GetNamespace gets the namespace associated with this IP Failover configurator plugin.
func (p *KeepalivedPlugin) GetNamespace() (string, error) {
namespace, _, err := p.Factory.DefaultNamespace()
if err != nil {
return "", err
}
glog.V(4).Infof("KeepAlived IP Failover config: %q - namespace: %q", p.Name, namespace)
return namespace, nil
}
// GetDeploymentConfig gets the deployment config associated with this IP Failover configurator plugin.
func (p *KeepalivedPlugin) GetDeploymentConfig() (*deployapi.DeploymentConfig, error) {
osClient, _, err := p.Factory.Clients()
if err != nil {
return nil, fmt.Errorf("error getting client: %v", err)
}
namespace, err := p.GetNamespace()
if err != nil {
return nil, fmt.Errorf("error getting namespace: %v", err)
}
dc, err := osClient.DeploymentConfigs(namespace).Get(p.Name)
if err != nil {
if errors.IsNotFound(err) {
glog.V(4).Infof("KeepAlived IP Failover DeploymentConfig: %s not found", p.Name)
return nil, nil
}
return nil, fmt.Errorf("error getting KeepAlived IP Failover DeploymentConfig %q: %v", p.Name, err)
}
glog.V(4).Infof("KeepAlived IP Failover DeploymentConfig: %q = %+v", p.Name, dc)
return dc, nil
}
// Generate the config and services for this IP Failover configuration plugin.
func (p *KeepalivedPlugin) Generate() (*kapi.List, error) {
selector, err := p.GetSelector()
if err != nil {
return nil, fmt.Errorf("error getting selector: %v", err)
}
if len(p.Options.VirtualIPs) == 0 {
return nil, fmt.Errorf("you must specify at least one virtual IP address (--virtual-ips=) for keepalived to expose")
}
dc, err := GenerateDeploymentConfig(p.Name, p.Options, selector)
if err != nil {
return nil, fmt.Errorf("error generating DeploymentConfig: %v", err)
}
configList := &kapi.List{Items: []runtime.Object{dc}}
glog.V(4).Infof("KeepAlived IP Failover DeploymentConfig: %q - generated config: %+v", p.Name, configList)
return configList, nil
}