/
types.go
195 lines (160 loc) · 4.75 KB
/
types.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
package model
import (
"context"
"crypto/sha256"
"fmt"
"sort"
"strconv"
"strings"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// StatusAction describes which type of operation was done/attempted to the target resource. Used to determine how to undo it.
type StatusAction string
const (
// ActionCreate imply the whole Named Kind should be created.
ActionCreate StatusAction = "create"
// ActionDelete imply the whole Named Kind was created and should be deleted.
ActionDelete StatusAction = "delete"
// ActionModify imply the Named Kind should be modified.
ActionModify StatusAction = "modify"
// ActionRevert imply the Named Kind was modified and should be reverted to original state.
ActionRevert StatusAction = "revert"
// ActionLocated imply the resource was found, but nothing was changed.
ActionLocated StatusAction = "located"
// StrategyExisting holds the name of the existing strategy.
StrategyExisting = "existing"
)
func Flip(action StatusAction) StatusAction {
switch action {
case ActionCreate:
return ActionDelete
case ActionDelete:
return ActionCreate
case ActionModify:
return ActionRevert
case ActionRevert:
return ActionModify
case ActionLocated:
return ActionLocated
}
return ActionRevert
}
type SessionContext struct {
context.Context //nolint:containedctx //reason needs refactoring https://github.com/maistra/istio-workspace/issues/1100
Name string
Namespace string
Route Route
Client client.Client
Log logr.Logger
}
// ToNamespacedName returns a types.NamespaceName object that represents this Session.
func (s *SessionContext) ToNamespacedName() types.NamespacedName {
return types.NamespacedName{
Namespace: s.Namespace,
Name: s.Name,
}
}
// Route references the strategy used to route to the target Refs.
type Route struct {
Type string
Name string
Value string
}
// Ref references the user specified Resource target and configuration.
type Ref struct {
KindName RefKindName
Remove bool
Namespace string
Strategy string
Args map[string]string
}
// Hash returns a predictable hash version for this object.
func (r *Ref) Hash() string {
digest := "kind:" + r.KindName.String()
digest += ";remove:" + strconv.FormatBool(r.Remove)
digest += ";namespace:" + r.Namespace
digest += ";strategy:" + r.Strategy
args := []string{}
for k := range r.Args {
args = append(args, k)
}
sort.Strings(args)
for _, k := range args {
digest += ";args[" + k + "]:" + r.Args[k]
}
sum := sha256.Sum256([]byte(digest))
sha := fmt.Sprintf("%x", sum)
return sha[:8]
}
// RefKindName is the target Resource and optional Resource Kind.
type RefKindName struct {
Kind string
Name string
}
// String returns the string formatted kind/name.
func (r RefKindName) String() string {
if r.Kind == "" {
return r.Name
}
return r.Kind + "/" + r.Name
}
// SupportsKind returns true if kind match or the kind is empty.
func (r RefKindName) SupportsKind(kind string) bool {
return r.Kind == "" || strings.EqualFold(r.Kind, kind)
}
// ParseRefKindName parses a String() representation into a Object.
func ParseRefKindName(exp string) RefKindName {
trimmedExp := strings.TrimSpace(strings.ToLower(exp))
parts := strings.Split(trimmedExp, "/")
if len(parts) == 2 {
return RefKindName{
Kind: parts[0],
Name: parts[1],
}
}
return RefKindName{Name: trimmedExp}
}
// HostName represents the Hostname of a service in a given namespace.
type HostName struct {
Name string
Namespace string
}
// Match returns true if this Hostname is equal to the short or long v of a dns name.
func (h *HostName) Match(name string) bool {
equalsShortName := h.Name == name
equalsFullDNSName := fmt.Sprint(h.Name, ".", h.Namespace, ".svc.cluster.local") == name
return equalsShortName || equalsFullDNSName
}
// String returns the String representation of a HostName.
func (h *HostName) String() string {
if h.Namespace != "" {
return fmt.Sprint(h.Name, ".", h.Namespace, ".svc.cluster.local")
}
return h.Name
}
func NewHostName(host string) HostName {
if strings.Contains(host, ".svc.cluster.local") {
parts := strings.Split(host, ".")
return HostName{Name: parts[0], Namespace: parts[1]}
}
return HostName{Name: host}
}
// GetTargetHostNames returns a list of Host names that the target Deployment can be reached under.
func GetTargetHostNames(store LocatorStatusStore) []HostName {
targets := store("Service")
hosts := make([]HostName, 0, len(targets))
for _, service := range targets {
hosts = append(hosts, HostName{Name: service.Name, Namespace: service.Namespace})
}
return hosts
}
type Resource struct {
Namespace string
Kind string
Name string
}
func (r *Resource) GetNamespaceName() string {
return r.Namespace + "/" + r.Name
}