[英]Using client-go to `kubectl apply` against the Kubernetes API directly with multiple types in a single YAML file
我正在使用https://github.com/kubernetes/client-go并且一切正常。
I have a manifest (YAML) for the official Kubernetes Dashboard: https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta4/aio/deploy/recommended.yaml
我想使用 client-go 在 Go 代码中模仿kubectl apply
此清单。
I understand that I need to do some (un)marshalling of the YAML bytes into the correct API types defined in package: https://github.com/kubernetes/api
我已经成功地为我的集群Create
了单个 API 类型,但是对于包含不同类型列表的清单,我该如何做呢? 是否有支持这些不同类型的资源kind: List*
?
我目前的解决方法是使用csplit
拆分 YAML 文件,并以 --- 作为分隔符
csplit /path/to/recommended.yaml /---/ '{*}' --prefix='dashboard.' --suffix-format='%03d.yaml'
接下来,我循环创建新的 (14) 部分,读取它们的字节,打开 UniversalDeserializer 的解码器返回的 object 的类型,并使用我的 k8s 客户端集调用正确的 API 方法。
我想以编程方式执行此操作,以将仪表板的任何新版本更新到我的集群中。 我还需要为 Metrics Server 和许多其他资源执行此操作。 另一种(可能更简单)的方法是将安装了 kubectl 的代码发送到容器映像并直接调用kubectl apply -f -
; 但这意味着我还需要将 kube 配置写入磁盘,或者内联传递它,以便 kubectl 可以使用它。
我发现这个问题很有帮助: https://github.com/kubernetes/client-go/issues/193解码器住在这里: https://github.com/kubernetes/apimachinery/tree/master/pkg/runtime/串行器
它在 client-go 中公开: https://github.com/kubernetes/client-go/blob/master/kubernetes/scheme/register.go#L69
我还查看了 kubectl 使用的 RunConvert 方法: https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/convert/convert.go#L139并假设我可以提供我自己的genericclioptions.IOStreams来获取 output 吗?
我还查看了标记为 [client-go] 的其他问题,但大多数使用旧示例或使用定义单一kind
的 YAML 文件,并且 API 此后发生了变化。
编辑:因为我需要为多个集群执行此操作,并且正在以编程方式创建集群(AWS EKS API + CloudFormation/ eksctl ),所以我想尽量减少跨多个集群上下文、跨多个 AWS 账户创建ServiceAccount
的开销。 理想情况下,创建我的客户端集所涉及的唯一身份验证步骤是使用aws-iam-authenticator使用集群数据(名称、区域、CA 证书等)获取令牌。 有一段时间没有发布 aws-iam-authenticator,但是master
的内容允许使用第三方角色跨账户角色和外部 ID 传递。 IMO,这比使用ServiceAccount
(和IRSA )更干净,因为应用程序(后端 API)需要与之交互。
编辑:我最近发现了 https://github.com/ericchiang/k8s 。 在高层次上,它绝对比 client-go 更简单,但不支持这种行为。
It sounds like you've figured out how to deserialize YAML files into Kubernetes runtime.Object
s, but the problem is dynamically deploying a runtime.Object
without writing special code for each Kind.
kubectl
通过直接与REST API交互来实现这一点。 具体来说,通过resource.Helper 。
在我的代码中,我有类似的东西:
import (
meta "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/apimachinery/pkg/runtime"
)
func createObject(kubeClientset kubernetes.Interface, restConfig rest.Config, obj runtime.Object) error {
// Create a REST mapper that tracks information about the available resources in the cluster.
groupResources, err := restmapper.GetAPIGroupResources(kubeClientset.Discovery())
if err != nil {
return err
}
rm := restmapper.NewDiscoveryRESTMapper(groupResources)
// Get some metadata needed to make the REST request.
gvk := obj.GetObjectKind().GroupVersionKind()
gk := schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}
mapping, err := rm.RESTMapping(gk, gvk.Version)
if err != nil {
return err
}
name, err := meta.NewAccessor().Name(obj)
if err != nil {
return err
}
// Create a client specifically for creating the object.
restClient, err := newRestClient(restConfig, mapping.GroupVersionKind.GroupVersion())
if err != nil {
return err
}
// Use the REST helper to create the object in the "default" namespace.
restHelper := resource.NewHelper(restClient, mapping)
return restHelper.Create("default", false, obj, &metav1.CreateOptions{})
}
func newRestClient(restConfig rest.Config, gv schema.GroupVersion) (rest.Interface, error) {
restConfig.ContentConfig = resource.UnstructuredPlusDefaultContentConfig()
restConfig.GroupVersion = &gv
if len(gv.Group) == 0 {
restConfig.APIPath = "/api"
} else {
restConfig.APIPath = "/apis"
}
return rest.RESTClientFor(&restConfig)
}
我能够在我的一个项目中实现这一点。 我必须使用kubectl
的 apply 命令中的大部分源代码才能使其正常工作。
https://github.com/billiford/go-clouddriver/blob/master/pkg/kubernetes/client.go#L63
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.