Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API Token from the UI #2198

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ else
done
endif

.PHONY: lint-fix
lint-fix:
MODULE=common make lint FIX_LINT=true
MODULE=k8sutils make lint FIX_LINT=true

.PHONY: build-odiglet
build-odiglet:
docker build -t $(ORG)/odigos-odiglet:$(TAG) . -f odiglet/Dockerfile --build-arg ODIGOS_VERSION=$(TAG)
Expand Down
15 changes: 3 additions & 12 deletions cli/cmd/describe.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"
"github.com/odigos-io/odigos/cli/pkg/kube"
cmdcontext "github.com/odigos-io/odigos/cli/pkg/cmd_context"
k8sconsts "github.com/odigos-io/odigos/k8sutils/pkg/consts"
"github.com/odigos-io/odigos/k8sutils/pkg/describe"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -141,7 +142,7 @@ var describeSourceStatefulSetCmd = &cobra.Command{
}

func executeRemoteOdigosDescribe(ctx context.Context, client *kube.Client, odigosNs string) string {
uiSvcProxyEndpoint := getUiServiceOdigosEndpoint(ctx, client, odigosNs)
uiSvcProxyEndpoint := fmt.Sprintf("/api/v1/namespaces/%s/services/%s:%d/proxy/api/describe/odigos", odigosNs, k8sconsts.OdigosUiServiceName, k8sconsts.OdigosUiServicePort)
request := client.Clientset.RESTClient().Get().AbsPath(uiSvcProxyEndpoint).Do(ctx)
response, err := request.Raw()
if err != nil {
Expand All @@ -162,13 +163,6 @@ func executeRemoteSourceDescribe(ctx context.Context, client *kube.Client, workl
}
}

func getUiServiceOdigosEndpoint(ctx context.Context, client *kube.Client, odigosNs string) string {
uiServiceName := "ui"
uiServicePort := 3000

return fmt.Sprintf("/api/v1/namespaces/%s/services/%s:%d/proxy/api/describe/odigos", odigosNs, uiServiceName, uiServicePort)
}

func getUiServiceSourceEndpoint(ctx context.Context, client *kube.Client, workloadKind string, workloadNs string, workloadName string) string {
ns, err := resources.GetOdigosNamespace(client, ctx)
if resources.IsErrNoOdigosNamespaceFound(err) {
Expand All @@ -179,10 +173,7 @@ func getUiServiceSourceEndpoint(ctx context.Context, client *kube.Client, worklo
os.Exit(1)
}

uiServiceName := "ui"
uiServicePort := 3000

return fmt.Sprintf("/api/v1/namespaces/%s/services/%s:%d/proxy/api/describe/source/namespace/%s/kind/%s/name/%s", ns, uiServiceName, uiServicePort, workloadNs, workloadKind, workloadName)
return fmt.Sprintf("/api/v1/namespaces/%s/services/%s:%d/proxy/api/describe/source/namespace/%s/kind/%s/name/%s", ns, k8sconsts.OdigosUiServiceName, k8sconsts.OdigosUiServicePort, workloadNs, workloadKind, workloadName)
}

func init() {
Expand Down
62 changes: 22 additions & 40 deletions cli/cmd/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"context"
"fmt"
"os"
"time"

"github.com/odigos-io/odigos/cli/cmd/resources"
cmdcontext "github.com/odigos-io/odigos/cli/pkg/cmd_context"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/k8sutils/pkg/consts"
odigosconsts "github.com/odigos-io/odigos/common/consts"
k8sconsts "github.com/odigos-io/odigos/k8sutils/pkg/consts"
"github.com/odigos-io/odigos/k8sutils/pkg/pro"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
updateRemoteFlag bool
)

var proCmd = &cobra.Command{
Expand All @@ -32,57 +33,38 @@ var proCmd = &cobra.Command{
os.Exit(1)
}
onPremToken := cmd.Flag("onprem-token").Value.String()
err = updateOdigosToken(ctx, client, ns, onPremToken)
if updateRemoteFlag {
err = executeRemoteUpdateToken(ctx, client, ns, onPremToken)
} else {
err = pro.UpdateOdigosToken(ctx, client, ns, onPremToken)
}

if err != nil {
fmt.Println("\033[31mERROR\033[0m Failed to update token:")
fmt.Println(err)
os.Exit(1)
} else {
fmt.Println()
fmt.Println("\u001B[32mSUCCESS:\u001B[0m Token updated successfully")
}

fmt.Println()
fmt.Println("\u001B[32mSUCCESS:\u001B[0m Token updated successfully")
},
}

func updateOdigosToken(ctx context.Context, client *kube.Client, namespace string, onPremToken string) error {
secret, err := client.CoreV1().Secrets(namespace).Get(ctx, consts.OdigosProSecretName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return fmt.Errorf("Tokens are not available in the open-source version of Odigos. Please contact Odigos team to inquire about pro version.")
}
return err
}
secret.Data[consts.OdigosOnpremTokenSecretKey] = []byte(onPremToken)

_, err = client.CoreV1().Secrets(namespace).Update(ctx, secret, metav1.UpdateOptions{})
func executeRemoteUpdateToken(ctx context.Context, client *kube.Client, namespace string, onPremToken string) error {
uiSvcProxyEndpoint := fmt.Sprintf("/api/v1/namespaces/%s/services/%s:%d/proxy/api/token/update/%s", namespace, k8sconsts.OdigosUiServiceName, k8sconsts.OdigosUiServicePort, onPremToken)
request := client.Clientset.RESTClient().Get().AbsPath(uiSvcProxyEndpoint).Do(ctx)
_, err := request.Raw()
if err != nil {
return err
} else {
return nil
}

daemonSet, err := client.AppsV1().DaemonSets(namespace).Get(ctx, "odiglet", metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get DaemonSet odiglet in namespace %s: %v", namespace, err)
}

// Modify the DaemonSet spec.template to trigger a rollout
if daemonSet.Spec.Template.Annotations == nil {
daemonSet.Spec.Template.Annotations = make(map[string]string)
}
daemonSet.Spec.Template.Annotations[odigosconsts.RolloutTriggerAnnotation] = time.Now().Format(time.RFC3339)

_, err = client.AppsV1().DaemonSets(namespace).Update(ctx, daemonSet, metav1.UpdateOptions{})
if err != nil {
fmt.Printf("Failed to restart Odiglets. Reason: %s\n", err)
fmt.Printf("To trigger a restart manually, run the following command:\n")
fmt.Printf("kubectl rollout restart daemonset odiglet -n %s\n", daemonSet.Namespace)
}

return nil
}

func init() {
rootCmd.AddCommand(proCmd)

proCmd.Flags().String("onprem-token", "", "On-prem token for Odigos")
proCmd.MarkFlagRequired("onprem-token")
proCmd.PersistentFlags().BoolVarP(&updateRemoteFlag, "remote", "r", false, "use odigos ui service in the cluster to update the onprem token")
}
5 changes: 3 additions & 2 deletions cli/cmd/resources/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sconsts "github.com/odigos-io/odigos/k8sutils/pkg/consts"

"github.com/odigos-io/odigos/cli/cmd/resources/resourcemanager"
"github.com/odigos-io/odigos/cli/pkg/kube"
Expand Down Expand Up @@ -288,8 +289,8 @@ func NewUIService(ns string) *corev1.Service {
},
Ports: []corev1.ServicePort{
{
Name: "ui",
Port: 3000,
Name: k8sconsts.OdigosUiServiceName,
Port: k8sconsts.OdigosUiServicePort,
},
{
Name: "otlp",
Expand Down
Loading
Loading