Skip to content

Commit

Permalink
Add an ability to print Openstack API debug logs (#72)
Browse files Browse the repository at this point in the history
- Adds functionality to write logs with higher severity than info
- Print Openstack API debug logs when velero runs with `--log-level=debug`
  • Loading branch information
kayrus authored Jun 27, 2023
1 parent a7f6700 commit 3043314
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"github.com/Lirt/velero-plugin-for-openstack/src/cinder"
"github.com/Lirt/velero-plugin-for-openstack/src/swift"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
veleroplugin "github.com/vmware-tanzu/velero/pkg/plugin/framework"
)

func main() {
veleroplugin.NewServer().
BindFlags(pflag.CommandLine).
RegisterObjectStore("community.openstack.org/openstack", newSwiftObjectStore).
RegisterVolumeSnapshotter("community.openstack.org/openstack", newCinderBlockStore).
Serve()
Expand Down
38 changes: 35 additions & 3 deletions src/utils/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ import (
"strconv"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/utils/client"
"github.com/gophercloud/utils/openstack/clientconfig"
"github.com/sirupsen/logrus"
)

// osDebugger satisfies the client.Logger interface to print debug API logs
type osDebugger struct {
log logrus.FieldLogger
}

func (d osDebugger) Printf(format string, args ...interface{}) {
d.log.Debugf(format, args...)
}

// Authenticate to OpenStack and write client result to **pc
func Authenticate(pc **gophercloud.ProviderClient, service string, config map[string]string, log logrus.FieldLogger) error {
var err error
Expand Down Expand Up @@ -68,16 +79,37 @@ func Authenticate(pc **gophercloud.ProviderClient, service string, config map[st
tlsConfig := &tls.Config{InsecureSkipVerify: tlsVerify}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = tlsConfig
clientOpts.HTTPClient = &http.Client{Transport: transport}

*pc, err = clientconfig.AuthenticatedClient(&clientOpts)
ao, err := clientconfig.AuthOptions(&clientOpts)
if err != nil {
return fmt.Errorf("failed to build auth options: %w", err)
}

*pc, err = openstack.NewClient(ao.IdentityEndpoint)
if err != nil {
return err
return fmt.Errorf("failed to create a provider: %w", err)
}
(*pc).HTTPClient.Transport = transport

// enable API debug logs
if log, ok := log.(*logrus.Logger); ok && log.IsLevelEnabled(logrus.DebugLevel) {
(*pc).HTTPClient.Transport = &client.RoundTripper{
Rt: transport,
Logger: osDebugger{log.WithFields(logrus.Fields{
"source": "openstack",
"component": service,
})},
}
}

// set user agent with a version
(*pc).UserAgent.Prepend("velero-plugin-for-openstack/" + Version + "@" + GitSHA)

err = openstack.Authenticate(*pc, *ao)
if err != nil {
return fmt.Errorf("failed to authenticate: %w", err)
}

log.Infof("Authentication against identity endpoint %v was successful", (*pc).IdentityEndpoint)

return nil
Expand Down

0 comments on commit 3043314

Please sign in to comment.