client-go阅读和使用(一) Client客户端对象

7 篇文章 0 订阅
5 篇文章 1 订阅

1.RESTClient客户端

  • 最基础的客户端。其他客户端都是基于它实现。ClientSet客户端

2.ClientSet客户端

  • 最经常使用的客户端。需要提前知道Resource所在的Group和对应的Version信息。
  • 不能直接访问CRD资源。需要通过client-gen重新生成ClientSet才能访问。
// Clientset contains the clients for groups. Each group has exactly one
// version included in a Clientset.
type Clientset struct {
    *discovery.DiscoveryClient
    argoprojV1alpha1 *argoprojv1alpha1.ArgoprojV1alpha1Client
}

// ArgoprojV1alpha1 retrieves the ArgoprojV1alpha1Client
func (c *Clientset) ArgoprojV1alpha1() argoprojv1alpha1.ArgoprojV1alpha1Interface {
    return c.argoprojV1alpha1
}

// Discovery retrieves the DiscoveryClient
func (c *Clientset) Discovery() discovery.DiscoveryInterface {
    if c == nil {
        return nil
    }
    return c.DiscoveryClient
}

// NewForConfig creates a new Clientset for the given config.
// If config's RateLimiter is not set and QPS and Burst are acceptable,
// NewForConfig will generate a rate-limiter in configShallowCopy.
func NewForConfig(c *rest.Config) (*Clientset, error) {
    configShallowCopy := *c
    if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
        if configShallowCopy.Burst <= 0 {
            return nil, fmt.Errorf("burst is required to be greater than 0 when RateLimiter is not set and QPS is set to greater than 0")
        }
        configShallowCopy.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(configShallowCopy.QPS, configShallowCopy.Burst)
    }
    var cs Clientset
    var err error
    cs.argoprojV1alpha1, err = argoprojv1alpha1.NewForConfig(&configShallowCopy)
    if err != nil {
        return nil, err
    }

    cs.DiscoveryClient, err = discovery.NewDiscoveryClientForConfig(&configShallowCopy)
    if err != nil {
        return nil, err
    }
    return &cs, nil
}

3.DynamicClient客户端

  • 可以对任意kubernetes资源进行RESTful操作,包括crd
  • 转换成Unstructured结构类型,Unstructured结构类型是通过map[string]interface{}转换的
type ResourceInterface interface {
   Create(ctx context.Context, obj *unstructured.Unstructured, options metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error)
   Update(ctx context.Context, obj *unstructured.Unstructured, options metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error)
   UpdateStatus(ctx context.Context, obj *unstructured.Unstructured, options metav1.UpdateOptions) (*unstructured.Unstructured, error)
   Delete(ctx context.Context, name string, options metav1.DeleteOptions, subresources ...string) error
   DeleteCollection(ctx context.Context, options metav1.DeleteOptions, listOptions metav1.ListOptions) error
   Get(ctx context.Context, name string, options metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error)
   List(ctx context.Context, opts metav1.ListOptions) (*unstructured.UnstructuredList, error)
   Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
   Patch(ctx context.Context, name string, pt types.PatchType, data []byte, options metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error)
}
// Unstructured allows objects that do not have Golang structs registered to be manipulated
// generically. This can be used to deal with the API objects from a plug-in. Unstructured
// objects still have functioning TypeMeta features-- kind, version, etc.
//
// WARNING: This object has accessors for the v1 standard metadata. You *MUST NOT* use this
// type if you are dealing with objects that are not in the server meta v1 schema.
//
// TODO: make the serialization part of this type distinct from the field accessors.
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:deepcopy-gen=true
type Unstructured struct {
   // Object is a JSON compatible map with string, float, int, bool, []interface{}, or
   // map[string]interface{}
   // children.
   Object map[string]interface{}
}

var _ metav1.Object = &Unstructured{}
var _ runtime.Unstructured = &Unstructured{}
var _ metav1.ListInterface = &Unstructured{}

4.DiscoveryClient客户端

  • 主要用于发现Kubernetes API Server所支持的资源组,资源版本,资源信息。
  • 可以将上述信息存储到本地,用于本地缓存(cache)。
// DiscoveryInterface holds the methods that discover server-supported API groups,
// versions and resources.
type DiscoveryInterface interface {
   RESTClient() restclient.Interface
   ServerGroupsInterface
   ServerResourcesInterface
   ServerVersionInterface
   OpenAPISchemaInterface
}

// CachedDiscoveryInterface is a DiscoveryInterface with cache invalidation and freshness.
// Note that If the ServerResourcesForGroupVersion method returns a cache miss
// error, the user needs to explicitly call Invalidate to clear the cache,
// otherwise the same cache miss error will be returned next time.
type CachedDiscoveryInterface interface {
   DiscoveryInterface
   // Fresh is supposed to tell the caller whether or not to retry if the cache
   // fails to find something (false = retry, true = no need to retry).
   //
   // TODO: this needs to be revisited, this interface can't be locked properly
   // and doesn't make a lot of sense.
   Fresh() bool
   // Invalidate enforces that no cached data that is older than the current time
   // is used.
   Invalidate()
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值