// ServerPreferredNamespacedGroupVersionResources uses the specified client to discover the set of preferred groupVersionResources that are namespaced
func ServerPreferredNamespacedGroupVersionResources(discoveryClient discovery.DiscoveryInterface) ([]unversioned.GroupVersionResource, error) {
	results := []unversioned.GroupVersionResource{}
	serverGroupList, err := discoveryClient.ServerGroups()
	if err != nil {
		return results, err
	}

	allErrs := []error{}
	for _, apiGroup := range serverGroupList.Groups {
		preferredVersion := apiGroup.PreferredVersion
		apiResourceList, err := discoveryClient.ServerResourcesForGroupVersion(preferredVersion.GroupVersion)
		if err != nil {
			allErrs = append(allErrs, err)
			continue
		}
		groupVersion := unversioned.GroupVersion{Group: apiGroup.Name, Version: preferredVersion.Version}
		for _, apiResource := range apiResourceList.APIResources {
			if !apiResource.Namespaced {
				continue
			}
			if strings.Contains(apiResource.Name, "/") {
				continue
			}
			results = append(results, groupVersion.WithResource(apiResource.Name))
		}
	}
	return results, utilerrors.NewAggregate(allErrs)
}
Exemple #2
0
// GetThirdPartyGroupVersions returns the thirdparty "group/versions"s and
// resources supported by the server. A user may delete a thirdparty resource
// when this function is running, so this function may return a "NotFound" error
// due to the race.
func GetThirdPartyGroupVersions(discovery discovery.DiscoveryInterface) ([]unversioned.GroupVersion, []unversioned.GroupVersionKind, error) {
	result := []unversioned.GroupVersion{}
	gvks := []unversioned.GroupVersionKind{}

	groupList, err := discovery.ServerGroups()
	if err != nil {
		// On forbidden or not found, just return empty lists.
		if kerrors.IsForbidden(err) || kerrors.IsNotFound(err) {
			return result, gvks, nil
		}

		return nil, nil, err
	}

	for ix := range groupList.Groups {
		group := &groupList.Groups[ix]
		for jx := range group.Versions {
			gv, err2 := unversioned.ParseGroupVersion(group.Versions[jx].GroupVersion)
			if err2 != nil {
				return nil, nil, err
			}
			// Skip GroupVersionKinds that have been statically registered.
			if registered.IsRegisteredVersion(gv) {
				continue
			}
			result = append(result, gv)

			resourceList, err := discovery.ServerResourcesForGroupVersion(group.Versions[jx].GroupVersion)
			if err != nil {
				return nil, nil, err
			}
			for kx := range resourceList.APIResources {
				gvks = append(gvks, gv.WithKind(resourceList.APIResources[kx].Kind))
			}
		}
	}
	return result, gvks, nil
}