Skip to content

Commit fc1c8b1

Browse files
authored
Fix discrepancy between the SA name in deploymentForMCPServer and deploymentNeedsUpdate (#1033)
1 parent a499040 commit fc1c8b1

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

cmd/thv-operator/controllers/mcpserver_controller.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func (r *MCPServerReconciler) updateRBACResourceIfNeeded(
328328

329329
// ensureRBACResources ensures that the RBAC resources are in place for the MCP server
330330
func (r *MCPServerReconciler) ensureRBACResources(ctx context.Context, mcpServer *mcpv1alpha1.MCPServer) error {
331-
proxyRunnerNameForRBAC := fmt.Sprintf("%s-proxy-runner", mcpServer.Name)
331+
proxyRunnerNameForRBAC := proxyRunnerServiceAccountName(mcpServer.Name)
332332

333333
// Ensure Role
334334
if err := r.ensureRBACResource(ctx, mcpServer, "Role", func() client.Object {
@@ -552,7 +552,7 @@ func (r *MCPServerReconciler) deploymentForMCPServer(m *mcpv1alpha1.MCPServer) *
552552
Labels: ls, // Keep original labels for pod template
553553
},
554554
Spec: corev1.PodSpec{
555-
ServiceAccountName: fmt.Sprintf("%s-proxy-runner", m.Name),
555+
ServiceAccountName: proxyRunnerServiceAccountName(m.Name),
556556
Containers: []corev1.Container{{
557557
Image: getToolhiveRunnerImage(),
558558
Name: "toolhive",
@@ -895,7 +895,8 @@ func deploymentNeedsUpdate(deployment *appsv1.Deployment, mcpServer *mcpv1alpha1
895895
}
896896

897897
// Check if the service account name has changed
898-
if deployment.Spec.Template.Spec.ServiceAccountName != "toolhive" {
898+
expectedServiceAccountName := proxyRunnerServiceAccountName(mcpServer.Name)
899+
if deployment.Spec.Template.Spec.ServiceAccountName != expectedServiceAccountName {
899900
return true
900901
}
901902

@@ -978,6 +979,11 @@ func resourceRequirementsForMCPServer(m *mcpv1alpha1.MCPServer) corev1.ResourceR
978979
return resources
979980
}
980981

982+
// proxyRunnerServiceAccountName returns the service account name for the proxy runner
983+
func proxyRunnerServiceAccountName(mcpServerName string) string {
984+
return fmt.Sprintf("%s-proxy-runner", mcpServerName)
985+
}
986+
981987
// labelsForMCPServer returns the labels for selecting the resources
982988
// belonging to the given MCPServer CR name.
983989
func labelsForMCPServer(name string) map[string]string {

cmd/thv-operator/controllers/mcpserver_resource_overrides_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,37 @@ func TestMergeStringMaps(t *testing.T) {
205205
})
206206
}
207207
}
208+
209+
func TestDeploymentNeedsUpdateServiceAccount(t *testing.T) {
210+
t.Parallel()
211+
212+
scheme := runtime.NewScheme()
213+
require.NoError(t, mcpv1alpha1.AddToScheme(scheme))
214+
215+
client := fake.NewClientBuilder().WithScheme(scheme).Build()
216+
r := &MCPServerReconciler{
217+
Client: client,
218+
Scheme: scheme,
219+
}
220+
221+
mcpServer := &mcpv1alpha1.MCPServer{
222+
ObjectMeta: metav1.ObjectMeta{
223+
Name: "test-server",
224+
Namespace: "default",
225+
},
226+
Spec: mcpv1alpha1.MCPServerSpec{
227+
Image: "test-image",
228+
Port: 8080,
229+
},
230+
}
231+
232+
// Create a deployment using the current implementation
233+
deployment := r.deploymentForMCPServer(mcpServer)
234+
require.NotNil(t, deployment)
235+
236+
// Test with the current deployment - this should NOT need update
237+
needsUpdate := deploymentNeedsUpdate(deployment, mcpServer)
238+
239+
// With the service account bug fixed, this should now return false
240+
assert.False(t, needsUpdate, "deploymentNeedsUpdate should return false when deployment matches MCPServer spec")
241+
}

0 commit comments

Comments
 (0)