Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#564 from TaoZou1/externallogger
Browse files Browse the repository at this point in the history
Support external logger for clean
  • Loading branch information
TaoZou1 authored May 8, 2024
2 parents 62adb8f + 9298b76 commit b66d024
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 2 additions & 5 deletions cmd_clean/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"os"
"time"

logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/vmware-tanzu/nsx-operator/pkg/clean"
"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/logger"
Expand Down Expand Up @@ -78,11 +76,10 @@ func main() {
cf.EnvoyHost = envoyHost
cf.EnvoyPort = envoyPort

logf.SetLogger(logger.ZapLogger(cf.DefaultConfig.Debug, config.LogLevel))
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

err := clean.Clean(ctx, cf)
log = logger.ZapLogger(cf.DefaultConfig.Debug, config.LogLevel)
err := clean.Clean(ctx, cf, &log, cf.DefaultConfig.Debug, config.LogLevel)
if err != nil {
log.Error(err, "failed to clean nsx resources")
os.Exit(1)
Expand Down
15 changes: 11 additions & 4 deletions pkg/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"fmt"
"time"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"

"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/logger"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
Expand All @@ -23,6 +20,11 @@ import (
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnetport"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/vpc"
nsxutil "github.com/vmware-tanzu/nsx-operator/pkg/nsx/util"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

var log = logger.Log
Expand All @@ -44,7 +46,12 @@ var Backoff = wait.Backoff{
// GetNSXClientFailed indicate that could not retrieve nsx client to perform cleanup operation
// InitCleanupServiceFailed indicate that error happened when trying to initialize cleanup service
// CleanupResourceFailed indicate that the cleanup operation failed at some services, the detailed will in the service logs
func Clean(ctx context.Context, cf *config.NSXOperatorConfig) error {
func Clean(ctx context.Context, cf *config.NSXOperatorConfig, logg *logr.Logger, debug bool, logLevel int) error {
if logg != nil {
logf.SetLogger(*logg)
} else {
logf.SetLogger(logger.ZapLogger(debug, logLevel))
}
log.Info("starting NSX cleanup")
if err := cf.ValidateConfigFromCmd(); err != nil {
return errors.Join(nsxutil.ValidationFailed, err)
Expand Down

0 comments on commit b66d024

Please sign in to comment.