Skip to content

Commit

Permalink
cmd/osm-*: log error codes for errors during startup
Browse files Browse the repository at this point in the history
Logs error codes for errors encountered during app
startup. Additionally moves error logging to the
caller/handler of the error to avoid duplicate
logging.

Part of openservicemesh#2866

Signed-off-by: Shashank Ram <shashr2204@gmail.com>
  • Loading branch information
shashankram committed Jun 30, 2021
1 parent da3a875 commit 31b0d23
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
7 changes: 5 additions & 2 deletions cmd/osm-controller/log_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/openservicemesh/osm/pkg/announcements"
"github.com/openservicemesh/osm/pkg/configurator"
"github.com/openservicemesh/osm/pkg/constants"
"github.com/openservicemesh/osm/pkg/errcode"
"github.com/openservicemesh/osm/pkg/k8s/events"
"github.com/openservicemesh/osm/pkg/logger"
)
Expand All @@ -25,7 +26,8 @@ func StartGlobalLogLevelHandler(cfg configurator.Configurator, stop <-chan struc
log.Info().Msgf("Setting initial log level from meshconfig: %s", logLevel)
err := logger.SetLogLevel(logLevel)
if err != nil {
log.Error().Msgf("Error setting initial log level from meshconfig: %v", err)
log.Error().Err(err).Str(errcode.Kind, errcode.ErrSettingLogLevel.String()).
Msg("Error setting initial log level from meshconfig")
} else {
currentLogLevel = logLevel
}
Expand All @@ -38,7 +40,8 @@ func StartGlobalLogLevelHandler(cfg configurator.Configurator, stop <-chan struc
if logLevel != currentLogLevel {
err := logger.SetLogLevel(logLevel)
if err != nil {
log.Error().Msgf("Error setting log level from meshconfig: %v", err)
log.Error().Err(err).Str(errcode.Kind, errcode.ErrSettingLogLevel.String()).
Msg("Error setting log level from meshconfig")
} else {
log.Info().Msgf("Global log level changed to: %s", logLevel)
currentLogLevel = logLevel
Expand Down
6 changes: 4 additions & 2 deletions cmd/osm-controller/osm-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ func main() {
cfg := configurator.NewConfigurator(versioned.NewForConfigOrDie(kubeConfig), stop, osmNamespace, osmMeshConfigName)
meshConfig, err := cfg.GetMeshConfigJSON()
if err != nil {
log.Error().Err(err).Msgf("Error parsing MeshConfig %s", osmMeshConfigName)
log.Error().Err(err).Str(errcode.Kind, errcode.ErrParsingMeshConfig.String()).
Msgf("Error parsing MeshConfig %s", osmMeshConfigName)
}
log.Info().Msgf("Initial MeshConfig %s: %s", osmMeshConfigName, meshConfig)

Expand Down Expand Up @@ -336,7 +337,8 @@ func getOSMControllerPod(kubeClient kubernetes.Interface) (*corev1.Pod, error) {

pod, err := kubeClient.CoreV1().Pods(osmNamespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
log.Error().Err(err).Msgf("Error retrieving osm-controller pod %s", podName)
log.Error().Err(err).Str(errcode.Kind, errcode.ErrFetchingControllerPod.String()).
Msgf("Error retrieving osm-controller pod %s", podName)
return nil, err
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/osm-injector/osm-injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"

"github.com/openservicemesh/osm/pkg/errcode"
configClientset "github.com/openservicemesh/osm/pkg/gen/client/config/clientset/versioned"

"github.com/openservicemesh/osm/pkg/certificate/providers"
Expand Down Expand Up @@ -138,7 +139,8 @@ func main() {
cfg := configurator.NewConfigurator(configClientset.NewForConfigOrDie(kubeConfig), stop, osmNamespace, osmMeshConfigName)
meshConfig, err := cfg.GetMeshConfigJSON()
if err != nil {
log.Error().Err(err).Msgf("Error parsing MeshConfig %s", osmMeshConfigName)
log.Error().Err(err).Str(errcode.Kind, errcode.ErrParsingMeshConfig.String()).
Msgf("Error parsing MeshConfig %s", osmMeshConfigName)
}
log.Info().Msgf("Initial MeshConfig %s: %v", osmMeshConfigName, meshConfig)

Expand Down Expand Up @@ -204,7 +206,8 @@ func getInjectorPod(kubeClient kubernetes.Interface) (*corev1.Pod, error) {

pod, err := kubeClient.CoreV1().Pods(osmNamespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
log.Error().Err(err).Msgf("Error retrieving osm-injector pod %s", podName)
log.Error().Err(err).Str(errcode.Kind, errcode.ErrFetchingInjectorPod.String()).
Msgf("Error retrieving osm-injector pod %s", podName)
return nil, err
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/osm-injector/reconcile.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/openservicemesh/osm/pkg/errcode"
"github.com/openservicemesh/osm/pkg/reconciler"
)

Expand All @@ -15,8 +17,7 @@ func createReconciler(kubeClient *kubernetes.Clientset) error {
Namespace: osmNamespace,
})
if err != nil {
log.Error().Err(err).Msg("Error creating controller manager")
return err
return errors.Wrap(err, "Error creating controller-runtime Manager for MutatingWebhookConfiguration's reconciler")
}

// Add a reconciler for osm-injector's mutatingwehbookconfiguration
Expand All @@ -27,15 +28,15 @@ func createReconciler(kubeClient *kubernetes.Clientset) error {
OsmWebhook: webhookConfigName,
OsmNamespace: osmNamespace,
}).SetupWithManager(mgr); err != nil {
log.Error().Err(err).Msg("Error creating controller to reconcile MutatingWebhookConfiguration")
return err
return errors.Wrap(err, "Error creating controller to reconcile MutatingWebhookConfiguration")
}

go func() {
// mgr.Start() below will block until stopped
// See: https://github.com/kubernetes-sigs/controller-runtime/blob/release-0.6/pkg/manager/internal.go#L507-L514
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
log.Error().Err(err).Msg("Error setting up signal handler for reconciler")
log.Error().Err(err).Str(errcode.Kind, errcode.ErrStartingReconcileManager.String()).
Msg("Error starting controller-runtime manager for MutatingWebhookConfigurartion's reconciler")
}
}()

Expand Down
40 changes: 38 additions & 2 deletions pkg/errcode/errcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@ const (
Kind = "error_code"
)

// Range 1000-1050 is reserved for errors related to application startup
// Range 1000-1050 is reserved for errors related to application startup or bootstrapping
const (
// ErrInvalidCLIArgument refers to an invalid CLI argument being specified
// ErrInvalidCLIArgument indicates an invalid CLI argument
ErrInvalidCLIArgument errCode = iota + 1000

// ErrSettingLogLevel indicates the specified log level could not be set
ErrSettingLogLevel

// ErrParsingMeshConfig indicates the MeshConfig resource could not be parsed
ErrParsingMeshConfig

// ErrFetchingControllerPod indicates the osm-controller pod resource could not be fetched
ErrFetchingControllerPod

// ErrFetchingInjectorPod indicates the osm-injector pod resource could not be fetched
ErrFetchingInjectorPod

// ErrStartingReconcileManager indicates the controller-runtime Manager failed to start
ErrStartingReconcileManager
)

// String returns the error code as a string, ex. E1000
Expand All @@ -28,5 +43,26 @@ func (e errCode) String() string {
var errCodeMap = map[errCode]string{
ErrInvalidCLIArgument: `
An invalid comment line argument was passed to the application.
`,

ErrSettingLogLevel: `
The specified log level could not be set in the system.
`,

ErrParsingMeshConfig: `
The 'osm-mesh-config' MeshConfig custom resource could not be parsed.
`,

ErrFetchingControllerPod: `
The osm-controller k8s pod resource was not able to be retrieved by the system.
`,

ErrFetchingInjectorPod: `
The osm-injector k8s pod resource was not able to be retrieved by the system.
`,

ErrStartingReconcileManager: `
The controller-runtime manager to manage the controller used to reconcile the
sidecar injector's MutatingWebhookConfiguration resource failed to start.
`,
}

0 comments on commit 31b0d23

Please sign in to comment.