diff --git a/controllers/heat_controller.go b/controllers/heat_controller.go index b62ba0c6..89676510 100644 --- a/controllers/heat_controller.go +++ b/controllers/heat_controller.go @@ -61,12 +61,16 @@ import ( type HeatReconciler struct { client.Client Kclient kubernetes.Interface - Log logr.Logger Scheme *runtime.Scheme } var keystoneAPI *keystonev1.KeystoneAPI +// getlog returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *HeatReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("Heat") +} + // +kubebuilder:rbac:groups=heat.openstack.org,resources=heats,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=heat.openstack.org,resources=heats/status,verbs=get;update;patch // +kubebuilder:rbac:groups=heat.openstack.org,resources=heats/finalizers,verbs=update @@ -100,7 +104,7 @@ var keystoneAPI *keystonev1.KeystoneAPI // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *HeatReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) instance := &heatv1beta1.Heat{} err := r.Client.Get(ctx, req.NamespacedName, instance) @@ -116,7 +120,7 @@ func (r *HeatReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resul r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { @@ -192,7 +196,7 @@ func (r *HeatReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resul } // SetupWithManager sets up the controller with the Manager. -func (r *HeatReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *HeatReconciler) SetupWithManager(mgr ctrl.Manager, ctx context.Context) error { // transportURLSecretFn - Watch for changes made to the secret associated with the RabbitMQ // TransportURL created and used by Heat CRs. Watch functions return a list of namespace-scoped @@ -205,6 +209,7 @@ func (r *HeatReconciler) SetupWithManager(mgr ctrl.Manager) error { // reconciliation for a Heat CR that does not need it. // // TODO: We also need a watch func to monitor for changes to the secret referenced by Heat.Spec.Secret + Log := r.GetLogger(ctx) transportURLSecretFn := func(o client.Object) []reconcile.Request { result := []reconcile.Request{} @@ -213,8 +218,8 @@ func (r *HeatReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(o.GetNamespace()), } - if err := r.Client.List(context.Background(), heats, listOpts...); err != nil { - r.Log.Error(err, "Unable to retrieve Heat CRs %v") + if err := r.Client.List(ctx, heats, listOpts...); err != nil { + Log.Error(err, "Unable to retrieve Heat CRs", "Error") return nil } @@ -227,7 +232,7 @@ func (r *HeatReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("TransportURL Secret %s belongs to TransportURL belonging to Heat CR %s", o.GetName(), cr.Name)) + Log.Info("TransportURL", "secret", o.GetName(), "belongs to TransportURL belonging to Heat CR", cr.Name) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -289,7 +294,8 @@ func (r *HeatReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *HeatReconciler) reconcileDelete(ctx context.Context, instance *heatv1beta1.Heat, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Heat delete") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Heat delete") // remove db finalizer first db, err := mariadbv1.GetDatabaseByName(ctx, helper, instance.Name) @@ -305,13 +311,14 @@ func (r *HeatReconciler) reconcileDelete(ctx context.Context, instance *heatv1be // Service is deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - r.Log.Info("Reconciled Heat delete successfully") + Log.Info("Reconciled Heat delete successfully") return ctrl.Result{}, nil } func (r *HeatReconciler) reconcileNormal(ctx context.Context, instance *heatv1beta1.Heat, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service") // Service account, role, binding rbacRules := []rbacv1.PolicyRule{ @@ -413,13 +420,13 @@ func (r *HeatReconciler) reconcileNormal(ctx context.Context, instance *heatv1be } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op))) + Log.Info("Successfully reconciled operation:", "TransportURL", transportURL.Name, "Operation", string(op)) } instance.Status.TransportURLSecret = transportURL.Status.SecretName if instance.Status.TransportURLSecret == "" { - r.Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name)) + Log.Info("Waiting for TransportURL secret to be created", "TransportURL", transportURL.Name) instance.Status.Conditions.Set(condition.FalseCondition( condition.RabbitMqTransportURLReadyCondition, @@ -556,7 +563,7 @@ func (r *HeatReconciler) reconcileNormal(ctx context.Context, instance *heatv1be return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op))) + Log.Info("Deployment successfully reconciled", "Deployment", instance.Name, "operation", string(op)) } // Mirror HeatEngine status' ReadyCount to this parent CR instance.Status.HeatEngineReadyCount = heatEngine.Status.ReadyCount @@ -579,7 +586,7 @@ func (r *HeatReconciler) reconcileNormal(ctx context.Context, instance *heatv1be return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op))) + Log.Info("Deployment successfully reconciled", "Deployment", instance.Name, "operation", string(op)) } // Mirror HeatAPI status' ReadyCount to this parent CR @@ -603,7 +610,7 @@ func (r *HeatReconciler) reconcileNormal(ctx context.Context, instance *heatv1be return ctrl.Result{}, err } if op != controllerutil.OperationResultNone { - r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op))) + Log.Info("Deployment successfully reconciled", "Deployment", instance.Name, "operation", string(op)) } // Mirror HeatCfnAPI status' ReadyCount to this parent CR @@ -615,7 +622,7 @@ func (r *HeatReconciler) reconcileNormal(ctx context.Context, instance *heatv1be instance.Status.Conditions.Set(c) } - r.Log.Info("Reconciled Heat successfully") + Log.Info("Reconciled Heat successfully") return ctrl.Result{}, nil } @@ -624,7 +631,8 @@ func (r *HeatReconciler) reconcileInit(ctx context.Context, helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Heat init") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Heat init") // // create service DB instance // @@ -725,23 +733,24 @@ func (r *HeatReconciler) reconcileInit(ctx context.Context, if err := r.Client.Status().Update(ctx, instance); err != nil { return ctrl.Result{}, err } - r.Log.Info(fmt.Sprintf("Job %s hash added - %s", jobDef.Name, instance.Status.Hash[heatv1beta1.DbSyncHash])) + Log.Info("Job hash added", "Job", jobDef.Name, "Hash", instance.Status.Hash[heatv1beta1.DbSyncHash]) } instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage) // run heat db sync - end - r.Log.Info("Reconciled Heat init successfully") + Log.Info("Reconciled Heat init successfully") return ctrl.Result{}, nil } func (r *HeatReconciler) reconcileUpdate(ctx context.Context, instance *heatv1beta1.Heat, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Heat update") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Heat update") // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled Heat update successfully") + Log.Info("Reconciled Heat update successfully") return ctrl.Result{}, nil } @@ -910,12 +919,13 @@ func (r *HeatReconciler) generateServiceConfigMaps( } func (r *HeatReconciler) reconcileUpgrade(ctx context.Context, instance *heatv1beta1.Heat, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Heat upgrade") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Heat upgrade") // TODO(bshephar): should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled Heat upgrade successfully") + Log.Info("Reconciled Heat upgrade successfully") return ctrl.Result{}, nil } @@ -926,6 +936,7 @@ func (r *HeatReconciler) createHashOfInputHashes( instance *heatv1beta1.Heat, envVars map[string]env.Setter, ) (string, error) { + Log := r.GetLogger(ctx) mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars) hash, err := util.ObjectHash(mergedMapVars) if err != nil { @@ -936,7 +947,7 @@ func (r *HeatReconciler) createHashOfInputHashes( if err := r.Client.Status().Update(ctx, instance); err != nil { return hash, err } - r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) + Log.Info("Input maps hash", "Input", common.InputHashName, "Hash", hash) } return hash, nil } @@ -973,6 +984,7 @@ func (r *HeatReconciler) ensureStackDomain( } password := strings.TrimSuffix(string(val), "\n") + Log := r.GetLogger(ctx) // // get admin authentication OpenStack // @@ -990,14 +1002,14 @@ func (r *HeatReconciler) ensureStackDomain( Name: heat.StackDomainName, Description: "Domain for Heat stacks", } - domainID, err := os.CreateDomain(r.Log, domain) + domainID, err := os.CreateDomain(Log, domain) if err != nil { return ctrl.Result{}, err } // Create Heat user userID, err := os.CreateUser( - r.Log, + Log, openstack.User{ Name: heat.StackDomainAdminUsername, Password: password, @@ -1010,7 +1022,7 @@ func (r *HeatReconciler) ensureStackDomain( // Add the user to the domain err = os.AssignUserDomainRole( - r.Log, + Log, "admin", userID, domainID) diff --git a/controllers/heatapi_controller.go b/controllers/heatapi_controller.go index e26f591d..15eca4fc 100644 --- a/controllers/heatapi_controller.go +++ b/controllers/heatapi_controller.go @@ -21,6 +21,7 @@ import ( "fmt" "time" + "github.com/go-logr/logr" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" k8s_errors "k8s.io/apimachinery/pkg/api/errors" @@ -34,7 +35,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" - "github.com/go-logr/logr" heatv1beta1 "github.com/openstack-k8s-operators/heat-operator/api/v1beta1" heat "github.com/openstack-k8s-operators/heat-operator/pkg/heat" heatapi "github.com/openstack-k8s-operators/heat-operator/pkg/heatapi" @@ -52,11 +52,25 @@ import ( "github.com/openstack-k8s-operators/lib-common/modules/common/util" ) +// GetClient - +func (r *HeatAPIReconciler) GetClient() client.Client { + return r.Client +} + +// GetKClient - +func (r *HeatAPIReconciler) GetKClient() kubernetes.Interface { + return r.Kclient +} + +// GetScheme - +func (r *HeatAPIReconciler) GetScheme() *runtime.Scheme { + return r.Scheme +} + // HeatAPIReconciler reconciles a Heat object type HeatAPIReconciler struct { client.Client Scheme *runtime.Scheme - Log logr.Logger Kclient kubernetes.Interface } @@ -70,6 +84,11 @@ var ( } ) +// getlog returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *HeatAPIReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("Heat") +} + // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatapis,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatapis/status,verbs=get;update;patch // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatapis/finalizers,verbs=update @@ -87,8 +106,7 @@ var ( // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *HeatAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) - + Log := r.GetLogger(ctx) // TODO(bshephar): your logic here // We should have a Database and a `heat.conf` file by this time. Handled by the heat_controller. // We just need to initialise the API in a deployment here, and allow scaling of the ReplicaSet. @@ -108,7 +126,7 @@ func (r *HeatAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -174,8 +192,8 @@ func (r *HeatAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re } // SetupWithManager sets up the controller with the Manager. -func (r *HeatAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { - +func (r *HeatAPIReconciler) SetupWithManager(mgr ctrl.Manager, ctx context.Context) error { + Log := r.GetLogger(ctx) configMapFn := func(o client.Object) []reconcile.Request { result := []reconcile.Request{} @@ -183,21 +201,21 @@ func (r *HeatAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(o.GetNamespace()), } - if err := r.Client.List(context.Background(), apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to get API CRs %v") + if err := r.Client.List(ctx, apis, listOpts...); err != nil { + Log.Error(err, "Unable to get API CRs") return nil } label := o.GetLabels() - if l, ok := label[labels.GetOwnerNameLabelSelector(labels.GetGroupLabel(heat.ServiceName))]; ok { + if lbl, ok := label[labels.GetOwnerNameLabelSelector(labels.GetGroupLabel(heat.ServiceName))]; ok { for _, cr := range apis.Items { - if l == heat.GetOwningHeatName(&cr) { + if lbl == heat.GetOwningHeatName(&cr) { name := client.ObjectKey{ Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info("", "ConfigMap object", o.GetName(), "and CR", cr.Name, "marked with label:", lbl) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -222,7 +240,8 @@ func (r *HeatAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *HeatAPIReconciler) reconcileDelete(ctx context.Context, instance *heatv1beta1.HeatAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling API Delete") + Log := r.GetLogger(ctx) + Log.Info("Reconciling API Delete") for _, ksSvc := range keystoneServices { keystoneEndpoint, err := keystonev1.GetKeystoneEndpointWithName(ctx, helper, ksSvc["name"], instance.Namespace) @@ -237,6 +256,7 @@ func (r *HeatAPIReconciler) reconcileDelete(ctx context.Context, instance *heatv } util.LogForObject(helper, "Removed finalizer from KeystoneEndpoint", instance) } + Log.Info("Removed finalizer from KeystoneEndpoint", "KeystoneEndpoint", instance) } keystoneService, err := keystonev1.GetKeystoneServiceWithName(ctx, helper, ksSvc["name"], instance.Namespace) @@ -249,14 +269,15 @@ func (r *HeatAPIReconciler) reconcileDelete(ctx context.Context, instance *heatv if err != nil && !k8s_errors.IsNotFound(err) { return ctrl.Result{}, err } - util.LogForObject(helper, "Removed finalizer from our KeystoneService", instance) + Log.Info("Removed finalizer from our KeystoneService") } + Log.Info("Removed finalizer from our KeystoneService", "KeystoneService", instance) } } // Service is deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - r.Log.Info("Reconciled API delete successfully") + Log.Info("Reconciled API delete successfully") return ctrl.Result{}, nil } @@ -267,7 +288,8 @@ func (r *HeatAPIReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info("Reconciling API init") + Log := r.GetLogger(ctx) + Log.Info("Reconciling API init") // // expose the service (create service and return the created endpoint URLs) @@ -433,12 +455,13 @@ func (r *HeatAPIReconciler) reconcileInit( } } - r.Log.Info("Reconciled API init successfully") + Log.Info("Reconciled API init successfully") return ctrl.Result{}, nil } func (r *HeatAPIReconciler) reconcileNormal(ctx context.Context, instance *heatv1beta1.HeatAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service") // ConfigMap configMapVars := make(map[string]env.Setter) @@ -617,27 +640,29 @@ func (r *HeatAPIReconciler) reconcileNormal(ctx context.Context, instance *heatv } // create Deployment - end - r.Log.Info("Reconciled API successfully") + Log.Info("Reconciled API successfully") return ctrl.Result{}, nil } func (r *HeatAPIReconciler) reconcileUpdate(ctx context.Context, instance *heatv1beta1.HeatAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling API update") + Log := r.GetLogger(ctx) + Log.Info("Reconciling API update") // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled API update successfully") + Log.Info("Reconciled API update successfully") return ctrl.Result{}, nil } func (r *HeatAPIReconciler) reconcileUpgrade(ctx context.Context, instance *heatv1beta1.HeatAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling API upgrade") + Log := r.GetLogger(ctx) + Log.Info("Reconciling API upgrade") // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled API upgrade successfully") + Log.Info("Reconciled API upgrade successfully") return ctrl.Result{}, nil } @@ -689,6 +714,8 @@ func (r *HeatAPIReconciler) createHashOfInputHashes( instance *heatv1beta1.HeatAPI, envVars map[string]env.Setter, ) (string, error) { + Log := r.GetLogger(ctx) + mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars) hash, err := util.ObjectHash(mergedMapVars) if err != nil { @@ -699,7 +726,7 @@ func (r *HeatAPIReconciler) createHashOfInputHashes( if err := r.Client.Status().Update(ctx, instance); err != nil { return hash, err } - r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) + Log.Info("Input maps", common.InputHashName, "hash", hash) } return hash, nil } diff --git a/controllers/heatcfnapi_controller.go b/controllers/heatcfnapi_controller.go index 2e791121..18d01ef7 100644 --- a/controllers/heatcfnapi_controller.go +++ b/controllers/heatcfnapi_controller.go @@ -21,6 +21,7 @@ import ( "fmt" "time" + "github.com/go-logr/logr" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" k8s_errors "k8s.io/apimachinery/pkg/api/errors" @@ -34,7 +35,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" - "github.com/go-logr/logr" heatv1beta1 "github.com/openstack-k8s-operators/heat-operator/api/v1beta1" heat "github.com/openstack-k8s-operators/heat-operator/pkg/heat" heatcfnapi "github.com/openstack-k8s-operators/heat-operator/pkg/heatcfnapi" @@ -52,11 +52,25 @@ import ( "github.com/openstack-k8s-operators/lib-common/modules/common/util" ) +// GetClient - +func (r *HeatCfnAPIReconciler) GetClient() client.Client { + return r.Client +} + +// GetKClient - +func (r *HeatCfnAPIReconciler) GetKClient() kubernetes.Interface { + return r.Kclient +} + +// GetScheme - +func (r *HeatCfnAPIReconciler) GetScheme() *runtime.Scheme { + return r.Scheme +} + // HeatCfnAPIReconciler reconciles a Heat object type HeatCfnAPIReconciler struct { client.Client Scheme *runtime.Scheme - Log logr.Logger Kclient kubernetes.Interface } @@ -70,6 +84,11 @@ var ( } ) +// getlog returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *HeatCfnAPIReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("HeatCfnAPI") +} + // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatcfnapis,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatcfnapis/status,verbs=get;update;patch // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatcfnapis/finalizers,verbs=update @@ -91,7 +110,7 @@ var ( // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *HeatCfnAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) + Log := r.GetLogger(ctx) // TODO(bshephar): your logic here // We should have a Database and a `heat.conf` file by this time. Handled by the heat_controller. @@ -112,7 +131,7 @@ func (r *HeatCfnAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -178,8 +197,8 @@ func (r *HeatCfnAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) } // SetupWithManager sets up the controller with the Manager. -func (r *HeatCfnAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { - +func (r *HeatCfnAPIReconciler) SetupWithManager(mgr ctrl.Manager, ctx context.Context) error { + Log := r.GetLogger(ctx) configMapFn := func(o client.Object) []reconcile.Request { result := []reconcile.Request{} @@ -187,21 +206,21 @@ func (r *HeatCfnAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(o.GetNamespace()), } - if err := r.Client.List(context.Background(), apis, listOpts...); err != nil { - r.Log.Error(err, "Unable to get CfnAPI CRs %v") + if err := r.Client.List(ctx, apis, listOpts...); err != nil { + Log.Error(err, "Unable to get CfnAPI CRs") return nil } label := o.GetLabels() - if l, ok := label[labels.GetOwnerNameLabelSelector(labels.GetGroupLabel(heat.ServiceName))]; ok { + if lbl, ok := label[labels.GetOwnerNameLabelSelector(labels.GetGroupLabel(heat.ServiceName))]; ok { for _, cr := range apis.Items { - if l == heat.GetOwningHeatName(&cr) { + if lbl == heat.GetOwningHeatName(&cr) { name := client.ObjectKey{ Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info("", "ConfigMap object", o.GetName(), "and CR", cr.Name, "marked with label:", lbl) result = append(result, reconcile.Request{NamespacedName: name}) } } @@ -226,7 +245,8 @@ func (r *HeatCfnAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *HeatCfnAPIReconciler) reconcileDelete(ctx context.Context, instance *heatv1beta1.HeatCfnAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling CfnAPI Delete") + Log := r.GetLogger(ctx) + Log.Info("Reconciling CfnAPI Delete") for _, ksSvc := range keystoneCfnServices { keystoneEndpoint, err := keystonev1.GetKeystoneEndpointWithName(ctx, helper, ksSvc["name"], instance.Namespace) @@ -241,6 +261,7 @@ func (r *HeatCfnAPIReconciler) reconcileDelete(ctx context.Context, instance *he } util.LogForObject(helper, "Removed finalizer from KeystoneEndpoint", instance) } + Log.Info("Removed finalizer from KeystoneEndpoint") } keystoneService, err := keystonev1.GetKeystoneServiceWithName(ctx, helper, ksSvc["name"], instance.Namespace) @@ -255,12 +276,13 @@ func (r *HeatCfnAPIReconciler) reconcileDelete(ctx context.Context, instance *he } util.LogForObject(helper, "Removed finalizer from our KeystoneService", instance) } + Log.Info("Removed finalizer from our KeystoneService") } } // Service is deleted so remove the finalizer. controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - r.Log.Info("Reconciled CfnAPI delete successfully") + Log.Info("Reconciled CfnAPI delete successfully") return ctrl.Result{}, nil } @@ -271,7 +293,8 @@ func (r *HeatCfnAPIReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info("Reconciling CfnAPI init") + Log := r.GetLogger(ctx) + Log.Info("Reconciling CfnAPI init") // // expose the service (create service and return the created endpoint URLs) @@ -437,12 +460,13 @@ func (r *HeatCfnAPIReconciler) reconcileInit( } } - r.Log.Info("Reconciled CfnAPI init successfully") + Log.Info("Reconciled CfnAPI init successfully") return ctrl.Result{}, nil } func (r *HeatCfnAPIReconciler) reconcileNormal(ctx context.Context, instance *heatv1beta1.HeatCfnAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Service") + Log := r.GetLogger(ctx) + Log.Info("Reconciling Service") // ConfigMap configMapVars := make(map[string]env.Setter) @@ -621,27 +645,29 @@ func (r *HeatCfnAPIReconciler) reconcileNormal(ctx context.Context, instance *he } // create Deployment - end - r.Log.Info("Reconciled CfnAPI successfully") + Log.Info("Reconciled CfnAPI successfully") return ctrl.Result{}, nil } func (r *HeatCfnAPIReconciler) reconcileUpdate(ctx context.Context, instance *heatv1beta1.HeatCfnAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling CfnAPI update") + Log := r.GetLogger(ctx) + Log.Info("Reconciling CfnAPI update") // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled CfnAPI update successfully") + Log.Info("Reconciled CfnAPI update successfully") return ctrl.Result{}, nil } func (r *HeatCfnAPIReconciler) reconcileUpgrade(ctx context.Context, instance *heatv1beta1.HeatCfnAPI, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling CfnAPI upgrade") + Log := r.GetLogger(ctx) + Log.Info("Reconciling CfnAPI upgrade") // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled CfnAPI upgrade successfully") + Log.Info("Reconciled CfnAPI upgrade successfully") return ctrl.Result{}, nil } @@ -693,6 +719,8 @@ func (r *HeatCfnAPIReconciler) createHashOfInputHashes( instance *heatv1beta1.HeatCfnAPI, envVars map[string]env.Setter, ) (string, error) { + Log := r.GetLogger(ctx) + mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars) hash, err := util.ObjectHash(mergedMapVars) if err != nil { @@ -703,7 +731,7 @@ func (r *HeatCfnAPIReconciler) createHashOfInputHashes( if err := r.Client.Status().Update(ctx, instance); err != nil { return hash, err } - r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) + Log.Info("", "Input maps hash", common.InputHashName, "Hash", hash) } return hash, nil } diff --git a/controllers/heatengine_controller.go b/controllers/heatengine_controller.go index 348d23df..b6b37c8a 100644 --- a/controllers/heatengine_controller.go +++ b/controllers/heatengine_controller.go @@ -53,10 +53,14 @@ import ( type HeatEngineReconciler struct { client.Client Scheme *runtime.Scheme - Log logr.Logger Kclient kubernetes.Interface } +// getlog returns a logger object with a prefix of "conroller.name" and aditional controller context fields +func (r *HeatEngineReconciler) GetLogger(ctx context.Context) logr.Logger { + return log.FromContext(ctx).WithName("Controllers").WithName("Heat") +} + // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatengines,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatengines/status,verbs=get;update;patch // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatengines/finalizers,verbs=update @@ -72,8 +76,7 @@ type HeatEngineReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile func (r *HeatEngineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) { - _ = log.FromContext(ctx) - + Log := r.GetLogger(ctx) instance := &heatv1beta1.HeatEngine{} // Does Heat engine already exist? @@ -90,7 +93,7 @@ func (r *HeatEngineReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.Client, r.Kclient, r.Scheme, - r.Log, + Log, ) if err != nil { return ctrl.Result{}, err @@ -150,7 +153,8 @@ func (r *HeatEngineReconciler) Reconcile(ctx context.Context, req ctrl.Request) } // SetupWithManager sets up the controller with the Manager. -func (r *HeatEngineReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *HeatEngineReconciler) SetupWithManager(mgr ctrl.Manager, ctx context.Context) error { + Log := r.GetLogger(ctx) configMapFn := func(o client.Object) []reconcile.Request { result := []reconcile.Request{} @@ -159,8 +163,8 @@ func (r *HeatEngineReconciler) SetupWithManager(mgr ctrl.Manager) error { listOpts := []client.ListOption{ client.InNamespace(o.GetNamespace()), } - if err := r.Client.List(context.Background(), engines, listOpts...); err != nil { - r.Log.Error(err, "Unable to get engine CRs %v") + if err := r.Client.List(ctx, engines, listOpts...); err != nil { + Log.Error(err, "Unable to get engine CRs") return nil } @@ -173,7 +177,7 @@ func (r *HeatEngineReconciler) SetupWithManager(mgr ctrl.Manager) error { Namespace: o.GetNamespace(), Name: cr.Name, } - r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) + Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) result = append(result, reconcile.Request{NamespacedName: name}) } @@ -194,10 +198,12 @@ func (r *HeatEngineReconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *HeatEngineReconciler) reconcileDelete(ctx context.Context, instance *heatv1beta1.HeatEngine, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Engine Delete") + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Engine Delete") controllerutil.RemoveFinalizer(instance, helper.GetFinalizer()) - r.Log.Info("Reconciled Engine delete successfully") + Log.Info("Reconciled API delete successfully") return ctrl.Result{}, nil } @@ -208,11 +214,13 @@ func (r *HeatEngineReconciler) reconcileInit( helper *helper.Helper, serviceLabels map[string]string, ) (ctrl.Result, error) { - r.Log.Info("Reconciling Engine init") + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Engine init") // TODO(tkajinam): Do we need this ? - r.Log.Info("Reconciled Engine init successfully") + Log.Info("Reconciled Engine init successfully") return ctrl.Result{}, nil } @@ -220,8 +228,9 @@ func (r *HeatEngineReconciler) reconcileNormal( ctx context.Context, instance *heatv1beta1.HeatEngine, helper *helper.Helper) (ctrl.Result, error) { + Log := r.GetLogger(ctx) - r.Log.Info("Reconciling Heat Engine") + Log.Info("Reconciling Heat Engine") // TODO(bshephar) Write the reconcile logic for Heat engine. Let's just create // the deployment. We don't need to expose Heat engine, it will just talk to the @@ -394,27 +403,31 @@ func (r *HeatEngineReconciler) reconcileNormal( instance.Status.Conditions.MarkTrue(condition.DeploymentReadyCondition, condition.DeploymentReadyMessage) } - r.Log.Info("Reconciled Engine successfully") + Log.Info("Reconciled Engine successfully") return ctrl.Result{}, nil } func (r *HeatEngineReconciler) reconcileUpdate(ctx context.Context, instance *heatv1beta1.HeatEngine, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Engine update") + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Engine update") // TODO: should have minor update tasks if required // - delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled Engine update successfully") + Log.Info("Reconciled Engine update successfully") return ctrl.Result{}, nil } func (r *HeatEngineReconciler) reconcileUpgrade(ctx context.Context, instance *heatv1beta1.HeatEngine, helper *helper.Helper) (ctrl.Result, error) { - r.Log.Info("Reconciling Engine upgrade") + Log := r.GetLogger(ctx) + + Log.Info("Reconciling Engine upgrade") // TODO: should have major version upgrade tasks // -delete dbsync hash from status to rerun it? - r.Log.Info("Reconciled Engine upgrade successfully") + Log.Info("Reconciled Engine upgrade successfully") return ctrl.Result{}, nil } @@ -466,6 +479,8 @@ func (r *HeatEngineReconciler) createHashOfInputHashes( instance *heatv1beta1.HeatEngine, envVars map[string]env.Setter, ) (string, error) { + Log := r.GetLogger(ctx) + mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars) hash, err := util.ObjectHash(mergedMapVars) if err != nil { @@ -476,7 +491,7 @@ func (r *HeatEngineReconciler) createHashOfInputHashes( if err := r.Client.Status().Update(ctx, instance); err != nil { return hash, err } - r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) + Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash)) } return hash, nil } diff --git a/main.go b/main.go index abc31940..4e4f29af 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "flag" "os" "strings" @@ -120,8 +121,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Heat"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(mgr, context.Background()); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Heat") os.Exit(1) } @@ -130,8 +130,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("HeatAPI"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(mgr, context.Background()); err != nil { setupLog.Error(err, "unable to create controller", "controller", "HeatAPI") os.Exit(1) } @@ -140,8 +139,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("HeatCfnAPI"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(mgr, context.Background()); err != nil { setupLog.Error(err, "unable to create controller", "controller", "HeatCfnAPI") os.Exit(1) } @@ -150,8 +148,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("HeatEngine"), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(mgr, context.Background()); err != nil { setupLog.Error(err, "unable to create controller", "controller", "HeatEngine") os.Exit(1) } diff --git a/tests/functional/suite_test.go b/tests/functional/suite_test.go index d1c7c40e..1c966678 100644 --- a/tests/functional/suite_test.go +++ b/tests/functional/suite_test.go @@ -21,18 +21,18 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" + routev1 "github.com/openshift/api/route/v1" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + heatv1 "github.com/openstack-k8s-operators/heat-operator/api/v1beta1" - memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1" rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1" keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1" test "github.com/openstack-k8s-operators/lib-common/modules/test" mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1" "github.com/openstack-k8s-operators/heat-operator/controllers" - infra_test "github.com/openstack-k8s-operators/infra-operator/apis/test/helpers" - keystone_test "github.com/openstack-k8s-operators/keystone-operator/api/test/helpers" - common_test "github.com/openstack-k8s-operators/lib-common/modules/common/test/helpers" - mariadb_test "github.com/openstack-k8s-operators/mariadb-operator/api/test/helpers" + . "github.com/openstack-k8s-operators/lib-common/modules/test/helpers" //+kubebuilder:scaffold:imports ) @@ -46,10 +46,7 @@ var ( ctx context.Context cancel context.CancelFunc logger logr.Logger - th *common_test.TestHelper - keystone *keystone_test.TestHelper - mariadb *mariadb_test.TestHelper - infra *infra_test.TestHelper + th *TestHelper namespace string ) @@ -77,10 +74,12 @@ var _ = BeforeSuite(func() { keystoneCRDs, err := test.GetCRDDirFromModule( "github.com/openstack-k8s-operators/keystone-operator/api", "../../go.mod", "bases") Expect(err).ShouldNot(HaveOccurred()) + routev1CRDs, err := test.GetOpenShiftCRDDir("route/v1", "../../go.mod") + Expect(err).ShouldNot(HaveOccurred()) mariaDBCRDs, err := test.GetCRDDirFromModule( "github.com/openstack-k8s-operators/mariadb-operator/api", "../../go.mod", "bases") Expect(err).ShouldNot(HaveOccurred()) - infraCRDs, err := test.GetCRDDirFromModule( + rabbitmqCRDs, err := test.GetCRDDirFromModule( "github.com/openstack-k8s-operators/infra-operator/apis", "../../go.mod", "bases") Expect(err).ShouldNot(HaveOccurred()) @@ -90,7 +89,8 @@ var _ = BeforeSuite(func() { filepath.Join("..", "..", "config", "crd", "bases"), keystoneCRDs, mariaDBCRDs, - infraCRDs, + rabbitmqCRDs, + routev1CRDs, }, ErrorIfCRDPathMissing: true, WebhookInstallOptions: envtest.WebhookInstallOptions{ @@ -113,24 +113,22 @@ var _ = BeforeSuite(func() { Expect(err).NotTo(HaveOccurred()) err = mariadbv1.AddToScheme(scheme.Scheme) Expect(err).NotTo(HaveOccurred()) - err = memcachedv1.AddToScheme(scheme.Scheme) - Expect(err).NotTo(HaveOccurred()) err = rabbitmqv1.AddToScheme(scheme.Scheme) Expect(err).NotTo(HaveOccurred()) + err = corev1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + err = appsv1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + err = routev1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) //+kubebuilder:scaffold:scheme k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) Expect(err).NotTo(HaveOccurred()) Expect(k8sClient).NotTo(BeNil()) - th = common_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger) + th = NewTestHelper(ctx, k8sClient, timeout, interval, logger) Expect(th).NotTo(BeNil()) - keystone = keystone_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger) - Expect(keystone).NotTo(BeNil()) - mariadb = mariadb_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger) - Expect(mariadb).NotTo(BeNil()) - infra = infra_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger) - Expect(infra).NotTo(BeNil()) // Start the controller-manager if goroutine webhookInstallOptions := &testEnv.WebhookInstallOptions @@ -159,24 +157,21 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("Heat"), - }).SetupWithManager(k8sManager) + }).SetupWithManager(k8sManager, context.Background()) Expect(err).ToNot(HaveOccurred()) err = (&controllers.HeatAPIReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("HeatAPI"), - }).SetupWithManager(k8sManager) + }).SetupWithManager(k8sManager, context.Background()) Expect(err).ToNot(HaveOccurred()) err = (&controllers.HeatEngineReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), Kclient: kclient, - Log: ctrl.Log.WithName("controllers").WithName("HeatEngine"), - }).SetupWithManager(k8sManager) + }).SetupWithManager(k8sManager, context.Background()) Expect(err).ToNot(HaveOccurred()) go func() {