/
util.go
87 lines (76 loc) · 2.53 KB
/
util.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
// Copyright 2017 Oracle and/or its affiliates. All rights reserved.
//
// 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 oci
import (
"k8s.io/apimachinery/pkg/util/sets"
"strings"
"sync"
"github.com/pkg/errors"
api "k8s.io/api/core/v1"
)
// Protects Load Balancers against multiple updates in parallel
type loadBalancerLocks struct {
locks sets.String
mux sync.Mutex
}
func NewLoadBalancerLocks() *loadBalancerLocks {
return &loadBalancerLocks{
locks: sets.NewString(),
}
}
func (lbl *loadBalancerLocks) TryAcquire(lbname string) bool {
lbl.mux.Lock()
defer lbl.mux.Unlock()
if lbl.locks.Has(lbname) {
return false
}
lbl.locks.Insert(lbname)
return true
}
func (lbl *loadBalancerLocks) Release(lbname string) {
lbl.mux.Lock()
defer lbl.mux.Unlock()
lbl.locks.Delete(lbname)
}
// MapProviderIDToResourceID parses the provider id and returns the instance ocid.
func MapProviderIDToResourceID(providerID string) (string, error) {
if providerID == "" {
return providerID, errors.New("provider ID is empty")
}
if strings.HasPrefix(providerID, providerPrefix) {
return strings.TrimPrefix(providerID, providerPrefix), nil
}
return providerID, nil
}
// NodeInternalIP returns the nodes internal ip
// A node managed by the CCM will always have an internal ip
// since it's not possible to deploy an instance without a private ip.
func NodeInternalIP(node *api.Node) string {
for _, addr := range node.Status.Addresses {
if addr.Type == api.NodeInternalIP {
return addr.Address
}
}
return ""
}
// RemoveDuplicatesFromList takes Slice and returns new Slice with no duplicate elements
// (e.g. if given list is {"a", "b", "a"}, function returns new slice with {"a", "b"}
func RemoveDuplicatesFromList(list []string) []string {
return sets.NewString(list...).List()
}
// DeepEqualLists diffs two slices and returns bool if the slices are equal/not-equal.
// the duplicates and order of items in both lists is ignored.
func DeepEqualLists(listA, listB []string) bool {
return sets.NewString(listA...).Equal(sets.NewString(listB...))
}