但是,我创建了Cloud Run客户端,无法找到列出通过GKE上的Cloud Run部署的服务的方式(对于Anthos)。
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredentials credential = GoogleCredentials.getApplicationDefault();
credential.createScoped("https://www.googleapis.com/auth/cloud-platform");
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credential);
CloudRun.Builder builder = new CloudRun.Builder(httpTransport, jsonFactory, requestInitializer);
return builder.setApplicationName(applicationName)
.setRootUrl(cloudRunRootUrl)
.build();
} catch (IOException e) {
e.printStackTrace();
}
services = cloudRun.namespaces().services()
.list("namespaces/default")
.execute()
.getItems();
我的“ hello”服务已部署在默认名称空间下的GKE群集上。上面的代码不起作用,因为客户端始终将“ default”视为project_id并抱怨权限问题。如果我输入project_id而不是“ default”,则权限错误消失了,但是找不到任何服务。
我尝试了另一个具有Google完全托管的云运行服务的项目,相同的代码返回结果(带有.list(“ namespaces /”))。
如何在GKE上访问服务?
我的下一个问题是,如何在GKE上以编程方式创建Cloud Run服务?
编辑-用于创建服务由于我不知道如何与GKE上的Cloud Run交互,因此我退后一步尝试使用完全托管的产品。以下创建服务的代码失败,并且错误消息并没有提供太多有用的见解以及如何使其起作用?
Service deployedService = null;
// Map<String,String> annotations = new HashMap<>();
// annotations.put("client.knative.dev/user-image","gcr.io/cloudrun/hello");
ServiceSpec spec = new ServiceSpec();
List<Container> containers = new ArrayList<>();
containers.add(new Container().setImage("gcr.io/cloudrun/hello"));
spec.setTemplate(new RevisionTemplate().setMetadata(new ObjectMeta().setName("hello-fully-managed-v0.1.0"))
.setSpec(new RevisionSpec().setContainerConcurrency(20)
.setContainers(containers)
.setTimeoutSeconds(100)
)
);
helloService.setApiVersion("serving.knative.dev/v1")
.setMetadata(new ObjectMeta().setName("hello-fully-managed")
.setNamespace("data-infrastructure-test-env")
// .setAnnotations(annotations)
)
.setSpec(spec)
.setKind("Service");
try {
deployedService = cloudRun.namespaces().services()
.create("namespaces/data-infrastructure-test-env",helloService)
.execute();
} catch (IOException e) {
e.printStackTrace();
response.add(e.toString());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
我收到的错误消息:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "The request has errors",
"reason" : "badRequest"
} ],
"message" : "The request has errors",
"status" : "INVALID_ARGUMENT"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
并且base_url是:https://europe-west1-run.googleapis.com
您的问题非常详细(关于Java,我不是专家),实际上那里有太多问题(理想情况下,请在此处仅提问1个问题)。但是,我将尝试回答您要求的一些内容:
[首先,Cloud Run(在GKE上托管,并在GKE上)均实现Knative Serving API。我已经在https://ahmet.im/blog/cloud-run-is-a-knative/上对此进行了解释,实际上,GKE上的Cloud Run只是安装到您的集群的开源Knative组件。
我的下一个问题是,如何在GKE上以编程方式创建Cloud Run服务?
您将使用Cloud Run API客户端库(例如,上面的new CloudRun
)会有一个[[非常困难的时间(如果可能的话),因为它们是为*.googleapis.com
端点设计的。
Knative API仅仅是Kubernetes对象。因此,您最好的选择是以下之一:
kubectl apply
。Cloud Run(托管)进行编程
us-central1-run.googleapis.com
。 (在每个API调用的REST API参考文档中都有记录。)我已经在此处详细撰写了一篇博客文章(Go中的示例代码),其中介绍了如何使用Knative Serving API在Cloud Run(托管)上创建/更新服务:https://ahmet.im/blog/gcloud-run-deploy/如果要查看gcloud run deploy
的工作方式以及调用的API,可以通过--log-http
选项来观察请求/响应流量。
关于收到的错误,似乎该错误消息没有帮助,但它可能来自任何地方(因为您试图在GCP客户端库中模仿Knative API)。我建议深入阅读我的博客文章和示例代码。