Operator-sdk: How to get third party resources inside controller code

Created on 12 Feb 2020  路  5Comments  路  Source: operator-framework/operator-sdk

Type of question

how to implement a specific feature

Question

What did you do?
In the operator controller the way to get the list of standard kubernetes objects is like this

listObj := &corev1.PersistentVolumeClaimList{}
    if err := r.client.List(context.TODO(), listObj, client.MatchingLabels{"release": "my-release"}); err != nil {
        return err
    }

There are some custom resources in the cluster those are not managed by this operator, and created using kubectl. So how to get those resource list ? one way to get those following this https://github.com/operator-framework/operator-sdk/blob/master/doc/user-guide.md#adding-3rd-party-resources-to-your-operator but problem is I dont have access to the code of that third party resource to find the type. Inside the cluster only CRD and CR are available. Moreover those thirdparty resources are closed source.

Is there any other way to find those resources inside controller code
What did you expect to see?
Find the list of custom resources without knowing the go type of the resource. using only CRD or CR

Environment

  • operator-sdk version:
  operator-sdk version: "v0.15.1", commit: "e35ec7b722ba095e6438f63fafb9e7326870b486", go version: "go1.13.6 darwin/amd64"
  • Kubernetes version information:
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3", GitCommit:"b3cbbae08ec52a7fc73d334838e18d17e8512749", GitTreeState:"clean", BuildDate:"2019-11-14T04:24:29Z", GoVersion:"go1.12.13", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.5", GitCommit:"20c265fef0741dd71a66480e35bd69f18351daea", GitTreeState:"clean", BuildDate:"2019-10-15T19:07:57Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}
  • Kubernetes cluster kind:
    docker-desktop on mac

Additional context
Goal: The operator will find those thirdparty resources and set the owner reference to itself, so that this operator can watch/manage those resources

triagsupport

Most helpful comment

Thank you very much @camilamacedo86
So far was able to get the list of ApiResoruces

    // Get a config to talk to the apiserver
    cfg, err := config.GetConfig()
    if err != nil {
        //reqLogger.Error(err, "cannot get rest client config")
        fmt.Println(err)
    }
    // Create the discoveryClient
    discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
    if err != nil {
        //reqLogger.Error(err, "Unable to create discovery client")
        fmt.Println(err)
    }
    // Get a list of all API's on the cluster
    apiGroup, apiResourceList, err := discoveryClient.ServerGroupsAndResources()
    if err != nil {
        fmt.Println(err)
    }

    // Looking for group.Name = "apiextensions.k8s.io"
    for i := 0; i < len(apiGroup); i++ {
        fmt.Println(apiGroup[i].Name)
    }
    fmt.Println("resources list")
    for i := 0; i < len(apiResourceList); i++ {
        if apiResourceList[i].GroupVersion == "manager.example.com/v1" {

        }
    }
kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
machines                           en           manager.example.com            true         Machine



md5-8cbf7cbfcabd2022ed12945091cb592b



kubectl get machines
NAME                         AGE
machine-reload   74m

All 5 comments

HI @ffoysal,

Note that by default you will be able to CRUD just resources/objects which are defined in the K8S API. So, for you can CRUD any other type which can be from other operators as well you need to add the schema to the manager.

Since you have the CRDs applied in the cluster, you will be able to discover its data to do the implementation. All steps are fully described in the blog How to use third-party APIs in Operator SDK projects. Note that this blog will use as an example the OCP API, however, you can do the same steps to use any other third-party.

Also, I truly recommend you check its Step 2: Use the Discovery API to see if the new API is present and do the suggested implementation to ensure that the third-party will exists too. There you will see an example over how you can see how to discover and check the API schemas via the go impl as checking the usage of the command kubectl api-resources which can help you with.

In this way, I am closing this since it shows sorted out with the info provided. However, please feel free to ping us here and let us know if you would like to re-open it because still needing help with the same subject.

Thank you very much @camilamacedo86
So far was able to get the list of ApiResoruces

    // Get a config to talk to the apiserver
    cfg, err := config.GetConfig()
    if err != nil {
        //reqLogger.Error(err, "cannot get rest client config")
        fmt.Println(err)
    }
    // Create the discoveryClient
    discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
    if err != nil {
        //reqLogger.Error(err, "Unable to create discovery client")
        fmt.Println(err)
    }
    // Get a list of all API's on the cluster
    apiGroup, apiResourceList, err := discoveryClient.ServerGroupsAndResources()
    if err != nil {
        fmt.Println(err)
    }

    // Looking for group.Name = "apiextensions.k8s.io"
    for i := 0; i < len(apiGroup); i++ {
        fmt.Println(apiGroup[i].Name)
    }
    fmt.Println("resources list")
    for i := 0; i < len(apiResourceList); i++ {
        if apiResourceList[i].GroupVersion == "manager.example.com/v1" {

        }
    }
kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
machines                           en           manager.example.com            true         Machine



md5-8cbf7cbfcabd2022ed12945091cb592b



kubectl get machines
NAME                         AGE
machine-reload   74m

Hi @ffoysal,

Really thank you for your feedback and for let us know that the information provided allow you to achieve your goal.

I know the third-party kind Machine, how can I get the list of machines from inside controller code ?? so that I can delete those third-party instances when operator CR get deleted ??

Hi @ffoysal,

You need to add the schema to the manager. Then, after adding the schema to the manager you can CRUD the resources in the same way that you do with the K8S ones.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nrvnrvn picture nrvnrvn  路  3Comments

danielsig727 picture danielsig727  路  4Comments

kristiandrucker picture kristiandrucker  路  5Comments

linuxbsdfreak picture linuxbsdfreak  路  4Comments

joelanford picture joelanford  路  3Comments