-
Notifications
You must be signed in to change notification settings - Fork 79
/
server_groups.go
126 lines (104 loc) · 3.58 KB
/
server_groups.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package worker
import (
"fmt"
"sort"
"strings"
"github.com/gardener/gardener/pkg/utils"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
api "github.com/gardener/gardener-extension-provider-openstack/pkg/apis/openstack"
)
func isServerGroupRequired(config *api.WorkerConfig) bool {
return config != nil && config.ServerGroup != nil && config.ServerGroup.Policy != ""
}
func generateServerGroupName(clusterName, poolName string) (string, error) {
suffix, err := utils.GenerateRandomString(10)
if err != nil {
return "", err
}
return fmt.Sprintf("%s-%s-%s", clusterName, poolName, suffix), nil
}
func filterServerGroupsByPrefix(sgs []servergroups.ServerGroup, prefix string) []servergroups.ServerGroup {
var result []servergroups.ServerGroup
for _, sg := range sgs {
if strings.HasPrefix(sg.Name, prefix) {
result = append(result, sg)
}
}
return result
}
// serverGroupDependencySet is a set implementation for ServerGroupDependency objects that uses the PoolName as identifying key.
type serverGroupDependencySet struct {
set map[string]api.ServerGroupDependency
}
// newServerGroupDependencySet creates a new serverGroupDependencySet.
func newServerGroupDependencySet(deps []api.ServerGroupDependency) serverGroupDependencySet {
m := make(map[string]api.ServerGroupDependency, len(deps))
for _, d := range deps {
m[d.PoolName] = d
}
return serverGroupDependencySet{m}
}
// upsert inserts a new value or updates a value present in the set.
func (s *serverGroupDependencySet) upsert(d *api.ServerGroupDependency) {
if d == nil {
return
}
s.set[d.PoolName] = *d
}
// getByPoolName retrieves a ServerGroupDependency if it matches the provided PoolName. It returns nil if there is no matching entry in the set.
func (s *serverGroupDependencySet) getByPoolName(pn string) *api.ServerGroupDependency {
d, ok := s.set[pn]
if !ok {
return nil
}
return &d
}
// getById retrieves a ServerGroupDependency if it matches the provided ID. It returns nil if there is no matching entry in the set.
func (s *serverGroupDependencySet) getById(id string) *api.ServerGroupDependency {
for _, v := range s.set {
if v.ID == id {
return &v
}
}
return nil
}
// deleteByPoolName deletes a ServerGroupDependency if it matches the provided PoolName. It is a no-op if there is no matching entry in the set.
func (s *serverGroupDependencySet) deleteByPoolName(pn string) {
delete(s.set, pn)
}
// deleteByID deletes a ServerGroupDependency if it matches the provided ID. It is a no-op if there is no matching entry in the set.
func (s *serverGroupDependencySet) deleteByID(id string) {
for _, v := range s.set {
if v.ID == id {
delete(s.set, v.PoolName)
break
}
}
}
// extract produces a slice from the elements contained in the set, sorted by PoolName.
func (s *serverGroupDependencySet) extract() []api.ServerGroupDependency {
if len(s.set) == 0 {
return nil
}
r := make([]api.ServerGroupDependency, 0, len(s.set))
for _, v := range s.set {
r = append(r, v)
}
// sort resulting slice to avoid randomization from map
sort.Slice(r, func(i, j int) bool {
return r[i].PoolName < r[j].PoolName
})
return r
}
// forEach executes function f for all elements contained in the set. If an error occurs the execution stops immediately.
func (s *serverGroupDependencySet) forEach(f func(dependency api.ServerGroupDependency) error) error {
for _, v := range s.set {
if err := f(v); err != nil {
return err
}
}
return nil
}