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

feat: Add Kernel Launcher and KernelTemplate and KernelSpec CRD #17

Merged
merged 13 commits into from
May 8, 2021
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ bin
*~

vendor/
/enterprise_gateway/
/enterprise_gateway/
.vscode/
2 changes: 2 additions & 0 deletions api/v1alpha1/jupytergateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type JupyterGatewaySpec struct {
// +optional
Resources *v1.ResourceRequirements `json:"resources,omitempty"`

Image string `json:"image,omitempty"`

ClusterRole *string `json:"clusterRole,omitempty"`
}

Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha1/jupyterkernelspec_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type JupyterKernelSpecSpec struct {
Image string `json:"image,omitempty"`
Env []v1.EnvVar `json:"env,omitempty"`
Command []string `json:"command,omitempty"`
ClassName string `json:"className,omitempty"`

Template *v1.ObjectReference `json:"template,omitempty"`
// TODO(gaocegege): Support resources and so on.
}

Expand Down
58 changes: 58 additions & 0 deletions api/v1alpha1/jupyterkerneltemplate_types.go
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{})
}
99 changes: 99 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

187 changes: 187 additions & 0 deletions cli/cmd/root.go
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")
}
Loading