|
| 1 | +/* |
| 2 | +Copyright 2025 The KCP Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package dynamicrestmapper |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/apimachinery/pkg/api/meta" |
| 21 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 22 | +) |
| 23 | + |
| 24 | +// This file adds mutable methods to our fork of upstream's DefaultRESTMapper. |
| 25 | + |
| 26 | +func (m *DefaultRESTMapper) empty() bool { |
| 27 | + // If one of the maps is empty, all of the maps are empty. |
| 28 | + return len(m.resourceToKind) == 0 |
| 29 | +} |
| 30 | + |
| 31 | +func (m *DefaultRESTMapper) add(typeMeta typeMeta) { |
| 32 | + kind := typeMeta.groupVersionKind() |
| 33 | + singular := typeMeta.groupVersionResourceSingular() |
| 34 | + plural := typeMeta.groupVersionResourcePlural() |
| 35 | + |
| 36 | + m.singularToPlural[singular] = plural |
| 37 | + m.pluralToSingular[plural] = singular |
| 38 | + |
| 39 | + m.resourceToKind[singular] = kind |
| 40 | + m.resourceToKind[plural] = kind |
| 41 | + |
| 42 | + m.kindToPluralResource[kind] = plural |
| 43 | + m.kindToScope[kind] = meta.RESTScopeRoot |
| 44 | +} |
| 45 | + |
| 46 | +func (m *DefaultRESTMapper) remove(typeMeta typeMeta) { |
| 47 | + kind := typeMeta.groupVersionKind() |
| 48 | + singular := typeMeta.groupVersionResourceSingular() |
| 49 | + plural := typeMeta.groupVersionResourcePlural() |
| 50 | + |
| 51 | + delete(m.singularToPlural, singular) |
| 52 | + delete(m.pluralToSingular, plural) |
| 53 | + |
| 54 | + delete(m.resourceToKind, singular) |
| 55 | + delete(m.resourceToKind, plural) |
| 56 | + |
| 57 | + delete(m.kindToPluralResource, kind) |
| 58 | + delete(m.kindToScope, kind) |
| 59 | +} |
| 60 | + |
| 61 | +func (m *DefaultRESTMapper) getGVKR(gvr schema.GroupVersionResource) typeMeta { |
| 62 | + kind := m.resourceToKind[gvr] |
| 63 | + singular := m.pluralToSingular[gvr] |
| 64 | + if singular.Empty() { |
| 65 | + singular = gvr |
| 66 | + } |
| 67 | + plural := m.singularToPlural[gvr] |
| 68 | + if plural.Empty() { |
| 69 | + plural = gvr |
| 70 | + } |
| 71 | + scope := m.kindToScope[kind] |
| 72 | + |
| 73 | + return newTypeMeta( |
| 74 | + kind.Group, |
| 75 | + kind.Version, |
| 76 | + kind.Kind, |
| 77 | + singular.Resource, |
| 78 | + plural.Resource, |
| 79 | + scope, |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +func (m *DefaultRESTMapper) getGVKRs(gr schema.GroupResource) ([]typeMeta, error) { |
| 84 | + gvrs, err := m.ResourcesFor(gr.WithVersion("")) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + gvkrs := make([]typeMeta, len(gvrs)) |
| 89 | + for i := range gvrs { |
| 90 | + gvkrs[i] = m.getGVKR(gvrs[i]) |
| 91 | + } |
| 92 | + return gvkrs, nil |
| 93 | +} |
| 94 | + |
| 95 | +// Applies the two slices to the known mappings. It is assumed there is |
| 96 | +// no overlap between toRemove and toAdd. |
| 97 | +func (m *DefaultRESTMapper) apply(toRemove []typeMeta, toAdd []typeMeta) { |
| 98 | + for i := range toRemove { |
| 99 | + m.remove(toRemove[i]) |
| 100 | + } |
| 101 | + |
| 102 | + for i := range toAdd { |
| 103 | + m.add(toAdd[i]) |
| 104 | + } |
| 105 | +} |
0 commit comments