Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle NotFound errors during migration #17080

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions pkg/oc/admin/migrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"strings"
"time"

"github.com/golang/glog"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,6 +50,11 @@ func AlwaysRequiresMigration(_ *resource.Info) (Reporter, error) {
return ReporterBool(true), nil
}

// timeStampNow returns the current time in the same format as glog
func timeStampNow() string {
return time.Now().Format("0102 15:04:05.000000")
}

// NotChanged is a Reporter returned by operations that are guaranteed to be read-only
var NotChanged = ReporterBool(false)

Expand Down Expand Up @@ -397,9 +403,9 @@ func (t *migrateTracker) report(prefix string, info *resource.Info, err error) {
ns = "-n " + ns
}
if err != nil {
fmt.Fprintf(t.out, "%-10s %s %s/%s: %v\n", prefix, ns, info.Mapping.Resource, info.Name, err)
fmt.Fprintf(t.out, "E%s %-10s %s %s/%s: %v\n", timeStampNow(), prefix, ns, info.Mapping.Resource, info.Name, err)
} else {
fmt.Fprintf(t.out, "%-10s %s %s/%s\n", prefix, ns, info.Mapping.Resource, info.Name)
fmt.Fprintf(t.out, "I%s %-10s %s %s/%s\n", timeStampNow(), prefix, ns, info.Mapping.Resource, info.Name)
}
}

Expand Down Expand Up @@ -484,11 +490,13 @@ func canRetry(err error) bool {
// All other errors are left in their natural state - they will not be retried unless
// they define a Temporary() method that returns true.
func DefaultRetriable(info *resource.Info, err error) error {
// tolerate the deletion of resources during migration
if err == nil || isNotFoundForInfo(info, err) {
return nil
}
switch {
case err == nil:
return nil
case isNotFoundForInfo(info, err):
// tolerate the deletion of resources during migration
// report unchanged since we did not actually migrate this object
return ErrUnchanged
case errors.IsMethodNotSupported(err):
return ErrNotRetriable{err}
case errors.IsConflict(err):
Expand All @@ -498,8 +506,9 @@ func DefaultRetriable(info *resource.Info, err error) error {
return ErrRetriable{err}
case errors.IsServerTimeout(err):
return ErrRetriable{err}
default:
return err
}
return err
}

// isNotFoundForInfo returns true iff the error is a not found for the specific info object.
Expand All @@ -515,6 +524,11 @@ func isNotFoundForInfo(info *resource.Info, err error) bool {
if details == nil {
return false
}
gvk := info.Object.GetObjectKind().GroupVersionKind()
return details.Name == info.Name && details.Kind == gvk.Kind && (details.Group == "" || details.Group == gvk.Group)
// get schema.GroupKind from the mapping since the actual object may not have type meta filled out
gk := info.Mapping.GroupVersionKind.GroupKind()
// based on case-insensitive string comparisons, the error matches info iff
// the name and kind match
// the group match, but only if both the error and info specify a group
return strings.EqualFold(details.Name, info.Name) && strings.EqualFold(details.Kind, gk.Kind) &&
(len(details.Group) == 0 || len(gk.Group) == 0 || strings.EqualFold(details.Group, gk.Group))
}
Loading