Skip to content

Commit

Permalink
update: move setting into common function
Browse files Browse the repository at this point in the history
Signed-off-by: Wen Zhou <[email protected]>
Signed-off-by: Zhou, Wen <[email protected]>
  • Loading branch information
zdtsw committed Mar 22, 2024
1 parent 356fd26 commit 2b5ffcc
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 102 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,10 @@ This will ensure that the doc for the apis are updated accordingly.

#### Controller level

Logger on DSC and DSCI and secretgen can only be changed from CSV with parameters:
--log-level info (as default) || debug || warning || error || panic || dpanic
--log-mode devel (as default) || prod
Logger on all controllers can only be changed from CSV with parameters: --log-mode devel
valid value: "" (as default) || prod || production || devel || development

This mainly impacts logging for pod startup, generating common resource, monitoring deployment.
With "debug", operator even prints Events messages into pod log.

#### Component level

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,7 @@ spec:
- --health-probe-bind-address=:8081
- --metrics-bind-address=0.0.0.0:8080
- --leader-elect
- --operator-name=opendatahub
command:
- /manager
env:
Expand Down
6 changes: 4 additions & 2 deletions components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ can be found [here](https://github.com/opendatahub-io/opendatahub-operator/tree/

```go
type ComponentInterface interface {
ReconcileComponent(cli client.Client, owner metav1.Object, DSCISpec *dsci.DSCInitializationSpec) error
Cleanup(cli client.Client, DSCISpec *dsci.DSCInitializationSpec) error
ReconcileComponent(ctx context.Context, cli client.Client, logger logr.Logger, owner metav1.Object, DSCISpec *dsciv1.DSCInitializationSpec, currentComponentStatus bool) error
Cleanup(cli client.Client, DSCISpec *dsciv1.DSCInitializationSpec) error
GetComponentName() string
GetManagementState() operatorv1.ManagementState
OverrideManifests(platform string) error
UpdatePrometheusConfig(cli client.Client, enable bool, component string) error
ConfigComponentLogger(logger logr.Logger, component string, dscispec *dsciv1.DSCInitializationSpec) logr.Logger
}
```

### Add reconcile and Events

- Once you set up the new component module, add the component to [Reconcile](https://github.com/opendatahub-io/opendatahub-operator/blob/acaaf31f43e371456363f3fd272aec91ba413482/controllers/datasciencecluster/datasciencecluster_controller.go#L135)
Expand Down
2 changes: 1 addition & 1 deletion components/codeflare/codeflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *CodeFlare) GetComponentName() string {
}

func (c *CodeFlare) ReconcileComponent(ctx context.Context, cli client.Client, logger logr.Logger, owner metav1.Object, dscispec *dsciv1.DSCInitializationSpec, _ bool) error {
l := c.ConfigLogger(logger, ComponentName, dscispec)
l := c.ConfigComponentLogger(logger, ComponentName, dscispec)
var imageParamMap = map[string]string{
"codeflare-operator-controller-image": "RELATED_IMAGE_ODH_CODEFLARE_OPERATOR_IMAGE", // no need mcad, embedded in cfo
"namespace": dscispec.ApplicationsNamespace,
Expand Down
47 changes: 6 additions & 41 deletions components/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (

"github.com/go-logr/logr"
operatorv1 "github.com/openshift/api/operator/v1"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/common"
)

// Component struct defines the basis for each OpenDataHub component configuration.
Expand Down Expand Up @@ -86,49 +85,15 @@ type ComponentInterface interface {
GetManagementState() operatorv1.ManagementState
OverrideManifests(platform string) error
UpdatePrometheusConfig(cli client.Client, enable bool, component string) error
ConfigLogger(logger logr.Logger, component string, dscispec *dsciv1.DSCInitializationSpec) logr.Logger
ConfigComponentLogger(logger logr.Logger, component string, dscispec *dsciv1.DSCInitializationSpec) logr.Logger
}

// by default, ConfigLogger with jsonEncoder, logLevel=Info, stackTraceLevel=Error
// when set in DSCI: devel Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Info with lower case for key
// when set in DSCI: prod Mode defaults(encoder=jsonEncoder, logLevel=Info, stackTraceLevel=Error with upper case for key.
func (c *Component) ConfigLogger(logger logr.Logger, component string, dscispec *dsciv1.DSCInitializationSpec) logr.Logger {
var opts zap.Options
// extend origal ConfigLoggers to include component name.
func (c *Component) ConfigComponentLogger(logger logr.Logger, component string, dscispec *dsciv1.DSCInitializationSpec) logr.Logger {
if dscispec.DevFlags != nil {
switch dscispec.DevFlags.LogMode {
case "devel", "development": // the most logging verbosity
opts = zap.Options{
Development: true,
StacktraceLevel: zapcore.WarnLevel,
Level: zapcore.InfoLevel,
DestWriter: os.Stdout,
}
case "prod", "production": // the least logging verbosity
opts = zap.Options{
Development: false,
StacktraceLevel: zapcore.ErrorLevel,
Level: zapcore.WarnLevel,
DestWriter: os.Stdout,
EncoderConfigOptions: []zap.EncoderConfigOption{func(config *zapcore.EncoderConfig) {
config.EncodeTime = zapcore.ISO8601TimeEncoder
config.EncodeDuration = zapcore.SecondsDurationEncoder
config.LevelKey = "LogLevel"
config.NameKey = "Log"
config.CallerKey = "Caller"
config.MessageKey = "Message"
config.TimeKey = "Time"
}},
TimeEncoder: zapcore.ISO8601TimeEncoder, // human readable not epoch
}
default:
fmt.Println("Invalid log mode, fall back to use 'production' as set in main.go")
return logger.WithName("DSC.Component." + component)
}
} else {
// fmt.Printf("Not use any devFlags log mode for %s\n", component)
return logger.WithName("DSC.Component." + component)
return common.ConfigLoggers(dscispec.DevFlags.LogMode).WithName("DSC.Components." + component)
}
return zap.New(zap.UseFlagOptions(&opts)).WithName("DSC.Component." + component)
return logger.WithName("DSC.Components." + component)
}

// UpdatePrometheusConfig update prometheus-configs.yaml to include/exclude <component>.rules
Expand Down
22 changes: 13 additions & 9 deletions components/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,23 @@ func (d *Dashboard) ReconcileComponent(ctx context.Context,
dscispec *dsciv1.DSCInitializationSpec,
currentComponentExist bool,
) error {
var imageParamMap = map[string]string{
"odh-dashboard-image": "RELATED_IMAGE_ODH_DASHBOARD_IMAGE",
}
enabled := d.GetManagementState() == operatorv1.Managed
monitoringEnabled := dscispec.Monitoring.ManagementState == operatorv1.Managed

var l logr.Logger
platform, err := deploy.GetPlatform(cli)
if err != nil {
return err
}

l := d.ConfigLogger(logger, ComponentName, dscispec)
if platform == deploy.SelfManagedRhods || platform == deploy.ManagedRhods {
l = d.ConfigLogger(logger, ComponentNameSupported, dscispec)
l = d.ConfigComponentLogger(logger, ComponentNameSupported, dscispec)
} else {
l = d.ConfigComponentLogger(logger, ComponentName, dscispec)
}

var imageParamMap = map[string]string{
"odh-dashboard-image": "RELATED_IMAGE_ODH_DASHBOARD_IMAGE",
}
enabled := d.GetManagementState() == operatorv1.Managed
monitoringEnabled := dscispec.Monitoring.ManagementState == operatorv1.Managed

// Update Default rolebinding
if enabled {
// Update Default rolebinding
Expand Down Expand Up @@ -173,6 +174,8 @@ func (d *Dashboard) ReconcileComponent(ctx context.Context,
if err := d.deployConsoleLink(cli, owner, platform, dscispec.ApplicationsNamespace, ComponentNameSupported); err != nil {
return err
}
l.Info("apply manifests done")

// CloudService Monitoring handling
if platform == deploy.ManagedRhods {
if enabled {
Expand Down Expand Up @@ -208,6 +211,7 @@ func (d *Dashboard) ReconcileComponent(ctx context.Context,
if err := d.deployConsoleLink(cli, owner, platform, dscispec.ApplicationsNamespace, ComponentName); err != nil {
return err
}
l.Info("apply manifests done")
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/datasciencepipelines/datasciencepipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (d *DataSciencePipelines) ReconcileComponent(ctx context.Context,
dscispec *dsciv1.DSCInitializationSpec,
_ bool,
) error {
l := d.ConfigLogger(logger, ComponentName, dscispec)
l := d.ConfigComponentLogger(logger, ComponentName, dscispec)
var imageParamMap = map[string]string{
// v1
"IMAGES_APISERVER": "RELATED_IMAGE_ODH_ML_PIPELINES_API_SERVER_IMAGE",
Expand Down
2 changes: 1 addition & 1 deletion components/kserve/kserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (k *Kserve) GetComponentName() string {

func (k *Kserve) ReconcileComponent(ctx context.Context, cli client.Client,
logger logr.Logger, owner metav1.Object, dscispec *dsciv1.DSCInitializationSpec, _ bool) error {
l := k.ConfigLogger(logger, ComponentName, dscispec)
l := k.ConfigComponentLogger(logger, ComponentName, dscispec)
// paramMap for Kserve to use.
var imageParamMap = map[string]string{}

Expand Down
2 changes: 1 addition & 1 deletion components/kueue/kueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (k *Kueue) GetComponentName() string {

func (k *Kueue) ReconcileComponent(ctx context.Context, cli client.Client, logger logr.Logger,
owner metav1.Object, dscispec *dsciv1.DSCInitializationSpec, _ bool) error {
l := k.ConfigLogger(logger, ComponentName, dscispec)
l := k.ConfigComponentLogger(logger, ComponentName, dscispec)
var imageParamMap = map[string]string{
"odh-kueue-controller-image": "RELATED_IMAGE_ODH_KUEUE_CONTROLLER_IMAGE", // new kueue image
}
Expand Down
2 changes: 1 addition & 1 deletion components/modelmeshserving/modelmeshserving.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (m *ModelMeshServing) ReconcileComponent(ctx context.Context,
dscispec *dsciv1.DSCInitializationSpec,
_ bool,
) error {
l := m.ConfigLogger(logger, ComponentName, dscispec)
l := m.ConfigComponentLogger(logger, ComponentName, dscispec)
var imageParamMap = map[string]string{
"odh-mm-rest-proxy": "RELATED_IMAGE_ODH_MM_REST_PROXY_IMAGE",
"odh-modelmesh-runtime-adapter": "RELATED_IMAGE_ODH_MODELMESH_RUNTIME_ADAPTER_IMAGE",
Expand Down
2 changes: 1 addition & 1 deletion components/modelregistry/modelregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (m *ModelRegistry) GetComponentName() string {

func (m *ModelRegistry) ReconcileComponent(_ context.Context, cli client.Client, logger logr.Logger,
owner metav1.Object, dscispec *dsciv1.DSCInitializationSpec, _ bool) error {
l := m.ConfigLogger(logger, ComponentName, dscispec)
l := m.ConfigComponentLogger(logger, ComponentName, dscispec)
var imageParamMap = map[string]string{
"IMAGES_MODELREGISTRY_OPERATOR": "RELATED_IMAGE_ODH_MODEL_REGISTRY_OPERATOR_IMAGE",
"IMAGES_GRPC_SERVICE": "RELATED_IMAGE_ODH_MLMD_GRPC_SERVER_IMAGE",
Expand Down
2 changes: 1 addition & 1 deletion components/ray/ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r *Ray) GetComponentName() string {

func (r *Ray) ReconcileComponent(ctx context.Context, cli client.Client, logger logr.Logger,
owner metav1.Object, dscispec *dsciv1.DSCInitializationSpec, _ bool) error {
l := r.ConfigLogger(logger, ComponentName, dscispec)
l := r.ConfigComponentLogger(logger, ComponentName, dscispec)

var imageParamMap = map[string]string{
"odh-kuberay-operator-controller-image": "RELATED_IMAGE_ODH_KUBERAY_OPERATOR_CONTROLLER_IMAGE",
Expand Down
2 changes: 1 addition & 1 deletion components/trustyai/trustyai.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (t *TrustyAI) ReconcileComponent(ctx context.Context, cli client.Client, lo
"trustyaiServiceImage": "RELATED_IMAGE_ODH_TRUSTYAI_SERVICE_IMAGE",
"trustyaiOperatorImage": "RELATED_IMAGE_ODH_TRUSTYAI_SERVICE_OPERATOR_IMAGE",
}
l := t.ConfigLogger(logger, ComponentName, dscispec)
l := t.ConfigComponentLogger(logger, ComponentName, dscispec)

enabled := t.GetManagementState() == operatorv1.Managed
monitoringEnabled := dscispec.Monitoring.ManagementState == operatorv1.Managed
Expand Down
2 changes: 1 addition & 1 deletion components/workbenches/workbenches.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (w *Workbenches) GetComponentName() string {

func (w *Workbenches) ReconcileComponent(ctx context.Context, cli client.Client, logger logr.Logger,
owner metav1.Object, dscispec *dsci.DSCInitializationSpec, _ bool) error {
l := w.ConfigLogger(logger, ComponentName, dscispec)
l := w.ConfigComponentLogger(logger, ComponentName, dscispec)
var imageParamMap = map[string]string{
"odh-notebook-controller-image": "RELATED_IMAGE_ODH_NOTEBOOK_CONTROLLER_IMAGE",
"odh-kf-notebook-controller-image": "RELATED_IMAGE_ODH_KF_NOTEBOOK_CONTROLLER_IMAGE",
Expand Down
16 changes: 0 additions & 16 deletions config/manager/kustomization.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: REPLACE_IMAGE:latest
newName: REPLACE_IMAGE
2 changes: 0 additions & 2 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ spec:
value: 'true'
args:
- --leader-elect
- --log-level=info
- --log-mode=devel
- --operator-name=opendatahub
image: controller:latest
imagePullPolicy: Always
Expand Down
1 change: 1 addition & 0 deletions docs/api-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ _Appears in:_
| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `manifestsUri` _string_ | Custom manifests uri for odh-manifests | | |
| `logmode` _string_ | | production | Enum: [devel development prod production] <br /> |


#### Monitoring
Expand Down
22 changes: 4 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
ofapiv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
ofapiv2 "github.com/operator-framework/api/pkg/operators/v2"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"go.uber.org/zap/zapcore"
admv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -46,7 +45,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"

kfdefv1 "github.com/opendatahub-io/opendatahub-operator/apis/kfdef.apps.kubeflow.org/v1"
Expand All @@ -58,6 +56,7 @@ import (
dscicontr "github.com/opendatahub-io/opendatahub-operator/v2/controllers/dscinitialization"
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/secretgenerator"
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/webhook"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/common"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/upgrade"
)
Expand All @@ -67,11 +66,6 @@ var (
setupLog = ctrl.Log.WithName("setup")
)

type loggerOption struct {
level string // debug, info, warning, error, panic, fatal
mode string // prod, devel
}

func init() { //nolint:gochecknoinits
//+kubebuilder:scaffold:scheme
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
Expand Down Expand Up @@ -106,7 +100,7 @@ func main() { //nolint:funlen
var dscApplicationsNamespace string
var dscMonitoringNamespace string
var operatorName string
loggerOpts := loggerOption{}
var logmode string

flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
Expand All @@ -118,19 +112,11 @@ func main() { //nolint:funlen
flag.StringVar(&dscMonitoringNamespace, "dsc-monitoring-namespace", "opendatahub", "The namespace where data science cluster"+
"monitoring stack will be deployed")
flag.StringVar(&operatorName, "operator-name", "opendatahub", "The name of the operator")
flag.StringVar(&loggerOpts.level, "log-level", "log", "Log level (debug, info, warning, error, dpanic, panic, fatal) or all caps, default to info")
flag.StringVar(&loggerOpts.mode, "log-mode", "prod", "Log mode (prod, devel), default to prod -- stracktrace on error")
flag.StringVar(&logmode, "log-mode", "", "Log mode ('', prod, devel), default to ''")

flag.Parse()

opts := zap.Options{ // default use production mode
Development: false,
StacktraceLevel: zapcore.ErrorLevel,
Level: zapcore.InfoLevel,
DestWriter: os.Stdout,
}
opts.BindFlags(flag.CommandLine)
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
ctrl.SetLogger(common.ConfigLoggers(logmode))

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Expand Down
Loading

0 comments on commit 2b5ffcc

Please sign in to comment.