Skip to content

Commit 0cb3f5b

Browse files
committed
Add PersistentPreRun to the root CLI cmd to set the kube client and cluster details
1 parent 56a0a48 commit 0cb3f5b

16 files changed

+98
-113
lines changed

cli/cmd/cloud.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ var cloudCmd = &cobra.Command{
2727
Short: "Manage odigos cloud",
2828
Long: `Used to interact with odigos managed service.`,
2929
Run: func(cmd *cobra.Command, args []string) {
30-
client, err := kube.CreateClient(cmd)
31-
if err != nil {
32-
kube.PrintClientErrorAndExit(err)
33-
}
30+
client := kube.GetCLIClient()
3431
ctx := cmd.Context()
3532

3633
ns, err := resources.GetOdigosNamespace(client, ctx)

cli/cmd/describe.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ var describeCmd = &cobra.Command{
2222
Long: `Print detailed description odigos deployment, which can be used to troubleshoot issues`,
2323
Run: func(cmd *cobra.Command, args []string) {
2424

25-
client, err := kube.CreateClient(cmd)
26-
if err != nil {
27-
kube.PrintClientErrorAndExit(err)
28-
}
25+
client := kube.GetCLIClient()
2926
ctx := cmd.Context()
3027

3128
odigosNs, err := resources.GetOdigosNamespace(client, ctx)
@@ -66,10 +63,7 @@ var describeSourceDeploymentCmd = &cobra.Command{
6663
Aliases: []string{"deploy", "deployments", "deploy.apps", "deployment.apps", "deployments.apps"},
6764
Args: cobra.ExactArgs(1),
6865
Run: func(cmd *cobra.Command, args []string) {
69-
client, err := kube.CreateClient(cmd)
70-
if err != nil {
71-
kube.PrintClientErrorAndExit(err)
72-
}
66+
client := kube.GetCLIClient()
7367
ctx := cmd.Context()
7468
name := args[0]
7569
ns := cmd.Flag("namespace").Value.String()
@@ -96,10 +90,7 @@ var describeSourceDaemonSetCmd = &cobra.Command{
9690
Aliases: []string{"ds", "daemonsets", "ds.apps", "daemonset.apps", "daemonsets.apps"},
9791
Args: cobra.ExactArgs(1),
9892
Run: func(cmd *cobra.Command, args []string) {
99-
client, err := kube.CreateClient(cmd)
100-
if err != nil {
101-
kube.PrintClientErrorAndExit(err)
102-
}
93+
client := kube.GetCLIClient()
10394

10495
ctx := cmd.Context()
10596
name := args[0]
@@ -127,10 +118,7 @@ var describeSourceStatefulSetCmd = &cobra.Command{
127118
Aliases: []string{"sts", "statefulsets", "sts.apps", "statefulset.apps", "statefulsets.apps"},
128119
Args: cobra.ExactArgs(1),
129120
Run: func(cmd *cobra.Command, args []string) {
130-
client, err := kube.CreateClient(cmd)
131-
if err != nil {
132-
kube.PrintClientErrorAndExit(err)
133-
}
121+
client := kube.GetCLIClient()
134122

135123
ctx := cmd.Context()
136124
name := args[0]

cli/cmd/diagnose.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ var diagnoseCmd = &cobra.Command{
3232
Long: `Diagnose Client Cluster to identify issues and resolve them. This command is useful for troubleshooting and debugging.`,
3333
Run: func(cmd *cobra.Command, args []string) {
3434
ctx := cmd.Context()
35-
client, err := kube.CreateClient(cmd)
36-
if err != nil {
37-
kube.PrintClientErrorAndExit(err)
38-
}
35+
client := kube.GetCLIClient()
3936

40-
err = startDiagnose(ctx, client)
37+
err := startDiagnose(ctx, client)
4138
if err != nil {
4239
fmt.Printf("The diagnose script crashed on: %v\n", err)
4340
}

cli/cmd/install.go

+31-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"os"
87
"time"
@@ -63,10 +62,7 @@ var installCmd = &cobra.Command{
6362
This command will install k8s components that will auto-instrument your applications with OpenTelemetry and send traces, metrics and logs to any telemetry backend`,
6463
Run: func(cmd *cobra.Command, args []string) {
6564

66-
client, err := kube.CreateClient(cmd)
67-
if err != nil {
68-
kube.PrintClientErrorAndExit(err)
69-
}
65+
client := kube.GetCLIClient()
7066
ctx := cmd.Context()
7167
ns := cmd.Flag("namespace").Value.String()
7268

@@ -78,26 +74,45 @@ This command will install k8s components that will auto-instrument your applicat
7874
}
7975

8076
// Check if the cluster meets the minimum requirements
81-
kc := cmd.Flag("kubeconfig").Value.String()
82-
details, err := autodetect.DetectK8SClusterDetails(ctx, kc, client)
83-
if !errors.Is(err, autodetect.ErrCannotDetectClusterKind) {
84-
autodetect.CurrentKubernetesVersion.Kind = details.Kind
85-
fmt.Printf("Detected cluster: Kubernetes kind: %s\n", details.Kind)
86-
} else {
77+
clusterKink := autodetect.GetClusterKind()
78+
if clusterKink == autodetect.KindUnknown {
8779
fmt.Println("Unknown Kubernetes cluster detected, proceeding with installation")
80+
} else {
81+
fmt.Printf("Detected cluster: Kubernetes kind: %s\n", clusterKink)
8882
}
8983

90-
if !errors.Is(err, autodetect.ErrCannotDetectK8sVersion) {
91-
autodetect.CurrentKubernetesVersion.Version = details.K8SVersion
92-
if details.K8SVersion.LessThan(minK8SVersionForInstallation) {
93-
fmt.Printf("\033[31mERROR\033[0m Odigos requires Kubernetes version %s or higher but found %s, aborting\n", minK8SVersionForInstallation.String(), details.K8SVersion.String())
84+
k8sVersion := autodetect.GetK8SVersion()
85+
if k8sVersion != nil {
86+
if k8sVersion.LessThan(minK8SVersionForInstallation) {
87+
fmt.Printf("\033[31mERROR\033[0m Odigos requires Kubernetes version %s or higher but found %s, aborting\n", minK8SVersionForInstallation.String(), k8sVersion.String())
9488
os.Exit(1)
9589
}
96-
fmt.Printf("Detected cluster: Kubernetes version: %s\n", details.K8SVersion.String())
90+
fmt.Printf("Detected cluster: Kubernetes version: %s\n", k8sVersion.String())
9791
} else {
9892
fmt.Println("Unknown Kubernetes version detected, proceeding with installation")
9993
}
10094

95+
96+
// kc := cmd.Flag("kubeconfig").Value.String()
97+
// details, err := autodetect.SetK8SClusterDetails(ctx, kc, client)
98+
// if !errors.Is(err, autodetect.ErrCannotDetectClusterKind) {
99+
// autodetect.CurrentClusterDetails.Kind = details.Kind
100+
// fmt.Printf("Detected cluster: Kubernetes kind: %s\n", details.Kind)
101+
// } else {
102+
// fmt.Println("Unknown Kubernetes cluster detected, proceeding with installation")
103+
// }
104+
105+
// if !errors.Is(err, autodetect.ErrCannotDetectK8sVersion) {
106+
// autodetect.CurrentClusterDetails.K8SVersion = details.K8SVersion
107+
// if details.K8SVersion.LessThan(minK8SVersionForInstallation) {
108+
// fmt.Printf("\033[31mERROR\033[0m Odigos requires Kubernetes version %s or higher but found %s, aborting\n", minK8SVersionForInstallation.String(), details.K8SVersion.String())
109+
// os.Exit(1)
110+
// }
111+
// fmt.Printf("Detected cluster: Kubernetes version: %s\n", details.K8SVersion.String())
112+
// } else {
113+
// fmt.Println("Unknown Kubernetes version detected, proceeding with installation")
114+
// }
115+
101116
var odigosProToken string
102117
odigosTier := common.CommunityOdigosTier
103118
if odigosCloudApiKeyFlag != "" {

cli/cmd/login.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ func restartPodsAfterCloudLogin(ctx context.Context, client *kube.Client, ns str
4646

4747
// both login and update trigger this function.
4848
func updateApiKey(cmd *cobra.Command, args []string) {
49-
client, err := kube.CreateClient(cmd)
50-
if err != nil {
51-
kube.PrintClientErrorAndExit(err)
52-
}
49+
client := kube.GetCLIClient()
5350
ctx := cmd.Context()
5451

5552
ns, err := resources.GetOdigosNamespace(client, ctx)

cli/cmd/logout.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ var logoutCmd = &cobra.Command{
2323
You can run 'odigos ui' to manage your Odigos installation locally.
2424
`,
2525
Run: func(cmd *cobra.Command, args []string) {
26-
client, err := kube.CreateClient(cmd)
27-
if err != nil {
28-
kube.PrintClientErrorAndExit(err)
29-
}
26+
client := kube.GetCLIClient()
3027
ctx := cmd.Context()
3128

3229
ns, err := resources.GetOdigosNamespace(client, ctx)

cli/cmd/profile.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ var profileCmd = &cobra.Command{
1717
Short: "Manage odigos profiles",
1818
Long: `Odigos profiles are used to apply some specific preset configuration to the odigos installation`,
1919
Run: func(cmd *cobra.Command, args []string) {
20-
client, err := kube.CreateClient(cmd)
21-
if err != nil {
22-
kube.PrintClientErrorAndExit(err)
23-
}
20+
client := kube.GetCLIClient()
2421
ctx := cmd.Context()
2522

2623
ns, err := resources.GetOdigosNamespace(client, ctx)
@@ -78,10 +75,7 @@ var addProfileCmd = &cobra.Command{
7875
Long: `Add a profile by its name to the current Odigos installation.`,
7976
Args: cobra.ExactArgs(1), // Ensure exactly one argument is passed (the profile name)
8077
Run: func(cmd *cobra.Command, args []string) {
81-
client, err := kube.CreateClient(cmd)
82-
if err != nil {
83-
kube.PrintClientErrorAndExit(err)
84-
}
78+
client := kube.GetCLIClient()
8579
ctx := cmd.Context()
8680

8781
ns, err := resources.GetOdigosNamespace(client, ctx)
@@ -164,10 +158,7 @@ var removeProfileCmd = &cobra.Command{
164158
Long: `Remove a profile by its name from the current Odigos installation.`,
165159
Args: cobra.ExactArgs(1), // Ensure exactly one argument is passed (the profile name)
166160
Run: func(cmd *cobra.Command, args []string) {
167-
client, err := kube.CreateClient(cmd)
168-
if err != nil {
169-
kube.PrintClientErrorAndExit(err)
170-
}
161+
client := kube.GetCLIClient()
171162
ctx := cmd.Context()
172163

173164
ns, err := resources.GetOdigosNamespace(client, ctx)

cli/cmd/resources/applyresources.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/odigos-io/odigos/api/odigos/v1alpha1"
99
"github.com/odigos-io/odigos/cli/cmd/resources/resourcemanager"
10+
"github.com/odigos-io/odigos/cli/pkg/autodetect"
1011
"github.com/odigos-io/odigos/cli/pkg/kube"
1112
"github.com/odigos-io/odigos/cli/pkg/log"
1213
"github.com/odigos-io/odigos/common"
@@ -32,7 +33,7 @@ func DeleteOldOdigosSystemObjects(ctx context.Context, client *kube.Client, ns s
3233
resources := kube.GetManagedResources(ns)
3334
for _, resource := range resources {
3435
l := log.Print(fmt.Sprintf("Syncing %s", resource.Resource.Resource))
35-
err := client.DeleteOldOdigosSystemObjects(ctx, resource, config.ConfigVersion)
36+
err := client.DeleteOldOdigosSystemObjects(ctx, resource, config.ConfigVersion, autodetect.GetK8SVersion())
3637
if err != nil {
3738
l.Error(err)
3839
os.Exit(1)

cli/cmd/resources/odiglet.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func NewOdigletDaemonSet(ns string, version string, imagePrefix string, imageNam
434434

435435
odigosSeLinuxHostVolumes := []corev1.Volume{}
436436
odigosSeLinuxHostVolumeMounts := []corev1.VolumeMount{}
437-
if openshiftEnabled || autodetect.CurrentKubernetesVersion.Kind == autodetect.KindOpenShift {
437+
if openshiftEnabled || autodetect.GetClusterKind() == autodetect.KindOpenShift {
438438
odigosSeLinuxHostVolumes = append(odigosSeLinuxHostVolumes, selinuxHostVolumes()...)
439439
odigosSeLinuxHostVolumeMounts = append(odigosSeLinuxHostVolumeMounts, selinuxHostVolumeMounts()...)
440440
}
@@ -452,7 +452,8 @@ func NewOdigletDaemonSet(ns string, version string, imagePrefix string, imageNam
452452
rollingUpdate := &appsv1.RollingUpdateDaemonSet{
453453
MaxUnavailable: &maxUnavailable,
454454
}
455-
if autodetect.CurrentKubernetesVersion.Version != nil && autodetect.CurrentKubernetesVersion.Version.AtLeast(k8sversion.MustParse("v1.22")) {
455+
k8sversionInCluster := autodetect.GetK8SVersion()
456+
if k8sversionInCluster != nil && k8sversionInCluster.AtLeast(k8sversion.MustParse("v1.22")) {
456457
maxSurge := intstr.FromInt(0)
457458
rollingUpdate.MaxSurge = &maxSurge
458459
}
@@ -726,13 +727,13 @@ func (a *odigletResourceManager) InstallFromScratch(ctx context.Context) error {
726727
}
727728

728729
// if openshift is enabled, we need to create additional SCC cluster role binding first
729-
if a.config.OpenshiftEnabled || autodetect.CurrentKubernetesVersion.Kind == autodetect.KindOpenShift {
730+
if a.config.OpenshiftEnabled || autodetect.GetClusterKind() == autodetect.KindOpenShift {
730731
resources = append(resources, NewSCCRoleBinding(a.ns))
731732
resources = append(resources, NewSCClusterRoleBinding(a.ns))
732733
}
733734

734735
// if gke, create resource quota
735-
if autodetect.CurrentKubernetesVersion.Kind == autodetect.KindGKE {
736+
if autodetect.GetClusterKind() == autodetect.KindGKE {
736737
resources = append(resources, NewResourceQuota(a.ns))
737738
}
738739

cli/cmd/root.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cmd
33
import (
44
"os"
55

6+
"github.com/odigos-io/odigos/cli/pkg/autodetect"
7+
"github.com/odigos-io/odigos/cli/pkg/kube"
68
"github.com/odigos-io/odigos/k8sutils/pkg/env"
79
"github.com/spf13/cobra"
810
)
@@ -20,9 +22,10 @@ Key Features of Odigos:
2022
- Streamlined Kubernetes operations with observability at the forefront.
2123
2224
Get started with Odigos today to effortlessly improve the observability of your Kubernetes services!`,
23-
// Uncomment the following line if your bare application
24-
// has an action associated with it:
25-
// Run: func(cmd *cobra.Command, args []string) { },
25+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
26+
client := kube.SetCLIClientOrExit(cmd)
27+
autodetect.SetK8SClusterDetails(cmd.Context(), kubeConfig, client)
28+
},
2629
}
2730

2831
var (

cli/cmd/ui.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ var uiCmd = &cobra.Command{
3535
Long: `Start the Odigos UI. This command will port-forward the odigos-ui pod to your local machine.`,
3636
Run: func(cmd *cobra.Command, args []string) {
3737
ctx := cmd.Context()
38-
client, err := kube.CreateClient(cmd)
39-
if err != nil {
40-
kube.PrintClientErrorAndExit(err)
41-
}
38+
client := kube.GetCLIClient()
4239

4340
ns, err := resources.GetOdigosNamespace(client, ctx)
4441
if err != nil {

cli/cmd/uninstall.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ var uninstallCmd = &cobra.Command{
3232
Use: "uninstall",
3333
Short: "Unistall Odigos from your cluster",
3434
Run: func(cmd *cobra.Command, args []string) {
35-
client, err := kube.CreateClient(cmd)
36-
37-
if err != nil {
38-
kube.PrintClientErrorAndExit(err)
39-
}
40-
35+
client := kube.GetCLIClient()
4136
ctx := cmd.Context()
4237

4338
ns, err := resources.GetOdigosNamespace(client, ctx)

cli/cmd/upgrade.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ var upgradeCmd = &cobra.Command{
3030
This command will upgrade the Odigos version in the cluster to the version of Odigos CLI
3131
and apply any required migrations and adaptations.`,
3232
Run: func(cmd *cobra.Command, args []string) {
33-
34-
client, err := kube.CreateClient(cmd)
35-
if err != nil {
36-
kube.PrintClientErrorAndExit(err)
37-
}
33+
client := kube.GetCLIClient()
3834
ctx := cmd.Context()
3935

4036
ns, err := resources.GetOdigosNamespace(client, ctx)

cli/cmd/version.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ func getOdigosVersionInCluster(cmd *cobra.Command) (string, error) {
6666
}
6767

6868
func getOdigosKubeClientAndNamespace(cmd *cobra.Command) (*kube.Client, string, error) {
69-
client, err := kube.CreateClient(cmd)
70-
if err != nil {
71-
return nil, "", err
72-
}
69+
client := kube.GetCLIClient()
7370
ctx := cmd.Context()
7471

7572
ns, err := resources.GetOdigosNamespace(client, ctx)

0 commit comments

Comments
 (0)