func (u *UnstructuredList) GroupVersionKind() schema.GroupVersionKind { gv, err := schema.ParseGroupVersion(u.GetAPIVersion()) if err != nil { return schema.GroupVersionKind{} } gvk := gv.WithKind(u.GetKind()) return gvk }
func (obj objectAccessor) SetAPIVersion(version string) { gvk := obj.GetObjectKind().GroupVersionKind() gv, err := schema.ParseGroupVersion(version) if err != nil { gv = schema.GroupVersion{Version: version} } gvk.Group, gvk.Version = gv.Group, gv.Version obj.GetObjectKind().SetGroupVersionKind(gvk) }
func (gv *GroupVersion) unmarshal(value []byte) error { var s string if err := json.Unmarshal(value, &s); err != nil { return err } parsed, err := schema.ParseGroupVersion(s) if err != nil { return err } gv.Group, gv.Version = parsed.Group, parsed.Version return nil }
func (m PriorityRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (mapping *RESTMapping, err error) { mappings, err := m.Delegate.RESTMappings(gk) if err != nil { return nil, err } // any versions the user provides take priority priorities := m.KindPriority if len(versions) > 0 { priorities = make([]schema.GroupVersionKind, 0, len(m.KindPriority)+len(versions)) for _, version := range versions { gv, err := schema.ParseGroupVersion(version) if err != nil { return nil, err } priorities = append(priorities, gv.WithKind(AnyKind)) } priorities = append(priorities, m.KindPriority...) } remaining := append([]*RESTMapping{}, mappings...) for _, pattern := range priorities { var matching []*RESTMapping for _, m := range remaining { if kindMatches(pattern, m.GroupVersionKind) { matching = append(matching, m) } } switch len(matching) { case 0: // if you have no matches, then nothing matched this pattern just move to the next continue case 1: // one match, return return matching[0], nil default: // more than one match, use the matched hits as the list moving to the next pattern. // this way you can have a series of selection criteria remaining = matching } } if len(remaining) == 1 { return remaining[0], nil } var kinds []schema.GroupVersionKind for _, m := range mappings { kinds = append(kinds, m.GroupVersionKind) } return nil, &AmbiguousKindError{PartialKind: gk.WithVersion(""), MatchingKinds: kinds} }
// GroupVersionResources converts APIResourceLists to the GroupVersionResources. func GroupVersionResources(rls []*metav1.APIResourceList) (map[schema.GroupVersionResource]struct{}, error) { gvrs := map[schema.GroupVersionResource]struct{}{} for _, rl := range rls { gv, err := schema.ParseGroupVersion(rl.GroupVersion) if err != nil { return nil, err } for i := range rl.APIResources { gvrs[schema.GroupVersionResource{Group: gv.Group, Version: gv.Version, Resource: rl.APIResources[i].Name}] = struct{}{} } } return gvrs, nil }
// NewObjectTyper constructs an ObjectTyper from discovery information. func NewObjectTyper(resources []*metav1.APIResourceList) (runtime.ObjectTyper, error) { ot := &ObjectTyper{registered: make(map[schema.GroupVersionKind]bool)} for _, resourceList := range resources { gv, err := schema.ParseGroupVersion(resourceList.GroupVersion) if err != nil { return nil, err } for _, resource := range resourceList.APIResources { ot.registered[gv.WithKind(resource.Kind)] = true } } return ot, nil }
// Interpret will return the APIVersion and Kind of the JSON wire-format // encoding of an object, or an error. func (SimpleMetaFactory) Interpret(data []byte) (*schema.GroupVersionKind, error) { findKind := struct { // +optional APIVersion string `json:"apiVersion,omitempty"` // +optional Kind string `json:"kind,omitempty"` }{} if err := json.Unmarshal(data, &findKind); err != nil { return nil, fmt.Errorf("couldn't get version/kind; json parse error: %v", err) } gv, err := schema.ParseGroupVersion(findKind.APIVersion) if err != nil { return nil, err } return &schema.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: findKind.Kind}, nil }
// NewDiscoveryRESTMapper returns a RESTMapper based on discovery information. func NewDiscoveryRESTMapper(resources []*metav1.APIResourceList, versionFunc meta.VersionInterfacesFunc) (*meta.DefaultRESTMapper, error) { rm := meta.NewDefaultRESTMapper(nil, versionFunc) for _, resourceList := range resources { gv, err := schema.ParseGroupVersion(resourceList.GroupVersion) if err != nil { return nil, err } for _, resource := range resourceList.APIResources { gvk := gv.WithKind(resource.Kind) scope := meta.RESTScopeRoot if resource.Namespaced { scope = meta.RESTScopeNamespace } rm.Add(gvk, scope) } } return rm, nil }
func setConfigDefaults(config *rest.Config) error { gv, err := schema.ParseGroupVersion("authorization.k8s.io/v1beta1") if err != nil { return err } // if authorization.k8s.io/v1beta1 is not enabled, return an error if !registered.IsEnabledVersion(gv) { return fmt.Errorf("authorization.k8s.io/v1beta1 is not enabled") } config.APIPath = "/apis" if config.UserAgent == "" { config.UserAgent = rest.DefaultKubernetesUserAgent() } copyGroupVersion := gv config.GroupVersion = ©GroupVersion config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: api.Codecs} return nil }
// NewAPIRegistrationManager constructs a new manager. The argument ought to be // the value of the KUBE_API_VERSIONS env var, or a value of this which you // wish to test. func NewAPIRegistrationManager(kubeAPIVersions string) (*APIRegistrationManager, error) { m := &APIRegistrationManager{ registeredVersions: map[schema.GroupVersion]struct{}{}, thirdPartyGroupVersions: []schema.GroupVersion{}, enabledVersions: map[schema.GroupVersion]struct{}{}, groupMetaMap: map[string]*apimachinery.GroupMeta{}, envRequestedVersions: []schema.GroupVersion{}, } if len(kubeAPIVersions) != 0 { for _, version := range strings.Split(kubeAPIVersions, ",") { gv, err := schema.ParseGroupVersion(version) if err != nil { return nil, fmt.Errorf("invalid api version: %s in KUBE_API_VERSIONS: %s.", version, kubeAPIVersions) } m.envRequestedVersions = append(m.envRequestedVersions, gv) } } return m, nil }
func init() { if apiMediaType := os.Getenv("KUBE_TEST_API_TYPE"); len(apiMediaType) > 0 { var ok bool mediaType, _, err := mime.ParseMediaType(apiMediaType) if err != nil { panic(err) } serializer, ok = runtime.SerializerInfoForMediaType(api.Codecs.SupportedMediaTypes(), mediaType) if !ok { panic(fmt.Sprintf("no serializer for %s", apiMediaType)) } } if storageMediaType := StorageMediaType(); len(storageMediaType) > 0 { var ok bool mediaType, _, err := mime.ParseMediaType(storageMediaType) if err != nil { panic(err) } storageSerializer, ok = runtime.SerializerInfoForMediaType(api.Codecs.SupportedMediaTypes(), mediaType) if !ok { panic(fmt.Sprintf("no serializer for %s", storageMediaType)) } } kubeTestAPI := os.Getenv("KUBE_TEST_API") if len(kubeTestAPI) != 0 { // priority is "first in list preferred", so this has to run in reverse order testGroupVersions := strings.Split(kubeTestAPI, ",") for i := len(testGroupVersions) - 1; i >= 0; i-- { gvString := testGroupVersions[i] groupVersion, err := schema.ParseGroupVersion(gvString) if err != nil { panic(fmt.Sprintf("Error parsing groupversion %v: %v", gvString, err)) } internalGroupVersion := schema.GroupVersion{Group: groupVersion.Group, Version: runtime.APIVersionInternal} Groups[groupVersion.Group] = TestGroup{ externalGroupVersion: groupVersion, internalGroupVersion: internalGroupVersion, internalTypes: api.Scheme.KnownTypes(internalGroupVersion), externalTypes: api.Scheme.KnownTypes(groupVersion), } } } if _, ok := Groups[api.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: api.GroupName, Version: registered.GroupOrDie(api.GroupName).GroupVersion.Version} Groups[api.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: api.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(api.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[extensions.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: extensions.GroupName, Version: registered.GroupOrDie(extensions.GroupName).GroupVersion.Version} Groups[extensions.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: extensions.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(extensions.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[autoscaling.GroupName]; !ok { internalTypes := make(map[string]reflect.Type) for k, t := range api.Scheme.KnownTypes(extensions.SchemeGroupVersion) { if k == "Scale" { continue } internalTypes[k] = t } externalGroupVersion := schema.GroupVersion{Group: autoscaling.GroupName, Version: registered.GroupOrDie(autoscaling.GroupName).GroupVersion.Version} Groups[autoscaling.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: extensions.SchemeGroupVersion, internalTypes: internalTypes, externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[autoscaling.GroupName+"IntraGroup"]; !ok { internalTypes := make(map[string]reflect.Type) for k, t := range api.Scheme.KnownTypes(extensions.SchemeGroupVersion) { if k == "Scale" { internalTypes[k] = t break } } externalGroupVersion := schema.GroupVersion{Group: autoscaling.GroupName, Version: registered.GroupOrDie(autoscaling.GroupName).GroupVersion.Version} Groups[autoscaling.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: autoscaling.SchemeGroupVersion, internalTypes: internalTypes, externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[batch.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: batch.GroupName, Version: registered.GroupOrDie(batch.GroupName).GroupVersion.Version} Groups[batch.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: batch.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(batch.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[apps.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: apps.GroupName, Version: registered.GroupOrDie(apps.GroupName).GroupVersion.Version} Groups[apps.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: extensions.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(extensions.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[policy.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: policy.GroupName, Version: registered.GroupOrDie(policy.GroupName).GroupVersion.Version} Groups[policy.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: policy.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(policy.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[federation.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: federation.GroupName, Version: registered.GroupOrDie(federation.GroupName).GroupVersion.Version} Groups[federation.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: federation.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(federation.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[rbac.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: rbac.GroupName, Version: registered.GroupOrDie(rbac.GroupName).GroupVersion.Version} Groups[rbac.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: rbac.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(rbac.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[storage.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: storage.GroupName, Version: registered.GroupOrDie(storage.GroupName).GroupVersion.Version} Groups[storage.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: storage.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(storage.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[certificates.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: certificates.GroupName, Version: registered.GroupOrDie(certificates.GroupName).GroupVersion.Version} Groups[certificates.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: certificates.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(certificates.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[imagepolicy.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: imagepolicy.GroupName, Version: registered.GroupOrDie(imagepolicy.GroupName).GroupVersion.Version} Groups[imagepolicy.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: imagepolicy.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(imagepolicy.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[authorization.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: authorization.GroupName, Version: registered.GroupOrDie(authorization.GroupName).GroupVersion.Version} Groups[authorization.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: authorization.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(authorization.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } if _, ok := Groups[kubeadm.GroupName]; !ok { externalGroupVersion := schema.GroupVersion{Group: kubeadm.GroupName, Version: registered.GroupOrDie(kubeadm.GroupName).GroupVersion.Version} Groups[kubeadm.GroupName] = TestGroup{ externalGroupVersion: externalGroupVersion, internalGroupVersion: kubeadm.SchemeGroupVersion, internalTypes: api.Scheme.KnownTypes(kubeadm.SchemeGroupVersion), externalTypes: api.Scheme.KnownTypes(externalGroupVersion), } } Default = Groups[api.GroupName] Autoscaling = Groups[autoscaling.GroupName] Batch = Groups[batch.GroupName] Apps = Groups[apps.GroupName] Policy = Groups[policy.GroupName] Certificates = Groups[certificates.GroupName] Extensions = Groups[extensions.GroupName] Federation = Groups[federation.GroupName] Rbac = Groups[rbac.GroupName] Storage = Groups[storage.GroupName] ImagePolicy = Groups[imagepolicy.GroupName] Authorization = Groups[authorization.GroupName] }