environment-agent

module
v0.0.0-...-f0edac9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: Apache-2.0

README

DCM Environment Agent

The Environment Agent is a lightweight process that runs in a target environment, acting as the intermediary between DCM and the Service Providers deployed in that environment.

It registers the environment to DCM, consumes resource operation requests from a messaging system (NATS), and routes them to the appropriate Service Provider.

Architecture

The agent supports a hybrid SP model:

  • Embedded SPs: SP code shipped within the agent binary (K8s Container, ACM Cluster, KubeVirt), enabled via configuration.
  • External SPs: Standalone SP processes that register to the agent via the REST API (POST /api/v1alpha1/providers).

Only one SP — embedded or external — may serve a given service type per agent.

For the full design, see the Environment Agent enhancement.

Development

Prerequisites
Build and Run
make build      # Build the binary
make run        # Run the agent
make test       # Run unit tests
make lint       # Run golangci-lint
make fmt        # Format code
make vet        # Run go vet
API Development

This project uses OpenAPI-first development. To modify the API:

  1. Edit api/v1alpha1/openapi.yaml
  2. Run make generate-api to regenerate code
  3. Run make check-aep to validate AEP compliance

Never edit generated files (*.gen.go) directly.

Container Image
make image-build   # Build container image using podman/docker
Local deployment (Kind + Compose)

See deploy/DEPLOY.md for full setup. This requires Kind with a running cluster and the utilities repo as a sibling directory (../utilities):

cp deploy/.env.example deploy/.env
make install-kubevirt          # when vm is in AGENT_EMBEDDED_SPS
make kubeconfig-for-compose
make compose-up
make kind-connect
make deploy-verify

For integration with the control-plane stack, see control-plane deploy/docs/environment-agent-kind.md.

To run the agent on the same cluster as embedded SP workloads, see deploy/docs/in-cluster.md (make k8s-deploy on Kind).

For how to enable each embedded SP (container, vm, cluster, storage, network) and the full list of environment variables each one reads, see deploy/docs/embedded-sps.md.

Control Plane Authentication

The agent authenticates outbound HTTP requests (registration and heartbeat) to the DCM control plane when the control plane has authentication enabled. Two modes are available; when neither is configured, requests are sent without an Authorization header (backward-compatible default).

See the DCM authentication user guide (dcm-project.github.io#22) for the control plane's OIDC/JWT behavior, issuer/audience configuration, and the reference Keycloak stack used by make compose-up.

The agent obtains short-lived JWTs from an OIDC token endpoint using the client_credentials grant, and refreshes them automatically before expiry.

Variable Description
DCM_AUTH_TOKEN_ENDPOINT OIDC token endpoint URL (e.g. https://keycloak:8443/realms/dcm/protocol/openid-connect/token)
DCM_AUTH_CLIENT_ID OAuth2 client ID for the agent's service account
DCM_AUTH_CLIENT_SECRET OAuth2 client secret

Caution: Using http:// for DCM_AUTH_TOKEN_ENDPOINT sends the client secret unencrypted on every token request. The agent logs a startup warning in this case; prefer https:// in any non-local deployment. The agent also refuses to follow any HTTP redirect returned by the token endpoint, so the client secret is never resent to a different origin.

All three variables must be set together. Partial configuration (e.g. endpoint without client ID) causes a startup-fatal error identifying the missing fields.

Tokens are cached in memory and refreshed proactively (with a 10-second safety buffer before the expires_in deadline). Token fetch failures do not crash the agent — the affected registration or heartbeat call fails and is retried by the existing backoff logic.

Keycloak Setup
  1. Create a service-account client in the DCM realm (e.g. environment-agent).
  2. Enable Client authentication and Service accounts roles.
  3. Assign the role(s) the control plane expects for agent registration.
  4. Attach an audience mapper for aud=dcm-api to the client (see caveat below).
  5. Set the three DCM_AUTH_* variables to the client's credentials.

Known gap — audience mapper missing for the agent client: The control plane requires aud=dcm-api on inbound tokens, but in the reference Keycloak realm the dcm-api audience mapper is shipped attached only to the dcm-proxy and dcm-cli clients, not to the agent's service-account client. Validating the full registration flow against that stock realm produces a token without the dcm-api audience, and the control plane rejects it with 401. Until an equivalent mapper is added for the agent's client (or the realm's reference config is updated upstream), explicitly attach a dcm-api audience mapper to the client created above before relying on Mode 1 against the reference stack.

Kubernetes Secrets

Store the client secret in a Kubernetes Secret and inject it via envFrom:

envFrom:
  - secretRef:
      name: environment-agent-auth

This avoids embedding secrets in pod specs or config maps.

Mode 2: Static Bearer Token (dev / simple deployments)
Variable Description
DCM_AUTH_TOKEN A pre-obtained JWT sent as-is on every request

Limitations:

  • No automatic refresh. When the token expires, the control plane returns 401, which is a non-retryable error — registration halts permanently until the agent is restarted with a fresh token.
  • Intended for development or short-lived environments where token lifetime exceeds the agent's expected uptime.
Config Precedence

If both modes are configured, client credentials takes precedence over the static token. The resolution order is:

  1. Client Credentials — DCM_AUTH_TOKEN_ENDPOINT + DCM_AUTH_CLIENT_ID + DCM_AUTH_CLIENT_SECRET all set
  2. Static Token — DCM_AUTH_TOKEN set
  3. No Auth — neither configured (no Authorization header)

API Endpoints

Method Endpoint Description
GET /api/v1alpha1/health Agent health check
GET /api/v1alpha1/providers List all SPs (includes health)
POST /api/v1alpha1/providers External SP registration
GET /api/v1alpha1/providers/{provider_id} Get a single SP by ID

Embedded SPs do not expose their service-type REST APIs (for example /api/v1alpha1/volumes) on the agent. The agent forwards create and delete in-process (or to external SP HTTP endpoints). List and get of provisioned instances go through the control-plane SP Resource Manager:

  • GET /api/v1alpha1/service-type-instances?service_type=<type>
  • GET /api/v1alpha1/service-type-instances/{instance_id}

Instance status is updated when embedded SPs publish CloudEvents to NATS and the SPRM status consumer processes them.

Error format

Errors follow RFC 9457 (application/problem+json). The type member is a project-controlled URI that identifies the problem, and each type always carries the same title and HTTP status:

type Status title
https://dcm-project.github.io/problems/invalid-argument 400 Invalid argument
https://dcm-project.github.io/problems/unauthenticated 401 Unauthenticated
https://dcm-project.github.io/problems/permission-denied 403 Permission denied
https://dcm-project.github.io/problems/not-found 404 Not found
https://dcm-project.github.io/problems/already-exists 409 Already exists
https://dcm-project.github.io/problems/unprocessable-entity 422 Unprocessable entity
https://dcm-project.github.io/problems/internal 500 Internal Server Error
https://dcm-project.github.io/problems/unavailable 503 Service unavailable

make check-problem-uris guards against reintroducing the IANA-reserved dcm.example.com documentation domain (RFC 2606) as a problem type URI.

63a545b (feat(api)!: migrate error handling from RFC 7807 to RFC 9457)

License

Apache 2.0 — see LICENSE for details.

Directories

Path Synopsis
api
cluster/v1alpha1
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
container/v1alpha1
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
database/v1alpha1
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
network/v1alpha1
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
storage/v1alpha1
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
v1alpha1
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
vm/v1alpha1
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
Package v1alpha1 provides primitives to interact with the openapi HTTP API.
cmd
internal
api/server
Package server provides primitives to interact with the openapi HTTP API.
Package server provides primitives to interact with the openapi HTTP API.
apiserver
Package apiserver provides HTTP server lifecycle management for the environment agent.
Package apiserver provides HTTP server lifecycle management for the environment agent.
auth
Package auth provides JWT authentication middleware for the environment agent API.
Package auth provides JWT authentication middleware for the environment agent API.
backoff
Package backoff provides utilities for calculating exponential backoff durations with jitter.
Package backoff provides utilities for calculating exponential backoff durations with jitter.
cloudevent
Package cloudevent provides CloudEvent v1.0 construction utilities.
Package cloudevent provides CloudEvent v1.0 construction utilities.
config
Package config handles environment-based configuration loading and validation.
Package config handles environment-based configuration loading and validation.
dcm
Package dcm implements DCM registration and heartbeat lifecycle management.
Package dcm implements DCM registration and heartbeat lifecycle management.
embedded
Package embedded wires in-process OpenShift service providers into the agent.
Package embedded wires in-process OpenShift service providers into the agent.
embedded/cluster
Package cluster embeds the ACM cluster service provider in the agent.
Package cluster embeds the ACM cluster service provider in the agent.
embedded/container
Package container embeds the k8s container service provider in the agent.
Package container embeds the k8s container service provider in the agent.
embedded/network
Package network embeds the k8s network service provider in the agent.
Package network embeds the k8s network service provider in the agent.
embedded/storage
Package storage embeds the storage service provider in the agent.
Package storage embeds the storage service provider in the agent.
embedded/util
Package util provides small helpers shared by embedded service provider handlers.
Package util provides small helpers shared by embedded service provider handlers.
embedded/vm
Package vm embeds the KubeVirt VM service provider in the agent.
Package vm embeds the KubeVirt VM service provider in the agent.
handler
Package handler implements the strict server interface by delegating to domain services.
Package handler implements the strict server interface by delegating to domain services.
health
Package health implements the health service domain logic.
Package health implements the health service domain logic.
health/monitor
Package monitor implements SP health monitoring: periodic checks, state machine transitions, and provider lifecycle management.
Package monitor implements SP health monitoring: periodic checks, state machine transitions, and provider lifecycle management.
httperror
Package httperror provides RFC 9457 problem details construction and mapping.
Package httperror provides RFC 9457 problem details construction and mapping.
messaging
Package messaging provides NATS/JetStream messaging client and topic management.
Package messaging provides NATS/JetStream messaging client and topic management.
openshift/acmcluster/app
Package app wires the ACM cluster service provider for in-process (embedded) use.
Package app wires the ACM cluster service provider for in-process (embedded) use.
openshift/acmcluster/cluster
Package cluster provides shared labels, conversion utilities, and platform-agnostic operations for cluster service provider implementations.
Package cluster provides shared labels, conversion utilities, and platform-agnostic operations for cluster service provider implementations.
openshift/acmcluster/cluster/baremetal
Package baremetal implements the BareMetal (Agent) platform Create operation.
Package baremetal implements the BareMetal (Agent) platform Create operation.
openshift/acmcluster/cluster/clustertest
Package clustertest provides shared test helpers for cluster service tests.
Package clustertest provides shared test helpers for cluster service tests.
openshift/acmcluster/cluster/dispatcher
Package dispatcher implements a composite ClusterService that dispatches operations to platform-specific sub-services.
Package dispatcher implements a composite ClusterService that dispatches operations to platform-specific sub-services.
openshift/acmcluster/cluster/kubevirtprovider
Package kubevirtprovider implements the KubeVirt platform Create operation.
Package kubevirtprovider implements the KubeVirt platform Create operation.
openshift/acmcluster/config
Package config provides environment-based configuration for the embedded cluster service provider.
Package config provides environment-based configuration for the embedded cluster service provider.
openshift/acmcluster/health
Package health implements dependency health checks for the service provider.
Package health implements dependency health checks for the service provider.
openshift/acmcluster/monitoring
Package monitoring watches HostedCluster and NodePool resources and publishes status CloudEvents.
Package monitoring watches HostedCluster and NodePool resources and publishes status CloudEvents.
openshift/acmcluster/service
Package service defines domain service interfaces for the service provider.
Package service defines domain service interfaces for the service provider.
openshift/acmcluster/service/status
Package status maps HyperShift conditions to DCM cluster statuses.
Package status maps HyperShift conditions to DCM cluster statuses.
openshift/acmcluster/util
Package util provides small generic helper functions.
Package util provides small generic helper functions.
openshift/acmcluster/version
Package version maps OpenShift release versions to Kubernetes minor versions.
Package version maps OpenShift release versions to Kubernetes minor versions.
openshift/container/app
Package app wires the container service provider for in-process (embedded) use.
Package app wires the container service provider for in-process (embedded) use.
openshift/container/config
Package config handles configuration loading from environment variables.
Package config handles configuration loading from environment variables.
openshift/container/dcm
Package dcm defines shared constants and conventions for the DCM service provider.
Package dcm defines shared constants and conventions for the DCM service provider.
openshift/container/kubernetes
Package kubernetes implements the container store using Kubernetes resources.
Package kubernetes implements the container store using Kubernetes resources.
openshift/container/monitoring
Package monitoring watches Kubernetes resources managed by DCM and publishes status change events via CloudEvents over NATS.
Package monitoring watches Kubernetes resources managed by DCM and publishes status change events via CloudEvents over NATS.
openshift/container/store
Package store defines the container repository interface and error types.
Package store defines the container repository interface and error types.
openshift/container/units
Package units provides resource unit conversions between API and Kubernetes formats.
Package units provides resource unit conversions between API and Kubernetes formats.
openshift/container/validate
Package validate provides request validation for container create operations.
Package validate provides request validation for container create operations.
openshift/database/config
Package config handles configuration loading from environment variables
Package config handles configuration loading from environment variables
openshift/database/dcm
Package dcm defines shared constants and conventions for the DCM service provider.
Package dcm defines shared constants and conventions for the DCM service provider.
openshift/database/kubernetes
Package kubernetes implements the database store using Kubernetes resources.
Package kubernetes implements the database store using Kubernetes resources.
openshift/database/store
Package store defines the database repository interface and error types.
Package store defines the database repository interface and error types.
openshift/database/units
Package units provides resource unit conversions between API and Kubernetes formats.
Package units provides resource unit conversions between API and Kubernetes formats.
openshift/database/validate
Package validate provides request validation for database create operations.
Package validate provides request validation for database create operations.
openshift/kubeconfig
Package kubeconfig builds Kubernetes REST configs for embedded SPs.
Package kubeconfig builds Kubernetes REST configs for embedded SPs.
openshift/kubevirtvm/app
Package app wires the KubeVirt VM service provider for in-process (embedded) use.
Package app wires the KubeVirt VM service provider for in-process (embedded) use.
openshift/kubevirtvm/config
Package config handles configuration loading for the embedded kubevirt VM service provider.
Package config handles configuration loading for the embedded kubevirt VM service provider.
openshift/kubevirtvm/constants
Package constants defines label keys for kubevirt VM resources.
Package constants defines label keys for kubevirt VM resources.
openshift/kubevirtvm/events
Package events publishes VM status change events to NATS JetStream.
Package events publishes VM status change events to NATS JetStream.
openshift/kubevirtvm/kubevirt
Package kubevirt provides a client for the KubeVirt API.
Package kubevirt provides a client for the KubeVirt API.
openshift/kubevirtvm/monitor
Package monitor watches KubeVirt VM instances and publishes status events.
Package monitor watches KubeVirt VM instances and publishes status events.
openshift/network/app
Package app wires the network service provider for in-process (embedded) use.
Package app wires the network service provider for in-process (embedded) use.
openshift/network/config
Package config handles configuration loading from environment variables.
Package config handles configuration loading from environment variables.
openshift/network/dcm
Package dcm defines shared constants and conventions for the DCM service provider.
Package dcm defines shared constants and conventions for the DCM service provider.
openshift/network/kubernetes
Package kubernetes implements Kubernetes-backed operations for the network SP.
Package kubernetes implements Kubernetes-backed operations for the network SP.
openshift/network/monitoring
Package monitoring watches network service resources managed by DCM and publishes status change events via CloudEvents over NATS.
Package monitoring watches network service resources managed by DCM and publishes status change events via CloudEvents over NATS.
openshift/network/store
Package store defines interfaces for network and health-check abstractions.
Package store defines interfaces for network and health-check abstractions.
openshift/network/validate
Package validate provides request validation for network operations.
Package validate provides request validation for network operations.
openshift/shared
Package shared holds settings common to all embedded OpenShift SPs.
Package shared holds settings common to all embedded OpenShift SPs.
openshift/storage/app
Package app wires the storage service provider for in-process (embedded) use.
Package app wires the storage service provider for in-process (embedded) use.
openshift/storage/config
Package config handles configuration loading from environment variables.
Package config handles configuration loading from environment variables.
openshift/storage/dcm
Package dcm defines shared constants and conventions for the DCM service provider.
Package dcm defines shared constants and conventions for the DCM service provider.
openshift/storage/kubernetes
Package kubernetes implements Kubernetes-backed operations for the storage SP.
Package kubernetes implements Kubernetes-backed operations for the storage SP.
openshift/storage/monitoring
Package monitoring watches Kubernetes PVCs managed by DCM and publishes status change events via CloudEvents over NATS.
Package monitoring watches Kubernetes PVCs managed by DCM and publishes status change events via CloudEvents over NATS.
openshift/storage/store
Package store defines interfaces for storage and health-check abstractions.
Package store defines interfaces for storage and health-check abstractions.
openshift/storage/validate
Package validate provides request validation for storage volume operations.
Package validate provides request validation for storage volume operations.
openshift/util
Package util provides small helpers shared across in-repo OpenShift service providers.
Package util provides small helpers shared across in-repo OpenShift service providers.
openshift/worker
Package worker provides shared lifecycle helpers for embedded SP apps.
Package worker provides shared lifecycle helpers for embedded SP apps.
provider
Package provider implements SP registration and management.
Package provider implements SP registration and management.
provider/service
Package service implements SP registration business logic.
Package service implements SP registration business logic.
provider/store
Package store defines the persistence interface and implementation for SP registrations.
Package store defines the persistence interface and implementation for SP registrations.
ptr
Package ptr provides generic pointer helper functions.
Package ptr provides generic pointer helper functions.
requestctx
Package requestctx provides request-scoped context helpers.
Package requestctx provides request-scoped context helpers.
routing
Package routing dispatches resource operation requests to service providers.
Package routing dispatches resource operation requests to service providers.
routing/retry
Package retry handles retry topic processing triggered by SP health transitions.
Package retry handles retry topic processing triggered by SP health transitions.
routing/routingtest
Package routingtest provides shared test fakes, CE builders, and assertion helpers for routing and retry integration tests.
Package routingtest provides shared test fakes, CE builders, and assertion helpers for routing and retry integration tests.
pkg
client
Package client provides primitives to interact with the openapi HTTP API.
Package client provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL