diff --git a/controllers/openstackcluster_controller.go b/controllers/openstackcluster_controller.go index 9aa71f92d6..8dcb6133e0 100644 --- a/controllers/openstackcluster_controller.go +++ b/controllers/openstackcluster_controller.go @@ -120,7 +120,7 @@ func (r *OpenStackClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req func reconcileDelete(ctx context.Context, log logr.Logger, client client.Client, patchHelper *patch.Helper, cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster) (ctrl.Result, error) { log.Info("Reconciling Cluster delete") - osProviderClient, clientOpts, err := provider.NewClientFromCluster(client, openStackCluster) + osProviderClient, clientOpts, err := provider.NewClientFromCluster(ctx, client, openStackCluster) if err != nil { return reconcile.Result{}, err } @@ -230,7 +230,7 @@ func reconcileNormal(ctx context.Context, log logr.Logger, client client.Client, return reconcile.Result{}, err } - osProviderClient, clientOpts, err := provider.NewClientFromCluster(client, openStackCluster) + osProviderClient, clientOpts, err := provider.NewClientFromCluster(ctx, client, openStackCluster) if err != nil { return reconcile.Result{}, err } diff --git a/controllers/openstackmachine_controller.go b/controllers/openstackmachine_controller.go index 6b2fe4d01a..ec11c5ce3e 100644 --- a/controllers/openstackmachine_controller.go +++ b/controllers/openstackmachine_controller.go @@ -172,12 +172,12 @@ func (r *OpenStackMachineReconciler) SetupWithManager(ctx context.Context, mgr c ). Watches( &source.Kind{Type: &infrav1.OpenStackCluster{}}, - handler.EnqueueRequestsFromMapFunc(r.OpenStackClusterToOpenStackMachines(ctrl.LoggerFrom(ctx))), + handler.EnqueueRequestsFromMapFunc(r.OpenStackClusterToOpenStackMachines(ctx)), ). WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)). Watches( &source.Kind{Type: &clusterv1.Cluster{}}, - handler.EnqueueRequestsFromMapFunc(r.requeueOpenStackMachinesForUnpausedCluster(ctrl.LoggerFrom(ctx))), + handler.EnqueueRequestsFromMapFunc(r.requeueOpenStackMachinesForUnpausedCluster(ctx)), builder.WithPredicates(predicates.ClusterUnpausedAndInfrastructureReady(ctrl.LoggerFrom(ctx))), ). Complete(r) @@ -188,7 +188,7 @@ func (r *OpenStackMachineReconciler) reconcileDelete(ctx context.Context, logger clusterName := fmt.Sprintf("%s-%s", cluster.ObjectMeta.Namespace, cluster.Name) - osProviderClient, clientOpts, err := provider.NewClientFromMachine(r.Client, openStackMachine) + osProviderClient, clientOpts, err := provider.NewClientFromMachine(ctx, r.Client, openStackMachine) if err != nil { return ctrl.Result{}, err } @@ -282,7 +282,7 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, logger clusterName := fmt.Sprintf("%s-%s", cluster.ObjectMeta.Namespace, cluster.Name) - osProviderClient, clientOpts, err := provider.NewClientFromMachine(r.Client, openStackMachine) + osProviderClient, clientOpts, err := provider.NewClientFromMachine(ctx, r.Client, openStackMachine) if err != nil { return ctrl.Result{}, err } @@ -399,7 +399,8 @@ func (r *OpenStackMachineReconciler) reconcileLoadBalancerMember(logger logr.Log // OpenStackClusterToOpenStackMachine is a handler.ToRequestsFunc to be used to enqeue requests for reconciliation // of OpenStackMachines. -func (r *OpenStackMachineReconciler) OpenStackClusterToOpenStackMachines(log logr.Logger) handler.MapFunc { +func (r *OpenStackMachineReconciler) OpenStackClusterToOpenStackMachines(ctx context.Context) handler.MapFunc { + log := ctrl.LoggerFrom(ctx) return func(o client.Object) []ctrl.Request { c, ok := o.(*infrav1.OpenStackCluster) if !ok { @@ -447,7 +448,8 @@ func (r *OpenStackMachineReconciler) getBootstrapData(machine *clusterv1.Machine return base64.StdEncoding.EncodeToString(value), nil } -func (r *OpenStackMachineReconciler) requeueOpenStackMachinesForUnpausedCluster(log logr.Logger) handler.MapFunc { +func (r *OpenStackMachineReconciler) requeueOpenStackMachinesForUnpausedCluster(ctx context.Context) handler.MapFunc { + log := ctrl.LoggerFrom(ctx) return func(o client.Object) []ctrl.Request { c, ok := o.(*clusterv1.Cluster) if !ok { diff --git a/pkg/cloud/services/provider/provider.go b/pkg/cloud/services/provider/provider.go index 45b18c0d05..5833a0b28a 100644 --- a/pkg/cloud/services/provider/provider.go +++ b/pkg/cloud/services/provider/provider.go @@ -25,9 +25,11 @@ import ( "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack" + osclient "github.com/gophercloud/utils/client" "github.com/gophercloud/utils/openstack/clientconfig" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" + "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/yaml" @@ -39,7 +41,7 @@ const ( CaSecretKey = "cacert" ) -func NewClientFromMachine(ctrlClient client.Client, openStackMachine *infrav1.OpenStackMachine) (*gophercloud.ProviderClient, *clientconfig.ClientOpts, error) { +func NewClientFromMachine(ctx context.Context, ctrlClient client.Client, openStackMachine *infrav1.OpenStackMachine) (*gophercloud.ProviderClient, *clientconfig.ClientOpts, error) { var cloud clientconfig.Cloud var caCert []byte @@ -49,7 +51,7 @@ func NewClientFromMachine(ctrlClient client.Client, openStackMachine *infrav1.Op namespace = openStackMachine.Namespace } var err error - cloud, caCert, err = getCloudFromSecret(ctrlClient, namespace, openStackMachine.Spec.CloudsSecret.Name, openStackMachine.Spec.CloudName) + cloud, caCert, err = getCloudFromSecret(ctx, ctrlClient, namespace, openStackMachine.Spec.CloudsSecret.Name, openStackMachine.Spec.CloudName) if err != nil { return nil, nil, err } @@ -57,7 +59,7 @@ func NewClientFromMachine(ctrlClient client.Client, openStackMachine *infrav1.Op return NewClient(cloud, caCert) } -func NewClientFromCluster(ctrlClient client.Client, openStackCluster *infrav1.OpenStackCluster) (*gophercloud.ProviderClient, *clientconfig.ClientOpts, error) { +func NewClientFromCluster(ctx context.Context, ctrlClient client.Client, openStackCluster *infrav1.OpenStackCluster) (*gophercloud.ProviderClient, *clientconfig.ClientOpts, error) { var cloud clientconfig.Cloud var caCert []byte @@ -67,7 +69,7 @@ func NewClientFromCluster(ctrlClient client.Client, openStackCluster *infrav1.Op namespace = openStackCluster.Namespace } var err error - cloud, caCert, err = getCloudFromSecret(ctrlClient, namespace, openStackCluster.Spec.CloudsSecret.Name, openStackCluster.Spec.CloudName) + cloud, caCert, err = getCloudFromSecret(ctx, ctrlClient, namespace, openStackCluster.Spec.CloudsSecret.Name, openStackCluster.Spec.CloudName) if err != nil { return nil, nil, err } @@ -106,6 +108,12 @@ func NewClient(cloud clientconfig.Cloud, caCert []byte) (*gophercloud.ProviderCl } provider.HTTPClient.Transport = &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: config} + if klog.V(6).Enabled() { + provider.HTTPClient.Transport = &osclient.RoundTripper{ + Rt: provider.HTTPClient.Transport, + Logger: &defaultLogger{}, + } + } err = openstack.Authenticate(provider, *opts) if err != nil { return nil, nil, fmt.Errorf("providerClient authentication err: %v", err) @@ -113,9 +121,15 @@ func NewClient(cloud clientconfig.Cloud, caCert []byte) (*gophercloud.ProviderCl return provider, clientOpts, nil } +type defaultLogger struct{} + +// Printf is a default Printf method. +func (defaultLogger) Printf(format string, args ...interface{}) { + klog.V(6).Infof(format, args...) +} + // getCloudFromSecret extract a Cloud from the given namespace:secretName. -func getCloudFromSecret(ctrlClient client.Client, secretNamespace string, secretName string, cloudName string) (clientconfig.Cloud, []byte, error) { - ctx := context.TODO() +func getCloudFromSecret(ctx context.Context, ctrlClient client.Client, secretNamespace string, secretName string, cloudName string) (clientconfig.Cloud, []byte, error) { emptyCloud := clientconfig.Cloud{} if secretName == "" {