|
| 1 | +// Package runtime provides workload deployment setup functionality |
| 2 | +// that was previously part of the transport package. |
| 3 | +package runtime |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + rt "github.com/stacklok/toolhive/pkg/container/runtime" |
| 10 | + "github.com/stacklok/toolhive/pkg/ignore" |
| 11 | + "github.com/stacklok/toolhive/pkg/logger" |
| 12 | + "github.com/stacklok/toolhive/pkg/permissions" |
| 13 | + "github.com/stacklok/toolhive/pkg/transport/types" |
| 14 | +) |
| 15 | + |
| 16 | +var transportEnvMap = map[types.TransportType]string{ |
| 17 | + types.TransportTypeSSE: "sse", |
| 18 | + types.TransportTypeStreamableHTTP: "streamable-http", |
| 19 | + types.TransportTypeStdio: "stdio", |
| 20 | +} |
| 21 | + |
| 22 | +// SetupResult contains the results of setting up a workload |
| 23 | +type SetupResult struct { |
| 24 | + ContainerName string |
| 25 | + TargetURI string |
| 26 | + TargetPort int |
| 27 | + TargetHost string |
| 28 | +} |
| 29 | + |
| 30 | +// Setup prepares and deploys a workload for use with a transport. |
| 31 | +// The runtime parameter provides access to container operations. |
| 32 | +// The permissionProfile is used to configure container permissions (including network mode). |
| 33 | +// The k8sPodTemplatePatch is a JSON string to patch the Kubernetes pod template. |
| 34 | +// Returns the container name and target URI for configuring the transport. |
| 35 | +func Setup( |
| 36 | + ctx context.Context, |
| 37 | + transportType types.TransportType, |
| 38 | + runtime rt.Deployer, |
| 39 | + containerName string, |
| 40 | + image string, |
| 41 | + cmdArgs []string, |
| 42 | + envVars, labels map[string]string, |
| 43 | + permissionProfile *permissions.Profile, |
| 44 | + k8sPodTemplatePatch string, |
| 45 | + isolateNetwork bool, |
| 46 | + ignoreConfig *ignore.Config, |
| 47 | + host string, |
| 48 | + targetPort int, |
| 49 | + targetHost string, |
| 50 | +) (*SetupResult, error) { |
| 51 | + // Add transport-specific environment variables |
| 52 | + env, ok := transportEnvMap[transportType] |
| 53 | + if !ok && transportType != types.TransportTypeStdio { |
| 54 | + return nil, fmt.Errorf("unsupported transport type: %s", transportType) |
| 55 | + } |
| 56 | + |
| 57 | + // For stdio transport, env is already set above |
| 58 | + if transportType == types.TransportTypeStdio { |
| 59 | + envVars["MCP_TRANSPORT"] = "stdio" |
| 60 | + } else { |
| 61 | + envVars["MCP_TRANSPORT"] = env |
| 62 | + |
| 63 | + // Use the target port for the container's environment variables |
| 64 | + envVars["MCP_PORT"] = fmt.Sprintf("%d", targetPort) |
| 65 | + envVars["FASTMCP_PORT"] = fmt.Sprintf("%d", targetPort) |
| 66 | + envVars["MCP_HOST"] = targetHost |
| 67 | + } |
| 68 | + |
| 69 | + // Create workload options |
| 70 | + containerOptions := rt.NewDeployWorkloadOptions() |
| 71 | + containerOptions.K8sPodTemplatePatch = k8sPodTemplatePatch |
| 72 | + containerOptions.IgnoreConfig = ignoreConfig |
| 73 | + |
| 74 | + if transportType == types.TransportTypeStdio { |
| 75 | + containerOptions.AttachStdio = true |
| 76 | + } else { |
| 77 | + // Expose the target port in the container |
| 78 | + containerPortStr := fmt.Sprintf("%d/tcp", targetPort) |
| 79 | + containerOptions.ExposedPorts[containerPortStr] = struct{}{} |
| 80 | + |
| 81 | + // Create host port bindings (configurable through the --host flag) |
| 82 | + portBindings := []rt.PortBinding{ |
| 83 | + { |
| 84 | + HostIP: host, |
| 85 | + HostPort: fmt.Sprintf("%d", targetPort), |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + // Set the port bindings |
| 90 | + containerOptions.PortBindings[containerPortStr] = portBindings |
| 91 | + } |
| 92 | + |
| 93 | + // Create the container |
| 94 | + logger.Infof("Deploying workload %s from image %s...", containerName, image) |
| 95 | + exposedPort, err := runtime.DeployWorkload( |
| 96 | + ctx, |
| 97 | + image, |
| 98 | + containerName, |
| 99 | + cmdArgs, |
| 100 | + envVars, |
| 101 | + labels, |
| 102 | + permissionProfile, |
| 103 | + transportType.String(), |
| 104 | + containerOptions, |
| 105 | + isolateNetwork, |
| 106 | + ) |
| 107 | + if err != nil { |
| 108 | + return nil, fmt.Errorf("failed to create container: %v", err) |
| 109 | + } |
| 110 | + logger.Infof("Container created: %s", containerName) |
| 111 | + |
| 112 | + result := &SetupResult{ |
| 113 | + ContainerName: containerName, |
| 114 | + TargetHost: targetHost, |
| 115 | + TargetPort: targetPort, |
| 116 | + } |
| 117 | + |
| 118 | + // For stdio transport, there's no target URI |
| 119 | + if transportType == types.TransportTypeStdio { |
| 120 | + result.TargetURI = "" |
| 121 | + } else { |
| 122 | + // Update target host and port if needed (for Kubernetes) |
| 123 | + if (transportType == types.TransportTypeSSE || transportType == types.TransportTypeStreamableHTTP) && rt.IsKubernetesRuntime() { |
| 124 | + // If the SSEHeadlessServiceName is set, use it as the target host |
| 125 | + if containerOptions.SSEHeadlessServiceName != "" { |
| 126 | + result.TargetHost = containerOptions.SSEHeadlessServiceName |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // we don't want to override the targetPort in a Kubernetes deployment. Because |
| 131 | + // by default the Kubernetes container deployer returns `0` for the exposedPort |
| 132 | + // therefore causing the "target port not set" error when it is assigned to the targetPort. |
| 133 | + // Issues: |
| 134 | + // - https://github.com/stacklok/toolhive/issues/902 |
| 135 | + // - https://github.com/stacklok/toolhive/issues/924 |
| 136 | + if !rt.IsKubernetesRuntime() { |
| 137 | + result.TargetPort = exposedPort |
| 138 | + } |
| 139 | + |
| 140 | + // Construct target URI |
| 141 | + result.TargetURI = fmt.Sprintf("http://%s:%d", result.TargetHost, result.TargetPort) |
| 142 | + } |
| 143 | + |
| 144 | + return result, nil |
| 145 | +} |
0 commit comments