forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
160 lines (124 loc) · 4.65 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
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
package keepalived
import (
"fmt"
"io"
"os"
"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"
configcmd "github.com/openshift/origin/pkg/config/cmd"
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 = ipfailover.DefaultWatchPort
}
glog.V(4).Infof("KeepAlived IP Failover config: %q - WatchPort: %+v", 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.OpenShiftClientConfig.Namespace()
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)
}
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
}
// Create the config and services associated with this IP Failover configuration.
func (p *KeepalivedPlugin) Create(out io.Writer) error {
namespace, err := p.GetNamespace()
if err != nil {
return fmt.Errorf("error getting Namespace: %v", err)
}
mapper, typer := p.Factory.Factory.Object()
bulk := configcmd.Bulk{
Mapper: mapper,
Typer: typer,
RESTClientFactory: p.Factory.Factory.RESTClient,
After: configcmd.NewPrintNameOrErrorAfter(mapper, p.Options.ShortOutput, "created", out, os.Stderr),
}
configList, err := p.Generate()
if err != nil {
return fmt.Errorf("error generating config: %v", err)
}
if errs := bulk.Create(configList, namespace); len(errs) != 0 {
return fmt.Errorf("error creating config: %+v", errs)
}
glog.V(4).Infof("Created KeepAlived IP Failover DeploymentConfig: %q", p.Name)
return nil
}