Skip to content

Commit e65d43a

Browse files
authored
feat: add service mesh discovery to operational layout (#381)
* discover service mesh installation through discovery API groups * fix lint/tests * add logic to assign discovered service mesh based on their priority
1 parent a241c2d commit e65d43a

File tree

7 files changed

+123
-21
lines changed

7 files changed

+123
-21
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ require (
3535
github.com/opencost/opencost/core v0.0.0-20241216191657-30e5d9a27f41
3636
github.com/orcaman/concurrent-map/v2 v2.0.1
3737
github.com/pkg/errors v0.9.1
38-
github.com/pluralsh/console/go/client v1.32.0
38+
github.com/pluralsh/console/go/client v1.32.1
3939
github.com/pluralsh/controller-reconcile-helper v0.1.0
4040
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
4141
github.com/pluralsh/polly v0.1.10

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1736,8 +1736,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
17361736
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
17371737
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo=
17381738
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8=
1739-
github.com/pluralsh/console/go/client v1.32.0 h1:V/VoiohHmhdwmPxQBDoF167yoau8jGWV+sxXV90dCgk=
1740-
github.com/pluralsh/console/go/client v1.32.0/go.mod h1:ZDRhjfDzbFLrHCfP3k6SaLlTDcjALCL8KIKQCYIHSJM=
1739+
github.com/pluralsh/console/go/client v1.32.1 h1:/FCgrfdfq/iqEGAY9zkalhoYbdncQPt4sBL2Ay5o5zE=
1740+
github.com/pluralsh/console/go/client v1.32.1/go.mod h1:ZDRhjfDzbFLrHCfP3k6SaLlTDcjALCL8KIKQCYIHSJM=
17411741
github.com/pluralsh/controller-reconcile-helper v0.1.0 h1:BV3dYZFH5rn8ZvZjtpkACSv/GmLEtRftNQj/Y4ddHEo=
17421742
github.com/pluralsh/controller-reconcile-helper v0.1.0/go.mod h1:RxAbvSB4/jkvx616krCdNQXPbpGJXW3J1L3rASxeFOA=
17431743
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=

pkg/cache/discovery_cache.go

+98-3
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,73 @@ package cache
33
import (
44
"context"
55
"fmt"
6+
"strings"
7+
"sync"
68
"time"
79

810
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"
1113
"k8s.io/apimachinery/pkg/runtime/schema"
1214
"k8s.io/client-go/discovery"
1315
"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
1466
)
1567

1668
var (
1769
// 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{}
1973
)
2074

2175
func init() {
@@ -26,6 +80,22 @@ func DiscoveryCache() cmap.ConcurrentMap[string, bool] {
2680
return discoveryCache
2781
}
2882

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+
2999
func RunDiscoveryCacheInBackgroundOrDie(ctx context.Context, discoveryClient *discovery.DiscoveryClient) {
30100
klog.Info("starting discovery cache")
31101
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 {
61131

62132
discoveryCache.Set(fmt.Sprintf("%s/%s", gv.String(), resource.Kind), true)
63133
discoveryCache.Set(gv.String(), true)
134+
135+
checkAndUpdateServiceMesh(gv.Group)
64136
}
65137
}
66138

67139
return err
68140
}
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+
}

pkg/client/cluster.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package client
22

33
import (
44
console "github.com/pluralsh/console/go/client"
5-
internalerrors "github.com/pluralsh/deployment-operator/internal/errors"
65
"github.com/pluralsh/polly/containers"
76
"k8s.io/apimachinery/pkg/api/errors"
87
"k8s.io/apimachinery/pkg/runtime/schema"
8+
9+
internalerrors "github.com/pluralsh/deployment-operator/internal/errors"
910
)
1011

1112
const (
@@ -45,7 +46,7 @@ func appendUniqueExternalDNSNamespace(slice []*string, newValue *string) []*stri
4546
return sliceSet.List()
4647
}
4748

48-
func (c *client) RegisterRuntimeServices(svcs map[string]*NamespaceVersion, serviceId *string) error {
49+
func (c *client) RegisterRuntimeServices(svcs map[string]*NamespaceVersion, serviceId *string, serviceMesh *console.ServiceMesh) error {
4950
inputs := make([]*console.RuntimeServiceAttributes, 0)
5051
var layouts *console.OperationalLayoutAttributes
5152
for name, nv := range svcs {
@@ -79,6 +80,7 @@ func (c *client) RegisterRuntimeServices(svcs map[string]*NamespaceVersion, serv
7980
}
8081
}
8182

83+
layouts.ServiceMesh = serviceMesh
8284
_, err := c.consoleClient.RegisterRuntimeServices(c.ctx, inputs, layouts, serviceId)
8385
return err
8486
}

pkg/client/console.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66

77
console "github.com/pluralsh/console/go/client"
8+
89
"github.com/pluralsh/deployment-operator/api/v1alpha1"
910
"github.com/pluralsh/deployment-operator/internal/helpers"
1011
v1 "github.com/pluralsh/deployment-operator/pkg/harness/stackrun/v1"
@@ -42,7 +43,7 @@ type Client interface {
4243
GetCredentials() (url, token string)
4344
PingCluster(attributes console.ClusterPing) error
4445
Ping(vsn string) error
45-
RegisterRuntimeServices(svcs map[string]*NamespaceVersion, serviceId *string) error
46+
RegisterRuntimeServices(svcs map[string]*NamespaceVersion, serviceId *string, serviceMesh *console.ServiceMesh) error
4647
UpsertVirtualCluster(parentID string, attributes console.ClusterAttributes) (*console.GetClusterWithToken_Cluster, error)
4748
IsClusterExists(id string) (bool, error)
4849
GetCluster(id string) (*console.TinyClusterFragment, error)

pkg/controller/service/reconciler_scraper.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"strings"
66

77
"github.com/Masterminds/semver/v3"
8-
"github.com/pluralsh/deployment-operator/pkg/client"
98
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109
"sigs.k8s.io/controller-runtime/pkg/log"
10+
11+
"github.com/pluralsh/deployment-operator/pkg/cache"
12+
"github.com/pluralsh/deployment-operator/pkg/client"
1113
)
1214

1315
func (s *ServiceReconciler) ScrapeKube(ctx context.Context) {
@@ -37,7 +39,8 @@ func (s *ServiceReconciler) ScrapeKube(ctx context.Context) {
3739
AddRuntimeServiceInfo(ds.Namespace, ds.GetLabels(), runtimeServices)
3840
}
3941
}
40-
if err := s.consoleClient.RegisterRuntimeServices(runtimeServices, nil); err != nil {
42+
43+
if err := s.consoleClient.RegisterRuntimeServices(runtimeServices, nil, cache.ServiceMesh()); err != nil {
4144
logger.Error(err, "failed to register runtime services, this is an ignorable error but could mean your console needs to be upgraded")
4245
}
4346
}

pkg/test/mocks/Client_mock.go

+11-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)