Operator-sdk: Question on reconcile of primary resources

Created on 8 Aug 2019  路  8Comments  路  Source: operator-framework/operator-sdk

Type of question

Help around the operator-sdk?

Question

This question is theoretical. I am just getting started working with the operator sdk.
I would like to reconcile on primary resources which are not created by our operator. For example, I would like the primary resources that we (say operator x) are monitoring are managed by another operator (say operator y). If operator y manages a database cluster (for example), my operator x would like to create a monitoring endpoint for every database cluster that's created. Is this possible currently with the Operator SDK?

Environment

  • operator-sdk version: v0.9.0

insert release or Git SHA here

  • Kubernetes version information:

    insert output of kubectl version here

Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.11", GitCommit:"637c7e288581ee40ab4ca210618a89a555b6e7e9", GitTreeState:"clean", BuildDate:"2018-11-26T14:38:32Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.11", GitCommit:"637c7e288581ee40ab4ca210618a89a555b6e7e9", GitTreeState:"clean", BuildDate:"2018-11-26T14:25:46Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

  • Kubernetes cluster kind:

Additional context
Add any other context about the question here.

Most helpful comment

HI @satishrao84,

So far I did not it, however, I cannot see why not it would not be possible. Note that in the reconcile you will be able to CRUD ( create/read/update/delete) objects which can be from your own APIs defined in your operator project, and/or k8s API, and/or any third-party as OCP. Also, see that you are able to dynamically, programmatically and in run-time execution perform the operations using the k8s client provided by the framework.

Following an example.

In the following code, for example, you will GET any Deployment which has the name = and it is in the by using the client-go. No matter if it was created or not by your operator project and the same idea can be applied to any object which would like to manage in the cluster.

  deployment := &appsv1.Deployment{}
  err := r.client.Get(context.TODO(), types.NamespacedName{Name: <name>, Namespace: <namespace>}, deployment)
  return deployment, err

So, regards your need/example "my operator X would like to create N for every Z that's was created by the operator Y.", I understand that you can for example to do:

  • Watch and/or List the objects which represent its Z created by the operator Y and/or the objects which were a result of the operator Y management ( E.g. pods created based on the deployments created by it )
  • Then you will create the objects which represent your solution/needs for each Z and/or for example pod found.

I hope that it is what you were looking for.

All 8 comments

HI @satishrao84,

So far I did not it, however, I cannot see why not it would not be possible. Note that in the reconcile you will be able to CRUD ( create/read/update/delete) objects which can be from your own APIs defined in your operator project, and/or k8s API, and/or any third-party as OCP. Also, see that you are able to dynamically, programmatically and in run-time execution perform the operations using the k8s client provided by the framework.

Following an example.

In the following code, for example, you will GET any Deployment which has the name = and it is in the by using the client-go. No matter if it was created or not by your operator project and the same idea can be applied to any object which would like to manage in the cluster.

  deployment := &appsv1.Deployment{}
  err := r.client.Get(context.TODO(), types.NamespacedName{Name: <name>, Namespace: <namespace>}, deployment)
  return deployment, err

So, regards your need/example "my operator X would like to create N for every Z that's was created by the operator Y.", I understand that you can for example to do:

  • Watch and/or List the objects which represent its Z created by the operator Y and/or the objects which were a result of the operator Y management ( E.g. pods created based on the deployments created by it )
  • Then you will create the objects which represent your solution/needs for each Z and/or for example pod found.

I hope that it is what you were looking for.

Thank you @camilamacedo86 !! This is exactly what I was looking for.

@camilamacedo86 / anyone else

If the other operator (Y) is not open source, how would I be able to import its APIs (in your example, appsv1) to get the deployments that it is responsible for? An operator is self contained, right? How can my operator X access operator Y's apis?

Note that for example the operator A will not access the operator B, the operators will read/update/delete/create objects in the cluster. So, if in the cluster has applied into it the definition ( CRD ) and the object(CR) X managed by operator B then the operator A would be able to read it for example.

However, see that for you are able to make the operator A be able to read the objects created by operator B then you will probably need to add its schema from the operator B in the operator A, like it is done in the following code example which is adding the route scheme from OCP API.

(main.go file)

import (
        ....
    routev1 "github.com/openshift/api/route/v1"
        ....
)

func main() {
         .... 
         log.Info("Registering Components.")
    // Setup Scheme for all resources
    if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
        log.Error(err, "")
        os.Exit(1)
    }
    //Add route Openshift scheme
    if err := routev1.AddToScheme(mgr.GetScheme()); err != nil {
        log.Error(err, "")
        os.Exit(1)
    }
    // Setup all Controllers
    if err := controller.AddToManager(mgr); err != nil {
        log.Error(err, "")
        os.Exit(1)
    }
        ....
}

I'd recommend you check this part of the docs.

@camilamacedo86 thanks for your response. To add the scheme from the Operator B (external operator) in the Operator A (my operator), I would need access to Operator B's code base i.e. github.com/operatorB/... What if Operator B is not on github (or any other public repo) and the vendor for Operator B has not made the code base publicly available? How can I import the Operator B's APIs to add its scheme in OPerator A?

HI @satishrao84,

So, I understand that your question is no longer related to Operator Framework itself. What you are looking for is how to import and dealing with private deps in go.

Following a few links that may help you.

With dep:

With go modules:

Please, let us know if your question is answered or if has any other reason for it is kept open.

Thanks @camilamacedo86

From what you're saying, In case the dependency (operator API) is not on github or any other public repo, there's no way to use that operator managed objects as primary resources by another operator. Is that what you're saying?

Just wanted to confirm this.

Thanks!

@satishrao84 If there is no source code available whatsoever for the third-party API, you have two options:

  1. Re-implement the third-party types (or at least what you need from them) and AddToScheme functions in your codebase
  2. Use unstructured.Unstructured as the type, add that to your scheme, and use the unstructured object's .Object field to access the YAML as map[string]interface{}

Adding the scheme mapping for unstructured.Unstructured looks like this:

gvk := schema.GroupVersionKind{
    Group: "db.example.com",
    Version: "v1alpha1",
    Kind: "ThirdPartyDatabase",
}
mgr.GetScheme().AddKnownTypeWithName(gvk, &unstructured.Unstructured{})
metav1.AddToGroupVersion(mgr.GetScheme(), gvk.GroupVersion())

I think we've answered the original question, so I'll go ahead and close. But if you have any follow up questions or need any clarifications, just let us know and we can definitely help out!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

flickerfly picture flickerfly  路  5Comments

gyliu513 picture gyliu513  路  3Comments

hasbro17 picture hasbro17  路  3Comments

linuxbsdfreak picture linuxbsdfreak  路  4Comments

lsalazar1 picture lsalazar1  路  4Comments