Skip to content

Commit

Permalink
feat: odigos describe with cluster gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
blumamir committed Oct 17, 2024
1 parent 5ffa212 commit dd51f6e
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 56 deletions.
27 changes: 27 additions & 0 deletions cli/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ var describeCmd = &cobra.Command{
Use: "describe",
Short: "Show details of a specific odigos entity",
Long: `Print detailed description of a specific odigos entity, which can be used to troubleshoot issues`,
Run: func(cmd *cobra.Command, args []string) {

client, err := kube.CreateClient(cmd)
if err != nil {
kube.PrintClientErrorAndExit(err)
}
ctx := cmd.Context()

odigosNs, err := resources.GetOdigosNamespace(client, ctx)
if err != nil {
if resources.IsErrNoOdigosNamespaceFound(err) {
fmt.Println("\033[31mERROR\033[0m Odigos is NOT yet installed in the current cluster")
} else {
fmt.Println("\033[31mERROR\033[0m Error detecting Odigos namespace in the current cluster")
}
return
}

var describeText string
if describeRemoteFlag {
// TODO: remote flag support
// describeText = executeRemoteDescribe(ctx, client, "odigos", "", "")
} else {
describeText = describe.DescribeOdigos(ctx, client.Clientset, client.OdigosClient, odigosNs)
}
fmt.Println(describeText)
},
}

var describeSourceCmd = &cobra.Command{
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/common/utils"
k8sconsts "github.com/odigos-io/odigos/k8sutils/pkg/consts"

"github.com/odigos-io/odigos/cli/cmd/resources"
"github.com/odigos-io/odigos/cli/pkg/kube"
Expand Down Expand Up @@ -62,7 +63,7 @@ This command will install k8s components that will auto-instrument your applicat
ns := cmd.Flag("namespace").Value.String()

// Check if Odigos already installed
cm, err := client.CoreV1().ConfigMaps(ns).Get(ctx, resources.OdigosDeploymentConfigMapName, metav1.GetOptions{})
cm, err := client.CoreV1().ConfigMaps(ns).Get(ctx, k8sconsts.OdigosDeploymentConfigMapName, metav1.GetOptions{})
if err == nil && cm != nil {
fmt.Printf("\033[31mERROR\033[0m Odigos is already installed in namespace\n")
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/odigos-io/odigos/cli/pkg/labels"
"github.com/odigos-io/odigos/cli/pkg/log"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/k8sutils/pkg/getters"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -60,7 +61,7 @@ func updateApiKey(cmd *cobra.Command, args []string) {
os.Exit(1)
}

currentOdigosVersion, err := getOdigosVersionInClusterFromConfigMap(ctx, client, ns)
currentOdigosVersion, err := getters.GetOdigosVersionInClusterFromConfigMap(ctx, client.Clientset, ns)
if err != nil {
fmt.Println("Odigos cloud login failed - unable to read the current Odigos version.")
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/odigos-io/odigos/cli/pkg/confirm"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/k8sutils/pkg/getters"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -37,7 +38,7 @@ var logoutCmd = &cobra.Command{
os.Exit(1)
}

currentOdigosVersion, err := getOdigosVersionInClusterFromConfigMap(ctx, client, ns)
currentOdigosVersion, err := getters.GetOdigosVersionInClusterFromConfigMap(ctx, client.Clientset, ns)
if err != nil {
fmt.Println("Odigos cloud login failed - unable to read the current Odigos version.")
os.Exit(1)
Expand Down
5 changes: 3 additions & 2 deletions cli/cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/odigos-io/odigos/cli/cmd/resources/odigospro"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/k8sutils/pkg/getters"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -92,7 +93,7 @@ var addProfileCmd = &cobra.Command{
os.Exit(1)
}

currentOdigosVersion, err := getOdigosVersionInClusterFromConfigMap(ctx, client, ns)
currentOdigosVersion, err := getters.GetOdigosVersionInClusterFromConfigMap(ctx, client.Clientset, ns)
if err != nil {
fmt.Println("Odigos cloud login failed - unable to read the current Odigos version.")
os.Exit(1)
Expand Down Expand Up @@ -178,7 +179,7 @@ var removeProfileCmd = &cobra.Command{
os.Exit(1)
}

currentOdigosVersion, err := getOdigosVersionInClusterFromConfigMap(ctx, client, ns)
currentOdigosVersion, err := getters.GetOdigosVersionInClusterFromConfigMap(ctx, client.Clientset, ns)
if err != nil {
fmt.Println("Odigos cloud login failed - unable to read the current Odigos version.")
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/resources/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/odigos-io/odigos/cli/pkg/containers"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/k8sutils/pkg/consts"
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -459,7 +460,7 @@ func NewAutoscalerDeployment(ns string, version string, imagePrefix string, imag
{
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: OdigosDeploymentConfigMapName,
Name: consts.OdigosDeploymentConfigMapName,
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/resources/keyvalproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/odigos-io/odigos/cli/pkg/containers"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/k8sutils/pkg/consts"
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -324,7 +325,7 @@ func NewKeyvalProxyDeployment(version string, ns string, imagePrefix string) *ap
{
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: OdigosDeploymentConfigMapName,
Name: consts.OdigosDeploymentConfigMapName,
},
},
},
Expand Down
7 changes: 2 additions & 5 deletions cli/cmd/resources/odigosdeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@ import (
"github.com/odigos-io/odigos/cli/cmd/resources/resourcemanager"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/k8sutils/pkg/consts"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
OdigosDeploymentConfigMapName = "odigos-deployment"
)

func NewOdigosDeploymentConfigMap(ns string, odigosVersion string) *corev1.ConfigMap {
return &corev1.ConfigMap{
TypeMeta: v1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: v1.ObjectMeta{
Name: OdigosDeploymentConfigMapName,
Name: consts.OdigosDeploymentConfigMapName,
Namespace: ns,
},
Data: map[string]string{
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/common/utils"
k8sconsts "github.com/odigos-io/odigos/k8sutils/pkg/consts"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -47,7 +48,7 @@ and apply any required migrations and adaptations.`,
skipVersionCheckFlag := cmd.Flag("skip-version-check")
if skipVersionCheckFlag == nil || !cmd.Flag("skip-version-check").Changed {

cm, err := client.CoreV1().ConfigMaps(ns).Get(ctx, resources.OdigosDeploymentConfigMapName, metav1.GetOptions{})
cm, err := client.CoreV1().ConfigMaps(ns).Get(ctx, k8sconsts.OdigosDeploymentConfigMapName, metav1.GetOptions{})
if err != nil {
fmt.Println("Odigos upgrade failed - unable to read the current Odigos version for migration")
os.Exit(1)
Expand Down
30 changes: 5 additions & 25 deletions cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"context"
"fmt"

"github.com/odigos-io/odigos/cli/cmd/resources"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/k8sutils/pkg/getters"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand All @@ -20,7 +19,7 @@ const (

var (
OdigosVersion string
OdigosClusterVersion string
odigosClusterVersion string
OdigosCommit string
OdigosDate string
)
Expand All @@ -37,7 +36,7 @@ var versionCmd = &cobra.Command{
fmt.Printf("%s\n", OdigosVersion)
}

OdigosClusterVersion, err := getOdigosVersionInCluster(cmd, clusterFlag)
OdigosClusterVersion, err := getOdigosVersionInCluster(cmd)

if clusterFlag && err == nil {
fmt.Printf("%s\n", OdigosClusterVersion)
Expand All @@ -57,32 +56,13 @@ var versionCmd = &cobra.Command{
},
}

func getOdigosVersionInCluster(cmd *cobra.Command, flag bool) (string, error) {
func getOdigosVersionInCluster(cmd *cobra.Command) (string, error) {
client, ns, err := getOdigosKubeClientAndNamespace(cmd)
if err != nil {
return "", err
}

OdigosClusterVersion, err = getOdigosVersionInClusterFromConfigMap(cmd.Context(), client, ns)
if err != nil {
return "", err
}

return OdigosClusterVersion, nil
}

func getOdigosVersionInClusterFromConfigMap(ctx context.Context, client *kube.Client, ns string) (string, error) {
cm, err := client.CoreV1().ConfigMaps(ns).Get(ctx, resources.OdigosDeploymentConfigMapName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("error detecting Odigos version in the current cluster")
}

odigosVersion, ok := cm.Data["ODIGOS_VERSION"]
if !ok || odigosVersion == "" {
return "", fmt.Errorf("error detecting Odigos version in the current cluster")
}

return odigosVersion, nil
return getters.GetOdigosVersionInClusterFromConfigMap(cmd.Context(), client.Clientset, ns)
}

func getOdigosKubeClientAndNamespace(cmd *cobra.Command) (*kube.Client, string, error) {
Expand Down
4 changes: 4 additions & 0 deletions k8sutils/pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const (
// OdigosCollectorRoleLabel is the label used to identify the role of the Odigos collector.
const OdigosCollectorRoleLabel = "odigos.io/collector-role"

const (
OdigosDeploymentConfigMapName = "odigos-deployment"
)

const (
OdigosClusterCollectorDeploymentName = "odigos-gateway"
OdigosClusterCollectorConfigMapName = OdigosClusterCollectorDeploymentName
Expand Down
28 changes: 28 additions & 0 deletions k8sutils/pkg/describe/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package describe

import (
"fmt"
"strings"
)

func wrapTextInRed(text string) string {
return "\033[31m" + text + "\033[0m"
}

func wrapTextInGreen(text string) string {
return "\033[32m" + text + "\033[0m"
}

func wrapTextSuccessOfFailure(text string, success bool) string {
if success {
return wrapTextInGreen(text)
} else {
return wrapTextInRed(text)
}
}

func describeText(sb *strings.Builder, indent int, printftext string, args ...interface{}) {
indentText := strings.Repeat(" ", indent)
lineText := fmt.Sprintf(printftext, args...)
sb.WriteString(fmt.Sprintf("%s%s\n", indentText, lineText))
}
Loading

0 comments on commit dd51f6e

Please sign in to comment.