From ba1e3245613120f3d3d704678730538e3f68aeed Mon Sep 17 00:00:00 2001 From: Cameron Wall Date: Wed, 25 Oct 2023 16:15:06 -0400 Subject: [PATCH] improve log legibility Signed-off-by: Cameron Wall improve log legibility Signed-off-by: Cameron Wall --- api/v1/multiclusterengine_webhook.go | 16 +++++------ api/v1/webhook_suite_test.go | 7 ++--- controllers/backplaneconfig_controller.go | 15 +--------- controllers/hosted.go | 9 +----- controllers/suite_test.go | 7 ++--- controllers/toggle_components.go | 35 +---------------------- controllers/uninstall.go | 3 +- main.go | 15 ++++------ pkg/foundation/cluster_manager.go | 3 +- pkg/foundation/hosted_cluster_manager.go | 4 +-- pkg/rendering/renderer.go | 6 +--- 11 files changed, 25 insertions(+), 95 deletions(-) diff --git a/api/v1/multiclusterengine_webhook.go b/api/v1/multiclusterengine_webhook.go index debe638e1..3a769c693 100644 --- a/api/v1/multiclusterengine_webhook.go +++ b/api/v1/multiclusterengine_webhook.go @@ -28,10 +28,10 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/discovery" + log "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" cl "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/config" - logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) @@ -40,10 +40,8 @@ const ( DefaultTargetNamespace = "multicluster-engine" ) -// log is for logging in this package. var ( - backplaneconfiglog = logf.Log.WithName("backplaneconfig-resource") - Client cl.Client + Client cl.Client ErrInvalidComponent = errors.New("invalid component config") ErrInvalidNamespace = errors.New("invalid TargetNamespace") @@ -146,7 +144,7 @@ var _ webhook.Defaulter = &MultiClusterEngine{} // Default implements webhook.Defaulter so a webhook will be registered for the type func (r *MultiClusterEngine) Default() { - backplaneconfiglog.Info("default", "name", r.Name) + log.Info("default", "name", r.Name) if r.Spec.TargetNamespace == "" { r.Spec.TargetNamespace = DefaultTargetNamespace } @@ -157,7 +155,7 @@ var _ webhook.Validator = &MultiClusterEngine{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type func (r *MultiClusterEngine) ValidateCreate() (admission.Warnings, error) { ctx := context.Background() - backplaneconfiglog.Info("validate create", "name", r.Name) + log.Info("validate create", "name", r.Name) if (r.Spec.AvailabilityConfig != HABasic) && (r.Spec.AvailabilityConfig != HAHigh) && (r.Spec.AvailabilityConfig != "") { return nil, ErrInvalidAvailability @@ -198,10 +196,10 @@ func (r *MultiClusterEngine) ValidateCreate() (admission.Warnings, error) { // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type func (r *MultiClusterEngine) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { - backplaneconfiglog.Info("validate update", "name", r.Name) + log.Info("validate update", "name", r.Name) oldMCE := old.(*MultiClusterEngine) - backplaneconfiglog.Info(oldMCE.Spec.TargetNamespace) + log.Info(oldMCE.Spec.TargetNamespace) if (r.Spec.TargetNamespace != oldMCE.Spec.TargetNamespace) && (oldMCE.Spec.TargetNamespace != "") { return nil, fmt.Errorf("%w: changes cannot be made to target namespace", ErrInvalidNamespace) } @@ -269,7 +267,7 @@ func (r *MultiClusterEngine) ValidateUpdate(old runtime.Object) (admission.Warni // ValidateDelete implements webhook.Validator so a webhook will be registered for the type func (r *MultiClusterEngine) ValidateDelete() (admission.Warnings, error) { // TODO(user): fill in your validation logic upon object deletion. - backplaneconfiglog.Info("validate delete", "name", r.Name) + log.Info("validate delete", "name", r.Name) ctx := context.Background() cfg, err := config.GetConfig() diff --git a/api/v1/webhook_suite_test.go b/api/v1/webhook_suite_test.go index 3c67e06b7..e5d8f2ba6 100644 --- a/api/v1/webhook_suite_test.go +++ b/api/v1/webhook_suite_test.go @@ -35,11 +35,10 @@ import ( //+kubebuilder:scaffold:imports "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" + log "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" - logf "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/zap" ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to @@ -57,8 +56,8 @@ func TestAPIs(t *testing.T) { } var _ = BeforeSuite(func() { - logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - + log.InitFlags(nil) + defer log.Flush() ctx, cancel = context.WithCancel(context.TODO()) By("bootstrapping test environment") diff --git a/controllers/backplaneconfig_controller.go b/controllers/backplaneconfig_controller.go index d18952fdd..f3c208625 100644 --- a/controllers/backplaneconfig_controller.go +++ b/controllers/backplaneconfig_controller.go @@ -48,13 +48,13 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/util/workqueue" + log "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -142,7 +142,6 @@ const ( // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. func (r *MultiClusterEngineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (retRes ctrl.Result, retErr error) { - log := log.FromContext(ctx) // Fetch the BackplaneConfig instance backplaneConfig, err := r.getBackplaneConfig(ctx, req) if err != nil && !apierrors.IsNotFound(err) { @@ -441,7 +440,6 @@ func (r *MultiClusterEngineReconciler) SetupWithManager(mgr ctrl.Manager) error // createTrustBundleConfigmap creates a configmap that will be injected with the // trusted CA bundle for use with the OCP cluster wide proxy func (r *MultiClusterEngineReconciler) createTrustBundleConfigmap(ctx context.Context, mce *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) // Get Trusted Bundle configmap name trustBundleName := defaultTrustBundleName @@ -496,7 +494,6 @@ func (r *MultiClusterEngineReconciler) createTrustBundleConfigmap(ctx context.Co // DeployAlwaysSubcomponents ensures all subcomponents exist func (r *MultiClusterEngineReconciler) DeployAlwaysSubcomponents(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) chartsDir := renderer.AlwaysChartsDir // Renders all templates from charts @@ -787,7 +784,6 @@ func (r *MultiClusterEngineReconciler) applyTemplate(ctx context.Context, backpl // deleteTemplate return true if resource does not exist and returns an error if a GET or DELETE errors unexpectedly. A false response without error // means the resource is in the process of deleting. func (r *MultiClusterEngineReconciler) deleteTemplate(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine, template *unstructured.Unstructured) (ctrl.Result, error) { - log := log.FromContext(ctx) err := r.Client.Get(ctx, types.NamespacedName{Name: template.GetName(), Namespace: template.GetNamespace()}, template) if err != nil && (apierrors.IsNotFound(err) || apimeta.IsNoMatchError(err)) { @@ -811,7 +807,6 @@ func (r *MultiClusterEngineReconciler) deleteTemplate(ctx context.Context, backp } func (r *MultiClusterEngineReconciler) ensureCustomResources(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) if foundation.CanInstallAddons(ctx, r.Client) { addonTemplates, err := foundation.GetAddons() @@ -840,7 +835,6 @@ func (r *MultiClusterEngineReconciler) ensureCustomResources(ctx context.Context func (r *MultiClusterEngineReconciler) ensureOpenShiftNamespaceLabel(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) existingNs := &corev1.Namespace{} err := r.Client.Get(ctx, types.NamespacedName{Name: backplaneConfig.Spec.TargetNamespace}, existingNs) @@ -871,7 +865,6 @@ func (r *MultiClusterEngineReconciler) ensureOpenShiftNamespaceLabel(ctx context } func (r *MultiClusterEngineReconciler) finalizeBackplaneConfig(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) error { - log := log.FromContext(ctx) ocpConsole, err := r.CheckConsole(ctx) if err != nil { @@ -1010,7 +1003,6 @@ func (r *MultiClusterEngineReconciler) finalizeBackplaneConfig(ctx context.Conte } func (r *MultiClusterEngineReconciler) getBackplaneConfig(ctx context.Context, req ctrl.Request) (*backplanev1.MultiClusterEngine, error) { - log := log.FromContext(ctx) backplaneConfig := &backplanev1.MultiClusterEngine{} err := r.Client.Get(ctx, req.NamespacedName, backplaneConfig) if err != nil { @@ -1029,7 +1021,6 @@ func (r *MultiClusterEngineReconciler) getBackplaneConfig(ctx context.Context, r // ensureUnstructuredResource ensures that the unstructured resource is applied in the cluster properly func (r *MultiClusterEngineReconciler) ensureUnstructuredResource(ctx context.Context, bpc *backplanev1.MultiClusterEngine, u *unstructured.Unstructured) (ctrl.Result, error) { - log := log.FromContext(ctx) found := &unstructured.Unstructured{} found.SetGroupVersionKind(u.GroupVersionKind()) @@ -1065,7 +1056,6 @@ func (r *MultiClusterEngineReconciler) ensureUnstructuredResource(ctx context.Co } func (r *MultiClusterEngineReconciler) setDefaults(ctx context.Context, m *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) updateNecessary := false if !utils.AvailabilityConfigIsValid(m.Spec.AvailabilityConfig) { @@ -1166,7 +1156,6 @@ func (r *MultiClusterEngineReconciler) setDefaults(ctx context.Context, m *backp } func (r *MultiClusterEngineReconciler) validateNamespace(ctx context.Context, m *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) newNs := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: m.Spec.TargetNamespace, @@ -1217,7 +1206,6 @@ func (r *MultiClusterEngineReconciler) validateImagePullSecret(ctx context.Conte } func (r *MultiClusterEngineReconciler) getClusterVersion(ctx context.Context) (string, error) { - log := log.FromContext(ctx) // If Unit test if val, ok := os.LookupEnv("UNIT_TEST"); ok && val == "true" { if _, exists := os.LookupEnv("ACM_HUB_OCP_VERSION"); exists { @@ -1243,7 +1231,6 @@ func (r *MultiClusterEngineReconciler) getClusterVersion(ctx context.Context) (s //+kubebuilder:rbac:groups="config.openshift.io",resources="ingresses",verbs=get;list;watch func (r *MultiClusterEngineReconciler) getClusterIngressDomain(ctx context.Context, mce *backplanev1.MultiClusterEngine) (string, error) { - log := log.FromContext(ctx) // If Unit test if val, ok := os.LookupEnv("UNIT_TEST"); ok && val == "true" { return "apps.installer-test-cluster.dev00.red-chesterfield.com", nil diff --git a/controllers/hosted.go b/controllers/hosted.go index 11a72aabd..040411cac 100644 --- a/controllers/hosted.go +++ b/controllers/hosted.go @@ -23,16 +23,15 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/clientcmd" + log "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "sigs.k8s.io/controller-runtime/pkg/log" ) var ErrBadFormat = errors.New("bad format") func (r *MultiClusterEngineReconciler) HostedReconcile(ctx context.Context, mce *backplanev1.MultiClusterEngine) (retRes ctrl.Result, retErr error) { - log := log.FromContext(ctx) defer func() { mce.Status = r.StatusManager.ReportStatus(*mce) @@ -226,8 +225,6 @@ func parseKubeCreds(secret *corev1.Secret) ([]byte, error) { func (r *MultiClusterEngineReconciler) ensureHostedImport(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine, hostedClient client.Client) (ctrl.Result, error) { - log := log.FromContext(ctx) - r.StatusManager.AddComponent(toggle.EnabledStatus(types.NamespacedName{Name: "managedcluster-import-controller-v2", Namespace: backplaneConfig.Spec.TargetNamespace})) r.StatusManager.RemoveComponent(toggle.DisabledStatus(types.NamespacedName{Name: "managedcluster-import-controller-v2", Namespace: backplaneConfig.Spec.TargetNamespace}, []*unstructured.Unstructured{})) templates, errs := renderer.RenderChart(toggle.HostingImportChartDir, backplaneConfig, r.Images) @@ -267,7 +264,6 @@ func (r *MultiClusterEngineReconciler) ensureHostedImport(ctx context.Context, b func (r *MultiClusterEngineReconciler) ensureNoHostedImport(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine, hostedClient client.Client) (ctrl.Result, error) { - log := log.FromContext(ctx) r.StatusManager.RemoveComponent(toggle.EnabledStatus(types.NamespacedName{Name: "managedcluster-import-controller-v2", Namespace: backplaneConfig.Spec.TargetNamespace})) r.StatusManager.AddComponent(toggle.DisabledStatus(types.NamespacedName{Name: "managedcluster-import-controller-v2", Namespace: backplaneConfig.Spec.TargetNamespace}, []*unstructured.Unstructured{})) templates, errs := renderer.RenderChart(toggle.HostingImportChartDir, backplaneConfig, r.Images) @@ -306,7 +302,6 @@ func (r *MultiClusterEngineReconciler) ensureNoHostedImport(ctx context.Context, } func (r *MultiClusterEngineReconciler) ensureHostedClusterManager(ctx context.Context, mce *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) cmName := fmt.Sprintf("%s-cluster-manager", mce.Name) r.StatusManager.AddComponent(status.ClusterManagerStatus{ @@ -421,7 +416,6 @@ func (r *MultiClusterEngineReconciler) ensureNoHostedClusterManager(ctx context. // setHostedDefaults configures the MCE with default values and updates func (r *MultiClusterEngineReconciler) setHostedDefaults(ctx context.Context, m *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) updateNecessary := false if !utils.AvailabilityConfigIsValid(m.Spec.AvailabilityConfig) { @@ -495,7 +489,6 @@ func (r *MultiClusterEngineReconciler) hostedApplyTemplate(ctx context.Context, // deleteTemplate return true if resource does not exist and returns an error if a GET or DELETE errors unexpectedly. A false response without error // means the resource is in the process of deleting. func (r *MultiClusterEngineReconciler) hostedDeleteTemplate(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine, template *unstructured.Unstructured, hostedClient client.Client) (ctrl.Result, error) { - log := log.FromContext(ctx) err := hostedClient.Get(ctx, types.NamespacedName{Name: template.GetName(), Namespace: template.GetNamespace()}, template) if err != nil && apierrors.IsNotFound(err) { diff --git a/controllers/suite_test.go b/controllers/suite_test.go index ac23f8856..08c59a2b2 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -47,11 +47,10 @@ import ( "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" + log "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" - logf "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/zap" ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to @@ -70,8 +69,8 @@ var cancel context.CancelFunc var reconciler MultiClusterEngineReconciler var _ = BeforeSuite(func() { - logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - + log.InitFlags(nil) + defer log.Flush() // SetupSignalHandler can only be called once, so we'll save the // context it returns and reuse it each time we start a new // manager. diff --git a/controllers/toggle_components.go b/controllers/toggle_components.go index c51769ea4..de1397a4b 100644 --- a/controllers/toggle_components.go +++ b/controllers/toggle_components.go @@ -24,9 +24,9 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" + log "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/log" operatorv1 "github.com/openshift/api/operator/v1" @@ -44,7 +44,6 @@ func (r *MultiClusterEngineReconciler) ensureConsoleMCE(ctx context.Context, bac r.StatusManager.RemoveComponent(toggle.DisabledStatus(namespacedName, []*unstructured.Unstructured{})) r.StatusManager.AddComponent(toggle.EnabledStatus(namespacedName)) - log := log.FromContext(ctx) templates, errs := renderer.RenderChart(toggle.ConsoleMCEChartsDir, backplaneConfig, r.Images) if len(errs) > 0 { for _, err := range errs { @@ -79,7 +78,6 @@ func (r *MultiClusterEngineReconciler) ensureConsoleMCE(ctx context.Context, bac } func (r *MultiClusterEngineReconciler) ensureNoConsoleMCE(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine, ocpConsole bool) (ctrl.Result, error) { - log := log.FromContext(ctx) namespacedName := types.NamespacedName{Name: "console-mce-console", Namespace: backplaneConfig.Spec.TargetNamespace} r.StatusManager.RemoveComponent(toggle.EnabledStatus(namespacedName)) if !ocpConsole { @@ -123,8 +121,6 @@ func (r *MultiClusterEngineReconciler) ensureManagedServiceAccount(ctx context.C // r.StatusManager.AddComponent(toggle.EnabledStatus(types.NamespacedName{Name: "managed-serviceaccount-addon-manager", Namespace: backplaneConfig.Spec.TargetNamespace})) r.StatusManager.AddComponent(status.NewPresentStatus(types.NamespacedName{Name: "managed-serviceaccount"}, clusterManagementAddOnGVK)) - log := log.FromContext(ctx) - // Render CRD templates crdPath := toggle.ManagedServiceAccountCRDPath crds, errs := renderer.RenderCRDs(crdPath) @@ -177,7 +173,6 @@ func (r *MultiClusterEngineReconciler) ensureManagedServiceAccount(ctx context.C } func (r *MultiClusterEngineReconciler) ensureNoManagedServiceAccount(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) // Renders all templates from charts chartPath := toggle.ManagedServiceAccountChartDir @@ -230,7 +225,6 @@ func (r *MultiClusterEngineReconciler) ensureNoManagedServiceAccount(ctx context // addPluginToConsoleResource ... func (r *MultiClusterEngineReconciler) addPluginToConsoleResource(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) console := &operatorv1.Console{} // If trying to check this resource from the CLI run - `oc get consoles.operator.openshift.io cluster`. // The default `console` is not the correct resource @@ -266,7 +260,6 @@ func (r *MultiClusterEngineReconciler) removePluginFromConsoleResource(ctx conte return ctrl.Result{}, nil } - log := log.FromContext(ctx) console := &operatorv1.Console{} // If trying to check this resource from the CLI run - `oc get consoles.operator.openshift.io cluster`. // The default `console` is not the correct resource @@ -301,8 +294,6 @@ func (r *MultiClusterEngineReconciler) ensureDiscovery(ctx context.Context, back r.StatusManager.RemoveComponent(toggle.DisabledStatus(namespacedName, []*unstructured.Unstructured{})) r.StatusManager.AddComponent(toggle.EnabledStatus(namespacedName)) - log := log.FromContext(ctx) - templates, errs := renderer.RenderChart(toggle.DiscoveryChartDir, backplaneConfig, r.Images) if len(errs) > 0 { for _, err := range errs { @@ -323,7 +314,6 @@ func (r *MultiClusterEngineReconciler) ensureDiscovery(ctx context.Context, back } func (r *MultiClusterEngineReconciler) ensureNoDiscovery(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) namespacedName := types.NamespacedName{Name: "discovery-operator", Namespace: backplaneConfig.Spec.TargetNamespace} // Renders all templates from charts @@ -354,8 +344,6 @@ func (r *MultiClusterEngineReconciler) ensureHive(ctx context.Context, backplane r.StatusManager.RemoveComponent(toggle.DisabledStatus(namespacedName, []*unstructured.Unstructured{})) r.StatusManager.AddComponent(toggle.EnabledStatus(namespacedName)) - log := log.FromContext(ctx) - templates, errs := renderer.RenderChart(toggle.HiveChartDir, backplaneConfig, r.Images) if len(errs) > 0 { for _, err := range errs { @@ -386,7 +374,6 @@ func (r *MultiClusterEngineReconciler) ensureHive(ctx context.Context, backplane } func (r *MultiClusterEngineReconciler) ensureNoHive(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) namespacedName := types.NamespacedName{Name: "hive-operator", Namespace: backplaneConfig.Spec.TargetNamespace} // Renders all templates from charts @@ -434,8 +421,6 @@ func (r *MultiClusterEngineReconciler) ensureAssistedService(ctx context.Context r.StatusManager.RemoveComponent(toggle.DisabledStatus(namespacedName, []*unstructured.Unstructured{})) r.StatusManager.AddComponent(toggle.EnabledStatus(namespacedName)) - log := log.FromContext(ctx) - templates, errs := renderer.RenderChartWithNamespace(toggle.AssistedServiceChartDir, backplaneConfig, r.Images, targetNamespace) if len(errs) > 0 { for _, err := range errs { @@ -462,8 +447,6 @@ func (r *MultiClusterEngineReconciler) ensureNoAssistedService(ctx context.Conte } namespacedName := types.NamespacedName{Name: "infrastructure-operator", Namespace: targetNamespace} - log := log.FromContext(ctx) - // Renders all templates from charts templates, errs := renderer.RenderChartWithNamespace(toggle.AssistedServiceChartDir, backplaneConfig, r.Images, targetNamespace) if len(errs) > 0 { @@ -498,8 +481,6 @@ func (r *MultiClusterEngineReconciler) ensureServerFoundation(ctx context.Contex r.StatusManager.RemoveComponent(toggle.DisabledStatus(namespacedName, []*unstructured.Unstructured{})) r.StatusManager.AddComponent(toggle.EnabledStatus(namespacedName)) - log := log.FromContext(ctx) - templates, errs := renderer.RenderChart(toggle.ServerFoundationChartDir, backplaneConfig, r.Images) if len(errs) > 0 { for _, err := range errs { @@ -520,7 +501,6 @@ func (r *MultiClusterEngineReconciler) ensureServerFoundation(ctx context.Contex } func (r *MultiClusterEngineReconciler) ensureNoServerFoundation(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) // Renders all templates from charts templates, errs := renderer.RenderChart(toggle.ServerFoundationChartDir, backplaneConfig, r.Images) @@ -569,8 +549,6 @@ func (r *MultiClusterEngineReconciler) ensureClusterLifecycle(ctx context.Contex r.StatusManager.RemoveComponent(toggle.DisabledStatus(namespacedName, []*unstructured.Unstructured{})) r.StatusManager.AddComponent(toggle.EnabledStatus(namespacedName)) - log := log.FromContext(ctx) - templates, errs := renderer.RenderChart(toggle.ClusterLifecycleChartDir, backplaneConfig, r.Images) if len(errs) > 0 { for _, err := range errs { @@ -591,7 +569,6 @@ func (r *MultiClusterEngineReconciler) ensureClusterLifecycle(ctx context.Contex } func (r *MultiClusterEngineReconciler) ensureNoClusterLifecycle(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) // Renders all templates from charts templates, errs := renderer.RenderChart(toggle.ClusterLifecycleChartDir, backplaneConfig, r.Images) @@ -634,8 +611,6 @@ func (r *MultiClusterEngineReconciler) ensureClusterManager(ctx context.Context, NamespacedName: types.NamespacedName{Name: "cluster-manager"}, }) - log := log.FromContext(ctx) - templates, errs := renderer.RenderChart(toggle.ClusterManagerChartDir, backplaneConfig, r.Images) if len(errs) > 0 { for _, err := range errs { @@ -667,7 +642,6 @@ func (r *MultiClusterEngineReconciler) ensureClusterManager(ctx context.Context, } func (r *MultiClusterEngineReconciler) ensureNoClusterManager(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) namespacedName := types.NamespacedName{Name: "cluster-manager", Namespace: backplaneConfig.Spec.TargetNamespace} // Renders all templates from charts @@ -731,8 +705,6 @@ func (r *MultiClusterEngineReconciler) ensureHyperShift(ctx context.Context, bac r.StatusManager.AddComponent(toggle.EnabledStatus(namespacedName)) r.StatusManager.AddComponent(status.NewPresentStatus(types.NamespacedName{Name: "hypershift-addon"}, clusterManagementAddOnGVK)) - log := log.FromContext(ctx) - templates, errs := renderer.RenderChart(toggle.HyperShiftChartDir, backplaneConfig, r.Images) if len(errs) > 0 { for _, err := range errs { @@ -765,7 +737,6 @@ func (r *MultiClusterEngineReconciler) ensureHyperShift(ctx context.Context, bac } func (r *MultiClusterEngineReconciler) ensureNoHyperShift(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) namespacedName := types.NamespacedName{Name: "hypershift-addon-manager", Namespace: backplaneConfig.Spec.TargetNamespace} // Ensure hypershift-addon is removed first @@ -951,7 +922,6 @@ func (r *MultiClusterEngineReconciler) removeHypershiftLocalHosting(ctx context. } func (r *MultiClusterEngineReconciler) ensureClusterProxyAddon(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) namespacedName := types.NamespacedName{Name: "cluster-proxy-addon-manager", Namespace: backplaneConfig.Spec.TargetNamespace} r.StatusManager.AddComponent(toggle.EnabledStatus(namespacedName)) @@ -990,7 +960,6 @@ func (r *MultiClusterEngineReconciler) ensureClusterProxyAddon(ctx context.Conte } func (r *MultiClusterEngineReconciler) ensureNoClusterProxyAddon(ctx context.Context, backplaneConfig *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) namespacedName := types.NamespacedName{Name: "cluster-proxy-addon-manager", Namespace: backplaneConfig.Spec.TargetNamespace} r.StatusManager.RemoveComponent(toggle.EnabledStatus(namespacedName)) r.StatusManager.AddComponent(toggle.DisabledStatus(namespacedName, []*unstructured.Unstructured{})) @@ -1055,7 +1024,6 @@ func (r *MultiClusterEngineReconciler) CheckConsole(ctx context.Context) (bool, } func (r *MultiClusterEngineReconciler) ensureLocalCluster(ctx context.Context, mce *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) if utils.IsUnitTest() { log.Info("skipping local cluster creation in unit tests") @@ -1184,7 +1152,6 @@ func (r *MultiClusterEngineReconciler) ensureLocalCluster(ctx context.Context, m } func (r *MultiClusterEngineReconciler) ensureNoLocalCluster(ctx context.Context, mce *backplanev1.MultiClusterEngine) (ctrl.Result, error) { - log := log.FromContext(ctx) if utils.IsUnitTest() { log.Info("skipping local cluster removal in unit tests") diff --git a/controllers/uninstall.go b/controllers/uninstall.go index 05181d71d..0ddcf3b9a 100644 --- a/controllers/uninstall.go +++ b/controllers/uninstall.go @@ -10,7 +10,7 @@ import ( backplanev1 "github.com/stolostron/backplane-operator/api/v1" "github.com/stolostron/backplane-operator/pkg/toggle" apimeta "k8s.io/apimachinery/pkg/api/meta" - "sigs.k8s.io/controller-runtime/pkg/log" + log "k8s.io/klog/v2" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -143,7 +143,6 @@ instead. */ func (r *MultiClusterEngineReconciler) removeLegacyPrometheusConfigurations(ctx context.Context, targetNamespace string, kind string) error { - log := log.FromContext(ctx) obj := &unstructured.Unstructured{} obj.SetGroupVersionKind(schema.GroupVersionKind{ diff --git a/main.go b/main.go index a633bd02a..574800dd0 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,6 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/client-go/util/retry" - "go.uber.org/zap/zapcore" admissionregistration "k8s.io/api/admissionregistration/v1" corev1 "k8s.io/api/core/v1" apixv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -58,11 +57,11 @@ import ( "k8s.io/apimachinery/pkg/types" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" + log "k8s.io/klog/v2" apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/healthz" - "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/webhook" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" @@ -137,16 +136,12 @@ func main() { flag.DurationVar(&retryPeriod, "leader-election-retry-period", 26*time.Second, ""+ "The duration the clients should wait between attempting acquisition and renewal "+ "of a leadership. This is only applicable if leader election is enabled.") - opts := zap.Options{ - Development: true, - TimeEncoder: zapcore.ISO8601TimeEncoder, - } - opts.BindFlags(flag.CommandLine) + // Initialize the logger. + log.InitFlags(nil) flag.Parse() + defer log.Flush() - ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) - - ctrl.Log.WithName("Backplane Operator version").Info(fmt.Sprintf("%#v", version.Get())) + log.Info(fmt.Sprintf("Backplane Operator version %#v", version.Get())) mgrOptions := ctrl.Options{ Scheme: scheme, diff --git a/pkg/foundation/cluster_manager.go b/pkg/foundation/cluster_manager.go index 67517b56d..e12afcfdb 100644 --- a/pkg/foundation/cluster_manager.go +++ b/pkg/foundation/cluster_manager.go @@ -20,8 +20,8 @@ import ( v1 "github.com/stolostron/backplane-operator/api/v1" "github.com/stolostron/backplane-operator/pkg/utils" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + log "k8s.io/klog/v2" ocmapiv1 "open-cluster-management.io/api/operator/v1" - "sigs.k8s.io/controller-runtime/pkg/log" ) const ( @@ -59,7 +59,6 @@ func AddonManagerImage(overrides map[string]string) string { } func ClusterManager(m *v1.MultiClusterEngine, overrides map[string]string) *unstructured.Unstructured { - log := log.FromContext(context.Background()) cmTolerations := []corev1.Toleration{} if m.Spec.Tolerations != nil { diff --git a/pkg/foundation/hosted_cluster_manager.go b/pkg/foundation/hosted_cluster_manager.go index 27d733040..43a2f7dec 100644 --- a/pkg/foundation/hosted_cluster_manager.go +++ b/pkg/foundation/hosted_cluster_manager.go @@ -3,7 +3,6 @@ package foundation import ( - "context" "fmt" corev1 "k8s.io/api/core/v1" @@ -12,8 +11,8 @@ import ( v1 "github.com/stolostron/backplane-operator/api/v1" "github.com/stolostron/backplane-operator/pkg/utils" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + log "k8s.io/klog/v2" ocmapiv1 "open-cluster-management.io/api/operator/v1" - "sigs.k8s.io/controller-runtime/pkg/log" ) func hostedName(m *v1.MultiClusterEngine) string { @@ -22,7 +21,6 @@ func hostedName(m *v1.MultiClusterEngine) string { // HostedClusterManager returns the ClusterManager in hosted mode func HostedClusterManager(m *v1.MultiClusterEngine, overrides map[string]string) *unstructured.Unstructured { - log := log.FromContext(context.Background()) cmTolerations := []corev1.Toleration{} if m.Spec.Tolerations != nil { diff --git a/pkg/rendering/renderer.go b/pkg/rendering/renderer.go index 52a5170cb..92a54078b 100644 --- a/pkg/rendering/renderer.go +++ b/pkg/rendering/renderer.go @@ -3,7 +3,6 @@ package renderer import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -21,7 +20,7 @@ import ( "helm.sh/helm/v3/pkg/engine" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/controller-runtime/pkg/log" + log "k8s.io/klog/v2" "sigs.k8s.io/yaml" ) @@ -183,7 +182,6 @@ func RenderCRDs(crdDir string) ([]*unstructured.Unstructured, []error) { } func RenderCharts(chartDir string, backplaneConfig *v1.MultiClusterEngine, images map[string]string) ([]*unstructured.Unstructured, []error) { - log := log.FromContext(context.Background()) var templates []*unstructured.Unstructured errs := []error{} if val, ok := os.LookupEnv("DIRECTORY_OVERRIDE"); ok { @@ -208,7 +206,6 @@ func RenderCharts(chartDir string, backplaneConfig *v1.MultiClusterEngine, image } func RenderChart(chartPath string, backplaneConfig *v1.MultiClusterEngine, images map[string]string) ([]*unstructured.Unstructured, []error) { - log := log.FromContext(context.Background()) errs := []error{} if val, ok := os.LookupEnv("DIRECTORY_OVERRIDE"); ok { chartPath = path.Join(val, chartPath) @@ -232,7 +229,6 @@ func RenderChartWithNamespace(chartPath string, backplaneConfig *v1.MultiCluster } func renderTemplates(chartPath string, backplaneConfig *v1.MultiClusterEngine, images map[string]string) ([]*unstructured.Unstructured, []error) { - log := log.FromContext(context.Background()) var templates []*unstructured.Unstructured errs := []error{} chart, err := loader.Load(chartPath)