diff --git a/.gitignore b/.gitignore index 5f79572..c961faf 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ bin *~ vendor/ -/enterprise_gateway/ \ No newline at end of file +/enterprise_gateway/ +.vscode/ diff --git a/api/v1alpha1/jupytergateway_types.go b/api/v1alpha1/jupytergateway_types.go index 2f6d60c..47b8c96 100644 --- a/api/v1alpha1/jupytergateway_types.go +++ b/api/v1alpha1/jupytergateway_types.go @@ -46,6 +46,8 @@ type JupyterGatewaySpec struct { // +optional Resources *v1.ResourceRequirements `json:"resources,omitempty"` + Image string `json:"image,omitempty"` + ClusterRole *string `json:"clusterRole,omitempty"` } diff --git a/api/v1alpha1/jupyterkernelspec_types.go b/api/v1alpha1/jupyterkernelspec_types.go index d003d78..a520376 100644 --- a/api/v1alpha1/jupyterkernelspec_types.go +++ b/api/v1alpha1/jupyterkernelspec_types.go @@ -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. } diff --git a/api/v1alpha1/jupyterkerneltemplate_types.go b/api/v1alpha1/jupyterkerneltemplate_types.go new file mode 100644 index 0000000..ed64350 --- /dev/null +++ b/api/v1alpha1/jupyterkerneltemplate_types.go @@ -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{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index d6f7049..521e453 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -219,6 +219,11 @@ func (in *JupyterKernelSpecSpec) DeepCopyInto(out *JupyterKernelSpecSpec) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.Template != nil { + in, out := &in.Template, &out.Template + *out = new(v1.ObjectReference) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new JupyterKernelSpecSpec. @@ -246,6 +251,100 @@ func (in *JupyterKernelSpecStatus) DeepCopy() *JupyterKernelSpecStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *JupyterKernelTemplate) DeepCopyInto(out *JupyterKernelTemplate) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + out.Status = in.Status +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new JupyterKernelTemplate. +func (in *JupyterKernelTemplate) DeepCopy() *JupyterKernelTemplate { + if in == nil { + return nil + } + out := new(JupyterKernelTemplate) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *JupyterKernelTemplate) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *JupyterKernelTemplateList) DeepCopyInto(out *JupyterKernelTemplateList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]JupyterKernelTemplate, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new JupyterKernelTemplateList. +func (in *JupyterKernelTemplateList) DeepCopy() *JupyterKernelTemplateList { + if in == nil { + return nil + } + out := new(JupyterKernelTemplateList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *JupyterKernelTemplateList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *JupyterKernelTemplateSpec) DeepCopyInto(out *JupyterKernelTemplateSpec) { + *out = *in + if in.Template != nil { + in, out := &in.Template, &out.Template + *out = new(v1.PodTemplate) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new JupyterKernelTemplateSpec. +func (in *JupyterKernelTemplateSpec) DeepCopy() *JupyterKernelTemplateSpec { + if in == nil { + return nil + } + out := new(JupyterKernelTemplateSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *JupyterKernelTemplateStatus) DeepCopyInto(out *JupyterKernelTemplateStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new JupyterKernelTemplateStatus. +func (in *JupyterKernelTemplateStatus) DeepCopy() *JupyterKernelTemplateStatus { + if in == nil { + return nil + } + out := new(JupyterKernelTemplateStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *JupyterNotebook) DeepCopyInto(out *JupyterNotebook) { *out = *in diff --git a/cli/cmd/root.go b/cli/cmd/root.go new file mode 100644 index 0000000..0f8ca8b --- /dev/null +++ b/cli/cmd/root.go @@ -0,0 +1,187 @@ +/* +Copyright © 2021 NAME HERE + +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") +} diff --git a/cli/main.go b/cli/main.go new file mode 100644 index 0000000..1561a0b --- /dev/null +++ b/cli/main.go @@ -0,0 +1,22 @@ +/* +Copyright © 2021 NAME HERE + +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 main + +import "github.com/tkestack/elastic-jupyter-operator/cli/cmd" + +func main() { + cmd.Execute() +} diff --git a/config/crd/bases/kubeflow.tkestack.io_jupyterkerneltemplates.yaml b/config/crd/bases/kubeflow.tkestack.io_jupyterkerneltemplates.yaml new file mode 100644 index 0000000..2aef764 --- /dev/null +++ b/config/crd/bases/kubeflow.tkestack.io_jupyterkerneltemplates.yaml @@ -0,0 +1,30 @@ + +--- +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.3.0 + creationTimestamp: null + name: jupyterkerneltemplates.kubeflow.tkestack.io +spec: + group: kubeflow.tkestack.io + names: + kind: JupyterKernelTemplate + listKind: JupyterKernelTemplateList + plural: jupyterkerneltemplates + singular: jupyterkerneltemplate + scope: Namespaced + subresources: + status: {} + version: v1alpha1 + versions: + - name: v1alpha1 + served: true + storage: true +status: + acceptedNames: + kind: "" + plural: "" + conditions: [] + storedVersions: [] diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index cf1f974..549fd1a 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -5,6 +5,7 @@ resources: - bases/kubeflow.tkestack.io_jupyternotebooks.yaml - bases/kubeflow.tkestack.io_jupytergateways.yaml - bases/kubeflow.tkestack.io_jupyterkernelspecs.yaml +- bases/kubeflow.tkestack.io_jupyterkerneltemplates.yaml # +kubebuilder:scaffold:crdkustomizeresource patchesStrategicMerge: @@ -13,6 +14,7 @@ patchesStrategicMerge: #- patches/webhook_in_jupyternotebooks.yaml #- patches/webhook_in_jupytergateways.yaml #- patches/webhook_in_jupyterkernelspecs.yaml +#- patches/webhook_in_jupyterkerneltemplates.yaml # +kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix. @@ -20,6 +22,7 @@ patchesStrategicMerge: #- patches/cainjection_in_jupyternotebooks.yaml #- patches/cainjection_in_jupytergateways.yaml #- patches/cainjection_in_jupyterkernelspecs.yaml +#- patches/cainjection_in_jupyterkerneltemplates.yaml # +kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/config/crd/patches/cainjection_in_jupyterkerneltemplates.yaml b/config/crd/patches/cainjection_in_jupyterkerneltemplates.yaml new file mode 100644 index 0000000..ab0f19f --- /dev/null +++ b/config/crd/patches/cainjection_in_jupyterkerneltemplates.yaml @@ -0,0 +1,8 @@ +# The following patch adds a directive for certmanager to inject CA into the CRD +# CRD conversion requires k8s 1.13 or later. +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + annotations: + cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) + name: jupyterkerneltemplates.kubeflow.tkestack.io diff --git a/config/crd/patches/webhook_in_jupyterkerneltemplates.yaml b/config/crd/patches/webhook_in_jupyterkerneltemplates.yaml new file mode 100644 index 0000000..3ec21d4 --- /dev/null +++ b/config/crd/patches/webhook_in_jupyterkerneltemplates.yaml @@ -0,0 +1,17 @@ +# The following patch enables conversion webhook for CRD +# CRD conversion requires k8s 1.13 or later. +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: jupyterkerneltemplates.kubeflow.tkestack.io +spec: + conversion: + strategy: Webhook + webhookClientConfig: + # this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank, + # but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager) + caBundle: Cg== + service: + namespace: system + name: webhook-service + path: /convert diff --git a/config/rbac/jupyterkerneltemplate_editor_role.yaml b/config/rbac/jupyterkerneltemplate_editor_role.yaml new file mode 100644 index 0000000..6813d77 --- /dev/null +++ b/config/rbac/jupyterkerneltemplate_editor_role.yaml @@ -0,0 +1,24 @@ +# permissions for end users to edit jupyterkerneltemplates. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: jupyterkerneltemplate-editor-role +rules: +- apiGroups: + - kubeflow.tkestack.io + resources: + - jupyterkerneltemplates + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - kubeflow.tkestack.io + resources: + - jupyterkerneltemplates/status + verbs: + - get diff --git a/config/rbac/jupyterkerneltemplate_viewer_role.yaml b/config/rbac/jupyterkerneltemplate_viewer_role.yaml new file mode 100644 index 0000000..34bfeb6 --- /dev/null +++ b/config/rbac/jupyterkerneltemplate_viewer_role.yaml @@ -0,0 +1,20 @@ +# permissions for end users to view jupyterkerneltemplates. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: jupyterkerneltemplate-viewer-role +rules: +- apiGroups: + - kubeflow.tkestack.io + resources: + - jupyterkerneltemplates + verbs: + - get + - list + - watch +- apiGroups: + - kubeflow.tkestack.io + resources: + - jupyterkerneltemplates/status + verbs: + - get diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 7877da5..0bcb6df 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -78,6 +78,26 @@ rules: - get - patch - update +- apiGroups: + - kubeflow.tkestack.io + resources: + - jupyterkerneltemplates + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - kubeflow.tkestack.io + resources: + - jupyterkerneltemplates/status + verbs: + - get + - patch + - update - apiGroups: - kubeflow.tkestack.io resources: diff --git a/config/samples/kubeflow.tkestack.io_v1alpha1_jupyterkernelspec-custom-launcher.yaml b/config/samples/kubeflow.tkestack.io_v1alpha1_jupyterkernelspec-custom-launcher.yaml new file mode 100644 index 0000000..05121d0 --- /dev/null +++ b/config/samples/kubeflow.tkestack.io_v1alpha1_jupyterkernelspec-custom-launcher.yaml @@ -0,0 +1,20 @@ +apiVersion: kubeflow.tkestack.io/v1alpha1 +kind: JupyterKernelSpec +metadata: + name: python-kubernetes +spec: + language: Python + displayName: "Python on Kubernetes as a JupyterKernelSpec" + image: ccr.ccs.tencentyun.com/kubeflow-oteam/jupyter-kernel-py:2.5.0 + template: + namespace: default + name: jupyterkerneltemplate-sample + command: + # Use the default scripts to launch the kernel. + - "kubeflow-launcher" + - "--RemoteProcessProxy.kernel-id" + - "{kernel_id}" + - "--RemoteProcessProxy.port-range" + - "{port_range}" + - "--RemoteProcessProxy.response-address" + - "{response_address}" diff --git a/config/samples/kubeflow.tkestack.io_v1alpha1_jupyterkerneltemplate.yaml b/config/samples/kubeflow.tkestack.io_v1alpha1_jupyterkerneltemplate.yaml new file mode 100644 index 0000000..cdd163b --- /dev/null +++ b/config/samples/kubeflow.tkestack.io_v1alpha1_jupyterkerneltemplate.yaml @@ -0,0 +1,15 @@ +apiVersion: kubeflow.tkestack.io/v1alpha1 +kind: JupyterKernelTemplate +metadata: + name: jupyterkerneltemplate-sample +spec: + template: + metadata: + app: enterprise-gateway + component: kernel + template: + spec: + restartPolicy: Never + containers: + - name: kernel + diff --git a/controllers/jupyterkerneltemplate_controller.go b/controllers/jupyterkerneltemplate_controller.go new file mode 100644 index 0000000..d277d0f --- /dev/null +++ b/controllers/jupyterkerneltemplate_controller.go @@ -0,0 +1,53 @@ +// 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 controllers + +import ( + "context" + + "github.com/go-logr/logr" + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + + kubeflowtkestackiov1alpha1 "github.com/tkestack/elastic-jupyter-operator/api/v1alpha1" +) + +// JupyterKernelTemplateReconciler reconciles a JupyterKernelTemplate object +type JupyterKernelTemplateReconciler struct { + client.Client + Log logr.Logger + Scheme *runtime.Scheme +} + +// +kubebuilder:rbac:groups=kubeflow.tkestack.io,resources=jupyterkerneltemplates,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=kubeflow.tkestack.io,resources=jupyterkerneltemplates/status,verbs=get;update;patch + +func (r *JupyterKernelTemplateReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { + _ = context.Background() + _ = r.Log.WithValues("jupyterkerneltemplate", req.NamespacedName) + + // your logic here + + return ctrl.Result{}, nil +} + +func (r *JupyterKernelTemplateReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&kubeflowtkestackiov1alpha1.JupyterKernelTemplate{}). + Complete(r) +} diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 5104851..413d30e 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -71,6 +71,9 @@ var _ = BeforeSuite(func(done Done) { err = kubeflowtkestackiov1alpha1.AddToScheme(scheme.Scheme) Expect(err).NotTo(HaveOccurred()) + err = kubeflowtkestackiov1alpha1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + // +kubebuilder:scaffold:scheme k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) diff --git a/docs/api/generated.asciidoc b/docs/api/generated.asciidoc index adc7b73..1f301e5 100644 --- a/docs/api/generated.asciidoc +++ b/docs/api/generated.asciidoc @@ -18,6 +18,8 @@ Package v1alpha1 contains API Schema definitions for the kubeflow.tkestack.io v1 - xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupytergatewaylist[$$JupyterGatewayList$$] - xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkernelspec[$$JupyterKernelSpec$$] - xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkernelspeclist[$$JupyterKernelSpecList$$] +- xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplate[$$JupyterKernelTemplate$$] +- xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplatelist[$$JupyterKernelTemplateList$$] - xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyternotebook[$$JupyterNotebook$$] - xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyternotebooklist[$$JupyterNotebookList$$] @@ -84,6 +86,7 @@ JupyterGatewaySpec defines the desired state of JupyterGateway | *`cullIdleTimeout`* __integer__ | Timeout (in seconds) after which a kernel is considered idle and ready to be culled. Values of 0 or lower disable culling. Very short timeouts may result in kernels being culled for users with poor network connections. Ref https://jupyter-notebook.readthedocs.io/en/stable/config.html | *`cullInterval`* __integer__ | The interval (in seconds) on which to check for idle kernels exceeding the cull timeout value. | *`resources`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#resourcerequirements-v1-core[$$ResourceRequirements$$]__ | Compute Resources required by this container. Cannot be updated. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/ +| *`image`* __string__ | | *`clusterRole`* __string__ | |=== @@ -165,6 +168,69 @@ JupyterKernelSpecSpec defines the desired state of JupyterKernelSpec | *`image`* __string__ | | *`env`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvar-v1-core[$$EnvVar$$] array__ | | *`command`* __string array__ | +| *`className`* __string__ | +| *`template`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#objectreference-v1-core[$$ObjectReference$$]__ | +|=== + + + + +[id="{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplate"] +==== JupyterKernelTemplate + +JupyterKernelTemplate is the Schema for the jupyterkerneltemplates API + +.Appears In: +**** +- xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplatelist[$$JupyterKernelTemplateList$$] +**** + +[cols="25a,75a", options="header"] +|=== +| Field | Description +| *`apiVersion`* __string__ | `kubeflow.tkestack.io/v1alpha1` +| *`kind`* __string__ | `JupyterKernelTemplate` +| *`TypeMeta`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#typemeta-v1-meta[$$TypeMeta$$]__ | +| *`metadata`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#objectmeta-v1-meta[$$ObjectMeta$$]__ | Refer to Kubernetes API documentation for fields of `metadata`. + +| *`spec`* __xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplatespec[$$JupyterKernelTemplateSpec$$]__ | +| *`status`* __xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplatestatus[$$JupyterKernelTemplateStatus$$]__ | +|=== + + +[id="{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplatelist"] +==== JupyterKernelTemplateList + +JupyterKernelTemplateList contains a list of JupyterKernelTemplate + + + +[cols="25a,75a", options="header"] +|=== +| Field | Description +| *`apiVersion`* __string__ | `kubeflow.tkestack.io/v1alpha1` +| *`kind`* __string__ | `JupyterKernelTemplateList` +| *`TypeMeta`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#typemeta-v1-meta[$$TypeMeta$$]__ | +| *`metadata`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#listmeta-v1-meta[$$ListMeta$$]__ | Refer to Kubernetes API documentation for fields of `metadata`. + +| *`items`* __xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplate[$$JupyterKernelTemplate$$]__ | +|=== + + +[id="{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplatespec"] +==== JupyterKernelTemplateSpec + +JupyterKernelTemplateSpec defines the desired state of JupyterKernelTemplate + +.Appears In: +**** +- xref:{anchor_prefix}-github-com-tkestack-elastic-jupyter-operator-api-v1alpha1-jupyterkerneltemplate[$$JupyterKernelTemplate$$] +**** + +[cols="25a,75a", options="header"] +|=== +| Field | Description +| *`template`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#podtemplate-v1-core[$$PodTemplate$$]__ | |=== diff --git a/go.mod b/go.mod index 55cdf19..7b1316b 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,11 @@ require ( github.com/elastic/crd-ref-docs v0.0.7 // indirect github.com/go-logr/logr v0.1.0 github.com/google/addlicense v0.0.0-20200906110928-a0294312aa76 // indirect + github.com/mitchellh/go-homedir v1.1.0 github.com/onsi/ginkgo v1.12.1 github.com/onsi/gomega v1.10.1 + github.com/spf13/cobra v0.0.5 + github.com/spf13/viper v1.3.2 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect k8s.io/api v0.18.6 k8s.io/apimachinery v0.18.6 diff --git a/go.sum b/go.sum index 94a12f4..3a780f3 100644 --- a/go.sum +++ b/go.sum @@ -209,6 +209,7 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huandu/xstrings v1.2.1 h1:v6IdmkCnDhJG/S0ivr58PeIfg+tyhqQYy4YsCsQ0Pdc= @@ -240,6 +241,7 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= @@ -261,7 +263,9 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0j github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= @@ -292,6 +296,7 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -333,17 +338,21 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/soheilhy/cmux v0.1.3/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= +github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/hack/enterprise_gateway/prepare.yaml b/hack/enterprise_gateway/prepare.yaml old mode 100644 new mode 100755 index 59ef6c5..636d91c --- a/hack/enterprise_gateway/prepare.yaml +++ b/hack/enterprise_gateway/prepare.yaml @@ -30,6 +30,9 @@ rules: - apiGroups: ["rbac.authorization.k8s.io"] resources: ["rolebindings"] verbs: ["get", "list", "create", "delete"] + - apiGroups: ["kubeflow.tkestack.io"] + resources: ["jupyterkerneltemplates"] + verbs: ["get", "list", "create", "delete"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole diff --git a/main.go b/main.go index 587c210..6957aca 100644 --- a/main.go +++ b/main.go @@ -94,6 +94,14 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "JupyterKernelSpec") os.Exit(1) } + if err = (&controllers.JupyterKernelTemplateReconciler{ + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("controllers").WithName("JupyterKernelTemplate"), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "JupyterKernelTemplate") + os.Exit(1) + } // +kubebuilder:scaffold:builder setupLog.Info("starting manager") diff --git a/pkg/gateway/generate.go b/pkg/gateway/generate.go index 2c93868..82823c9 100644 --- a/pkg/gateway/generate.go +++ b/pkg/gateway/generate.go @@ -245,6 +245,10 @@ func (g generator) DesiredDeploymentWithoutOwner( }, } + if g.gateway.Spec.Image != "" { + d.Spec.Template.Spec.Containers[0].Image = g.gateway.Spec.Image + } + if g.gateway.Spec.CullIdleTimeout != nil { env := v1.EnvVar{ Name: "EG_CULL_IDLE_TIMEOUT", diff --git a/pkg/kernelspec/generate.go b/pkg/kernelspec/generate.go index 40f40c5..36989f5 100644 --- a/pkg/kernelspec/generate.go +++ b/pkg/kernelspec/generate.go @@ -47,6 +47,9 @@ const ( fileName = "kernel.json" defaultClassName = "enterprise_gateway.services.processproxies.k8s.KubernetesProcessProxy" + + keyKernelTemplateName = "--kernel-template-name" + keyKernelTemplateNamespace = "--kernel-template-namespace" ) // generator defines the generator which is used to generate @@ -104,6 +107,15 @@ func (g generator) desiredJSON() (string, error) { }, Argv: g.kernelSpec.Spec.Command, } + + // Set the class name to desired. + if g.kernelSpec.Spec.ClassName != "" { + c.Metadata.ProcessProxy.ClassName = g.kernelSpec.Spec.ClassName + } + // Set the namespace and name for the jupyter kernel spec. + c.Argv = append(c.Argv, + keyKernelTemplateName, g.kernelSpec.Spec.Template.Name, + keyKernelTemplateNamespace, g.kernelSpec.Spec.Template.Namespace) v, err := json.Marshal(c) return string(v), err }