-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Kernel Launcher and KernelTemplate and KernelSpec CRD (#17)
* UPDATE Signed-off-by: cegao <cegao@tencent.com> * feat: Add config Signed-off-by: cegao <cegao@tencent.com> * fix: Add git ignore file Signed-off-by: cegao <cegao@tencent.com> * feat: Update api doc Signed-off-by: cegao <cegao@tencent.com> * feat: Add CRDs Signed-off-by: cegao <cegao@tencent.com> * chore: Update golang dependencies Signed-off-by: cegao <cegao@tencent.com> * feat: Add launchers Signed-off-by: cegao <cegao@tencent.com> * feat: Add class name Signed-off-by: cegao <cegao@tencent.com> * feat: Add support to custom launcher Signed-off-by: cegao <cegao@tencent.com> * feat: Add env Signed-off-by: cegao <cegao@tencent.com> * chore: Add new permission Signed-off-by: cegao <cegao@tencent.com> * feat: Update API to support custom gateway image Signed-off-by: cegao <cegao@tencent.com> * chore: Update API Signed-off-by: cegao <cegao@tencent.com>
- Loading branch information
Showing
25 changed files
with
691 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ bin | |
*~ | ||
|
||
vendor/ | ||
/enterprise_gateway/ | ||
/enterprise_gateway/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Tencent is pleased to support the open source community by making TKEStack | ||
// available. | ||
|
||
// Copyright (C) 2012-2020 Tencent. All Rights Reserved. | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
// this file except in compliance with the License. You may obtain a copy of the | ||
// License at | ||
|
||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations under the License. | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// JupyterKernelTemplateSpec defines the desired state of JupyterKernelTemplate | ||
type JupyterKernelTemplateSpec struct { | ||
Template *v1.PodTemplate `json:"template,omitempty"` | ||
} | ||
|
||
// JupyterKernelTemplateStatus defines the observed state of JupyterKernelTemplate | ||
type JupyterKernelTemplateStatus struct { | ||
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster | ||
// Important: Run "make" to regenerate code after modifying this file | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
// +kubebuilder:subresource:status | ||
|
||
// JupyterKernelTemplate is the Schema for the jupyterkerneltemplates API | ||
type JupyterKernelTemplate struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec JupyterKernelTemplateSpec `json:"spec,omitempty"` | ||
Status JupyterKernelTemplateStatus `json:"status,omitempty"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
|
||
// JupyterKernelTemplateList contains a list of JupyterKernelTemplate | ||
type JupyterKernelTemplateList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []JupyterKernelTemplate `json:"items"` | ||
} | ||
|
||
func init() { | ||
SchemeBuilder.Register(&JupyterKernelTemplate{}, &JupyterKernelTemplateList{}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
Copyright © 2021 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
|
||
"github.com/tkestack/elastic-jupyter-operator/api/v1alpha1" | ||
) | ||
|
||
const ( | ||
envKernelPodName = "KERNEL_POD_NAME" | ||
envKernelImage = "KERNEL_IMAGE" | ||
envKernelNamespace = "KERNEL_NAMESPACE" | ||
envKernelID = "KERNEL_ID" | ||
envPortRange = "EG_PORT_RANGE" | ||
envResponseAddress = "EG_RESPONSE_ADDRESS" | ||
envPublicKey = "EG_PUBLIC_KEY" | ||
envKernelLanguage = "KERNEL_LANGUAGE" | ||
envKernelName = "KERNEL_NAME" | ||
envKernelSpark = "KERNEL_SPARK_CONTEXT_INIT_MODE" | ||
envKernelUsername = "KERNEL_USERNAME" | ||
|
||
labelKernelID = "kernel_id" | ||
) | ||
|
||
var kernelID, portRange, responseAddr, | ||
publicKey, sparkContextInitMode, | ||
kernelTemplateName, kernelTemplateNamespace string | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "kubeflow-launcher", | ||
Short: "Launch kernels", | ||
Long: `Launch kernels in the jupyter enterprise gateway`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if kernelTemplateName == "" || kernelTemplateNamespace == "" { | ||
panic(fmt.Errorf("Failed to get the template's name or namespace")) | ||
} | ||
|
||
if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil { | ||
panic(err) | ||
} | ||
|
||
cfg, err := config.GetConfig() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
cli, err := client.New(cfg, client.Options{ | ||
Scheme: scheme.Scheme, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
kt := &v1alpha1.JupyterKernelTemplate{} | ||
if err := cli.Get(context.TODO(), client.ObjectKey{ | ||
Namespace: kernelTemplateNamespace, | ||
Name: kernelTemplateName, | ||
}, kt); err != nil { | ||
panic(err) | ||
} | ||
|
||
tpl := kt.Spec.Template | ||
|
||
// Set image from the kernel spec. | ||
image := os.Getenv(envKernelImage) | ||
if image != "" && len(tpl.Template.Spec.Containers) != 0 { | ||
tpl.Template.Spec.Containers[0].Image = image | ||
} | ||
|
||
pod := &v1.Pod{ | ||
ObjectMeta: tpl.ObjectMeta, | ||
Spec: tpl.Template.Spec, | ||
} | ||
|
||
pod.Name = os.Getenv(envKernelPodName) | ||
pod.Namespace = os.Getenv(envKernelNamespace) | ||
if pod.Labels == nil { | ||
pod.Labels = make(map[string]string) | ||
} | ||
pod.Labels[labelKernelID] = kernelID | ||
|
||
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, | ||
v1.EnvVar{ | ||
Name: envPortRange, | ||
Value: portRange, | ||
}, | ||
v1.EnvVar{ | ||
Name: envResponseAddress, | ||
Value: responseAddr, | ||
}, | ||
v1.EnvVar{ | ||
Name: envPublicKey, | ||
Value: publicKey, | ||
}, | ||
v1.EnvVar{ | ||
Name: envKernelID, | ||
Value: kernelID, | ||
}, | ||
v1.EnvVar{ | ||
Name: envKernelLanguage, | ||
Value: os.Getenv(envKernelLanguage), | ||
}, | ||
v1.EnvVar{ | ||
Name: envKernelName, | ||
Value: os.Getenv(envKernelName), | ||
}, | ||
v1.EnvVar{ | ||
Name: envKernelNamespace, | ||
Value: os.Getenv(envKernelNamespace), | ||
}, | ||
v1.EnvVar{ | ||
Name: envKernelSpark, | ||
Value: sparkContextInitMode, | ||
}, | ||
v1.EnvVar{ | ||
Name: envKernelUsername, | ||
Value: os.Getenv(envKernelUsername), | ||
}, | ||
) | ||
|
||
if err := cli.Create(context.TODO(), pod); err != nil { | ||
panic(err) | ||
} | ||
|
||
log.Println(kernelID, portRange, | ||
responseAddr, publicKey, sparkContextInitMode) | ||
log.Println("done") | ||
|
||
}, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
|
||
rootCmd.Flags().StringVar(&kernelID, | ||
"RemoteProcessProxy.kernel-id", "", "kernel id") | ||
rootCmd.Flags().StringVar(&portRange, | ||
"RemoteProcessProxy.port-range", "", "port range") | ||
rootCmd.Flags().StringVar(&responseAddr, | ||
"RemoteProcessProxy.response-address", "", "response address") | ||
rootCmd.Flags().StringVar(&publicKey, | ||
"RemoteProcessProxy.public-key", "", "public key") | ||
rootCmd.Flags().StringVar(&sparkContextInitMode, | ||
"RemoteProcessProxy.spark-context-initialization-mode", | ||
"", "spark context init mode") | ||
|
||
rootCmd.Flags().StringVar(&kernelTemplateName, | ||
"kernel-template-name", "", "kernel template CRD name") | ||
rootCmd.Flags().StringVar(&kernelTemplateNamespace, | ||
"kernel-template-namespace", "", "kernel template CRD namesapce") | ||
} |
Oops, something went wrong.