當前位置: 首頁>>代碼示例>>Golang>>正文


Golang unversioned.ExtractGroupVersions函數代碼示例

本文整理匯總了Golang中k8s/io/kubernetes/pkg/api/unversioned.ExtractGroupVersions函數的典型用法代碼示例。如果您正苦於以下問題:Golang ExtractGroupVersions函數的具體用法?Golang ExtractGroupVersions怎麽用?Golang ExtractGroupVersions使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了ExtractGroupVersions函數的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestGetServerGroupsWithV1Server

func TestGetServerGroupsWithV1Server(t *testing.T) {
    server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
        var obj interface{}
        switch req.URL.Path {
        case "/api":
            obj = &unversioned.APIVersions{
                Versions: []string{
                    "v1",
                },
            }
        default:
            w.WriteHeader(http.StatusNotFound)
            return
        }
        output, err := json.Marshal(obj)
        if err != nil {
            t.Fatalf("unexpected encoding error: %v", err)
            return
        }
        w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusOK)
        w.Write(output)
    }))
    defer server.Close()
    client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
    // ServerGroups should not return an error even if server returns error at /api and /apis
    apiGroupList, err := client.ServerGroups()
    if err != nil {
        t.Fatalf("unexpected error: %v", err)
    }
    groupVersions := unversioned.ExtractGroupVersions(apiGroupList)
    if !reflect.DeepEqual(groupVersions, []string{"v1"}) {
        t.Errorf("expected: %q, got: %q", []string{"v1"}, groupVersions)
    }
}
開發者ID:juanluisvaladas,項目名稱:origin,代碼行數:35,代碼來源:discovery_client_test.go

示例2: SwaggerSchema

// SwaggerSchema retrieves and parses the swagger API schema the server supports.
func (d *DiscoveryClient) SwaggerSchema(version unversioned.GroupVersion) (*swagger.ApiDeclaration, error) {
    if version.Empty() {
        return nil, fmt.Errorf("groupVersion cannot be empty")
    }

    groupList, err := d.ServerGroups()
    if err != nil {
        return nil, err
    }
    groupVersions := unversioned.ExtractGroupVersions(groupList)
    // This check also takes care the case that kubectl is newer than the running endpoint
    if stringDoesntExistIn(version.String(), groupVersions) {
        return nil, fmt.Errorf("API version: %v is not supported by the server. Use one of: %v", version, groupVersions)
    }
    var path string
    if len(d.LegacyPrefix) > 0 && version == v1.SchemeGroupVersion {
        path = "/swaggerapi" + d.LegacyPrefix + "/" + version.Version
    } else {
        path = "/swaggerapi/apis/" + version.Group + "/" + version.Version
    }

    body, err := d.restClient.Get().AbsPath(path).Do().Raw()
    if err != nil {
        return nil, err
    }
    var schema swagger.ApiDeclaration
    err = json.Unmarshal(body, &schema)
    if err != nil {
        return nil, fmt.Errorf("got '%s': %v", string(body), err)
    }
    return &schema, nil
}
開發者ID:eljefedelrodeodeljefe,項目名稱:kubernetes,代碼行數:33,代碼來源:discovery_client.go

示例3: NegotiateVersion

// NegotiateVersion queries the server's supported api versions to find
// a version that both client and server support.
// - If no version is provided, try registered client versions in order of
//   preference.
// - If version is provided and the server does not support it,
//   return an error.
func NegotiateVersion(client DiscoveryInterface, requiredGV *schema.GroupVersion, clientRegisteredGVs []schema.GroupVersion) (*schema.GroupVersion, error) {
    clientVersions := sets.String{}
    for _, gv := range clientRegisteredGVs {
        clientVersions.Insert(gv.String())
    }
    groups, err := client.ServerGroups()
    if err != nil {
        // This is almost always a connection error, and higher level code should treat this as a generic error,
        // not a negotiation specific error.
        return nil, err
    }
    versions := unversioned.ExtractGroupVersions(groups)
    serverVersions := sets.String{}
    for _, v := range versions {
        serverVersions.Insert(v)
    }

    // If version explicitly requested verify that both client and server support it.
    // If server does not support warn, but try to negotiate a lower version.
    if requiredGV != nil {
        if !clientVersions.Has(requiredGV.String()) {
            return nil, fmt.Errorf("client does not support API version %q; client supported API versions: %v", requiredGV, clientVersions)

        }
        // If the server supports no versions, then we should just use the preferredGV
        // This can happen because discovery fails due to 403 Forbidden errors
        if len(serverVersions) == 0 {
            return requiredGV, nil
        }
        if serverVersions.Has(requiredGV.String()) {
            return requiredGV, nil
        }
        // If we are using an explicit config version the server does not support, fail.
        return nil, fmt.Errorf("server does not support API version %q", requiredGV)
    }

    for _, clientGV := range clientRegisteredGVs {
        if serverVersions.Has(clientGV.String()) {
            // Version was not explicitly requested in command config (--api-version).
            // Ok to fall back to a supported version with a warning.
            // TODO: caesarxuchao: enable the warning message when we have
            // proper fix. Please refer to issue #14895.
            // if len(version) != 0 {
            // 	glog.Warningf("Server does not support API version '%s'. Falling back to '%s'.", version, clientVersion)
            // }
            t := clientGV
            return &t, nil
        }
    }

    // if we have no server versions and we have no required version, choose the first clientRegisteredVersion
    if len(serverVersions) == 0 && len(clientRegisteredGVs) > 0 {
        return &clientRegisteredGVs[0], nil
    }

    return nil, fmt.Errorf("failed to negotiate an api version; server supports: %v, client supports: %v",
        serverVersions, clientVersions)
}
開發者ID:paralin,項目名稱:kubernetes,代碼行數:64,代碼來源:helper.go

示例4: ServerResources

// ServerResources returns the supported resources for all groups and versions.
func (d *DiscoveryClient) ServerResources() (map[string]*unversioned.APIResourceList, error) {
    apiGroups, err := d.ServerGroups()
    if err != nil {
        return nil, err
    }
    groupVersions := unversioned.ExtractGroupVersions(apiGroups)
    result := map[string]*unversioned.APIResourceList{}
    for _, groupVersion := range groupVersions {
        resources, err := d.ServerResourcesForGroupVersion(groupVersion)
        if err != nil {
            return nil, err
        }
        result[groupVersion] = resources
    }
    return result, nil
}
開發者ID:eljefedelrodeodeljefe,項目名稱:kubernetes,代碼行數:17,代碼來源:discovery_client.go

示例5: runAPIVersionsTest

func runAPIVersionsTest(c *client.Client) {
    g, err := c.ServerGroups()
    clientVersion := c.APIVersion().String()
    if err != nil {
        glog.Fatalf("Failed to get api versions: %v", err)
    }
    versions := unversioned.ExtractGroupVersions(g)

    // Verify that the server supports the API version used by the client.
    for _, version := range versions {
        if version == clientVersion {
            glog.Infof("Version test passed")
            return
        }
    }
    glog.Fatalf("Server does not support APIVersion used by client. Server supported APIVersions: '%v', client APIVersion: '%v'", versions, clientVersion)
}
開發者ID:Clarifai,項目名稱:kubernetes,代碼行數:17,代碼來源:integration.go

示例6: TestGetServerGroupsWithBrokenServer

func TestGetServerGroupsWithBrokenServer(t *testing.T) {
    for _, statusCode := range []int{http.StatusNotFound, http.StatusForbidden} {
        server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
            w.WriteHeader(statusCode)
        }))
        defer server.Close()
        client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
        // ServerGroups should not return an error even if server returns Not Found or Forbidden error at all end points
        apiGroupList, err := client.ServerGroups()
        if err != nil {
            t.Fatalf("unexpected error: %v", err)
        }
        groupVersions := unversioned.ExtractGroupVersions(apiGroupList)
        if len(groupVersions) != 0 {
            t.Errorf("expected empty list, got: %q", groupVersions)
        }
    }
}
開發者ID:juanluisvaladas,項目名稱:origin,代碼行數:18,代碼來源:discovery_client_test.go

示例7: getVersionSet

func getVersionSet(client discovery.ServerGroupsInterface) (versionSet, error) {
    defVersions := newVersionSet("v1")

    groups, err := client.ServerGroups()
    if err != nil {
        return defVersions, err
    }

    // FIXME: The Kubernetes test fixture for cli appears to always return nil
    // for calls to Discovery().ServerGroups(). So in this case, we return
    // the default API list. This is also a safe value to return in any other
    // odd-ball case.
    if groups == nil {
        return defVersions, nil
    }

    versions := unversioned.ExtractGroupVersions(groups)
    return newVersionSet(versions...), nil
}
開發者ID:bacongobbler,項目名稱:helm,代碼行數:19,代碼來源:release_server.go

示例8: TestAPIVersions

func TestAPIVersions(t *testing.T) {
    _, s := framework.RunAMaster(t)
    defer s.Close()

    c := client.NewOrDie(&restclient.Config{Host: s.URL, ContentConfig: restclient.ContentConfig{GroupVersion: testapi.Default.GroupVersion()}})

    clientVersion := c.APIVersion().String()
    g, err := c.ServerGroups()
    if err != nil {
        t.Fatalf("Failed to get api versions: %v", err)
    }
    versions := unversioned.ExtractGroupVersions(g)

    // Verify that the server supports the API version used by the client.
    for _, version := range versions {
        if version == clientVersion {
            return
        }
    }
    t.Errorf("Server does not support APIVersion used by client. Server supported APIVersions: '%v', client APIVersion: '%v'", versions, clientVersion)
}
開發者ID:XbinZh,項目名稱:kubernetes,代碼行數:21,代碼來源:client_test.go

示例9: RunApiVersions

func RunApiVersions(f *cmdutil.Factory, w io.Writer) error {
    if len(os.Args) > 1 && os.Args[1] == "apiversions" {
        printDeprecationWarning("api-versions", "apiversions")
    }

    client, err := f.Client()
    if err != nil {
        return err
    }

    groupList, err := client.Discovery().ServerGroups()
    if err != nil {
        return fmt.Errorf("Couldn't get available api versions from server: %v\n", err)
    }
    apiVersions := unversioned.ExtractGroupVersions(groupList)
    sort.Strings(apiVersions)
    for _, v := range apiVersions {
        fmt.Fprintln(w, v)
    }
    return nil
}
開發者ID:XbinZh,項目名稱:kubernetes,代碼行數:21,代碼來源:apiversions.go

示例10: getVersionSet

func (s *releaseServer) getVersionSet() (versionSet, error) {
    defVersions := newVersionSet("v1")
    cli, err := s.env.KubeClient.APIClient()
    if err != nil {
        log.Printf("API Client for Kubernetes is missing: %s.", err)
        return defVersions, err
    }

    groups, err := cli.Discovery().ServerGroups()
    if err != nil {
        return defVersions, err
    }

    // FIXME: The Kubernetes test fixture for cli appears to always return nil
    // for calls to Discovery().ServerGroups(). So in this case, we return
    // the default API list. This is also a safe value to return in any other
    // odd-ball case.
    if groups == nil {
        return defVersions, nil
    }

    versions := unversioned.ExtractGroupVersions(groups)
    return newVersionSet(versions...), nil
}
開發者ID:technosophos,項目名稱:k8s-helm,代碼行數:24,代碼來源:release_server.go

示例11: NegotiateVersion

// NegotiateVersion queries the server's supported api versions to find
// a version that both client and server support.
// - If no version is provided, try registered client versions in order of
//   preference.
// - If version is provided, but not default config (explicitly requested via
//   commandline flag), and is unsupported by the server, print a warning to
//   stderr and try client's registered versions in order of preference.
// - If version is config default, and the server does not support it,
//   return an error.
func NegotiateVersion(client *Client, c *restclient.Config, requestedGV *unversioned.GroupVersion, clientRegisteredGVs []unversioned.GroupVersion) (*unversioned.GroupVersion, error) {
    var err error
    if client == nil {
        client, err = New(c)
        if err != nil {
            return nil, err
        }
    }
    clientVersions := sets.String{}
    for _, gv := range clientRegisteredGVs {
        clientVersions.Insert(gv.String())
    }
    groups, err := client.ServerGroups()
    if err != nil {
        // This is almost always a connection error, and higher level code should treat this as a generic error,
        // not a negotiation specific error.
        return nil, err
    }
    versions := unversioned.ExtractGroupVersions(groups)
    serverVersions := sets.String{}
    for _, v := range versions {
        serverVersions.Insert(v)
    }

    // If no version requested, use config version (may also be empty).
    // make a copy of the original so we don't risk mutating input here or in the returned value
    var preferredGV *unversioned.GroupVersion
    switch {
    case requestedGV != nil:
        t := *requestedGV
        preferredGV = &t
    case c.GroupVersion != nil:
        t := *c.GroupVersion
        preferredGV = &t
    }

    // If version explicitly requested verify that both client and server support it.
    // If server does not support warn, but try to negotiate a lower version.
    if preferredGV != nil {
        if !clientVersions.Has(preferredGV.String()) {
            return nil, fmt.Errorf("client does not support API version %q; client supported API versions: %v", preferredGV, clientVersions)

        }
        if serverVersions.Has(preferredGV.String()) {
            return preferredGV, nil
        }
        // If we are using an explicit config version the server does not support, fail.
        if (c.GroupVersion != nil) && (*preferredGV == *c.GroupVersion) {
            return nil, fmt.Errorf("server does not support API version %q", preferredGV)
        }
    }

    for _, clientGV := range clientRegisteredGVs {
        if serverVersions.Has(clientGV.String()) {
            // Version was not explicitly requested in command config (--api-version).
            // Ok to fall back to a supported version with a warning.
            // TODO: caesarxuchao: enable the warning message when we have
            // proper fix. Please refer to issue #14895.
            // if len(version) != 0 {
            // 	glog.Warningf("Server does not support API version '%s'. Falling back to '%s'.", version, clientVersion)
            // }
            t := clientGV
            return &t, nil
        }
    }
    return nil, fmt.Errorf("failed to negotiate an api version; server supports: %v, client supports: %v",
        serverVersions, clientVersions)
}
開發者ID:odacremolbap,項目名稱:kubernetes,代碼行數:77,代碼來源:helper.go


注:本文中的k8s/io/kubernetes/pkg/api/unversioned.ExtractGroupVersions函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。