Skip to content

Commit 81975d5

Browse files
Feat: Initial refactor for dynamic multi-agent architecture (Part 1)
This commit lays the groundwork for a new architecture where the main vm-dhcp-controller dynamically manages individual vm-dhcp-agent Deployments, one per active IPPool. Changes include: 1. Controller RBAC: Updated the main controller's RBAC permissions to allow full lifecycle management (create, get, list, watch, update, patch, delete) of Deployments and ServiceAccounts in its namespace, and ClusterRoleBindings cluster-wide. 2. IPPool Controller Refactor (`pkg/controller/ippool/controller.go`): - Removed the previous `syncAgentDeployment` function (which managed a single static agent deployment). - Introduced `reconcileAgentForIPPool` as the new core reconciliation logic. - Implemented resource naming helpers (`sanitizeNameForKubernetes`, `getAgentResourceName`). - Implemented deletion logic for agent resources (Deployment, SA, CRB) when an IPPool is deleted or paused. - Implemented create/get logic for agent ServiceAccounts and ClusterRoleBindings (binding to a shared agent ClusterRole), including setting OwnerReferences to the IPPool. - `OnChange` and `OnRemove` handlers now call `reconcileAgentForIPPool`. - The creation/update logic for the agent Deployment itself within `reconcileAgentForIPPool` is still a TODO. 3. Static Agent Helm Templates Removed/Modified: - Deleted `chart/templates/agent-deployment.yaml` and `chart/templates/agent-serviceaccount.yaml`. - Updated `chart/templates/rbac.yaml` to define a new shared ClusterRole for dynamic agent instances (`{{ .Release.Name }}-vm-dhcp-agent-shared-permissions`) and removed the ClusterRoleBinding for the old static agent. - Cleaned up obsolete fields related to the static agent in `chart/values.yaml`. The controller now expects environment variables like `SHARED_AGENT_CLUSTERROLE_NAME` to correctly reference shared resources.
1 parent 51ff3a8 commit 81975d5

File tree

5 files changed

+233
-395
lines changed

5 files changed

+233
-395
lines changed

chart/templates/agent-deployment.yaml

Lines changed: 0 additions & 69 deletions
This file was deleted.

chart/templates/agent-serviceaccount.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

chart/templates/rbac.yaml

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,28 @@ rules:
2525
resources: [ "virtualmachines" ]
2626
verbs: [ "get", "watch", "list" ]
2727
---
28+
# This is the new shared ClusterRole for all dynamically created DHCP agents
2829
apiVersion: rbac.authorization.k8s.io/v1
2930
kind: ClusterRole
3031
metadata:
31-
name: {{ include "harvester-vm-dhcp-controller.name" . }}-agent
32+
# Name needs to be consistent and known by the main controller (e.g., via env var)
33+
# Let's use a name based on Release.Name for now.
34+
# This was previously {{ .Release.Name }}-dhcp-agent-clusterrole
35+
# Let's rename to {{ .Release.Name }}-vm-dhcp-agent-shared-permissions
36+
name: {{ .Release.Name }}-vm-dhcp-agent-shared-permissions
3237
rules:
33-
- apiGroups: [ "network.harvesterhci.io" ]
34-
resources: [ "ippools", "ippools/status" ]
35-
verbs: [ "get", "watch", "list" ]
38+
- apiGroups: ["coordination.k8s.io"]
39+
resources: ["leases"] # For leader election
40+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
41+
- apiGroups: [""]
42+
resources: ["configmaps"] # For leader election if not using leases
43+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
44+
- apiGroups: [""]
45+
resources: ["events"]
46+
verbs: ["create", "patch"]
47+
- apiGroups: ["network.harvesterhci.io"]
48+
resources: ["ippools"] # To get its IPPool config (the one specified in IPPOOL_REF)
49+
verbs: ["get", "watch", "list"] # Needs get for specific IPPool, list/watch for its cache
3650
---
3751
apiVersion: rbac.authorization.k8s.io/v1
3852
kind: ClusterRole
@@ -79,21 +93,22 @@ subjects:
7993
name: {{ include "harvester-vm-dhcp-controller.serviceAccountName" . }}
8094
namespace: {{ .Release.Namespace }}
8195
---
82-
apiVersion: rbac.authorization.k8s.io/v1
83-
kind: ClusterRoleBinding
84-
metadata:
85-
name: {{ include "harvester-vm-dhcp-controller.name" . }}-agent
86-
labels:
87-
{{- include "harvester-vm-dhcp-controller.labels" . | nindent 4 }}
88-
roleRef:
89-
apiGroup: rbac.authorization.k8s.io
90-
kind: ClusterRole
91-
name: {{ include "harvester-vm-dhcp-controller.name" . }}-agent
92-
subjects:
93-
- kind: ServiceAccount
94-
name: {{ include "harvester-vm-dhcp-controller.serviceAccountName" . }}-agent
95-
namespace: {{ .Release.Namespace }}
96-
---
96+
# Removed static agent ClusterRoleBinding
97+
# apiVersion: rbac.authorization.k8s.io/v1
98+
# kind: ClusterRoleBinding
99+
# metadata:
100+
# name: {{ include "harvester-vm-dhcp-controller.name" . }}-agent
101+
# labels:
102+
# {{- include "harvester-vm-dhcp-controller.labels" . | nindent 4 }}
103+
# roleRef:
104+
# apiGroup: rbac.authorization.k8s.io
105+
# kind: ClusterRole
106+
# name: {{ include "harvester-vm-dhcp-controller.name" . }}-agent # This role was also removed and replaced by the shared one
107+
# subjects:
108+
# - kind: ServiceAccount
109+
# name: {{ include "harvester-vm-dhcp-controller.serviceAccountName" . }}-agent # Static agent SA, no longer created by default
110+
# namespace: {{ .Release.Namespace }}
111+
# ---
97112
apiVersion: rbac.authorization.k8s.io/v1
98113
kind: ClusterRoleBinding
99114
metadata:
@@ -117,7 +132,32 @@ metadata:
117132
rules:
118133
- apiGroups: ["apps"]
119134
resources: ["deployments"]
120-
verbs: ["get", "list", "watch", "patch", "update"]
135+
verbs: ["get", "list", "watch", "patch", "update", "create", "delete"]
136+
- apiGroups: [""] # Core API group
137+
resources: ["serviceaccounts"]
138+
verbs: ["create", "get", "list", "watch", "delete"]
139+
---
140+
apiVersion: rbac.authorization.k8s.io/v1
141+
kind: ClusterRole # New ClusterRole to manage ClusterRoleBindings
142+
metadata:
143+
name: {{ include "harvester-vm-dhcp-controller.name" . }}-crb-manager
144+
rules:
145+
- apiGroups: ["rbac.authorization.k8s.io"]
146+
resources: ["clusterrolebindings"]
147+
verbs: ["create", "get", "list", "watch", "delete"]
148+
---
149+
apiVersion: rbac.authorization.k8s.io/v1
150+
kind: ClusterRoleBinding # Bind the new ClusterRole to the controller's SA
151+
metadata:
152+
name: {{ include "harvester-vm-dhcp-controller.name" . }}-manage-crbs
153+
subjects:
154+
- kind: ServiceAccount
155+
name: {{ include "harvester-vm-dhcp-controller.serviceAccountName" . }}
156+
namespace: {{ .Release.Namespace }}
157+
roleRef:
158+
apiGroup: rbac.authorization.k8s.io
159+
kind: ClusterRole
160+
name: {{ include "harvester-vm-dhcp-controller.name" . }}-crb-manager
121161
---
122162
apiVersion: rbac.authorization.k8s.io/v1
123163
kind: RoleBinding

chart/values.yaml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,23 @@ image:
1010
# Overrides the image tag whose default is the chart appVersion.
1111
tag: "main-head"
1212

13-
# Agent configuration
13+
# Agent configuration (defaults for dynamically created agents)
1414
agent:
15-
enabled: true # Controls whether agent deployment and related resources are created
16-
replicaCount: 2
15+
replicaCount: 1 # Each dynamic agent deployment will likely have 1 replica
1716
image:
1817
repository: rancher/harvester-vm-dhcp-agent # Specific agent image
1918
pullPolicy: IfNotPresent
2019
tag: "main-head" # Or specific version for agent
2120
# Flag to disable leader election within the agent pods
21+
# Note: Each dynamic agent instance will perform its own leader election if not disabled.
22+
# Consider if leader election is needed per-agent-instance or if a different HA model is desired.
23+
# For now, keeping this, assuming each agent instance (per IPPool) might need it if it were replicated.
2224
noLeaderElection: false
25+
# serviceAccount.create, .name and rbac.create are no longer used for a single static agent.
26+
# The controller creates SAs and CRBs for each dynamic agent.
27+
# Annotations for dynamically created SAs could be added here if needed.
2328
serviceAccount:
24-
# Specifies whether a service account should be created for the agent
25-
create: true
26-
# Annotations to add to the agent service account
2729
annotations: {}
28-
# The name of the service account to use for the agent.
29-
# If not set and create is true, a name is generated using the fullname template + "-agent"
30-
name: ""
31-
rbac:
32-
# Specifies whether RBAC resources (ClusterRole, ClusterRoleBinding) should be created for the agent
33-
create: true
3430
# Pod security context for agent pods
3531
podSecurityContext: {}
3632
# Security context for agent containers

0 commit comments

Comments
 (0)