/
common.go
140 lines (119 loc) · 2.74 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
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
package utils
import (
"fmt"
"math/rand"
"strings"
"golang.org/x/exp/constraints"
gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)
type MapFunc[T any, U any] func(T) U
type FilterFunc[T any] func(T) bool
func min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
func Chunks[T any](in []T, size int) [][]T {
out := [][]T{}
if size <= 0 {
return out
}
for low := 0; low < len(in); low += size {
high := min(low+size, len(in))
out = append(out, in[low:high])
}
return out
}
func Truncate(name string, length int) string {
if len(name) > length {
name = name[:length]
}
return strings.Trim(name, "-")
}
func SliceMap[T any, U any](in []T, f MapFunc[T, U]) []U {
out := make([]U, len(in))
for i, t := range in {
out[i] = f(t)
}
return out
}
func SliceMapToPtr[T any](in []T) []*T {
return SliceMap(in, func(t T) *T { return &t })
}
func SliceFilter[T any](in []T, f FilterFunc[T]) []T {
out := []T{}
for _, t := range in {
if f(t) {
out = append(out, t)
}
}
return out
}
func LatticeServiceName(k8sSourceRouteName string, k8sSourceRouteNamespace string) string {
return fmt.Sprintf("%s-%s", Truncate(k8sSourceRouteName, 20), Truncate(k8sSourceRouteNamespace, 18))
}
func TargetRefToLatticeResourceName(
targetRef *gwv1alpha2.PolicyTargetReference,
parentNamespace string,
) (string, error) {
// For Service Network, the name is just the Gateway's name.
if targetRef.Kind == "Gateway" {
return string(targetRef.Name), nil
}
// For VPC Lattice Service, the name is Route's name, followed by hyphen (-), then the Route's namespace.
// If the Route's namespace is not provided, we assume it's the parent's namespace.
if targetRef.Kind == "HTTPRoute" || targetRef.Kind == "GRPCRoute" {
namespace := parentNamespace
if targetRef.Namespace != nil {
namespace = string(*targetRef.Namespace)
}
return LatticeServiceName(string(targetRef.Name), namespace), nil
}
return "", fmt.Errorf("unsupported targetRef Kind: %s", targetRef.Kind)
}
func RandomAlphaString(length int) string {
str := make([]rune, length)
for i := range str {
str[i] = rune(rand.Intn(26) + 'a')
}
return string(str)
}
type none struct{}
type Set[T comparable] struct {
set map[T]none
}
func NewSet[T comparable](objs ...T) Set[T] {
s := Set[T]{
set: make(map[T]none, len(objs)),
}
for _, t := range objs {
s.set[t] = none{}
}
return s
}
func (s *Set[T]) Put(t T) {
if s.set == nil {
s.set = map[T]none{}
}
s.set[t] = none{}
}
func (s *Set[T]) Delete(t T) {
delete(s.set, t)
}
func (s *Set[T]) Contains(t T) bool {
if s.set == nil {
s.set = map[T]none{}
}
_, ok := s.set[t]
return ok
}
func (s *Set[T]) Items() []T {
out := make([]T, len(s.set))
i := 0
for t := range s.set {
out[i] = t
i++
}
return out
}