gcloud Admin API-创建Cloud Run容器

时间:2020-10-20 19:52:06

标签: go gcloud

我正在尝试使用gcloud的go api创建一个容器。

我已经建立了一个项目:

$ gcloud projects list
PROJECT_ID          NAME         PROJECT_NUMBER
sql-manager-293118  sql-manager  789332021319

还配置了我的应用程序默认凭据。

下面的代码运行良好,但是在创建服务的调用中出现以下错误:

<p>The requested URL <code>/v1alpha1/projects/sql-manager-293118/services?alt=json&amp;prettyPrint=false</code> was not found on this server.  <ins>That’s all we know.</ins>

我还使用api运行了更简单的请求(例如,列出了此给定服务的所有可用位置),并且能够重新运行数据而不会出现问题。

任何指导表示赞赏。

package main

import (
    "context"
    "fmt"
    // "sort"

    "google.golang.org/api/option"
    "google.golang.org/api/run/v1alpha1"
)

const (
    createDefaultClientFlag = true
    scopes = run.CloudPlatformScope
    // List the Cloud Run services in this location
    serviceName = "test"
    locationsId = "us-central1"
    projectId = "sql-manager-293118"
    imageName = "gcr.io/cloudrun/hello"
)

func createDefaultClient(ctx context.Context) (*run.APIService, error) {
    return run.NewService(ctx)
}

func main() {
    // https://godoc.org/google.golang.org/api/run/v1#NewService
    var err error = nil
    var runService *run.APIService = nil

    ctx := context.Background()
    runService, err = createDefaultClient(ctx)

    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    projectsLocationsService := *run.NewProjectsLocationsService(runService)

    // Define the service to deploy
    tmpservice := &run.Service{
        ApiVersion: "serving.knative.dev/v1alpha1",
        Kind:       "Service",
        Metadata: &run.ObjectMeta{
            Name:      serviceName,
            Namespace: projectId,
        },
        Spec: &run.ServiceSpec{
            RunLatest: &run.ServiceSpecRunLatest{
                Configuration: &run.ConfigurationSpec{
                    RevisionTemplate: &run.RevisionTemplate{
                        Metadata: &run.ObjectMeta{
                            DeletionGracePeriodSeconds: 0,
                        },
                        Spec: &run.RevisionSpec{
                            Container: &run.Container{
                                Image: imageName,
                                Resources: &run.ResourceRequirements{
                                    Limits: map[string]string{"memory": "256Mi"},
                                },
                                Stdin:     false,
                                StdinOnce: false,
                                Tty:       false,
                            },
                            ContainerConcurrency: 80,
                            TimeoutSeconds:       300,
                        },
                    },
                },
            },
        },
    }

    createCall := projectsLocationsService.Services.Create("projects/" + projectId, tmpservice)
    service, err := createCall.Do()
    fmt.Println(service, err)
    if err != nil {
        fmt.Printf("Error creating new locationservice: %s", err)
    }

    fmt.Printf("%#v", service.Spec)
}

1 个答案:

答案 0 :(得分:0)

DazWilkin是正确的,我应该一直使用/projects/${PROJECT}/locations/us-central1/