Skip to content

Commit a1186a9

Browse files
committed
feat: add configurable resource settings for MCP tools and agents
- Add Resources field to Agent CRD SharedDeploymentSpec - Update translator to use configurable resources with fallback defaults - Add resource configurations to all MCP tools (grafana-mcp, querydoc, kagent-tools) - Add resource configurations to all agents (k8s, istio, helm, etc.) - Regenerate CRDs with new Resources field - Enable production-ready resource management and security compliance - Refactor inline function to helper function to avoid code duplication Fixes #897 Signed-off-by: Ronit <ronitk964@gmail.com>
1 parent 34e75e4 commit a1186a9

File tree

13 files changed

+403
-18
lines changed

13 files changed

+403
-18
lines changed

go/api/v1alpha2/agent_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ type SharedDeploymentSpec struct {
122122
Env []corev1.EnvVar `json:"env,omitempty"`
123123
// +optional
124124
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
125+
// +optional
126+
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
125127
}
126128

127129
// ToolProviderType represents the tool provider type

go/api/v1alpha2/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/config/crd/bases/kagent.dev_agents.yaml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,66 @@ spec:
25842584
format: int32
25852585
minimum: 1
25862586
type: integer
2587+
resources:
2588+
description: ResourceRequirements describes the compute resource
2589+
requirements.
2590+
properties:
2591+
claims:
2592+
description: |-
2593+
Claims lists the names of resources, defined in spec.resourceClaims,
2594+
that are used by this container.
2595+
2596+
This field depends on the
2597+
DynamicResourceAllocation feature gate.
2598+
2599+
This field is immutable. It can only be set for containers.
2600+
items:
2601+
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
2602+
properties:
2603+
name:
2604+
description: |-
2605+
Name must match the name of one entry in pod.spec.resourceClaims of
2606+
the Pod where this field is used. It makes that resource available
2607+
inside a container.
2608+
type: string
2609+
request:
2610+
description: |-
2611+
Request is the name chosen for a request in the referenced claim.
2612+
If empty, everything from the claim is made available, otherwise
2613+
only the result of this request.
2614+
type: string
2615+
required:
2616+
- name
2617+
type: object
2618+
type: array
2619+
x-kubernetes-list-map-keys:
2620+
- name
2621+
x-kubernetes-list-type: map
2622+
limits:
2623+
additionalProperties:
2624+
anyOf:
2625+
- type: integer
2626+
- type: string
2627+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
2628+
x-kubernetes-int-or-string: true
2629+
description: |-
2630+
Limits describes the maximum amount of compute resources allowed.
2631+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
2632+
type: object
2633+
requests:
2634+
additionalProperties:
2635+
anyOf:
2636+
- type: integer
2637+
- type: string
2638+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
2639+
x-kubernetes-int-or-string: true
2640+
description: |-
2641+
Requests describes the minimum amount of compute resources required.
2642+
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
2643+
otherwise to an implementation-defined value. Requests cannot exceed Limits.
2644+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
2645+
type: object
2646+
type: object
25872647
volumeMounts:
25882648
items:
25892649
description: VolumeMount describes a mounting of a Volume
@@ -4803,6 +4863,66 @@ spec:
48034863
format: int32
48044864
minimum: 1
48054865
type: integer
4866+
resources:
4867+
description: ResourceRequirements describes the compute resource
4868+
requirements.
4869+
properties:
4870+
claims:
4871+
description: |-
4872+
Claims lists the names of resources, defined in spec.resourceClaims,
4873+
that are used by this container.
4874+
4875+
This field depends on the
4876+
DynamicResourceAllocation feature gate.
4877+
4878+
This field is immutable. It can only be set for containers.
4879+
items:
4880+
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
4881+
properties:
4882+
name:
4883+
description: |-
4884+
Name must match the name of one entry in pod.spec.resourceClaims of
4885+
the Pod where this field is used. It makes that resource available
4886+
inside a container.
4887+
type: string
4888+
request:
4889+
description: |-
4890+
Request is the name chosen for a request in the referenced claim.
4891+
If empty, everything from the claim is made available, otherwise
4892+
only the result of this request.
4893+
type: string
4894+
required:
4895+
- name
4896+
type: object
4897+
type: array
4898+
x-kubernetes-list-map-keys:
4899+
- name
4900+
x-kubernetes-list-type: map
4901+
limits:
4902+
additionalProperties:
4903+
anyOf:
4904+
- type: integer
4905+
- type: string
4906+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
4907+
x-kubernetes-int-or-string: true
4908+
description: |-
4909+
Limits describes the maximum amount of compute resources allowed.
4910+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
4911+
type: object
4912+
requests:
4913+
additionalProperties:
4914+
anyOf:
4915+
- type: integer
4916+
- type: string
4917+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
4918+
x-kubernetes-int-or-string: true
4919+
description: |-
4920+
Requests describes the minimum amount of compute resources required.
4921+
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
4922+
otherwise to an implementation-defined value. Requests cannot exceed Limits.
4923+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
4924+
type: object
4925+
type: object
48064926
volumeMounts:
48074927
items:
48084928
description: VolumeMount describes a mounting of a Volume

go/internal/controller/translator/adk_api_translator.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (a *adkApiTranslator) validateAgent(ctx context.Context, agent *v1alpha2.Ag
206206
}
207207

208208
toolAgent := &v1alpha2.Agent{}
209-
err := a.kube.Get(ctx, agentRef, toolAgent)
209+
err = a.kube.Get(ctx, agentRef, toolAgent)
210210
if err != nil {
211211
return err
212212
}
@@ -387,16 +387,7 @@ func (a *adkApiTranslator) buildManifest(
387387
Command: cmd,
388388
Args: dep.Args,
389389
Ports: []corev1.ContainerPort{{Name: "http", ContainerPort: dep.Port}},
390-
Resources: corev1.ResourceRequirements{
391-
Requests: corev1.ResourceList{
392-
corev1.ResourceCPU: resource.MustParse("100m"),
393-
corev1.ResourceMemory: resource.MustParse("384Mi"),
394-
},
395-
Limits: corev1.ResourceList{
396-
corev1.ResourceCPU: resource.MustParse("2000m"),
397-
corev1.ResourceMemory: resource.MustParse("1Gi"),
398-
},
399-
},
390+
Resources: *dep.Resources,
400391
Env: env,
401392
ReadinessProbe: &corev1.Probe{
402393
ProbeHandler: corev1.ProbeHandler{
@@ -486,7 +477,6 @@ func (a *adkApiTranslator) translateInlineAgent(ctx context.Context, agent *v1al
486477
case tool.McpServer != nil:
487478
toolsByServer[tool.McpServer.TypedLocalReference] = append(toolsByServer[tool.McpServer.TypedLocalReference], tool.McpServer.ToolNames...)
488479
case tool.Agent != nil:
489-
490480
agentRef := types.NamespacedName{
491481
Namespace: agent.Namespace,
492482
Name: tool.Agent.Name,
@@ -498,7 +488,7 @@ func (a *adkApiTranslator) translateInlineAgent(ctx context.Context, agent *v1al
498488

499489
// Translate a nested tool
500490
toolAgent := &v1alpha2.Agent{}
501-
err := a.kube.Get(ctx, agentRef, toolAgent)
491+
err = a.kube.Get(ctx, agentRef, toolAgent)
502492
if err != nil {
503493
return nil, nil, nil, err
504494
}
@@ -1003,6 +993,23 @@ type resolvedDeployment struct {
1003993
Labels map[string]string
1004994
Annotations map[string]string
1005995
Env []corev1.EnvVar
996+
Resources *corev1.ResourceRequirements
997+
}
998+
999+
// setDefaultResources sets default resource requirements if not specified
1000+
func setDefaultResources(dep *resolvedDeployment) {
1001+
if dep.Resources == nil {
1002+
dep.Resources = &corev1.ResourceRequirements{
1003+
Requests: corev1.ResourceList{
1004+
corev1.ResourceCPU: resource.MustParse("100m"),
1005+
corev1.ResourceMemory: resource.MustParse("384Mi"),
1006+
},
1007+
Limits: corev1.ResourceList{
1008+
corev1.ResourceCPU: resource.MustParse("2000m"),
1009+
corev1.ResourceMemory: resource.MustParse("1Gi"),
1010+
},
1011+
}
1012+
}
10061013
}
10071014

10081015
func (a *adkApiTranslator) resolveInlineDeployment(agent *v1alpha2.Agent, mdd *modelDeploymentData) (*resolvedDeployment, error) {
@@ -1049,8 +1056,12 @@ func (a *adkApiTranslator) resolveInlineDeployment(agent *v1alpha2.Agent, mdd *m
10491056
Labels: maps.Clone(spec.Labels),
10501057
Annotations: maps.Clone(spec.Annotations),
10511058
Env: append(slices.Clone(spec.Env), mdd.EnvVars...),
1059+
Resources: spec.Resources,
10521060
}
10531061

1062+
// Set default resources if not specified
1063+
setDefaultResources(dep)
1064+
10541065
// Set default replicas if not specified
10551066
if dep.Replicas == nil {
10561067
dep.Replicas = ptr.To(int32(1))
@@ -1107,8 +1118,12 @@ func (a *adkApiTranslator) resolveByoDeployment(agent *v1alpha2.Agent) (*resolve
11071118
Labels: maps.Clone(spec.Labels),
11081119
Annotations: maps.Clone(spec.Annotations),
11091120
Env: slices.Clone(spec.Env),
1121+
Resources: spec.Resources,
11101122
}
11111123

1124+
// Set default resources if not specified
1125+
setDefaultResources(dep)
1126+
11121127
return dep, nil
11131128
}
11141129

helm/agents/helm/templates/agent.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,6 @@ spec:
224224
- "The latest upgrade of the 'api-gateway' release failed. Can you help identify the problem?"
225225
- "How do I check the rendered Kubernetes manifests for the 'web-server' release without deploying them?"
226226
- "The pods managed by the 'message-broker' Helm release are frequently restarting. What should I investigate?"
227+
deployment:
228+
resources:
229+
{{- toYaml .Values.resources | nindent 8 }}

helm/agents/helm/values.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
modelConfigRef: ""
1+
modelConfigRef: ""
2+
3+
resources:
4+
requests:
5+
cpu: 100m
6+
memory: 256Mi
7+
limits:
8+
cpu: 1000m
9+
memory: 1Gi

helm/agents/istio/templates/agent.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,6 @@ spec:
300300
- "The sidecar injection is not working for pods in 'app-ns'. What should I check?"
301301
- "query_documentation for best practices on Istio performance tuning."
302302
- "Describe the Istio ingress gateway pods in 'istio-system' namespace."
303+
deployment:
304+
resources:
305+
{{- toYaml .Values.resources | nindent 8 }}

helm/agents/istio/values.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
modelConfigRef: ""
1+
modelConfigRef: ""
2+
3+
resources:
4+
requests:
5+
cpu: 100m
6+
memory: 256Mi
7+
limits:
8+
cpu: 1000m
9+
memory: 1Gi

helm/agents/k8s/templates/agent.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,7 @@ spec:
181181
examples:
182182
- "Check for RBAC misconfigurations."
183183
- "Audit my network policies."
184-
- "Identify potential security vulnerabilities in my cluster."
184+
- "Identify potential security vulnerabilities in my cluster."
185+
deployment:
186+
resources:
187+
{{- toYaml .Values.resources | nindent 8 }}

helm/agents/k8s/values.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
modelConfigRef: ""
1+
modelConfigRef: ""
2+
3+
resources:
4+
requests:
5+
cpu: 100m
6+
memory: 256Mi
7+
limits:
8+
cpu: 1000m
9+
memory: 1Gi

0 commit comments

Comments
 (0)