Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#497 from seanpang-vmware/errortype
Browse files Browse the repository at this point in the history
Use error type instead of error code in cleanup
  • Loading branch information
seanpang-vmware authored Jan 24, 2024
2 parents 2207ee2 + 9accc65 commit d77ed9b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cmd_clean/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func main() {

logf.SetLogger(logger.ZapLogger(cf.DefaultConfig.Debug, config.LogLevel))

status, err := clean.Clean(cf)
err := clean.Clean(cf)
if err != nil {
log.Error(err, "failed to clean nsx resources", "status", status)
log.Error(err, "failed to clean nsx resources", "status", err.Error())
os.Exit(1)
}
os.Exit(0)
Expand Down
20 changes: 13 additions & 7 deletions pkg/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package clean

import (
"errors"
"fmt"

"k8s.io/client-go/util/retry"
Expand All @@ -27,19 +28,24 @@ var log = logger.Log
// including security policy, static route, subnet, subnet port, subnet set, vpc, ip pool, nsx service account
// it is usually used when nsx-operator is uninstalled and remove all the resources created by nsx-operator
// return error if any, return nil if no error
func Clean(cf *config.NSXOperatorConfig) (Status, error) {
// the error type include followings:
// ValidationFailed indicate that the config is incorrect and failed to pass validation
// 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(cf *config.NSXOperatorConfig) error {
log.Info("starting NSX cleanup")
if err := cf.ValidateConfigFromCmd(); err != nil {
return ValidationFailed, err
return errors.Join(ValidationFailed, err)
}
nsxClient := nsx.GetClient(cf)
if nsxClient == nil {
return GetNSXClientFailed, fmt.Errorf("failed to get nsx client")
return GetNSXClientFailed
}
if cleanupService, err := InitializeCleanupService(cf); err != nil {
return InitCleanupServiceFailed, fmt.Errorf("failed to initialize cleanup service: %w", err)
return errors.Join(InitCleanupServiceFailed, err)
} else if cleanupService.err != nil {
return InitCleanupServiceFailed, cleanupService.err
return errors.Join(InitCleanupServiceFailed, cleanupService.err)
} else {
for _, clean := range cleanupService.cleans {
if err := retry.OnError(retry.DefaultRetry, func(err error) bool {
Expand All @@ -54,12 +60,12 @@ func Clean(cf *config.NSXOperatorConfig) (Status, error) {
}
return nil
}); err != nil {
return CleanupResourceFailed, err
return errors.Join(CleanupResourceFailed, err)
}
}
}
log.Info("cleanup NSX resources successfully")
return OK, nil
return nil
}

// InitializeCleanupService initializes all the CR services
Expand Down
5 changes: 4 additions & 1 deletion pkg/clean/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ type Status struct {
Message string
}

func (s Status) Error() string {
return s.Message
}

var (
OK = Status{Code: 0, Message: "cleanup successfully"}
ValidationFailed = Status{Code: 1, Message: "failed to validate config"}
GetNSXClientFailed = Status{Code: 2, Message: "failed to get nsx client"}
InitCleanupServiceFailed = Status{Code: 3, Message: "failed to initialize cleanup service"}
Expand Down

0 comments on commit d77ed9b

Please sign in to comment.