-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
61 lines (55 loc) · 1.7 KB
/
common.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
package main
import (
"fmt"
"strings"
)
// replaceOwner expects an input in the form of:
// heritage=external-dns,external-dns/owner=infra,external-dns/resource=ingress/sys-terraform-applier/terraform-applier-pub
// and will replace the external-dns/owner=infra part with the new owner id.
func replaceOwner(input, newOwner string) (string, error) {
pairs := strings.Split(input, ",")
found := false
for i, pair := range pairs {
if strings.HasPrefix(pair, "external-dns/owner=") {
found = true
pairs[i] = "external-dns/owner=" + newOwner
break
}
}
if !found {
return "", fmt.Errorf("`external-dns/owner` key not found in input")
}
return strings.Join(pairs, ","), nil
}
// verifyOwner expects an input in the form of:
// heritage=external-dns,external-dns/owner=infra,external-dns/resource=ingress/sys-terraform-applier/terraform-applier-pub
// and returns true if the owner matches the passed string.
func verifyOwner(input, owner string) bool {
pairs := strings.Split(input, ",")
for _, pair := range pairs {
if strings.HasPrefix(pair, "external-dns/owner=") {
o := strings.TrimPrefix(pair, "external-dns/owner=")
return owner == o
}
}
return false
}
// sanitizeDNSAddress get an address and ensures that there is a trailing dot
// in it
func sanitizeDNSAddress(address string) string {
if !strings.HasSuffix(address, ".") {
return address + "."
}
return address
}
// addressInList takes an address and a list of hostnames and checks if the
// sanitized name is found in the list
func addressInList(address string, hostnames []string) bool {
a := sanitizeDNSAddress(address)
for _, hostname := range hostnames {
if a == sanitizeDNSAddress(hostname) {
return true
}
}
return false
}