Skip to content

Commit 9aaee96

Browse files
Extended Ping (#100)
Adds the ability to detect cluster distribution and return that info in ping requests. Also refactor pinging into its own package as the logic might become more expansive as this evolves.
1 parent 722e0c0 commit 9aaee96

File tree

7 files changed

+127
-9
lines changed

7 files changed

+127
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/orcaman/concurrent-map/v2 v2.0.1
1313
github.com/osteele/liquid v1.3.2
1414
github.com/pkg/errors v0.9.1
15-
github.com/pluralsh/console-client-go v0.0.57
15+
github.com/pluralsh/console-client-go v0.0.64
1616
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
1717
github.com/pluralsh/polly v0.1.4
1818
github.com/samber/lo v1.38.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
563563
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
564564
github.com/pluralsh/console-client-go v0.0.57 h1:XVs2fSrHCU/gB79DKqmsHF9Fo/D9oy8R69oSewFgGfI=
565565
github.com/pluralsh/console-client-go v0.0.57/go.mod h1:u/RjzXE3wtl3L6wiWxwhQHSpxFX46+EYvpkss2mALN4=
566+
github.com/pluralsh/console-client-go v0.0.64 h1:IZDbjDS+VMHVpIabcx2YYsBMzvtefbBv1LAVxsi1aNw=
567+
github.com/pluralsh/console-client-go v0.0.64/go.mod h1:u/RjzXE3wtl3L6wiWxwhQHSpxFX46+EYvpkss2mALN4=
566568
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=
567569
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34/go.mod h1:IagWXKFYu6NTHzcJx2dJyrIlZ1Sv2PH3fhOtplA9qOs=
568570
github.com/pluralsh/polly v0.1.4 h1:Kz90peCgvsfF3ERt8cujr5TR9z4wUlqQE60Eg09ZItY=

pkg/agent/agent.go

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

33
import (
44
"fmt"
5-
"strings"
65
"time"
76

87
"k8s.io/apimachinery/pkg/util/wait"
@@ -18,6 +17,7 @@ import (
1817
"github.com/pluralsh/deployment-operator/pkg/applier"
1918
"github.com/pluralsh/deployment-operator/pkg/client"
2019
"github.com/pluralsh/deployment-operator/pkg/manifests"
20+
"github.com/pluralsh/deployment-operator/pkg/ping"
2121
deploysync "github.com/pluralsh/deployment-operator/pkg/sync"
2222
"github.com/pluralsh/deployment-operator/pkg/websocket"
2323
)
@@ -29,6 +29,7 @@ var (
2929
type Agent struct {
3030
consoleClient *client.Client
3131
discoveryClient *discovery.DiscoveryClient
32+
pinger *ping.Pinger
3233
config *rest.Config
3334
engine *deploysync.Engine
3435
deathChan chan interface{}
@@ -80,8 +81,11 @@ func New(config *rest.Config, refresh, processingTimeout time.Duration, consoleU
8081
return nil, err
8182
}
8283

84+
pinger := ping.New(consoleClient, dc, f)
85+
8386
return &Agent{
8487
discoveryClient: dc,
88+
pinger: pinger,
8589
consoleClient: consoleClient,
8690
engine: engine,
8791
deathChan: deathChan,
@@ -119,15 +123,10 @@ func (agent *Agent) Run() {
119123
agent.svcQueue.Add(svc.ID)
120124
}
121125

122-
info, err := agent.discoveryClient.ServerVersion()
123-
if err != nil {
124-
log.Error(err, "failed to fetch cluster version")
125-
return false, nil
126-
}
127-
vs := strings.Split(info.GitVersion, "-")
128-
if err := agent.consoleClient.Ping(strings.TrimPrefix(vs[0], "v")); err != nil {
126+
if err := agent.pinger.Ping(); err != nil {
129127
log.Error(err, "failed to ping cluster after scheduling syncs")
130128
}
129+
131130
agent.engine.ScrapeKube()
132131
return false, nil
133132
})

pkg/client/cluster.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import (
44
console "github.com/pluralsh/console-client-go"
55
)
66

7+
func (c *Client) PingCluster(attributes console.ClusterPing) error {
8+
_, err := c.consoleClient.PingCluster(c.ctx, attributes)
9+
return err
10+
}
11+
712
func (c *Client) Ping(vsn string) error {
813
_, err := c.consoleClient.PingCluster(c.ctx, console.ClusterPing{CurrentVersion: vsn})
914
return err

pkg/ping/build.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ping
2+
3+
import (
4+
"strings"
5+
6+
console "github.com/pluralsh/console-client-go"
7+
"github.com/samber/lo"
8+
"k8s.io/apimachinery/pkg/version"
9+
)
10+
11+
func pingAttributes(info *version.Info, pods []string) console.ClusterPing {
12+
vs := strings.Split(info.GitVersion, "-")
13+
return console.ClusterPing{
14+
CurrentVersion: strings.TrimPrefix(vs[0], "v"),
15+
Distro: lo.ToPtr(findDistro(append(pods, info.GitVersion))),
16+
}
17+
}

pkg/ping/distro.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ping
2+
3+
import (
4+
"strings"
5+
6+
console "github.com/pluralsh/console-client-go"
7+
)
8+
9+
func findDistro(vals []string) console.ClusterDistro {
10+
for _, v := range vals {
11+
if dist, ok := distro(v); ok {
12+
return dist
13+
}
14+
}
15+
16+
return console.ClusterDistroGeneric
17+
}
18+
19+
func distro(val string) (console.ClusterDistro, bool) {
20+
if strings.Contains(val, "eks") {
21+
return console.ClusterDistroEks, true
22+
}
23+
24+
if strings.Contains(val, "aks") || strings.Contains(val, "azure") {
25+
return console.ClusterDistroAks, true
26+
}
27+
28+
if strings.Contains(val, "gke") {
29+
return console.ClusterDistroGke, true
30+
}
31+
32+
if strings.Contains(val, "k3s") {
33+
return console.ClusterDistroK3s, true
34+
}
35+
36+
if strings.Contains(val, "rke") {
37+
return console.ClusterDistroRke, true
38+
}
39+
40+
return console.ClusterDistroGeneric, false
41+
}

pkg/ping/pinger.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ping
2+
3+
import (
4+
"context"
5+
6+
"github.com/pluralsh/deployment-operator/pkg/client"
7+
"github.com/samber/lo"
8+
corev1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/client-go/discovery"
11+
"k8s.io/kubectl/pkg/cmd/util"
12+
)
13+
14+
type Pinger struct {
15+
consoleClient *client.Client
16+
discoveryClient *discovery.DiscoveryClient
17+
factory util.Factory
18+
}
19+
20+
func New(console *client.Client, discovery *discovery.DiscoveryClient, factory util.Factory) *Pinger {
21+
return &Pinger{
22+
consoleClient: console,
23+
discoveryClient: discovery,
24+
factory: factory,
25+
}
26+
}
27+
28+
func (p *Pinger) Ping() error {
29+
info, err := p.discoveryClient.ServerVersion()
30+
if err != nil {
31+
return err
32+
}
33+
34+
cs, err := p.factory.KubernetesClientSet()
35+
if err != nil {
36+
return nil
37+
}
38+
39+
podNames := []string{}
40+
// can find some distro information by checking what's running in kube-system
41+
if pods, err := cs.CoreV1().Pods("kube-system").List(context.TODO(), metav1.ListOptions{}); err == nil {
42+
podNames = lo.Map(pods.Items, func(pod corev1.Pod, ind int) string {
43+
return pod.Name
44+
})
45+
}
46+
47+
attrs := pingAttributes(info, podNames)
48+
if err := p.consoleClient.PingCluster(attrs); err != nil {
49+
attrs.Distro = nil
50+
return p.consoleClient.PingCluster(attrs) // fallback to no distro to support old console servers
51+
}
52+
53+
return nil
54+
}

0 commit comments

Comments
 (0)