Skip to content

Commit

Permalink
WIP: consume kloglevel at runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Romani <[email protected]>
  • Loading branch information
ffromani committed Sep 24, 2024
1 parent f30ee1f commit 16bef84
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 13 deletions.
12 changes: 12 additions & 0 deletions api/numaresourcesoperator/v1/numaresourcesoperator_normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,20 @@ import (
"sort"

corev1 "k8s.io/api/core/v1"

operatorv1 "github.com/openshift/api/operator/v1"
)

func NormalizeSpec(spec *NUMAResourcesOperatorSpec) {
defaultLog := operatorv1.Normal
if spec.LogLevel == "" {
spec.LogLevel = defaultLog
}
if spec.OperatorLogLevel == nil {
spec.OperatorLogLevel = &defaultLog
}
}

func (nodeGroup NodeGroup) NormalizeConfig() NodeGroupConfig {
conf := DefaultNodeGroupConfig()
if nodeGroup.Config == nil {
Expand Down
9 changes: 9 additions & 0 deletions api/numaresourcesoperator/v1/numaresourcesoperator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ type NUMAResourcesOperatorSpec struct {
// +optional
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Optional ignore pod namespace/name glob patterns"
PodExcludes []NamespacedName `json:"podExcludes,omitempty"`
// Valid values are: "Normal", "Debug", "Trace", "TraceAll".
// Defaults to "Normal".
// +optional
// +kubebuilder:default=Normal
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Operator log verbosity"
OperatorLogLevel *operatorv1.LogLevel `json:"operatorLogLevel,omitempty"`
}

// +kubebuilder:validation:Enum=Disabled;Enabled;EnabledExclusiveResources
Expand Down Expand Up @@ -136,6 +142,9 @@ type NUMAResourcesOperatorStatus struct {
// RelatedObjects list of objects of interest for this operator
//+operator-sdk:csv:customresourcedefinitions:type=status,displayName="Related Objects"
RelatedObjects []configv1.ObjectReference `json:"relatedObjects,omitempty"`
// OperatorLogLevel is the current log verbosity of the operator.
//+operator-sdk:csv:customresourcedefinitions:type=status,displayName="Operator log verbosity"
OperatorLogLevel operatorv1.LogLevel `json:"operatorLogLevel,omitempty"`
}

// MachineConfigPool defines the observed state of each MachineConfigPool selected by node groups
Expand Down
6 changes: 6 additions & 0 deletions api/numaresourcesoperator/v1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ spec:
x-kubernetes-map-type: atomic
type: object
type: array
operatorLogLevel:
default: Normal
description: |-
Valid values are: "Normal", "Debug", "Trace", "TraceAll".
Defaults to "Normal".
enum:
- ""
- Normal
- Debug
- Trace
- TraceAll
type: string
podExcludes:
description: Optional Namespace/Name glob patterns of pod to ignore
at node level
Expand Down Expand Up @@ -401,6 +413,16 @@ spec:
- name
type: object
type: array
operatorLogLevel:
description: OperatorLogLevel is the current log verbosity of the
operator.
enum:
- ""
- Normal
- Debug
- Trace
- TraceAll
type: string
relatedObjects:
description: RelatedObjects list of objects of interest for this operator
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ metadata:
}
]
capabilities: Basic Install
createdAt: "2024-09-17T08:34:36Z"
createdAt: "2024-09-24T16:05:02Z"
olm.skipRange: '>=4.17.0 <4.18.0'
operators.operatorframework.io/builder: operator-sdk-v1.36.1
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
Expand Down Expand Up @@ -126,6 +126,11 @@ spec:
path: nodeGroups[0].config.tolerations
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:text
- description: |-
Valid values are: "Normal", "Debug", "Trace", "TraceAll".
Defaults to "Normal".
displayName: Operator log verbosity
path: operatorLogLevel
- description: Optional Namespace/Name glob patterns of pod to ignore at node
level
displayName: Optional ignore pod namespace/name glob patterns
Expand All @@ -149,6 +154,9 @@ spec:
applied to this MachineConfigPool
displayName: Optional configuration enforced on this NodeGroup
path: machineconfigpools[0].config
- description: OperatorLogLevel is the current log verbosity of the operator.
displayName: Operator log verbosity
path: operatorLogLevel
- description: RelatedObjects list of objects of interest for this operator
displayName: Related Objects
path: relatedObjects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ spec:
x-kubernetes-map-type: atomic
type: object
type: array
operatorLogLevel:
default: Normal
description: |-
Valid values are: "Normal", "Debug", "Trace", "TraceAll".
Defaults to "Normal".
enum:
- ""
- Normal
- Debug
- Trace
- TraceAll
type: string
podExcludes:
description: Optional Namespace/Name glob patterns of pod to ignore
at node level
Expand Down Expand Up @@ -401,6 +413,16 @@ spec:
- name
type: object
type: array
operatorLogLevel:
description: OperatorLogLevel is the current log verbosity of the
operator.
enum:
- ""
- Normal
- Debug
- Trace
- TraceAll
type: string
relatedObjects:
description: RelatedObjects list of objects of interest for this operator
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ spec:
path: nodeGroups[0].config.tolerations
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:text
- description: |-
Valid values are: "Normal", "Debug", "Trace", "TraceAll".
Defaults to "Normal".
displayName: Operator log verbosity
path: operatorLogLevel
- description: Optional Namespace/Name glob patterns of pod to ignore at node
level
displayName: Optional ignore pod namespace/name glob patterns
Expand All @@ -88,6 +93,9 @@ spec:
applied to this MachineConfigPool
displayName: Optional configuration enforced on this NodeGroup
path: machineconfigpools[0].config
- description: OperatorLogLevel is the current log verbosity of the operator.
displayName: Operator log verbosity
path: operatorLogLevel
- description: RelatedObjects list of objects of interest for this operator
displayName: Related Objects
path: relatedObjects
Expand Down
19 changes: 19 additions & 0 deletions controllers/numaresourcesoperator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ import (
rteupdate "github.com/openshift-kni/numaresources-operator/pkg/objectupdate/rte"
"github.com/openshift-kni/numaresources-operator/pkg/status"
"github.com/openshift-kni/numaresources-operator/pkg/validation"

intkloglevel "github.com/openshift-kni/numaresources-operator/internal/kloglevel"
)

const numaResourcesRetryPeriod = 1 * time.Minute
Expand Down Expand Up @@ -132,6 +134,20 @@ func (r *NUMAResourcesOperatorReconciler) Reconcile(ctx context.Context, req ctr
return r.updateStatus(ctx, instance, status.ConditionDegraded, status.ConditionTypeIncorrectNUMAResourcesOperatorResourceName, message)
}

needStatusUpdate := false
nropv1.NormalizeSpec(&instance.Spec)

// do as early as possible, so we don't miss log messages
if lev := *instance.Spec.OperatorLogLevel; lev != instance.Status.OperatorLogLevel {
err := intkloglevel.Set(loglevel.ToKlog(lev))
if err != nil {
klog.InfoS("cannot updated log level dynamically", "desiredLevel", lev)
} else {
instance.Status.OperatorLogLevel = lev
needStatusUpdate = true
}
}

if err := validation.NodeGroups(instance.Spec.NodeGroups); err != nil {
return r.updateStatus(ctx, instance, status.ConditionDegraded, validation.NodeGroupsError, err.Error())
}
Expand All @@ -152,6 +168,9 @@ func (r *NUMAResourcesOperatorReconciler) Reconcile(ctx context.Context, req ctr

result, condition, err := r.reconcileResource(ctx, instance, trees)
if condition != "" {
needStatusUpdate = true
}
if needStatusUpdate {
// TODO: use proper reason
reason, message := condition, messageFromError(err)
_, _ = r.updateStatus(ctx, instance, condition, reason, message)
Expand Down
12 changes: 0 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,3 @@ func webhookTLSOpts(enableHTTP2 bool) []func(config *tls.Config) {

return []func(config *tls.Config){disableHTTP2}
}

// getKlogLevel reconstructs the klog verb level, because
// the klog package doesn't give a clean easy way to access
// the setting, so we have to jumps through some hoops.
func getKlogLevel() int {
for j := 1; j < 15; j++ {
if !klog.V(klog.Level(j)).Enabled() {
return j - 1
}
}
return 0
}

0 comments on commit 16bef84

Please sign in to comment.