Skip to content

Commit

Permalink
Add trace log for gophercloud client, fix contexts
Browse files Browse the repository at this point in the history
This activates the default logging for the gophercloud client,
when setting the log level to 6.
This adapts some contexts.

Signed-off-by: Sean Schneeweiss <[email protected]>
  • Loading branch information
seanschneeweiss committed May 7, 2021
1 parent d520355 commit cdb8936
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 8 additions & 6 deletions controllers/openstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 20 additions & 6 deletions pkg/cloud/services/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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

Expand All @@ -49,15 +51,15 @@ 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
}
}
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

Expand All @@ -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
}
Expand Down Expand Up @@ -106,16 +108,28 @@ 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)
}
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 == "" {
Expand Down

0 comments on commit cdb8936

Please sign in to comment.