~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/socialweb-computer-console

console.socialweb.computer: kcp, kubectl and ssh compiled to wasm in a browser tab with a terminal; a kcp fork

0
0
main
76 lines · 2.5 KB Raw History
/*
Copyright 2026 The kcp Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package dynamicrestmapper

import (
	"slices"
	"strings"

	"k8s.io/apimachinery/pkg/api/meta"
	"k8s.io/apimachinery/pkg/runtime/schema"
)

// This file adds version-fallback lookups on top of our fork of upstream's
// DefaultRESTMapper. The fork (defaultrestmapper_kcp.go) is generated by
// hack/gen-patch-defaultrestmapper.sh and must not be modified by hand.

// restMappingsWithFallback behaves like RESTMappings, except when no version
// is requested and the lookup fails: defaultGroupVersions holds a single,
// highest version per group, so a kind that is not served in that version
// would not be found at all -- e.g. apis.kcp.io defaults to v1alpha2 because
// of APIExport, while APIExportEndpointSlice only exists in v1alpha1. Fall
// back to every version of the group that does serve the kind, preferring
// the newest.
func (m *DefaultRESTMapper) restMappingsWithFallback(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
	mappings, err := m.RESTMappings(gk, versions...)
	if err == nil || len(versions) > 0 {
		return mappings, err
	}

	var fallback []string
	for gvk := range m.kindToPluralResource {
		if gvk.Group == gk.Group && gvk.Kind == gk.Kind {
			fallback = append(fallback, gvk.Version)
		}
	}
	if len(fallback) == 0 {
		return nil, err
	}
	slices.SortFunc(fallback, func(a, b string) int {
		return strings.Compare(b, a)
	})

	mappings = nil
	for _, version := range fallback {
		versionMappings, err := m.RESTMappings(gk, version)
		if err != nil {
			return nil, err
		}
		mappings = append(mappings, versionMappings...)
	}
	return mappings, nil
}

// restMappingWithFallback behaves like RESTMapping, with the same version
// fallback as restMappingsWithFallback.
func (m *DefaultRESTMapper) restMappingWithFallback(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
	mappings, err := m.restMappingsWithFallback(gk, versions...)
	if err != nil {
		return nil, err
	}
	return mappings[0], nil
}