本文整理匯總了Golang中k8s/io/kubernetes/pkg/api/unversioned.ParseGroupVersion函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseGroupVersion函數的具體用法?Golang ParseGroupVersion怎麽用?Golang ParseGroupVersion使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ParseGroupVersion函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: newEtcd
func newEtcd(ns runtime.NegotiatedSerializer, storageGroupVersionString, memoryGroupVersionString string, etcdConfig etcdstorage.EtcdConfig) (etcdStorage storage.Interface, err error) {
if storageGroupVersionString == "" {
return etcdStorage, fmt.Errorf("storageVersion is required to create a etcd storage")
}
storageVersion, err := unversioned.ParseGroupVersion(storageGroupVersionString)
if err != nil {
return nil, fmt.Errorf("couldn't understand storage version %v: %v", storageGroupVersionString, err)
}
memoryVersion, err := unversioned.ParseGroupVersion(memoryGroupVersionString)
if err != nil {
return nil, fmt.Errorf("couldn't understand memory version %v: %v", memoryGroupVersionString, err)
}
var storageConfig etcdstorage.EtcdStorageConfig
storageConfig.Config = etcdConfig
s, ok := ns.SerializerForMediaType("application/json", nil)
if !ok {
return nil, fmt.Errorf("unable to find serializer for JSON")
}
glog.Infof("constructing etcd storage interface.\n sv: %v\n mv: %v\n", storageVersion, memoryVersion)
encoder := ns.EncoderForVersion(s, storageVersion)
decoder := ns.DecoderToVersion(s, memoryVersion)
if memoryVersion.Group != storageVersion.Group {
// Allow this codec to translate between groups.
if err = versioning.EnableCrossGroupEncoding(encoder, memoryVersion.Group, storageVersion.Group); err != nil {
return nil, fmt.Errorf("error setting up encoder for %v: %v", storageGroupVersionString, err)
}
if err = versioning.EnableCrossGroupDecoding(decoder, storageVersion.Group, memoryVersion.Group); err != nil {
return nil, fmt.Errorf("error setting up decoder for %v: %v", storageGroupVersionString, err)
}
}
storageConfig.Codec = runtime.NewCodec(encoder, decoder)
return storageConfig.NewStorage()
}
示例2: mergeGroupVersionIntoMap
// dest must be a map of group to groupVersion.
func mergeGroupVersionIntoMap(gvList string, dest map[string]unversioned.GroupVersion) error {
for _, gvString := range strings.Split(gvList, ",") {
if gvString == "" {
continue
}
// We accept two formats. "group/version" OR
// "group=group/version". The latter is used when types
// move between groups.
if !strings.Contains(gvString, "=") {
gv, err := unversioned.ParseGroupVersion(gvString)
if err != nil {
return err
}
dest[gv.Group] = gv
} else {
parts := strings.SplitN(gvString, "=", 2)
gv, err := unversioned.ParseGroupVersion(parts[1])
if err != nil {
return err
}
dest[parts[0]] = gv
}
}
return nil
}
示例3: rawExtensionToEmbeddedObject
// rawExtensionToEmbeddedObject does the conversion you would expect from the name, using the information
// given in conversion.Scope. It's placed in all schemes as a ConversionFunc to enable plugins;
// see the comment for RawExtension.
func (self *Scheme) rawExtensionToEmbeddedObject(in *RawExtension, out *EmbeddedObject, s conversion.Scope) error {
if len(in.RawJSON) == 0 || (len(in.RawJSON) == 4 && string(in.RawJSON) == "null") {
out.Object = nil
return nil
}
// Figure out the type and kind of the output object.
inGroupVersionString, outGroupVersionString, scheme := self.fromScope(s)
dataKind, err := scheme.raw.DataKind(in.RawJSON)
if err != nil {
return err
}
inVersion, err := unversioned.ParseGroupVersion(inGroupVersionString)
if err != nil {
return err
}
outVersion, err := unversioned.ParseGroupVersion(outGroupVersionString)
if err != nil {
return err
}
// We have to make this object ourselves because we don't store the version field for
// plugin objects.
inObj, err := scheme.New(inVersion.WithKind(dataKind.Kind))
if err != nil {
return err
}
err = DecodeInto(scheme, in.RawJSON, inObj)
if err != nil {
return err
}
// Make the desired internal version, and do the conversion.
outObj, err := scheme.New(outVersion.WithKind(dataKind.Kind))
if err != nil {
return err
}
err = scheme.Convert(inObj, outObj)
if err != nil {
return err
}
// Last step, clear the Kind field; that should always be blank in memory.
err = s.Convert(
&emptyPlugin{PluginBase: PluginBase{Kind: ""}},
outObj,
conversion.SourceToDest|conversion.IgnoreMissingFields|conversion.AllowDifferentFieldTypeNames,
)
if err != nil {
return err
}
out.Object = outObj
return nil
}
示例4: ClientConfigForVersion
// ClientConfigForVersion returns the correct config for a server
func (c *ClientCache) ClientConfigForVersion(version string) (*client.Config, error) {
if c.defaultConfig == nil {
config, err := c.loader.ClientConfig()
if err != nil {
return nil, err
}
c.defaultConfig = config
if c.matchVersion {
if err := client.MatchesServerVersion(c.defaultClient, config); err != nil {
return nil, err
}
}
}
if config, ok := c.configs[version]; ok {
return config, nil
}
// TODO: have a better config copy method
config := *c.defaultConfig
// TODO these fall out when we finish the refactor
var preferredGV *unversioned.GroupVersion
if len(version) > 0 {
gv, err := unversioned.ParseGroupVersion(version)
if err != nil {
return nil, err
}
preferredGV = &gv
}
registeredGVs := []unversioned.GroupVersion{}
for _, gvString := range registered.RegisteredVersions {
gv, err := unversioned.ParseGroupVersion(gvString)
if err != nil {
return nil, err
}
registeredGVs = append(registeredGVs, gv)
}
negotiatedVersion, err := client.NegotiateVersion(c.defaultClient, &config, preferredGV, registeredGVs)
if err != nil {
return nil, err
}
config.GroupVersion = negotiatedVersion
client.SetKubernetesDefaults(&config)
c.configs[version] = &config
// `version` does not necessarily equal `config.Version`. However, we know that we call this method again with
// `config.Version`, we should get the the config we've just built.
configCopy := config
c.configs[config.GroupVersion.String()] = &configCopy
return &config, nil
}
示例5: AddHPAScaleRefEdges
func AddHPAScaleRefEdges(g osgraph.Graph) {
for _, node := range g.NodesByKind(kubegraph.HorizontalPodAutoscalerNodeKind) {
hpaNode := node.(*kubegraph.HorizontalPodAutoscalerNode)
syntheticMeta := kapi.ObjectMeta{
Name: hpaNode.HorizontalPodAutoscaler.Spec.ScaleRef.Name,
Namespace: hpaNode.HorizontalPodAutoscaler.Namespace,
}
var groupVersionResource unversioned.GroupVersionResource
resource := strings.ToLower(hpaNode.HorizontalPodAutoscaler.Spec.ScaleRef.Kind)
if groupVersion, err := unversioned.ParseGroupVersion(hpaNode.HorizontalPodAutoscaler.Spec.ScaleRef.APIVersion); err == nil {
groupVersionResource = groupVersion.WithResource(resource)
} else {
groupVersionResource = unversioned.GroupVersionResource{Resource: resource}
}
groupVersionResource, err := registered.RESTMapper().ResourceFor(groupVersionResource)
if err != nil {
continue
}
var syntheticNode graph.Node
switch groupVersionResource.GroupResource() {
case kapi.Resource("replicationcontrollers"):
syntheticNode = kubegraph.FindOrCreateSyntheticReplicationControllerNode(g, &kapi.ReplicationController{ObjectMeta: syntheticMeta})
case deployapi.Resource("deploymentconfigs"):
syntheticNode = deploygraph.FindOrCreateSyntheticDeploymentConfigNode(g, &deployapi.DeploymentConfig{ObjectMeta: syntheticMeta})
default:
continue
}
g.AddEdge(hpaNode, syntheticNode, ScalingEdgeKind)
}
}
示例6: DecodeList
// DecodeList alters the list in place, attempting to decode any objects found in
// the list that have the runtime.Unknown type. Any errors that occur are returned
// after the entire list is processed. Decoders are tried in order.
func DecodeList(objects []Object, decoders ...ObjectDecoder) []error {
errs := []error(nil)
for i, obj := range objects {
switch t := obj.(type) {
case *Unknown:
for _, decoder := range decoders {
gv, err := unversioned.ParseGroupVersion(t.APIVersion)
if err != nil {
errs = append(errs, err)
break
}
if !decoder.Recognizes(gv.WithKind(t.Kind)) {
continue
}
obj, err := Decode(decoder, t.RawJSON)
if err != nil {
errs = append(errs, err)
break
}
objects[i] = obj
break
}
}
}
return errs
}
示例7: GroupVersion
func (g TestGroup) GroupVersion() *unversioned.GroupVersion {
gv, err := unversioned.ParseGroupVersion(g.GroupVersionUnderTest)
if err != nil {
panic(err)
}
return &gv
}
示例8: Convert_runtime_RawExtension_To_runtime_Object
// Convert_runtime_RawExtension_To_runtime_Object well, this is the reason why there was runtime.Embedded. The `out` here is hopeless.
// The caller doesn't know the type ahead of time and that means this method can't communicate the return value. This sucks really badly.
// I'm going to set the `in.Object` field can have callers to this function do magic to pull it back out. I'm also going to bitch about it.
func Convert_runtime_RawExtension_To_runtime_Object(in *runtime.RawExtension, out runtime.Object, s conversion.Scope) error {
if in == nil || len(in.Raw) == 0 || in.Object != nil {
return nil
}
// the scheme knows all available group versions, so its possible to build the decoder properly, but it would require some refactoring
srcVersion, err := unversioned.ParseGroupVersion(s.Meta().SrcVersion)
if err != nil {
return err
}
decodedObject, err := runtime.Decode(kapi.Codecs.UniversalDecoder(srcVersion), in.Raw)
if err != nil {
return err
}
internalObject, err := kapi.Scheme.ConvertToVersion(decodedObject, s.Meta().DestVersion)
if err != nil {
return err
}
in.Object = internalObject
return nil
}
示例9: Convert_api_Image_To_v1beta3_Image
// The docker metadata must be cast to a version
func Convert_api_Image_To_v1beta3_Image(in *newer.Image, out *Image, s conversion.Scope) error {
if err := s.Convert(&in.ObjectMeta, &out.ObjectMeta, 0); err != nil {
return err
}
out.DockerImageReference = in.DockerImageReference
out.DockerImageManifest = in.DockerImageManifest
out.DockerImageManifestMediaType = in.DockerImageManifestMediaType
out.DockerImageConfig = in.DockerImageConfig
gvString := in.DockerImageMetadataVersion
if len(gvString) == 0 {
gvString = "1.0"
}
if !strings.Contains(gvString, "/") {
gvString = "/" + gvString
}
version, err := unversioned.ParseGroupVersion(gvString)
if err != nil {
return err
}
data, err := runtime.Encode(api.Codecs.LegacyCodec(version), &in.DockerImageMetadata, version)
if err != nil {
return err
}
out.DockerImageMetadata.Raw = data
out.DockerImageMetadataVersion = version.Version
return nil
}
示例10: serverAPIVersions
// serverAPIVersions fetches the server versions available from the groupless API at the given prefix
func serverAPIVersions(c *kclient.Client, grouplessPrefix string) ([]unversioned.GroupVersion, error) {
// Get versions doc
body, err := c.Get().AbsPath(grouplessPrefix).Do().Raw()
if err != nil {
return []unversioned.GroupVersion{}, err
}
// Unmarshal
var v unversioned.APIVersions
err = json.Unmarshal(body, &v)
if err != nil {
return []unversioned.GroupVersion{}, fmt.Errorf("got '%s': %v", string(body), err)
}
// Convert to GroupVersion structs
serverAPIVersions := []unversioned.GroupVersion{}
for _, version := range v.Versions {
gv, err := unversioned.ParseGroupVersion(version)
if err != nil {
return []unversioned.GroupVersion{}, err
}
serverAPIVersions = append(serverAPIVersions, gv)
}
return serverAPIVersions, nil
}
示例11: ConvertToVersion
// ConvertToVersion attempts to convert an input object to its matching Kind in another
// version within this scheme. Will return an error if the provided version does not
// contain the inKind (or a mapping by name defined with AddKnownTypeWithName). Will also
// return an error if the conversion does not result in a valid Object being
// returned. The serializer handles loading/serializing nested objects.
func (s *Scheme) ConvertToVersion(in Object, outVersion string) (Object, error) {
gv, err := unversioned.ParseGroupVersion(outVersion)
if err != nil {
return nil, err
}
switch in.(type) {
case *Unknown, *Unstructured, *UnstructuredList:
old := in.GetObjectKind().GroupVersionKind()
defer in.GetObjectKind().SetGroupVersionKind(old)
setTargetVersion(in, s, gv)
return in, nil
}
t := reflect.TypeOf(in)
if t.Kind() != reflect.Ptr {
return nil, fmt.Errorf("only pointer types may be converted: %v", t)
}
t = t.Elem()
if t.Kind() != reflect.Struct {
return nil, fmt.Errorf("only pointers to struct types may be converted: %v", t)
}
var kind unversioned.GroupVersionKind
if unversionedKind, ok := s.unversionedTypes[t]; ok {
kind = unversionedKind
} else {
kinds, ok := s.typeToGVK[t]
if !ok || len(kinds) == 0 {
return nil, fmt.Errorf("%v is not a registered type and cannot be converted into version %q", t, outVersion)
}
for _, kind := range kinds {
if gv.Version == kind.Version && gv.Group == kind.Group {
setTargetKind(in, kind)
return in, nil
}
}
kind = kinds[0]
}
outKind := gv.WithKind(kind.Kind)
inKind, err := s.ObjectKind(in)
if err != nil {
return nil, err
}
out, err := s.New(outKind)
if err != nil {
return nil, err
}
flags, meta := s.generateConvertMeta(inKind.GroupVersion(), gv, in)
if err := s.converter.Convert(in, out, flags, meta); err != nil {
return nil, err
}
setTargetVersion(out, s, gv)
return out, nil
}
示例12: setExtensionsDefaults
func setExtensionsDefaults(config *Config) error {
// if experimental group is not registered, return an error
g, err := latest.Group("extensions")
if err != nil {
return err
}
config.Prefix = "apis/"
if config.UserAgent == "" {
config.UserAgent = DefaultKubernetesUserAgent()
}
// TODO: Unconditionally set the config.Version, until we fix the config.
//if config.Version == "" {
gv, err := unversioned.ParseGroupVersion(g.GroupVersion)
if err != nil {
return err
}
config.GroupVersion = &gv
//}
versionInterfaces, err := g.InterfacesFor(config.GroupVersion.String())
if err != nil {
return fmt.Errorf("Extensions API group/version '%v' is not recognized (valid values: %s)",
config.GroupVersion, strings.Join(latest.GroupOrDie("extensions").Versions, ", "))
}
config.Codec = versionInterfaces.Codec
if config.QPS == 0 {
config.QPS = 5
}
if config.Burst == 0 {
config.Burst = 10
}
return nil
}
示例13: InfoForObject
// InfoForObject creates an Info object for the given Object. An error is returned
// if the object cannot be introspected. Name and namespace will be set into Info
// if the mapping's MetadataAccessor can retrieve them.
func (m *Mapper) InfoForObject(obj runtime.Object) (*Info, error) {
gvString, kind, err := m.ObjectVersionAndKind(obj)
if err != nil {
return nil, fmt.Errorf("unable to get type info from the object %q: %v", reflect.TypeOf(obj), err)
}
gv, err := unversioned.ParseGroupVersion(gvString)
if err != nil {
return nil, fmt.Errorf("unable to parse group/version from %q: %v", gvString, err)
}
mapping, err := m.RESTMapping(unversioned.GroupKind{Group: gv.Group, Kind: kind}, gv.Version)
if err != nil {
return nil, fmt.Errorf("unable to recognize %q: %v", kind, err)
}
client, err := m.ClientForMapping(mapping)
if err != nil {
return nil, fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
}
name, _ := mapping.MetadataAccessor.Name(obj)
namespace, _ := mapping.MetadataAccessor.Namespace(obj)
resourceVersion, _ := mapping.MetadataAccessor.ResourceVersion(obj)
return &Info{
Mapping: mapping,
Client: client,
Namespace: namespace,
Name: name,
Object: obj,
ResourceVersion: resourceVersion,
}, nil
}
示例14: TestDirectCodec
func TestDirectCodec(t *testing.T) {
s := GetDirectCodecTestScheme()
cf := newCodecFactory(s, newSerializersForScheme(s, testMetaFactory{}))
serializer, _ := cf.SerializerForFileExtension("json")
df := DirectCodecFactory{cf}
ignoredGV, err := unversioned.ParseGroupVersion("ignored group/ignored version")
if err != nil {
t.Fatal(err)
}
directEncoder := df.EncoderForVersion(serializer, ignoredGV)
directDecoder := df.DecoderToVersion(serializer, ignoredGV)
out, err := runtime.Encode(directEncoder, &ExternalTestType1{})
if err != nil {
t.Fatal(err)
}
if string(out) != `{"myVersionKey":"v1","myKindKey":"ExternalTestType1"}`+"\n" {
t.Fatal(string(out))
}
a, _, err := directDecoder.Decode(out, nil, nil)
e := &ExternalTestType1{
MyWeirdCustomEmbeddedVersionKindField: MyWeirdCustomEmbeddedVersionKindField{
APIVersion: "v1",
ObjectKind: "ExternalTestType1",
},
}
if !semantic.DeepEqual(e, a) {
t.Fatalf("expect %v, got %v", e, a)
}
}
示例15: ValidateBytes
func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
version, kind, err := runtime.UnstructuredJSONScheme.DataVersionAndKind(data)
if err != nil {
return err
}
gv, err := unversioned.ParseGroupVersion(version)
if err != nil {
return fmt.Errorf("unable to parse group/version from %q: %v", version, err)
}
if ok := registered.IsRegisteredAPIGroupVersion(gv); !ok {
return fmt.Errorf("API version %q isn't supported, only supports API versions %q", version, registered.RegisteredGroupVersions)
}
resource, _ := meta.KindToResource(kind, false)
gvk, err := c.mapper.KindFor(resource)
if err != nil {
return fmt.Errorf("could not find api group for %s: %v", kind, err)
}
if gvk.Group == "extensions" {
if c.c.ExtensionsClient == nil {
return errors.New("unable to validate: no experimental client")
}
return getSchemaAndValidate(c.c.ExtensionsClient.RESTClient, data, "apis/", version, c.cacheDir)
}
return getSchemaAndValidate(c.c.RESTClient, data, "api", version, c.cacheDir)
}