简体   繁体   English

什么是 Kube.netes client-go “clientset”?

[英]What is a Kubernetes client-go "clientset"?

In the kube.netes go client, what is a clientset ?在 kube.netes clientset客户端中,什么是客户端集?

It is defined in multiple places.它在多个地方定义。

  1. In the client-go package. https://github.com/kube.netes/client-go/blob/62b2cb756b8cea8fba00764ff123993eb44dbd48/kube.netes/clientset.go#L120client-go -go package. https://github.com/kube.netes/client-go/blob/62b2cb756b8cea8fba00764ff123993eb44dbd48/kube.netes/clientset.go#L120

  2. In the kube.netes package https://github.com/kube.netes/kube.netes/blob/80e344644e2b6222296f2f03551a8d0273c7cbce/pkg/client/clientset_generated/internalclientset/clientset.go#L64kube.netes package https://github.com/kube.netes/kube.netes/blob/80e344644e2b6222296f2f03551a8d0273c7cbce/pkg/client/clientset_generated/internalclientset/clientset.go#L64

The documentation says the same thing for both of them:文档对他们两个说了同样的话:

Clientset contains the clients for groups. Clientset 包含组的客户端。 Each group has exactly one version included in a Clientset.每个组都有一个版本包含在客户端集中。

This is confusing.这令人困惑。 What is a group?什么是组?

Every resource type in Kubernetes (Pods, Deployments, Services and so on) is a member of an API group . Kubernetes 中的每种资源类型(Pod、部署、服务等)都是API 组的成员。 These logically "group" the different types.这些逻辑上“分组”了不同的类型。 Some examples of groups are组的一些例子是

  • core
  • extensions
  • batch
  • apps
  • authentication
  • autoscaling

Groups also contain versions . 组还包含版本 Versions allow developers to introduce breaking changes to APIs, and manage them as they do.版本允许开发人员对 API 进行重大更改,并按照他们的方式进行管理。 Some examples of versions inside a group组内版本的一些示例

  • core/v1
  • extensions/v1beta
  • apps/v1beta1
  • batch/v1 , batch/v2alpha1 (notice the two versions inside the same group) batch/v1 , batch/v2alpha1 (注意同一组内的两个版本)
  • authentication/v1 , authentication/v1beta1 authentication/v1 , authentication/v1beta1
  • autoscaling/v1 , autoscaling/v2alpha1 autoscaling/v1autoscaling/v2alpha1

So the client documentation is saying that it's creating a different client for every group.因此,客户端文档说它为每个组创建了不同的客户端。

The description given by @Jose Armesto is correct, I would like to support it with a snippet. @Jose Armesto 给出的描述是正确的,我想用一个片段来支持它。

package main 
import (
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/kubernetes"
)

var kubeconfig string

func init() {
   // kubeconfig file parsing
   flag.StringVar(&kubeconfig, "kubeconfig", "", "path to Kubernetes config file")
   flag.Parse()
}

func main() {
   // create the config object from kubeconfig
   config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)

   // create clientset (set of muliple clients) for each Group (e.g. Core), 
   // the Version (V1) of Group and Kind (e.g. Pods) so GVK.
   clientset, err := kubernetes.NewForConfig(config)

   // executes GET request to K8s API to get pods 'cart' from 'prepayment' namespace
   pod, err := clientset.CoreV1().Pods("prepayment").Get("cart", metav1.GetOptions{})
}

The code is actually the same for the two locations.这两个位置的代码实际上是相同的。 The Kube.netes project writes and manages all the code in a mono-repo ( kube.netes/kube.netes ), but then publishes the code they want other projects to consume to other repos to work better with the Go module system. Kube.netes 项目在单一存储库 ( kube.netes/kube.netes ) 中编写和管理所有代码,然后将他们希望其他项目使用的代码发布到其他存储库,以便更好地与 Go 模块系统一起工作。 You can see a reference to the details of this publishing process in this table , though I'm not sure where the process is clearly documented.您可以在此表中看到对此发布过程的详细信息的引用,但我不确定该过程在哪里被清楚地记录下来。

BTW - you should consume this code through k8s.io/client-go as an import, which will fetch from the github.com/kube.netes/client-go repository.顺便说一句 - 您应该通过k8s.io/client-go使用此代码作为导入,它将从github.com/kube.netes/client-go存储库中获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM