|
| 1 | +package session |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 9 | + "k8s.io/cli-runtime/pkg/genericiooptions" |
| 10 | + "k8s.io/client-go/kubernetes" |
| 11 | + "k8s.io/kubectl/pkg/cmd/portforward" |
| 12 | + cmdutil "k8s.io/kubectl/pkg/cmd/util" |
| 13 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + DASHBOARD_PORT = 8265 |
| 19 | + CLIENT_PORT = 10001 |
| 20 | +) |
| 21 | + |
| 22 | +type SessionOptions struct { |
| 23 | + ioStreams *genericiooptions.IOStreams |
| 24 | + configFlags *genericclioptions.ConfigFlags |
| 25 | + ResourceName string |
| 26 | + Namespace string |
| 27 | +} |
| 28 | + |
| 29 | +func NewSessionOptions(streams genericiooptions.IOStreams) *SessionOptions { |
| 30 | + return &SessionOptions{ |
| 31 | + ioStreams: &streams, |
| 32 | + configFlags: genericclioptions.NewConfigFlags(true), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func NewSessionCommand(streams genericiooptions.IOStreams) *cobra.Command { |
| 37 | + options := NewSessionOptions(streams) |
| 38 | + factory := cmdutil.NewFactory(options.configFlags) |
| 39 | + |
| 40 | + cmd := &cobra.Command{ |
| 41 | + Use: "session NAME", |
| 42 | + Short: "Forward local ports to the Ray resources. Currently only supports RayCluster.", |
| 43 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + if err := options.Complete(cmd, args); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + if err := options.Validate(); err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + return options.Run(cmd.Context(), factory) |
| 51 | + }, |
| 52 | + } |
| 53 | + options.configFlags.AddFlags(cmd.Flags()) |
| 54 | + return cmd |
| 55 | +} |
| 56 | + |
| 57 | +func (options *SessionOptions) Complete(cmd *cobra.Command, args []string) error { |
| 58 | + if len(args) != 1 { |
| 59 | + return cmdutil.UsageErrorf(cmd, "%s", cmd.Use) |
| 60 | + } |
| 61 | + options.ResourceName = args[0] |
| 62 | + |
| 63 | + if *options.configFlags.Namespace == "" { |
| 64 | + options.Namespace = "default" |
| 65 | + } else { |
| 66 | + options.Namespace = *options.configFlags.Namespace |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func (options *SessionOptions) Validate() error { |
| 73 | + // Overrides and binds the kube config then retrieves the merged result |
| 74 | + config, err := options.configFlags.ToRawKubeConfigLoader().RawConfig() |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("Error retrieving raw config: %w", err) |
| 77 | + } |
| 78 | + if len(config.CurrentContext) == 0 { |
| 79 | + return fmt.Errorf("no context is currently set, use %q to select a new one", "kubectl config use-context <context>") |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func (options *SessionOptions) Run(ctx context.Context, factory cmdutil.Factory) error { |
| 85 | + kubeClientSet, err := factory.KubernetesClientSet() |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to initialize clientset: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + svcName, err := findServiceName(ctx, kubeClientSet, options.Namespace, options.ResourceName) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + portForwardCmd := portforward.NewCmdPortForward(factory, *options.ioStreams) |
| 96 | + portForwardCmd.SetArgs([]string{svcName, fmt.Sprintf("%d:%d", DASHBOARD_PORT, DASHBOARD_PORT), fmt.Sprintf("%d:%d", CLIENT_PORT, CLIENT_PORT)}) |
| 97 | + |
| 98 | + fmt.Printf("Ray Dashboard: http://localhost:%d\nRay Interactive Client: http://localhost:%d\n\n", DASHBOARD_PORT, CLIENT_PORT) |
| 99 | + |
| 100 | + if err := portForwardCmd.ExecuteContext(ctx); err != nil { |
| 101 | + return fmt.Errorf("failed to port-forward: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func findServiceName(ctx context.Context, kubeClientSet kubernetes.Interface, namespace, resourceName string) (string, error) { |
| 108 | + listopts := metav1.ListOptions{ |
| 109 | + LabelSelector: fmt.Sprintf("ray.io/cluster=%s, ray.io/node-type=head", resourceName), |
| 110 | + } |
| 111 | + |
| 112 | + rayHeadSvcs, err := kubeClientSet.CoreV1().Services(namespace).List(ctx, listopts) |
| 113 | + if err != nil { |
| 114 | + return "", fmt.Errorf("unable to retrieve ray head services: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + if len(rayHeadSvcs.Items) == 0 { |
| 118 | + return "", fmt.Errorf("no ray head services found") |
| 119 | + } |
| 120 | + if len(rayHeadSvcs.Items) > 1 { |
| 121 | + return "", fmt.Errorf("more than one ray head service found") |
| 122 | + } |
| 123 | + |
| 124 | + rayHeadSrc := rayHeadSvcs.Items[0] |
| 125 | + return "service/" + rayHeadSrc.Name, nil |
| 126 | +} |
0 commit comments