-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
722e0c0
commit 9aaee96
Showing
7 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |