This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
/
types.go
71 lines (59 loc) · 2.39 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
// Package identity implements types and utility routines related to the identity of a workload, as used within OSM.
package identity
import (
"fmt"
"strings"
)
const (
// namespaceNameSeparator used for marshalling/unmarshalling MeshService to a string or vice versa
namespaceNameSeparator = "/"
)
// ServiceIdentity is the type used to represent the identity for a service
// For Kubernetes services this string will be in the format: <ServiceAccount>.<Namespace>
type ServiceIdentity string
// New returns a new ServiceIdentity for the given name and namespace.
func New(name, namespace string) ServiceIdentity {
return ServiceIdentity(fmt.Sprintf("%s.%s", name, namespace))
}
// WildcardServiceIdentity is a wildcard to match all service identities
const WildcardServiceIdentity ServiceIdentity = "*"
// String returns the ServiceIdentity as a string
func (si ServiceIdentity) String() string {
return string(si)
}
// IsWildcard determines if the ServiceIdentity is a wildcard
func (si ServiceIdentity) IsWildcard() bool {
return si == WildcardServiceIdentity
}
// AsPrincipal converts the ServiceIdentity to a Principal with the given trust domain.
func (si ServiceIdentity) AsPrincipal(trustDomain string) string {
if si.IsWildcard() {
return si.String()
}
return fmt.Sprintf("%s.%s", si.String(), trustDomain)
}
// ToK8sServiceAccount converts a ServiceIdentity to a K8sServiceAccount to help with transition from K8sServiceAccount to ServiceIdentity
func (si ServiceIdentity) ToK8sServiceAccount() K8sServiceAccount {
// By convention as of release-v0.8 ServiceIdentity is in the format: <ServiceAccount>.<Namespace>.cluster.local
// We can split by "." and will have service account in the first position and namespace in the second.
chunks := strings.Split(si.String(), ".")
name := chunks[0]
namespace := chunks[1]
return K8sServiceAccount{
Namespace: namespace,
Name: name,
}
}
// K8sServiceAccount is a type for a namespaced service account
type K8sServiceAccount struct {
Namespace string
Name string
}
// String returns the string representation of the service account object
func (sa K8sServiceAccount) String() string {
return fmt.Sprintf("%s%s%s", sa.Namespace, namespaceNameSeparator, sa.Name)
}
// ToServiceIdentity converts K8sServiceAccount to the newer ServiceIdentity
func (sa K8sServiceAccount) ToServiceIdentity() ServiceIdentity {
return New(sa.Name, sa.Namespace)
}