@@ -3,19 +3,73 @@ package cache
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
7
+ "sync"
6
8
"time"
7
9
8
10
cmap "github.com/orcaman/concurrent-map/v2"
9
- "github.com/pluralsh/deployment-operator/internal/helpers "
10
- "github.com/pluralsh/deployment-operator/internal/metrics "
11
+ "github.com/pluralsh/console/go/client "
12
+ "github.com/samber/lo "
11
13
"k8s.io/apimachinery/pkg/runtime/schema"
12
14
"k8s.io/client-go/discovery"
13
15
"k8s.io/klog/v2"
16
+
17
+ "github.com/pluralsh/deployment-operator/internal/helpers"
18
+ "github.com/pluralsh/deployment-operator/internal/metrics"
19
+ )
20
+
21
+ type ServiceMeshResourceGroup string
22
+
23
+ const (
24
+ // ServiceMeshResourceGroupIstio is a base group name used by Istio
25
+ // Ref: https://github.com/istio/istio/blob/6186a80cb220ecbd7e1cc82044fe3a6fc2876c63/operator/pkg/apis/register.go#L27-L31
26
+ ServiceMeshResourceGroupIstio ServiceMeshResourceGroup = "istio.io"
27
+
28
+ // ServiceMeshResourceGroupCilium is a base group name used by Cilium
29
+ // Ref: https://github.com/cilium/cilium/blob/99b4bc0d0b628f22c024f3ea74ef21007a831f52/pkg/k8s/apis/cilium.io/register.go#L7-L8
30
+ ServiceMeshResourceGroupCilium ServiceMeshResourceGroup = "cilium.io"
31
+
32
+ // ServiceMeshResourceGroupLinkerd is a base group name used by Linkerd
33
+ // Ref: https://github.com/linkerd/linkerd2/blob/e055c32f31ae7618281fed1eb5c304b0d1389a52/controller/gen/apis/serviceprofile/register.go#L3-L4
34
+ ServiceMeshResourceGroupLinkerd ServiceMeshResourceGroup = "linkerd.io"
35
+
36
+ // ServiceMeshResourceGroupNone represents an empty or unknown service mesh
37
+ ServiceMeshResourceGroupNone ServiceMeshResourceGroup = ""
38
+ )
39
+
40
+ func (in ServiceMeshResourceGroup ) String () string {
41
+ return string (in )
42
+ }
43
+
44
+ func (in ServiceMeshResourceGroup ) Priority () ServiceMeshResourcePriority {
45
+ switch in {
46
+ case ServiceMeshResourceGroupIstio :
47
+ return ServiceMeshResourcePriorityIstio
48
+ case ServiceMeshResourceGroupCilium :
49
+ return ServiceMeshResourcePriorityCilium
50
+ case ServiceMeshResourceGroupLinkerd :
51
+ return ServiceMeshResourcePriorityLinkerd
52
+ default :
53
+ return ServiceMeshResourcePriorityNone
54
+ }
55
+ }
56
+
57
+ // ServiceMeshResourcePriority determines the order in which the ServiceMeshResourceGroup
58
+ // is assigned. Lower number means higher priority.
59
+ type ServiceMeshResourcePriority uint8
60
+
61
+ const (
62
+ ServiceMeshResourcePriorityIstio ServiceMeshResourcePriority = iota
63
+ ServiceMeshResourcePriorityCilium
64
+ ServiceMeshResourcePriorityLinkerd
65
+ ServiceMeshResourcePriorityNone = 255
14
66
)
15
67
16
68
var (
17
69
// Maps a GroupVersion to resource Kind.
18
- discoveryCache cmap.ConcurrentMap [string , bool ]
70
+ discoveryCache cmap.ConcurrentMap [string , bool ]
71
+ serviceMesh = ServiceMeshResourceGroupNone
72
+ serviceMeshRWLock = sync.RWMutex {}
19
73
)
20
74
21
75
func init () {
@@ -26,6 +80,22 @@ func DiscoveryCache() cmap.ConcurrentMap[string, bool] {
26
80
return discoveryCache
27
81
}
28
82
83
+ func ServiceMesh () * client.ServiceMesh {
84
+ serviceMeshRWLock .RLock ()
85
+ defer serviceMeshRWLock .RUnlock ()
86
+
87
+ switch serviceMesh {
88
+ case ServiceMeshResourceGroupIstio :
89
+ return lo .ToPtr (client .ServiceMeshIstio )
90
+ case ServiceMeshResourceGroupCilium :
91
+ return lo .ToPtr (client .ServiceMeshCilium )
92
+ case ServiceMeshResourceGroupLinkerd :
93
+ return lo .ToPtr (client .ServiceMeshLinkerd )
94
+ default :
95
+ return nil
96
+ }
97
+ }
98
+
29
99
func RunDiscoveryCacheInBackgroundOrDie (ctx context.Context , discoveryClient * discovery.DiscoveryClient ) {
30
100
klog .Info ("starting discovery cache" )
31
101
err := helpers .BackgroundPollUntilContextCancel (ctx , 5 * time .Minute , true , true , func (_ context.Context ) (done bool , err error ) {
@@ -61,8 +131,33 @@ func updateDiscoveryCache(discoveryClient *discovery.DiscoveryClient) error {
61
131
62
132
discoveryCache .Set (fmt .Sprintf ("%s/%s" , gv .String (), resource .Kind ), true )
63
133
discoveryCache .Set (gv .String (), true )
134
+
135
+ checkAndUpdateServiceMesh (gv .Group )
64
136
}
65
137
}
66
138
67
139
return err
68
140
}
141
+
142
+ func checkAndUpdateServiceMesh (group string ) {
143
+ serviceMeshRWLock .Lock ()
144
+ defer serviceMeshRWLock .Unlock ()
145
+
146
+ newServiceMesh := ServiceMeshResourceGroupNone
147
+
148
+ switch {
149
+ case strings .Contains (group , ServiceMeshResourceGroupIstio .String ()):
150
+ newServiceMesh = ServiceMeshResourceGroupIstio
151
+ case strings .Contains (group , ServiceMeshResourceGroupCilium .String ()):
152
+ newServiceMesh = ServiceMeshResourceGroupCilium
153
+ case strings .Contains (group , ServiceMeshResourceGroupLinkerd .String ()):
154
+ newServiceMesh = ServiceMeshResourceGroupLinkerd
155
+ }
156
+
157
+ // Lower number means higher priority, so override only if
158
+ // new resource group name matches service mesh with lower
159
+ // priority number.
160
+ if serviceMesh .Priority () > newServiceMesh .Priority () {
161
+ serviceMesh = newServiceMesh
162
+ }
163
+ }
0 commit comments