Skip to content

Commit

Permalink
Extended Ping (#100)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaeljguarino authored Jan 2, 2024
1 parent 722e0c0 commit 9aaee96
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/osteele/liquid v1.3.2
github.com/pkg/errors v0.9.1
github.com/pluralsh/console-client-go v0.0.57
github.com/pluralsh/console-client-go v0.0.64
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
github.com/pluralsh/polly v0.1.4
github.com/samber/lo v1.38.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pluralsh/console-client-go v0.0.57 h1:XVs2fSrHCU/gB79DKqmsHF9Fo/D9oy8R69oSewFgGfI=
github.com/pluralsh/console-client-go v0.0.57/go.mod h1:u/RjzXE3wtl3L6wiWxwhQHSpxFX46+EYvpkss2mALN4=
github.com/pluralsh/console-client-go v0.0.64 h1:IZDbjDS+VMHVpIabcx2YYsBMzvtefbBv1LAVxsi1aNw=
github.com/pluralsh/console-client-go v0.0.64/go.mod h1:u/RjzXE3wtl3L6wiWxwhQHSpxFX46+EYvpkss2mALN4=
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34/go.mod h1:IagWXKFYu6NTHzcJx2dJyrIlZ1Sv2PH3fhOtplA9qOs=
github.com/pluralsh/polly v0.1.4 h1:Kz90peCgvsfF3ERt8cujr5TR9z4wUlqQE60Eg09ZItY=
Expand Down
15 changes: 7 additions & 8 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package agent

import (
"fmt"
"strings"
"time"

"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/pluralsh/deployment-operator/pkg/applier"
"github.com/pluralsh/deployment-operator/pkg/client"
"github.com/pluralsh/deployment-operator/pkg/manifests"
"github.com/pluralsh/deployment-operator/pkg/ping"
deploysync "github.com/pluralsh/deployment-operator/pkg/sync"
"github.com/pluralsh/deployment-operator/pkg/websocket"
)
Expand All @@ -29,6 +29,7 @@ var (
type Agent struct {
consoleClient *client.Client
discoveryClient *discovery.DiscoveryClient
pinger *ping.Pinger
config *rest.Config

Check failure on line 33 in pkg/agent/agent.go

View workflow job for this annotation

GitHub Actions / Lint

field `config` is unused (unused)
engine *deploysync.Engine
deathChan chan interface{}
Expand Down Expand Up @@ -80,8 +81,11 @@ func New(config *rest.Config, refresh, processingTimeout time.Duration, consoleU
return nil, err
}

pinger := ping.New(consoleClient, dc, f)

return &Agent{
discoveryClient: dc,
pinger: pinger,
consoleClient: consoleClient,
engine: engine,
deathChan: deathChan,
Expand Down Expand Up @@ -119,15 +123,10 @@ func (agent *Agent) Run() {
agent.svcQueue.Add(svc.ID)
}

info, err := agent.discoveryClient.ServerVersion()
if err != nil {
log.Error(err, "failed to fetch cluster version")
return false, nil
}
vs := strings.Split(info.GitVersion, "-")
if err := agent.consoleClient.Ping(strings.TrimPrefix(vs[0], "v")); err != nil {
if err := agent.pinger.Ping(); err != nil {
log.Error(err, "failed to ping cluster after scheduling syncs")
}

agent.engine.ScrapeKube()
return false, nil
})
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
console "github.com/pluralsh/console-client-go"
)

func (c *Client) PingCluster(attributes console.ClusterPing) error {
_, err := c.consoleClient.PingCluster(c.ctx, attributes)
return err
}

func (c *Client) Ping(vsn string) error {
_, err := c.consoleClient.PingCluster(c.ctx, console.ClusterPing{CurrentVersion: vsn})
return err
Expand Down
17 changes: 17 additions & 0 deletions pkg/ping/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ping

import (
"strings"

console "github.com/pluralsh/console-client-go"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/version"
)

func pingAttributes(info *version.Info, pods []string) console.ClusterPing {
vs := strings.Split(info.GitVersion, "-")
return console.ClusterPing{
CurrentVersion: strings.TrimPrefix(vs[0], "v"),
Distro: lo.ToPtr(findDistro(append(pods, info.GitVersion))),
}
}
41 changes: 41 additions & 0 deletions pkg/ping/distro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ping

import (
"strings"

console "github.com/pluralsh/console-client-go"
)

func findDistro(vals []string) console.ClusterDistro {
for _, v := range vals {
if dist, ok := distro(v); ok {
return dist
}
}

return console.ClusterDistroGeneric
}

func distro(val string) (console.ClusterDistro, bool) {
if strings.Contains(val, "eks") {
return console.ClusterDistroEks, true
}

if strings.Contains(val, "aks") || strings.Contains(val, "azure") {
return console.ClusterDistroAks, true
}

if strings.Contains(val, "gke") {
return console.ClusterDistroGke, true
}

if strings.Contains(val, "k3s") {
return console.ClusterDistroK3s, true
}

if strings.Contains(val, "rke") {
return console.ClusterDistroRke, true
}

return console.ClusterDistroGeneric, false
}
54 changes: 54 additions & 0 deletions pkg/ping/pinger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ping

import (
"context"

"github.com/pluralsh/deployment-operator/pkg/client"
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"k8s.io/kubectl/pkg/cmd/util"
)

type Pinger struct {
consoleClient *client.Client
discoveryClient *discovery.DiscoveryClient
factory util.Factory
}

func New(console *client.Client, discovery *discovery.DiscoveryClient, factory util.Factory) *Pinger {
return &Pinger{
consoleClient: console,
discoveryClient: discovery,
factory: factory,
}
}

func (p *Pinger) Ping() error {
info, err := p.discoveryClient.ServerVersion()
if err != nil {
return err
}

cs, err := p.factory.KubernetesClientSet()
if err != nil {
return nil
}

podNames := []string{}
// can find some distro information by checking what's running in kube-system
if pods, err := cs.CoreV1().Pods("kube-system").List(context.TODO(), metav1.ListOptions{}); err == nil {
podNames = lo.Map(pods.Items, func(pod corev1.Pod, ind int) string {
return pod.Name
})
}

attrs := pingAttributes(info, podNames)
if err := p.consoleClient.PingCluster(attrs); err != nil {
attrs.Distro = nil
return p.consoleClient.PingCluster(attrs) // fallback to no distro to support old console servers
}

return nil
}

0 comments on commit 9aaee96

Please sign in to comment.