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

[release-3.11] migrate: ignore resources that cannot be listed and updated #21075

Merged
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
34 changes: 24 additions & 10 deletions pkg/oc/cli/admin/migrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,17 @@ func (o *ResourceOptions) Complete(f kcmdutil.Factory, c *cobra.Command) error {
return err
}

// if o.Include has * we need to update it via discovery and o.DefaultExcludes and o.OverlappingResources
resourceNames := sets.NewString()
for i, s := range o.Include {
if resourceNames.Has(s) {
continue
}
if s != "*" {
resourceNames.Insert(s)
break
continue
}

all, err := FindAllCanonicalResources(discoveryClient, mapper)
if err != nil {
return fmt.Errorf("could not calculate the list of available resources: %v", err)
}
exclude := sets.NewString()
for _, gr := range o.DefaultExcludes {
if len(o.OverlappingResources) > 0 {
Expand All @@ -250,7 +247,16 @@ func (o *ResourceOptions) Complete(f kcmdutil.Factory, c *cobra.Command) error {
}
exclude.Insert(gr.String())
}

candidate := sets.NewString()

// keep this logic as close to the point of use as possible so that we limit our dependency on discovery
// since discovery is cached this does not repeatedly call out to the API
all, err := FindAllCanonicalResources(discoveryClient, mapper)
if err != nil {
return fmt.Errorf("could not calculate the list of available resources: %v", err)
}

for _, gr := range all {
// if the user specifies a resource that matches resource or resource+group, skip it
if resourceNames.Has(gr.Resource) || resourceNames.Has(gr.String()) || exclude.Has(gr.String()) {
Expand Down Expand Up @@ -719,18 +725,21 @@ func DefaultRetriable(info *resource.Info, err error) error {
}
}

// FindAllCanonicalResources returns all resource names that map directly to their kind (Kind -> Resource -> Kind)
// and are not subresources. This is the closest mapping possible from the client side to resources that can be
// listed and updated. Note that this may return some virtual resources (like imagestreamtags) that can be otherwise
// represented.
// FindAllCanonicalResources returns all resources that:
// 1. map directly to their kind (Kind -> Resource -> Kind)
// 2. are not subresources
// 3. can be listed and updated
// Note that this may return some virtual resources (like imagestreamtags) that can be otherwise represented.
// TODO: add a field to APIResources for "virtual" (or that points to the canonical resource).
// TODO: fallback to the scheme when discovery is not possible.
func FindAllCanonicalResources(d discovery.ServerResourcesInterface, m meta.RESTMapper) ([]schema.GroupResource, error) {
set := make(map[schema.GroupResource]struct{})

// this call doesn't fail on aggregated apiserver failures
all, err := d.ServerResources()
if err != nil {
return nil, err
}

for _, serverResource := range all {
gv, err := schema.ParseGroupVersion(serverResource.GroupVersion)
if err != nil {
Expand All @@ -741,6 +750,10 @@ func FindAllCanonicalResources(d discovery.ServerResourcesInterface, m meta.REST
if strings.Contains(r.Name, "/") {
continue
}
// ignore resources that cannot be listed and updated
if !sets.NewString(r.Verbs...).HasAll("list", "update") {
continue
}
// because discovery info doesn't tell us whether the object is virtual or not, perform a lookup
// by the kind for resource (which should be the canonical resource) and then verify that the reverse
// lookup (KindsFor) does not error.
Expand All @@ -751,6 +764,7 @@ func FindAllCanonicalResources(d discovery.ServerResourcesInterface, m meta.REST
}
}
}

var groupResources []schema.GroupResource
for k := range set {
groupResources = append(groupResources, k)
Expand Down