Golang client

If you want to create/get/update/delete those OpenKruise resources in a Golang project or list-watch them using informer, you may need a Golang client for OpenKruise.

In that way, you should use the kruise-api repository, which only includes schema definition and clientsets of Kruise.

DO NOT bring the whole kruise repository as dependency into your project.

Usage

Firstly, import kruise-api into your go.mod file (the version better to be the Kruise version you installed):

  1. require github.com/openkruise/kruise-api v1.7.0
Kubernetes Version in your ProjectImport Kruise-api < v0.10Import Kruise-api >= v0.10
< 1.18v0.x.y (x <= 9)v0.x.y-legacy (x >= 10)
>= 1.18v0.x.y-1.18 (7 <= x <= 9)v0.x.y (x >= 10)

Then, there are two ways to use kruise-api in your code: use it directly or with controller-runtime.

It is recommended that you can use it with controller-runtime if your project is generated by kubebuilder or operator-sdk, which means controller-runtime is already imported in your project. Otherwise, you may use it directly.

Use kruise-api directly

  1. New Kruise client using your rest config:
  1. import kruiseclientset "github.com/openkruise/kruise-api/client/clientset/versioned"
  2. // cfg is the rest config defined in client-go, you should get it using kubeconfig or serviceaccount
  3. kruiseClient := kruiseclientset.NewForConfigOrDie(cfg)
  1. Get/List Kruise resources:
  1. cloneSet, err := kruiseClient.AppsV1alpha1().CloneSets(namespace).Get(name, metav1.GetOptions{})
  2. cloneSetList, err := kruiseClient.AppsV1alpha1().CloneSets(namespace).List(metav1.ListOptions{})
  1. Create/Update Kruise resources:
  1. import kruiseappsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
  2. cloneSet := kruiseappsv1alpha1.CloneSet{
  3. // ...
  4. }
  5. err = kruiseClient.AppsV1alpha1().CloneSets(namespace).Create(&cloneSet, metav1.CreateOptions)
  1. // Get first
  2. cloneSet, err := kruiseClient.AppsV1alpha1().CloneSets(namespace).Get(name, metav1.GetOptions{})
  3. if err != nil {
  4. return err
  5. }
  6. // Modify object, such as replicas or template
  7. cloneSet.Spec.Replicas = utilpointer.Int32Ptr(5)
  8. // Update
  9. // This might get conflict, should retry it
  10. if err = kruiseClient.AppsV1alpha1().CloneSets(namespace).Update(&cloneSet, metav1.UpdateOptions); err != nil {
  11. return err
  12. }
  1. Watch Kruise resources:
  1. import kruiseinformer "github.com/openkruise/kruise-api/client/informers/externalversions"
  2. kruiseInformerFactory := kruiseinformer.NewSharedInformerFactory(kruiseClient, 0)
  3. kruiseInformerFactory.Apps().V1alpha1().CloneSets().Informer().AddEventHandler(...)
  4. kruiseInformerFactory.Start(...)

Use kruise-api with controller-runtime

  1. Add kruise apis into the scheme in your main.go
  1. import kruiseapi "github.com/openkruise/kruise-api"
  2. // ...
  3. _ = kruiseapi.AddToScheme(scheme)
  1. New client

This is needed when use controller-runtime client directly.

If your project is generated by kubebuilder or operator-sdk, you should get the client from mgr.GetClient() instead of the example below.

  1. import "sigs.k8s.io/controller-runtime/pkg/client"
  2. apiClient, err := client.New(c, client.Options{Scheme: scheme})
  1. Get/List
  1. import (
  2. kruiseappsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
  3. "sigs.k8s.io/controller-runtime/pkg/client"
  4. )
  5. cloneSet := kruiseappsv1alpha1.CloneSet{}
  6. err = apiClient.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, &cloneSet)
  7. cloneSetList := kruiseappsv1alpha1.CloneSetList{}
  8. err = apiClient.List(context.TODO(), &cloneSetList, client.InNamespace(instance.Namespace))
  1. Create/Update/Delete

Create a new CloneSet:

  1. import kruiseappsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
  2. cloneSet := kruiseappsv1alpha1.CloneSet{
  3. // ...
  4. }
  5. err = apiClient.Create(context.TODO(), &cloneSet)

Update an existing CloneSet:

  1. import kruiseappsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
  2. // Get first
  3. cloneSet := kruiseappsv1alpha1.CloneSet{}
  4. if err = apiClient.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, &cloneSet); err != nil {
  5. return err
  6. }
  7. // Modify object, such as replicas or template
  8. cloneSet.Spec.Replicas = utilpointer.Int32Ptr(5)
  9. // Update
  10. // This might get conflict, should retry it
  11. if err = apiClient.Update(context.TODO(), &cloneSet); err != nil {
  12. return err
  13. }
  1. List watch and informer

If your project is generated by kubebuilder or operator-sdk and get the client from mgr.GetClient(), then methods like Get/List have already queried from informer instead of apiserver.