Skip to content

Commit

Permalink
Merge pull request #48 from opendatahub-io/stable
Browse files Browse the repository at this point in the history
[master] sync the commit from Stable branch opendatahub to master
  • Loading branch information
harshad16 authored Apr 5, 2024
2 parents a3ec337 + 7511785 commit eba99bd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
11 changes: 11 additions & 0 deletions components/notebook-controller/controllers/notebook_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const DefaultServingPort = 80
const AnnotationRewriteURI = "notebooks.kubeflow.org/http-rewrite-uri"
const AnnotationHeadersRequestSet = "notebooks.kubeflow.org/http-headers-request-set"
const AnnotationNotebookRestart = "notebooks.opendatahub.io/notebook-restart"
const WorkbenchLabel = "opendatahub.io/workbenches"

const PrefixEnvVar = "NB_PREFIX"

Expand Down Expand Up @@ -158,6 +159,15 @@ func (r *NotebookReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
log.Error(err, "error getting Statefulset")
return ctrl.Result{}, err
}

// Copy the pod template labels, but reconcilation is not required
// exclusively based on ths pod template labels
if *ss.Spec.Replicas != *foundStateful.Spec.Replicas {
if !reflect.DeepEqual(foundStateful.Spec.Template.ObjectMeta.Labels, ss.Spec.Template.ObjectMeta.Labels) {
foundStateful.Spec.Template.ObjectMeta.Labels = ss.Spec.Template.ObjectMeta.Labels
}
}

// Update the foundStateful object and write the result back if there are any changes
if !justCreated && reconcilehelper.CopyStatefulSetFields(ss, foundStateful) {
log.Info("Updating StatefulSet", "namespace", ss.Namespace, "name", ss.Name)
Expand Down Expand Up @@ -417,6 +427,7 @@ func generateStatefulSet(instance *v1beta1.Notebook) *appsv1.StatefulSet {
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{
"statefulset": instance.Name,
"notebook-name": instance.Name,
WorkbenchLabel: "true",
}},
Spec: *instance.Spec.Template.Spec.DeepCopy(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ var _ = Describe("The Openshift Notebook controller", func() {

By("By simulating the existence of odh-trusted-ca-bundle ConfigMap")
// Create a ConfigMap similar to odh-trusted-ca-bundle for simulation
workbenchTrustedCACertBundle := "workbench-trusted-ca-bundle"
trustedCACertBundle := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "odh-trusted-ca-bundle",
Expand Down Expand Up @@ -232,7 +233,7 @@ var _ = Describe("The Openshift Notebook controller", func() {
Name: "trusted-ca",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: trustedCACertBundle.Name},
LocalObjectReference: corev1.LocalObjectReference{Name: workbenchTrustedCACertBundle},
Optional: pointer.Bool(true),
Items: []corev1.KeyToPath{
{
Expand Down
32 changes: 18 additions & 14 deletions components/odh-notebook-controller/controllers/notebook_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

const (
WorkbenchLabel = "opendatahub.io/workbenches"
)

//+kubebuilder:webhook:path=/mutate-notebook-v1,mutating=true,failurePolicy=fail,sideEffects=None,groups=kubeflow.org,resources=notebooks,verbs=create;update,versions=v1,name=notebooks.opendatahub.io,admissionReviewVersions=v1

// NotebookWebhook holds the webhook configuration.
Expand Down Expand Up @@ -240,7 +236,6 @@ func (w *NotebookWebhook) Handle(ctx context.Context, req admission.Request) adm

// Inject the reconciliation lock only on new notebook creation
if req.Operation == admissionv1.Create {
AddWorkbenchLabel(notebook)
err = InjectReconciliationLock(&notebook.ObjectMeta)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
Expand Down Expand Up @@ -281,13 +276,6 @@ func (w *NotebookWebhook) InjectDecoder(d *admission.Decoder) error {
return nil
}

// AddWorkbenchLabel adds an exclusive static label to the Notebook pods
func AddWorkbenchLabel(notebook *nbv1.Notebook) {
currentLabels := notebook.ObjectMeta.GetLabels()
notebook.ObjectMeta.Labels[WorkbenchLabel] = "true"
notebook.ObjectMeta.SetLabels(currentLabels)
}

// CheckAndMountCACertBundle checks if the odh-trusted-ca-bundle ConfigMap is present
func CheckAndMountCACertBundle(ctx context.Context, cli client.Client, notebook *nbv1.Notebook, log logr.Logger) error {

Expand All @@ -308,8 +296,24 @@ func CheckAndMountCACertBundle(ctx context.Context, cli client.Client, notebook
workbenchConfigMap := &corev1.ConfigMap{}
err := cli.Get(ctx, client.ObjectKey{Namespace: notebook.Namespace, Name: workbenchConfigMapName}, workbenchConfigMap)
if err != nil {
log.Info("workbench-trusted-ca-bundle ConfigMap is not present, skipping mounting of certificates.")
return nil
log.Info("workbench-trusted-ca-bundle ConfigMap is not present, start creating it...")
// create the ConfigMap if it does not exist
workbenchConfigMap = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: workbenchConfigMapName,
Namespace: notebook.Namespace,
Labels: map[string]string{"opendatahub.io/managed-by": "workbenches"},
},
Data: map[string]string{
"ca-bundle.crt": odhConfigMap.Data["ca-bundle.crt"],
},
}
err = cli.Create(ctx, workbenchConfigMap)
if err != nil {
log.Info("Failed to create workbench-trusted-ca-bundle ConfigMap")
return nil
}
log.Info("Created workbench-trusted-ca-bundle ConfigMap")
}

cm := workbenchConfigMap
Expand Down

0 comments on commit eba99bd

Please sign in to comment.