0

I have a cluster with some deployments / services / etc... inside an OKE, which I usually connect to via kubectl from my pc.

The question is: is it possible to delete a pod inside that cluster from a Oracle Function? What I want to do is build a CI/CD chain, triggering my function via a Gateway to execute my "ci-function", and this part works well.

I'm writing my functions in Go using oci-go-sdk , but here is the problem:

I can obtain the Kubeconfig file of my cluster with:

   resp, err := client.CreateKubeconfig(ctx, containerengine.CreateKubeconfigRequest{

  ClusterId: &cID,

  })

But this Kubeconfig file contains:

- name: user-**********
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1beta1
      args:
      - ce
      - cluster
      - generate-token
      - --cluster-id
      - ocid1.cluster.oc1.*************************
      - --region
      - eu-frankfurt-1
      command: oci
      env: []

which need oci installed inside the function env, which I am not able to install.

Also, oci-cli used there is opensource, here is the interesting part that generate the token used here: https://github.com/oracle/oci-cli/blob/cf04fa4f08238cb1ee4287f0354603ba92e60647/services/container_engine/src/oci_cli_co… But I wasn't able to recreate this part and use it inside kubeconfig directly.

Someone know any way to do this?

Thanks in advance

4

2 回答 2

3

我认为您已经弄清楚了这一点,但是在您的函数中与其他 OCI 服务交互的最佳方式是使用函数的“资源主体”对该服务进行身份验证。这是为您的 Function 提供的身份,以便您可以编写策略以允许它与其他 OCI 资源交互。这可以节省您自己处理任何凭据的时间,因为代表此身份的临时 API 密钥由服务传递到您的函数中。

要与 Kubernetes 集群交互,您需要一个 kubeconfig,正如您所见,这可以由 OKE API 生成。

返回的 kubeconfig 使用基于临时时间的令牌对集群进行身份验证,正如您在交互式用例中看到的那样,在 OCI CLI 中实现了此令牌生成。

不幸的是,SDK 中没有实现此方法,因此您需要执行以下操作之一。

  • 将生成令牌的 CLI 中的代码复制到您的函数代码中,必要时翻译成您选择的语言,然后将此生成的令牌传递给您的 kubeconfig

  • 保留执行 OCI CLI 以获取令牌的配置,安装 OCI CLI 并使 OCI CLI 使用资源主体生成令牌

既然您说您已经尝试过第一种方法但没有成功,并且由于第一种方法有些繁琐,我将介绍您如何实现第二种方法。

要安装 OCI CLI,您需要控制您的 docker 构建过程,以便您可以修改生成的函数映像的内容。每个 FDK 都有一个“隐式”Dockerfile。您可以在 Fn CLI 中找到这些样板,然后可以将该 Dockerfile 放在您的函数目录中,并将 func.yaml 中的函数类型更改为“docker”。有一篇关于如何在此处提取隐式 dockerfile 的博客文章https://constructive-laziness.blogspot.com/2020/05/the-case-of-vanishing-dockerfile.html 现在您可以添加到 Dockerfile 步骤来安装OCI CLI 基于此处的说明: https ://docs.cloud.oracle.com/en-us/iaas/Content/API/SDKDocs/climanualinst.htm 由于 Go 的 FDK 基础映像是基于 alpine 的,因此该过程可能需要进行一些修改以适应它。

要使 OCI CLI 使用资源主体,您需要将环境变量设置OCI_CLI_AUTHresource_principal 这可以在ENVDockerfile 的最后阶段的一行中完成

您还需要确保您的函数位于动态组中,并根据此处的文档为您的函数制定适当的策略https://docs.cloud.oracle.com/en-us/iaas/Content/Functions /Tasks/functionsaccessingociresources.htm?Highlight=functions%20resource%20principal

(免责声明,我在函数团队为 Oracle 工作,据我所知,上述建议是正确的,但不构成官方支持)

于 2020-06-10T09:01:40.500 回答
0

如https://docs.cloud.oracle.com/en-us/iaas/Content/API/SDKDocs/gosdk.htm中所述,您应该能够使用 Go SDK 更新 OCI 函数中的集群和 NodePool

或者,您还应该能够在您的 OCI 函数中的 Go 代码中启动 HTTP 客户端并调用 OKE 的 UpdateCluster 和 UpdateNodePool REST API,参见例如https://docs.cloud.oracle.com/en-us/iaas/ api/#/en/containerengine/20180222/NodePool/UpdateNodePool

于 2020-06-07T07:03:10.717 回答