AmitKumarDas / metac

It is metacontroller and more
Apache License 2.0
57 stars 16 forks source link

local gctl: auto set namespace selector against watch #102

Open AmitKumarDas opened 4 years ago

AmitKumarDas commented 4 years ago

Problem Statement: I would like to use Metac as a library that is packaged as my own binary. This binary will get deployed as a Kubernetes Deployment or STS. I would like to use MetaController CR as a config that should only consider watches in this Deployment/STS namespace.

grzesuav commented 4 years ago

@AmitKumarDas maybe could you elaborate what is needed to achieve this behavior ?

AmitKumarDas commented 4 years ago

I will try to explain via a sample config for GenericController.

NOTE: Metac config is a yaml file with schema that adheres to one or more GenericController(s). This config is typically placed at /etc/config/metac/ path of the container. This container hosts metac binary that reads this config & in turn starts the informers (one informer per watch mentioned in GenericController). This mode of running metac is also known as local mode.

NOTE: Refer https://github.com/AmitKumarDas/metac/blob/master/start/start.go to learn on this mode of running metac

NOTE: In my use-case this binary is a golang project that uses metac as a library

Following is a sample config that makes use of 2 GenericControllers. On the whole, we want the metac binary to start informers for watches with kind CStorClusterPlan & CStorClusterStorageSet

apiVersion: metac.openebs.io/v1alpha1
kind: GenericController
metadata:
  name: sync-plan
  namespace: cspauto
spec:
  # I want to watch kind CStorClusterPlan that are created under namespace
  # that matches the namespace of metac binary (that has this config file)
  watch:
    apiVersion: dao.mayadata.io/v1alpha1
    resource: cstorclusterplans
  attachments:
  - apiVersion: dao.mayadata.io/v1alpha1
    resource: cstorclusterstoragesets
    updateStrategy:
      method: InPlace
  - apiVersion: dao.mayadata.io/v1alpha1
    resource: cstorclusterconfigs
  hooks:
    sync:
      inline:
        funcName: sync/cstorclusterplan
---
apiVersion: metac.openebs.io/v1alpha1
kind: GenericController
metadata:
  name: sync-storageset
  namespace: cspauto
spec:
  # I want to watch kind CStorClusterStorageSet that are created under namespace
  # that matches the namespace of metac binary (that has this config file)
  watch:
    apiVersion: dao.mayadata.io/v1alpha1
    resource: cstorclusterstoragesets
  attachments:
  - apiVersion: dao.mayadata.io/v1alpha1
    resource: storages
    updateStrategy:
      method: InPlace
  hooks:
    sync:
      inline:
        funcName: sync/cstorclusterstorageset

Coming back to the problem statement, I would like to spin up multiple metac binaries in dedicated namespaces. I want each binary to lookup up the watches that belong to its own namespace & proceed with its reconciliation.

Possible solution:

We can define following new environment variables e.g.:

Now when metac starts, the resulting GenericControllers will be like following:

apiVersion: metac.openebs.io/v1alpha1
kind: GenericController
metadata:
  name: sync-plan
  namespace: cspauto
spec:
  watch:
    apiVersion: dao.mayadata.io/v1alpha1
    resource: cstorclusterplans
    advancedSelector:
      selectorTerms:
      - matchFields:
          metadata.namespace: <POD's namesapce>
  attachments:
  - apiVersion: dao.mayadata.io/v1alpha1
    resource: cstorclusterstoragesets
    updateStrategy:
      method: InPlace
  - apiVersion: dao.mayadata.io/v1alpha1
    resource: cstorclusterconfigs
  hooks:
    sync:
      inline:
        funcName: sync/cstorclusterplan
---
apiVersion: metac.openebs.io/v1alpha1
kind: GenericController
metadata:
  name: sync-storageset
  namespace: cspauto
spec:
  watch:
    apiVersion: dao.mayadata.io/v1alpha1
    resource: cstorclusterstoragesets
    advancedSelector:
      selectorTerms:
      - matchFields:
          metadata.namespace: <POD's namesapce>
  attachments:
  - apiVersion: dao.mayadata.io/v1alpha1
    resource: storages
    updateStrategy:
      method: InPlace
  hooks:
    sync:
      inline:
        funcName: sync/cstorclusterstorageset

Above is applicable when metac binary runs by looking up its config file (described above). In other words metac was started with following args:

 args:
        - --run-as-local