Multiple Repositories per Namespace

Let several Git repositories deploy into the same namespace and have the Remediator Agent open merge requests only in the repository that owns each violating resource.

Teams often deploy into a shared namespace from more than one repository, for example an API and a worker, each with its own Helm chart. Starting with nirmata-agent v0.2.11, several entries of the repo-namespace mapping can share a targetNamespace, and the Remediator Agent guarantees that:

  • each repository gets merge requests only for the resources it deploys;
  • each violating resource is remediated once, in one repository, so you do not get duplicate merge requests for the same fix (as long as all mappings of the namespace are in one Remediator);
  • a violating resource that no single repository owns is not silently missed: it is reported with a reason and a suggested fix (see Limitations for exceptions).

This applies to localCluster mode only. Namespaces with a single mapping, and argoHub, fluxHub, and VCS targets, behave as before.


How Ownership Is Decided

In a namespace with two or more mappings, the agent reads every mapping’s repository first. Then, before it plans any change or opens any merge request, it picks one owner per violating resource:

  1. Helm release. If the live resource has the meta.helm.sh/release-name annotation (set by helm upgrade --install), the mapping whose chart was rendered with that release owns it. Each mapping’s release is its releaseName, or the release discovered for its chart.
  2. Manifest. Otherwise (no annotation, or it cannot be read), the mapping whose repository has a manifest with that kind and name owns it, if exactly one does.

Child resources. Pods, ReplicaSets and ControllerRevisions, and Jobs created by a CronJob, are never remediated themselves. The agent follows their controller references, up to four levels (for example Pod → ReplicaSet → Deployment), to the resource that carries the Helm release, and decides ownership on that resource:

  • if the controller is owned, the child is dropped and the controller is fixed in its repository;
  • otherwise the controller is reported once, with the child’s violated policies, and the condition message says via N child resource(s);
  • if the chain cannot be followed (the controller was deleted or recreated, or reading it is forbidden), or the controller does not violate and has no Helm release, the child is left to its parent.

Helm hooks. In a shared namespace, Helm hook resources are mapped to their templates too. If the owning mapping has no template for a hook, the hook is reported as unowned: no source file in owning repo.

Failed mappings. If a mapping of a shared namespace fails to build, or a mapping skips a chart it cannot render or a template it cannot fetch, the agent cannot tell which manifests that repository has. It then does not decide owners from manifests alone:

  • if a mapping fails to build, nothing in the namespace is remediated;
  • if a chart or template is skipped, only resources decided by their Helm release are remediated.

Both cases are reported in ResourcesNotRemediated and retried on the next run.


Prerequisites

  • nirmata-agent v0.2.11 or later, in localCluster mode, configured as described in Getting Started and Configuration.
  • Git credentials for every repository in the mapping. See GitLab or the GitHub options in Configuration.
  • Read access to violating kinds and their controllers. The agent reads object metadata only. It lists each kind of a shared namespace once per run, so it needs get and list on the violating resource kinds and on their controllers. The nirmata-agent chart’s ClusterRole covers the common workload kinds, including controllerrevisions. When listing a kind is forbidden, its objects are read with get. When that is forbidden too, the manifest rule decides.

Configure the Mapping

Add one entry per repository, all with the same targetNamespace. Give each Helm entry that shares a namespace its own releaseName, set to the release that repository deploys:

apiVersion: v1
kind: ConfigMap
metadata:
  name: repo-namespace-mapping
  namespace: nirmata
data:
  mapping: |
    [
      {"repo": "https://gitlab.example.com/payments/api",    "branch": "main", "path": "chart", "targetNamespace": "payments", "releaseName": "payments-api"},
      {"repo": "https://gitlab.example.com/payments/worker", "branch": "main", "path": "chart", "targetNamespace": "payments", "releaseName": "payments-worker"}
    ]

The Remediator references the ConfigMap as usual:

spec:
  environment:
    type: localCluster
  target:
    localCluster:
      repoNamespaceMappingRef:
        name: repo-namespace-mapping
        namespace: nirmata
        key: mapping

releaseName is optional for a single mapping, but set it explicitly when:

  • several mappings in the namespace render Helm charts;
  • the same chart is installed more than once in the namespace;
  • Helm stores releases in ConfigMaps (HELM_DRIVER=configmap).

Without it, the agent looks up the deployed release of the chart (by Chart.yaml name) in targetNamespace. If there is none, or more than one, it falls back to the Chart.yaml name, and the rendered resource names may not match the live ones.

Cluster-scoped resources follow their own rules. See Cluster-Scoped Resources.


Resources That Are Not Remediated

Violating resources without a single owner get no merge request. Each one is reported with a reason:

ReasonMeaningWhat to do
unowned: release not mappedThe resource’s Helm release is rendered by no mappingAdd a mapping for the repo that deploys the release
unowned: not managed by a mapped repoNo Helm release, and no mapped repo has the manifest (applied with kubectl, created by an operator)Fix it manually, or map the repo it comes from
unowned: no source file in owning repoThe release’s mapping has no manifest for it (for example a Helm hook)Fix it in the owning repo manually
ambiguous: releaseSeveral mappings render the resource’s releaseSet a distinct, correct releaseName on each mapping
ambiguous: manifestNo Helm release, and several mapped repos have the manifestRemove the unintended copy, or deploy the resource with Helm so its release decides

Where to see them

Remediator condition. A run-level ResourcesNotRemediated condition (status Failed, no collector ID) lists the resources. The controller also logs the full list at warning level.

Metrics. Two gauges, set on every run (including 0), count them per shared namespace:

MetricLabelsCounts
remediator_unowned_resourcestarget_namespace, namespace, k8s_uidViolating resources that no mapping owns
remediator_ambiguous_resourcestarget_namespace, namespace, k8s_uidViolating resources that several mappings claim

RemediationRecord. The same list is stored on a per-namespace RemediationRecord with the label serviceagents.nirmata.io/record-kind: namespace and the collector ID namespace/<clusterID>/<namespace>. Each entry of status.notRemediated[] has:

FieldContent
resourceKeyKind/namespace/name
policiesThe violated policies
reasonReleaseNotMapped, NotHelmManaged, NoSourceFileInOwningRepo, AmbiguousRelease, or AmbiguousManifest (in the order of the table above)
releaseThe Helm release, when there is one
candidatesThe repositories that could own it (<url>#<path> when one repo is mapped more than once)

At most 200 entries are kept; notRemediatedTotal holds the full count. The record is written at the end of each run with the run ID and the combined phase of the namespace’s mappings (Failed if any failed, else Succeeded if any succeeded, else Skipped). It is created only when there is something to report, and emptied (not deleted) once there is nothing left.

Nirmata Control Hub. Open Agent Hub, select the Remediator, and open a run from the Runs tab:

  • The Results tab groups outcomes into Merge requests (n) and Not remediated (n). When the stored list was truncated, the group shows “Showing n of total”. Each merge request card shows its repository, for example MR payments/api!88 on GitLab or PR org/repo#42 on GitHub.

  • Each Not remediated card shows the resource, a reason tag, the violated policies, the Helm release, the candidate repositories, and the suggested fix.

  • The Summary tab shows a warning with the count and a per-reason breakdown, a See Results button that opens the Results tab, and a Not remediated row. Neither appears when every resource was remediated.


Mapping Validation

The agent checks entries that share a namespace and reports problems in a run-level RepoMappingsAmbiguous condition (status Failed, no collector ID). It is a warning: the run is not failed and every entry is still processed, but resources whose owner cannot be decided get no merge request until the mapping is fixed. For example, when two entries set the same releaseName, the resources of that release are reported as ambiguous: release, and the resources of the release the other entry should have named are reported as unowned: release not mapped.

ProblemFix
Two entries set the same releaseNameSet each entry’s releaseName to the release its repo deploys
Two entries have the same repo and branch, and one path equals or contains the otherUse paths that do not contain each other
An entry has no releaseName and its chart has more than one deployed release in the namespace (checked during the run, when the chart is rendered)Set releaseName; without it the chart is rendered with its Chart.yaml name and its resources do not match the live ones
Two entries have no releaseName and their charts were rendered with the same release (checked during the run)Set a distinct releaseName on each entry; until then their resources are reported as ambiguous: release
A Helm entry has no releaseName and no deployed release was found for its chart, so it was rendered with its Chart.yaml name (checked during the run)Set releaseName to the release the repo deploys, or check that the chart is deployed in the namespace

The condition shows up to five problems; the controller logs list all of them.


When Ownership Moves

The owner of a resource can change between runs, for example when an app moves to another repository or a mapping’s releaseName changes. The agent then cleans up the old repository’s open merge request so the fix is not duplicated or lost:

SituationWhat happens
Full move: none of the merge request’s resources is still owned by its repositoryThe old merge request is closed with a comment linking the new owner’s merge request
Partial move: the merge request still fixes some resourcesThe old merge request is updated in place: it keeps the fixes for the resources that stayed, drops the changes for the moved ones, and gets a comment naming the moved resources and their new merge requests
Move back: a resource returns to a repository whose merge request was closed for itA new merge request is opened in that repository, and the other repository’s merge request is closed
No replacement yet (the new owner is in dry-run or issue mode, or its merge request could not be created)The old merge request is commented on but not closed. A later run closes it once the replacement exists
The provider cannot close, or closing failsThe merge request is commented on and left open

Merge requests recorded before v0.2.11 are commented on but never closed automatically.

The run-level SupersededMRs condition reports what was done: Completed with counts, or Failed when something could not be done, for example left open: ... or still contains changes for ... (retried next run).


Verify the Setup

Editing the mapping ConfigMap is picked up on the next scheduled run. To start a run now, restart the agent:

kubectl -n nirmata rollout restart deploy/nirmata-agent

After the run, check the run-level conditions:

kubectl -n nirmata get remediator <name> -o yaml

kubectl -n nirmata get remediator <name> -o json \
  | jq -r '.status.conditions[] | select(.type=="ResourcesNotRemediated" or .type=="RepoMappingsAmbiguous" or .type=="SupersededMRs") | "\(.type) \(.status) \(.message)"'

List the per-namespace records and the resources they report:

kubectl -n nirmata get remediationrecords -l serviceagents.nirmata.io/record-kind=namespace

kubectl -n nirmata get remediationrecords -l serviceagents.nirmata.io/record-kind=namespace -o json \
  | jq -r '.items[].status | "total: \(.notRemediatedTotal // 0)", (.notRemediated[]? | "\(.reason) \(.resourceKey)")'

The merge request links are stored on the per-repository RemediationRecords, not on the Remediator. List them and confirm that each merge request was opened only in the repository that deploys the resource:

kubectl -n nirmata get remediationrecords -l '!serviceagents.nirmata.io/record-kind' -o json \
  | jq -r '.items[] | .spec.collectorId as $c | .status.resourceStates // {} | to_entries[] | .value.actionMetadata.gitPRAction.prs[]?.prUrl | "\($c) \(.)"'

The agent opens one merge request per changed file, so a repository can get several merge requests in one run (for example one per Helm template or values file it edits).


Limitations

These are known in v0.2.11:

  • Prescriptive mode with Helm charts needs a resource recommendation. If a policy only caps a value (for example a CPU limit) and no recommendation exists for the container (for example from a VerticalPodAutoscaler), the Helm resource is skipped with policy-cap-only Helm edits are not supported yet (cannot verify requests <= limits). Prescriptive mode also only acts on policies annotated policies.kyverno.io/category: Resource Management whose deny condition compares the limit, for example limits.cpu GreaterThan <value>.
  • AI mode with multi-document YAML. In a shared namespace, a plain YAML file that holds several resources can get one merge request per resource. Prescriptive mode and Helm charts are not affected.
  • A violating Pod whose controller has no manifest in any mapped repository is not reported.
  • Merge requests created before v0.2.11 are commented on when ownership moves, but never closed automatically.
  • Agent Hub “Not remediated” view needs a Nirmata Control Hub release that includes it. Older releases show only the merge requests.
  • Upgrading from v0.2.10. An LLMConfig secret reference can no longer point to another namespace. Copy the Secret into the LLMConfig’s namespace before upgrading.
  • Cosmetic issues. In prescriptive mode the run condition still says “Generated N remediation plan(s) with LLM validation”, and the CI-check note on a merge request can name Deployment/test-release instead of the real release.

Troubleshooting

SymptomCause and fix
The same resource gets a merge request in two repositoriesThe mappings are in two different Remediators, or RepoMappingsAmbiguous reports a problem. Move all mappings of the namespace into one Remediator’s ConfigMap and fix the reported problems
Resources are reported as ambiguous: releaseTwo entries render the same release. Set a distinct releaseName on each entry
Resources are reported as unowned: release not mappedThe release is deployed from a repository that is not in the mapping, or releaseName does not match the deployed release. Check with helm list -n <namespace>
Nothing in a shared namespace is remediatedOne of the namespace’s mappings failed to build. Check the ProcessingTargetInformation condition and lastRunSummary.errors, fix the mapping, and wait for the next run
A resource applied with kubectl or created by an operator gets no merge requestIt has no Helm release and no mapped repository has its manifest (unowned: not managed by a mapped repo). Fix it manually, or map the repository it comes from