Skip to content

Commit 0a0cb7c

Browse files
authored
feat: add namespace inference to the runtime service fetch process (#363)
* add namespace inference to the runtime service fetch process * gen mocks * refactor
1 parent f0fdff0 commit 0a0cb7c

6 files changed

Lines changed: 97 additions & 28 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ require (
3434
github.com/opencost/opencost/core v0.0.0-20241216191657-30e5d9a27f41
3535
github.com/orcaman/concurrent-map/v2 v2.0.1
3636
github.com/pkg/errors v0.9.1
37-
github.com/pluralsh/console/go/client v1.28.0
37+
github.com/pluralsh/console/go/client v1.28.2
3838
github.com/pluralsh/controller-reconcile-helper v0.1.0
3939
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
4040
github.com/pluralsh/polly v0.1.10

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,8 +1199,8 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjL
11991199
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
12001200
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
12011201
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1202-
github.com/pluralsh/console/go/client v1.28.0 h1:UlXVT68KfpE5jc0XAfRV355DX/5np6zOu9KVyJcNr6I=
1203-
github.com/pluralsh/console/go/client v1.28.0/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
1202+
github.com/pluralsh/console/go/client v1.28.2 h1:fNK0xPuGtbGKK1RTbGyRXP9HN5nCHoZUddRxYIdoWUE=
1203+
github.com/pluralsh/console/go/client v1.28.2/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
12041204
github.com/pluralsh/controller-reconcile-helper v0.1.0 h1:BV3dYZFH5rn8ZvZjtpkACSv/GmLEtRftNQj/Y4ddHEo=
12051205
github.com/pluralsh/controller-reconcile-helper v0.1.0/go.mod h1:RxAbvSB4/jkvx616krCdNQXPbpGJXW3J1L3rASxeFOA=
12061206
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=

pkg/client/cluster.go

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ package client
22

33
import (
44
console "github.com/pluralsh/console/go/client"
5+
internalerrors "github.com/pluralsh/deployment-operator/internal/errors"
6+
"github.com/pluralsh/polly/containers"
57
"k8s.io/apimachinery/pkg/api/errors"
68
"k8s.io/apimachinery/pkg/runtime/schema"
9+
)
710

8-
internalerrors "github.com/pluralsh/deployment-operator/internal/errors"
11+
const (
12+
certManagerServiceName = "cert-manager"
13+
ebsCsiDriverServiceName = "aws-ebs-csi-driver"
14+
externalDNSServiceName = "external-dns"
15+
linkerdServiceName = "Linkerd"
16+
istioServiceName = "istio"
17+
ciliumServiceName = "cilium"
918
)
1019

1120
func (c *client) PingCluster(attributes console.ClusterPing) error {
@@ -18,15 +27,59 @@ func (c *client) Ping(vsn string) error {
1827
return err
1928
}
2029

21-
func (c *client) RegisterRuntimeServices(svcs map[string]string, serviceId *string) error {
30+
func initLayouts(layouts *console.OperationalLayoutAttributes) *console.OperationalLayoutAttributes {
31+
if layouts == nil {
32+
return &console.OperationalLayoutAttributes{
33+
Namespaces: &console.ClusterNamespacesAttributes{},
34+
}
35+
}
36+
return layouts
37+
}
38+
39+
func appendUniqueExternalDNSNamespace(slice []*string, newValue *string) []*string {
40+
if slice == nil {
41+
slice = make([]*string, 0)
42+
}
43+
sliceSet := containers.ToSet[*string](slice)
44+
sliceSet.Add(newValue)
45+
return sliceSet.List()
46+
}
47+
48+
func (c *client) RegisterRuntimeServices(svcs map[string]*NamespaceVersion, serviceId *string) error {
2249
inputs := make([]*console.RuntimeServiceAttributes, 0)
23-
for name, version := range svcs {
50+
var layouts *console.OperationalLayoutAttributes
51+
for name, nv := range svcs {
2452
inputs = append(inputs, &console.RuntimeServiceAttributes{
2553
Name: name,
26-
Version: version,
54+
Version: nv.Version,
2755
})
56+
switch name {
57+
case certManagerServiceName:
58+
layouts = initLayouts(layouts)
59+
layouts.Namespaces.CertManager = &nv.Namespace
60+
case ebsCsiDriverServiceName:
61+
layouts = initLayouts(layouts)
62+
layouts.Namespaces.EbsCsiDriver = &nv.Namespace
63+
case externalDNSServiceName:
64+
layouts = initLayouts(layouts)
65+
layouts.Namespaces.ExternalDNS = appendUniqueExternalDNSNamespace(layouts.Namespaces.ExternalDNS, &nv.Namespace)
66+
}
67+
if nv.PartOf != "" {
68+
switch nv.PartOf {
69+
case linkerdServiceName:
70+
layouts = initLayouts(layouts)
71+
layouts.Namespaces.Linkerd = &nv.Namespace
72+
case istioServiceName:
73+
layouts = initLayouts(layouts)
74+
layouts.Namespaces.Istio = &nv.Namespace
75+
case ciliumServiceName:
76+
layouts = initLayouts(layouts)
77+
layouts.Namespaces.Cilium = &nv.Namespace
78+
}
79+
}
2880
}
29-
_, err := c.consoleClient.RegisterRuntimeServices(c.ctx, inputs, serviceId)
81+
82+
_, err := c.consoleClient.RegisterRuntimeServices(c.ctx, inputs, layouts, serviceId)
3083
return err
3184
}
3285

pkg/client/console.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import (
55
"net/http"
66

77
console "github.com/pluralsh/console/go/client"
8-
98
"github.com/pluralsh/deployment-operator/api/v1alpha1"
109
"github.com/pluralsh/deployment-operator/internal/helpers"
1110
v1 "github.com/pluralsh/deployment-operator/pkg/harness/stackrun/v1"
1211
)
1312

13+
type NamespaceVersion struct {
14+
Namespace string
15+
Version string
16+
PartOf string
17+
}
18+
1419
type client struct {
1520
ctx context.Context
1621
consoleClient console.ConsoleClient
@@ -37,7 +42,7 @@ type Client interface {
3742
GetCredentials() (url, token string)
3843
PingCluster(attributes console.ClusterPing) error
3944
Ping(vsn string) error
40-
RegisterRuntimeServices(svcs map[string]string, serviceId *string) error
45+
RegisterRuntimeServices(svcs map[string]*NamespaceVersion, serviceId *string) error
4146
UpsertVirtualCluster(parentID string, attributes console.ClusterAttributes) (*console.GetClusterWithToken_Cluster, error)
4247
IsClusterExists(id string) (bool, error)
4348
GetCluster(id string) (*console.TinyClusterFragment, error)

pkg/controller/service/reconciler_scraper.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"context"
5+
"github.com/pluralsh/deployment-operator/pkg/client"
56
"strings"
67

78
"github.com/Masterminds/semver/v3"
@@ -12,72 +13,80 @@ import (
1213
func (s *ServiceReconciler) ScrapeKube(ctx context.Context) {
1314
logger := log.FromContext(ctx)
1415
logger.Info("attempting to collect all runtime services for the cluster")
15-
runtimeServices := map[string]string{}
16+
runtimeServices := map[string]*client.NamespaceVersion{}
1617
deployments, err := s.clientset.AppsV1().Deployments("").List(ctx, metav1.ListOptions{})
1718
if err == nil {
1819
logger.Info("aggregating from deployments")
1920
for _, deployment := range deployments.Items {
20-
AddRuntimeServiceInfo(deployment.GetLabels(), runtimeServices)
21+
AddRuntimeServiceInfo(deployment.Namespace, deployment.GetLabels(), runtimeServices)
2122
}
2223
}
2324

2425
statefulSets, err := s.clientset.AppsV1().StatefulSets("").List(ctx, metav1.ListOptions{})
2526
if err == nil {
2627
logger.Info("aggregating from statefulsets")
2728
for _, ss := range statefulSets.Items {
28-
AddRuntimeServiceInfo(ss.GetLabels(), runtimeServices)
29+
AddRuntimeServiceInfo(ss.Namespace, ss.GetLabels(), runtimeServices)
2930
}
3031
}
3132

3233
daemonSets, err := s.clientset.AppsV1().DaemonSets("").List(ctx, metav1.ListOptions{})
3334
if err == nil {
3435
logger.Info("aggregating from daemonsets")
35-
for _, ss := range daemonSets.Items {
36-
AddRuntimeServiceInfo(ss.GetLabels(), runtimeServices)
36+
for _, ds := range daemonSets.Items {
37+
AddRuntimeServiceInfo(ds.Namespace, ds.GetLabels(), runtimeServices)
3738
}
3839
}
3940
if err := s.consoleClient.RegisterRuntimeServices(runtimeServices, nil); err != nil {
4041
logger.Error(err, "failed to register runtime services, this is an ignorable error but could mean your console needs to be upgraded")
4142
}
4243
}
4344

44-
func AddRuntimeServiceInfo(labels map[string]string, acc map[string]string) {
45+
func AddRuntimeServiceInfo(namespace string, labels map[string]string, acc map[string]*client.NamespaceVersion) {
4546
if labels == nil {
4647
return
4748
}
4849

4950
if vsn, ok := labels["app.kubernetes.io/version"]; ok {
5051
if name, ok := labels["app.kubernetes.io/name"]; ok {
5152
addVersion(acc, name, vsn)
53+
acc[name].Namespace = namespace
54+
if partOf, ok := labels["app.kubernetes.io/part-of"]; ok {
55+
acc[name].PartOf = partOf
56+
}
5257
return
5358
}
5459

5560
if name, ok := labels["app.kubernetes.io/part-of"]; ok {
5661
addVersion(acc, name, vsn)
62+
acc[name].Namespace = namespace
5763
}
5864
}
65+
5966
}
6067

61-
func addVersion(services map[string]string, name, vsn string) {
68+
func addVersion(services map[string]*client.NamespaceVersion, name, vsn string) {
6269
old, ok := services[name]
6370
if !ok {
64-
services[name] = vsn
71+
services[name] = &client.NamespaceVersion{
72+
Version: vsn,
73+
}
6574
return
6675
}
6776

68-
parsedOld, err := semver.NewVersion(strings.TrimPrefix(old, "v"))
77+
parsedOld, err := semver.NewVersion(strings.TrimPrefix(old.Version, "v"))
6978
if err != nil {
70-
services[name] = vsn
79+
services[name].Version = vsn
7180
return
7281
}
7382

7483
parsedNew, err := semver.NewVersion(strings.TrimPrefix(vsn, "v"))
7584
if err != nil {
76-
services[name] = vsn
85+
services[name].Version = vsn
7786
return
7887
}
7988

8089
if parsedNew.LessThan(parsedOld) {
81-
services[name] = vsn
90+
services[name].Version = vsn
8291
}
8392
}

pkg/test/mocks/Client_mock.go

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)