diff --git a/cmd/osm-controller/log_handler.go b/cmd/osm-controller/log_handler.go index 3a5b91d091..f6b1ee783a 100644 --- a/cmd/osm-controller/log_handler.go +++ b/cmd/osm-controller/log_handler.go @@ -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" ) @@ -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 } @@ -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 diff --git a/cmd/osm-controller/osm-controller.go b/cmd/osm-controller/osm-controller.go index fa2d0a2068..d3c1fb4e21 100644 --- a/cmd/osm-controller/osm-controller.go +++ b/cmd/osm-controller/osm-controller.go @@ -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) @@ -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 } diff --git a/cmd/osm-injector/osm-injector.go b/cmd/osm-injector/osm-injector.go index f882a1214a..1f8b2bf9ab 100644 --- a/cmd/osm-injector/osm-injector.go +++ b/cmd/osm-injector/osm-injector.go @@ -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" @@ -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) @@ -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 } diff --git a/cmd/osm-injector/reconcile.go b/cmd/osm-injector/reconcile.go index 34be9e9c9b..125b6aad11 100644 --- a/cmd/osm-injector/reconcile.go +++ b/cmd/osm-injector/reconcile.go @@ -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" ) @@ -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 @@ -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") } }() diff --git a/pkg/errcode/errcode.go b/pkg/errcode/errcode.go index 4c5f3011c2..c84d5f3fb7 100644 --- a/pkg/errcode/errcode.go +++ b/pkg/errcode/errcode.go @@ -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 @@ -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. `, }