-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
port.go
208 lines (186 loc) · 5.33 KB
/
port.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package openstacktasks
import (
"fmt"
secgroup "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"k8s.io/klog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/openstack"
)
//go:generate fitask -type=Port
type Port struct {
ID *string
Name *string
Network *Network
Subnets []*Subnet
SecurityGroups []*SecurityGroup
AdditionalSecurityGroups []string
Lifecycle *fi.Lifecycle
}
// GetDependencies returns the dependencies of the Port task
func (e *Port) GetDependencies(tasks map[string]fi.Task) []fi.Task {
var deps []fi.Task
for _, task := range tasks {
if _, ok := task.(*Subnet); ok {
deps = append(deps, task)
}
if _, ok := task.(*SecurityGroup); ok {
deps = append(deps, task)
}
if _, ok := task.(*Network); ok {
deps = append(deps, task)
}
}
return deps
}
var _ fi.CompareWithID = &Port{}
func (s *Port) CompareWithID() *string {
return s.ID
}
func NewPortTaskFromCloud(cloud openstack.OpenstackCloud, lifecycle *fi.Lifecycle, port *ports.Port, find *Port) (*Port, error) {
additionalSecurityGroupIDs := map[string]struct{}{}
if find != nil {
for _, sg := range find.AdditionalSecurityGroups {
opt := secgroup.ListOpts{
Name: sg,
}
gs, err := cloud.ListSecurityGroups(opt)
if err != nil {
continue
}
if len(gs) == 0 {
continue
}
additionalSecurityGroupIDs[gs[0].ID] = struct{}{}
}
}
sgs := []*SecurityGroup{}
for _, sgid := range port.SecurityGroups {
if _, ok := additionalSecurityGroupIDs[sgid]; ok {
continue
}
sgs = append(sgs, &SecurityGroup{
ID: fi.String(sgid),
Lifecycle: lifecycle,
})
}
subnets := make([]*Subnet, len(port.FixedIPs))
for i, subn := range port.FixedIPs {
subnets[i] = &Subnet{
ID: fi.String(subn.SubnetID),
Lifecycle: lifecycle,
}
}
actual := &Port{
ID: fi.String(port.ID),
Name: fi.String(port.Name),
Network: &Network{ID: fi.String(port.NetworkID)},
SecurityGroups: sgs,
Subnets: subnets,
Lifecycle: lifecycle,
}
if find != nil {
find.ID = actual.ID
actual.AdditionalSecurityGroups = find.AdditionalSecurityGroups
}
return actual, nil
}
func (s *Port) Find(context *fi.Context) (*Port, error) {
cloud := context.Cloud.(openstack.OpenstackCloud)
opt := ports.ListOpts{
Name: fi.StringValue(s.Name),
}
rs, err := cloud.ListPorts(opt)
if err != nil {
return nil, err
}
if rs == nil {
return nil, nil
} else if len(rs) != 1 {
return nil, fmt.Errorf("found multiple ports with name: %s", fi.StringValue(s.Name))
}
return NewPortTaskFromCloud(cloud, s.Lifecycle, &rs[0], s)
}
func (s *Port) Run(context *fi.Context) error {
return fi.DefaultDeltaRunMethod(s, context)
}
func (_ *Port) CheckChanges(a, e, changes *Port) error {
if a == nil {
if e.Name == nil {
return fi.RequiredField("Name")
}
if e.Network == nil {
return fi.RequiredField("Network")
}
} else {
if changes.Name != nil {
return fi.CannotChangeField("Name")
}
if changes.Network != nil {
return fi.CannotChangeField("Network")
}
}
return nil
}
func (*Port) RenderOpenstack(t *openstack.OpenstackAPITarget, a, e, changes *Port) error {
if a == nil {
klog.V(2).Infof("Creating Port with name: %q", fi.StringValue(e.Name))
opt, err := portCreateOptsFromPortTask(t, a, e, changes)
if err != nil {
return fmt.Errorf("Error creating port cloud opts: %v", err)
}
v, err := t.Cloud.CreatePort(opt)
if err != nil {
return fmt.Errorf("Error creating port: %v", err)
}
e.ID = fi.String(v.ID)
klog.V(2).Infof("Creating a new Openstack port, id=%s", v.ID)
return nil
}
e.ID = a.ID
klog.V(2).Infof("Using an existing Openstack port, id=%s", fi.StringValue(e.ID))
return nil
}
func portCreateOptsFromPortTask(t *openstack.OpenstackAPITarget, a, e, changes *Port) (ports.CreateOptsBuilder, error) {
sgs := make([]string, len(e.SecurityGroups)+len(e.AdditionalSecurityGroups))
for i, sg := range e.SecurityGroups {
sgs[i] = fi.StringValue(sg.ID)
}
for i, sg := range e.AdditionalSecurityGroups {
opt := secgroup.ListOpts{
Name: sg,
}
gs, err := t.Cloud.ListSecurityGroups(opt)
if err != nil {
continue
}
if len(gs) == 0 {
return nil, fmt.Errorf("Additional SecurityGroup not found for name %s", sg)
}
sgs[i+len(e.SecurityGroups)] = gs[0].ID
}
fixedIPs := make([]ports.IP, len(e.Subnets))
for i, subn := range e.Subnets {
fixedIPs[i] = ports.IP{
SubnetID: fi.StringValue(subn.ID),
}
}
return ports.CreateOpts{
Name: fi.StringValue(e.Name),
NetworkID: fi.StringValue(e.Network.ID),
SecurityGroups: &sgs,
FixedIPs: fixedIPs,
}, nil
}