Skip to content

Commit

Permalink
Update API Token from the UI (#2198)
Browse files Browse the repository at this point in the history
This pull request includes multiple changes aimed at improving code
maintainability and functionality across several files. The most
significant changes involve adding a new `lint-fix` target to the
`Makefile`, refactoring the `cli/cmd/describe.go` file to use constants,
and updating the `frontend/graph/generated.go` file to include a new
mutation for updating API tokens.

### Makefile improvements:
* Added a `lint-fix` target to the `Makefile` to allow automatic fixing
of linting issues for the `common` and `k8sutils` modules.

### CLI command refactoring:
* Refactored `cli/cmd/describe.go` to use constants from `k8sconsts` for
service names and ports, improving code readability and maintainability.
[[1]](diffhunk://#diff-1a964108ddfe26caa973e0a9d810ffcf037627b43a50f42e9f87b624d52783c6R11)
[[2]](diffhunk://#diff-1a964108ddfe26caa973e0a9d810ffcf037627b43a50f42e9f87b624d52783c6L144-R145)
[[3]](diffhunk://#diff-1a964108ddfe26caa973e0a9d810ffcf037627b43a50f42e9f87b624d52783c6L165-L171)
[[4]](diffhunk://#diff-1a964108ddfe26caa973e0a9d810ffcf037627b43a50f42e9f87b624d52783c6L182-R176)
* Updated `cli/cmd/pro.go` to include a new flag `updateRemoteFlag` and
refactored the token update logic to use a remote update function if the
flag is set.
[[1]](diffhunk://#diff-433dfb4fa2ecc9c2fe6e9a0996b24b5c205837c86b9201767e7f4956e98b41a5L7-R17)
[[2]](diffhunk://#diff-433dfb4fa2ecc9c2fe6e9a0996b24b5c205837c86b9201767e7f4956e98b41a5L35-R69)

### Frontend GraphQL schema updates:
* Added a new mutation `UpdateAPIToken` to the GraphQL schema in
`frontend/graph/generated.go`, allowing API tokens to be updated.
[[1]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2R309)
[[2]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L500-R508)
[[3]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2R1677-R1688)
[[4]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2R2832-R2846)
[[5]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L9733-R9763)
[[6]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L9747-R9776)
[[7]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L9759-R9800)
[[8]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L9797-R9810)
[[9]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L9914-R9928)
[[10]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L9928-R9941)
[[11]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L9940-R9965)
[[12]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L9974-R9983)
[[13]](diffhunk://#diff-4bacf1f13939a5c243f3f83d21f4560b331d13667d81ea5945ed1f57ddb205f2L9995-R9996)

### Miscellaneous updates:
* Updated `cli/cmd/resources/ui.go` to use constants from `k8sconsts`
for service names and ports, ensuring consistency across the codebase.
[[1]](diffhunk://#diff-c286e10d34710a80a59127b2b7951e8a33d9b9554e47d2f2b827fd690f2e53abR15)
[[2]](diffhunk://#diff-c286e10d34710a80a59127b2b7951e8a33d9b9554e47d2f2b827fd690f2e53abL291-R293)

---------

Co-authored-by: Avihu Hanya <[email protected]>
  • Loading branch information
BenElferink and AvihuHenya authored Jan 13, 2025
1 parent 3789351 commit 3611a3b
Show file tree
Hide file tree
Showing 21 changed files with 590 additions and 270 deletions.
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

0 comments on commit 3611a3b

Please sign in to comment.