AbdelrhmanHamouda / locust-k8s-operator

Deploy and manage Locust tests on Kubernetes.
https://abdelrhmanhamouda.github.io/locust-k8s-operator/
Apache License 2.0
53 stars 12 forks source link

[BUG] CRs Created Outside Operator Namespace #214

Closed M4C4R closed 2 months ago

M4C4R commented 3 months ago

Describe the bug CRs (custom resources) can be created outside the operator namespace, but the operator lacks permissions to fulfill the requests as it attempts to create the jobs/services within the namespace of the CR.

Exception occurred during Job creation: Failure executing: PATCH at: ... Message: jobs.batch "demo-test-master" is forbidden: User "system:serviceaccount:locust:locust-locust-k8s-operator" cannot patch resource "jobs" in API group "batch" in the namespace "...".

Exception occurred during Service deletion: Failure executing: DELETE at: ... Message: services "demo-test-master" is forbidden: User "system:serviceaccount:locust:locust-locust-k8s-operator" cannot delete resource "services" in API group "" in the namespace "...".

To Reproduce Steps to reproduce the behaviour:

  1. Deploy the operator in namespace X
  2. Create a locust test in namespace Y
  3. Observe logs of operator

Expected behaviour I expected the jobs/services to be created within the namespace of the operator, not of the CR.

This is as granting the following at a cluster level is excessive:

  - apiGroups: [ '*' ]
    resources:
      - services
    verbs:
      - get
      - list
      - create
      - update
      - delete
  - apiGroups: [ '*' ]
    resources:
      - jobs
    verbs:
      - get
      - list
      - create
      - update
      - delete
      - patch

Additional context: We plan on bundling our locust tests with our application charts, so they will be deployed into the same namespace as the application - not the operator.

AbdelrhmanHamouda commented 2 months ago

Hello @M4C4R, Thank you for reporting this issue. So few things to unpack here;

does that address all your points?

M4C4R commented 2 months ago

Hi

The Operator "attempting" to deploy the resources in the same namespace the CR was deployed in, is actually a feature not a bug. There are several reasons for this but on of them is to make writing a test easier since the user wont need to worry about k8s namespace networking when trying to figure out which API to call.

Understood, but as it lacks cluster-wide access by default, it may as well only watch its own namespace. Unless of course it can be configured to deploy the resources into its own operator namespace, where it does have access (I still think this is a nice option as we then don't scatter locust pods across namespaces).

Adding a cluster role sounds like an easy win and will unblock our use case, but I do hope you consider being able to control where the locust resources are deployed.

thanks @AbdelrhmanHamouda

AbdelrhmanHamouda commented 2 months ago

@M4C4R i'm going to push a change to this within this week or the one after

AbdelrhmanHamouda commented 2 months ago

this is now in 0.10.0