Skip to content

Commit

Permalink
added correct error logging for JSON unmarshal err in getContainerNam…
Browse files Browse the repository at this point in the history
…esFromAnnotationImageChangeFieldPaths
  • Loading branch information
shalberd committed Oct 27, 2023
1 parent 10e7c77 commit 97f256e
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions components/notebook-controller/controllers/notebook_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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"

// annotation that makes OpenShift ImagePolicy admission plug-in resolve the image-field from an imagestream name:tag reference
const AnnotationImageChangeTrigger = "image.openshift.io/triggers"

Expand All @@ -62,11 +63,11 @@ const PrefixEnvVar = "NB_PREFIX"
// https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.11/#podsecuritycontext-v1-core
const DefaultFSGroup = int64(100)

func getContainerNamesFromAnnotationImageChangeFieldPaths(dataJson string) []string {
func getContainerNamesFromAnnotationImageChangeFieldPaths(dataJson string, log logr.Logger) []string {
annotationImageChangeContent := AnnotationImageChangeContent{}
err := json.Unmarshal([]byte(dataJson), &annotationImageChangeContent)
if err != nil {
fmt.Println("JSON decode error!")
log.Error(err, fmt.Sprintf("Notebook image change trigger annotation image.openshift.io/triggers JSON array decode error, check for correct annotation value JSON format : %s.", dataJson))
return nil
}

Expand All @@ -83,16 +84,16 @@ func getContainerNamesFromAnnotationImageChangeFieldPaths(dataJson string) []str
return containerNameSlice
}

func getImageChangeTriggerReferencedContainerNames(meta metav1.ObjectMeta) []string {
if meta.GetAnnotations() == nil {
return nil
}
func getImageChangeTriggerReferencedContainerNames(meta metav1.ObjectMeta, log logr.Logger) []string {
if meta.GetAnnotations() == nil {
return nil
}

if valAnnotationImageChangeTrigger, ok := meta.GetAnnotations()[AnnotationImageChangeTrigger]; ok {
return getContainerNamesFromAnnotationImageChangeFieldPaths(valAnnotationImageChangeTrigger)
} else {
return nil
}
if valAnnotationImageChangeTrigger, ok := meta.GetAnnotations()[AnnotationImageChangeTrigger]; ok {
return getContainerNamesFromAnnotationImageChangeFieldPaths(valAnnotationImageChangeTrigger, log)
} else {
return nil
}
}

/*
Expand Down Expand Up @@ -205,8 +206,8 @@ func (r *NotebookReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, err
}

isImageChangeTriggerSet := metav1.HasAnnotation(instance.ObjectMeta, AnnotationImageChangeTrigger)
imageChangeTriggerReferencedContainerNames := getImageChangeTriggerReferencedContainerNames(instance.ObjectMeta)
isImageChangeTriggerSet := metav1.HasAnnotation(instance.ObjectMeta, AnnotationImageChangeTrigger)
imageChangeTriggerReferencedContainerNames := getImageChangeTriggerReferencedContainerNames(instance.ObjectMeta, log)

// Update the foundStateful object and write the result back if there are any changes
if !justCreated && reconcilehelper.CopyStatefulSetFields(ss, foundStateful, isImageChangeTriggerSet, imageChangeTriggerReferencedContainerNames, log) {
Expand Down Expand Up @@ -451,19 +452,19 @@ func generateStatefulSet(instance *v1beta1.Notebook) *appsv1.StatefulSet {
replicas = 0
}

annotations := make(map[string]string)
meta := instance.ObjectMeta
annotations := make(map[string]string)
meta := instance.ObjectMeta

// keep Notebook image change trigger annotation key and value and use it later in StatefulSet metadata
if annotationImageChangeTriggerVal, ok := meta.GetAnnotations()[AnnotationImageChangeTrigger]; ok {
annotations[AnnotationImageChangeTrigger] = annotationImageChangeTriggerVal
}
// keep Notebook image change trigger annotation key and value and use it later in StatefulSet metadata
if annotationImageChangeTriggerVal, ok := meta.GetAnnotations()[AnnotationImageChangeTrigger]; ok {
annotations[AnnotationImageChangeTrigger] = annotationImageChangeTriggerVal
}

ss := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: instance.Name,
Namespace: instance.Namespace,
Annotations: annotations,
Name: instance.Name,
Namespace: instance.Namespace,
Annotations: annotations,
},
Spec: appsv1.StatefulSetSpec{
Replicas: &replicas,
Expand Down

0 comments on commit 97f256e

Please sign in to comment.