-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto detect kubernetes cluster (#1322)
Closes #1321 --------- Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
- Loading branch information
Showing
12 changed files
with
301 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package autodetect | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type aksDetector struct{} | ||
|
||
func (a aksDetector) Detect(ctx context.Context, args DetectionArguments) (Kind, error) { | ||
if strings.HasSuffix(args.ServerEndpoint, "azmk8s.io:443") { | ||
return KindAKS, nil | ||
} | ||
|
||
// Look for nodes that have an AKS specific label | ||
listOpts := metav1.ListOptions{ | ||
LabelSelector: "kubernetes.azure.com/cluster", | ||
// Only need one | ||
Limit: 1, | ||
} | ||
|
||
nodes, err := args.KubeClient.CoreV1().Nodes().List(ctx, listOpts) | ||
if err != nil { | ||
return KindUnknown, err | ||
} | ||
if len(nodes.Items) > 0 { | ||
return KindAKS, nil | ||
} | ||
|
||
return KindUnknown, nil | ||
} |
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,24 @@ | ||
package autodetect | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
) | ||
|
||
type eksDetector struct{} | ||
|
||
func (e eksDetector) Detect(ctx context.Context, args DetectionArguments) (Kind, error) { | ||
if strings.Contains(args.ServerVersion, "-eks-") { | ||
return KindEKS, nil | ||
} | ||
|
||
if strings.HasSuffix(args.ClusterName, ".eksctl.io") { | ||
return KindEKS, nil | ||
} | ||
|
||
if strings.HasSuffix(args.ServerEndpoint, "eks.amazonaws.com") { | ||
return KindEKS, nil | ||
} | ||
|
||
return KindUnknown, nil | ||
} |
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,20 @@ | ||
package autodetect | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
) | ||
|
||
type gkeDetector struct{} | ||
|
||
func (g gkeDetector) Detect(ctx context.Context, args DetectionArguments) (Kind, error) { | ||
if strings.Contains(args.ServerVersion, "-gke.") { | ||
return KindGKE, nil | ||
} | ||
|
||
if strings.HasPrefix(args.ClusterName, "gke_") { | ||
return KindGKE, nil | ||
} | ||
|
||
return KindUnknown, nil | ||
} |
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,16 @@ | ||
package autodetect | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
) | ||
|
||
type k3sDetector struct{} | ||
|
||
func (k k3sDetector) Detect(ctx context.Context, args DetectionArguments) (Kind, error) { | ||
if strings.Contains(args.ServerVersion, "+k3s") { | ||
return KindK3s, nil | ||
} | ||
|
||
return KindUnknown, nil | ||
} |
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,70 @@ | ||
package autodetect | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/odigos-io/odigos/cli/pkg/kube" | ||
k8sutils "github.com/odigos-io/odigos/k8sutils/pkg/client" | ||
) | ||
|
||
type Kind string | ||
|
||
var availableDetectors = []Detector{&kindDetector{}, &eksDetector{}, &gkeDetector{}, &minikubeDetector{}, &k3sDetector{}, &openshiftDetector{}, &aksDetector{}} | ||
|
||
type KubernetesVersion struct { | ||
Kind Kind | ||
Version string | ||
} | ||
|
||
var CurrentKubernetesVersion KubernetesVersion | ||
|
||
const ( | ||
KindUnknown Kind = "Unknown" | ||
KindMinikube Kind = "Minikube" | ||
KindKind Kind = "KinD" | ||
KindEKS Kind = "EKS" | ||
KindGKE Kind = "GKE" | ||
KindAKS Kind = "AKS" | ||
KindK3s Kind = "k3s" | ||
KindOpenShift Kind = "Openshift" | ||
) | ||
|
||
// DetectionArguments are the arguments passed to the Detect function | ||
type DetectionArguments struct { | ||
k8sutils.ClusterDetails | ||
ServerVersion string | ||
KubeClient *kube.Client | ||
} | ||
|
||
type Detector interface { | ||
Detect(ctx context.Context, args DetectionArguments) (Kind, error) | ||
} | ||
|
||
func KubernetesClusterProduct(ctx context.Context, kc string, client *kube.Client) (Kind, string) { | ||
details := k8sutils.GetCurrentClusterDetails(kc) | ||
serverVersion, err := client.Discovery().ServerVersion() | ||
kubeVersion := fmt.Sprintf("%s.%s", serverVersion.Major, serverVersion.Minor) | ||
gitServerVersion := "" | ||
if err == nil { | ||
gitServerVersion = serverVersion.GitVersion | ||
} | ||
|
||
args := DetectionArguments{ | ||
ClusterDetails: details, | ||
ServerVersion: gitServerVersion, | ||
KubeClient: client, | ||
} | ||
|
||
for _, detector := range availableDetectors { | ||
kind, err := detector.Detect(ctx, args) | ||
if err != nil { | ||
continue | ||
} | ||
if kind != KindUnknown { | ||
return kind, kubeVersion | ||
} | ||
} | ||
|
||
return KindUnknown, kubeVersion | ||
} |
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,16 @@ | ||
package autodetect | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
) | ||
|
||
type kindDetector struct{} | ||
|
||
func (k kindDetector) Detect(ctx context.Context, args DetectionArguments) (Kind, error) { | ||
if strings.HasPrefix(args.ClusterName, "kind-") || strings.HasPrefix(args.CurrentContext, "kind-") { | ||
return KindKind, nil | ||
} | ||
|
||
return KindUnknown, nil | ||
} |
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 autodetect | ||
|
||
import "context" | ||
|
||
type minikubeDetector struct{} | ||
|
||
func (m minikubeDetector) Detect(ctx context.Context, args DetectionArguments) (Kind, error) { | ||
if args.ClusterName == "minikube" { | ||
return KindMinikube, nil | ||
} | ||
|
||
if args.CurrentContext == "minikube" { | ||
return KindMinikube, nil | ||
} | ||
|
||
return KindUnknown, nil | ||
} |
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,21 @@ | ||
package autodetect | ||
|
||
import "context" | ||
|
||
type openshiftDetector struct{} | ||
|
||
func (o openshiftDetector) Detect(ctx context.Context, args DetectionArguments) (Kind, error) { | ||
apiList, err := args.KubeClient.Discovery().ServerGroups() | ||
if err != nil { | ||
return KindUnknown, err | ||
} | ||
|
||
apiGroups := apiList.Groups | ||
for i := 0; i < len(apiGroups); i++ { | ||
if apiGroups[i].Name == "route.openshift.io" { | ||
return KindOpenShift, nil | ||
} | ||
} | ||
|
||
return KindUnknown, nil | ||
} |
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