|
| 1 | +// Package export provides functionality for exporting ToolHive configurations to various formats. |
| 2 | +package export |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "sigs.k8s.io/yaml" |
| 11 | + |
| 12 | + v1alpha1 "github.com/stacklok/toolhive/cmd/thv-operator/api/v1alpha1" |
| 13 | + "github.com/stacklok/toolhive/pkg/runner" |
| 14 | + "github.com/stacklok/toolhive/pkg/transport/types" |
| 15 | +) |
| 16 | + |
| 17 | +// WriteK8sManifest converts a RunConfig to a Kubernetes MCPServer resource and writes it as YAML |
| 18 | +func WriteK8sManifest(config *runner.RunConfig, w io.Writer) error { |
| 19 | + mcpServer, err := runConfigToMCPServer(config) |
| 20 | + if err != nil { |
| 21 | + return fmt.Errorf("failed to convert RunConfig to MCPServer: %w", err) |
| 22 | + } |
| 23 | + |
| 24 | + yamlBytes, err := yaml.Marshal(mcpServer) |
| 25 | + if err != nil { |
| 26 | + return fmt.Errorf("failed to marshal MCPServer to YAML: %w", err) |
| 27 | + } |
| 28 | + |
| 29 | + _, err = w.Write(yamlBytes) |
| 30 | + return err |
| 31 | +} |
| 32 | + |
| 33 | +// runConfigToMCPServer converts a RunConfig to a Kubernetes MCPServer resource |
| 34 | +// nolint:gocyclo // Complexity due to mapping multiple config fields to K8s resource |
| 35 | +func runConfigToMCPServer(config *runner.RunConfig) (*v1alpha1.MCPServer, error) { |
| 36 | + // Use the base name or container name for the Kubernetes resource name |
| 37 | + name := config.BaseName |
| 38 | + if name == "" { |
| 39 | + name = config.ContainerName |
| 40 | + } |
| 41 | + if name == "" { |
| 42 | + name = config.Name |
| 43 | + } |
| 44 | + |
| 45 | + // Sanitize the name to be a valid Kubernetes resource name |
| 46 | + name = sanitizeK8sName(name) |
| 47 | + |
| 48 | + mcpServer := &v1alpha1.MCPServer{ |
| 49 | + TypeMeta: metav1.TypeMeta{ |
| 50 | + APIVersion: "toolhive.stacklok.com/v1alpha1", |
| 51 | + Kind: "MCPServer", |
| 52 | + }, |
| 53 | + ObjectMeta: metav1.ObjectMeta{ |
| 54 | + Name: name, |
| 55 | + }, |
| 56 | + Spec: v1alpha1.MCPServerSpec{ |
| 57 | + Image: config.Image, |
| 58 | + Transport: string(config.Transport), |
| 59 | + Args: config.CmdArgs, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + // Set port if specified |
| 64 | + if config.Port > 0 { |
| 65 | + // #nosec G115 -- Port values are validated elsewhere, safe conversion |
| 66 | + mcpServer.Spec.Port = int32(config.Port) |
| 67 | + } |
| 68 | + |
| 69 | + // Set target port if specified |
| 70 | + if config.TargetPort > 0 { |
| 71 | + // #nosec G115 -- Port values are validated elsewhere, safe conversion |
| 72 | + mcpServer.Spec.TargetPort = int32(config.TargetPort) |
| 73 | + } |
| 74 | + |
| 75 | + // Set proxy mode if transport is stdio |
| 76 | + if config.Transport == types.TransportTypeStdio && config.ProxyMode != "" { |
| 77 | + mcpServer.Spec.ProxyMode = string(config.ProxyMode) |
| 78 | + } |
| 79 | + |
| 80 | + // Convert environment variables |
| 81 | + if len(config.EnvVars) > 0 { |
| 82 | + mcpServer.Spec.Env = make([]v1alpha1.EnvVar, 0, len(config.EnvVars)) |
| 83 | + for key, value := range config.EnvVars { |
| 84 | + mcpServer.Spec.Env = append(mcpServer.Spec.Env, v1alpha1.EnvVar{ |
| 85 | + Name: key, |
| 86 | + Value: value, |
| 87 | + }) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Convert volumes |
| 92 | + if len(config.Volumes) > 0 { |
| 93 | + mcpServer.Spec.Volumes = make([]v1alpha1.Volume, 0, len(config.Volumes)) |
| 94 | + for i, vol := range config.Volumes { |
| 95 | + volume, err := parseVolumeString(vol, i) |
| 96 | + if err != nil { |
| 97 | + return nil, fmt.Errorf("failed to parse volume %q: %w", vol, err) |
| 98 | + } |
| 99 | + mcpServer.Spec.Volumes = append(mcpServer.Spec.Volumes, volume) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Convert permission profile |
| 104 | + if config.PermissionProfile != nil { |
| 105 | + // For now, we export permission profiles as inline ConfigMaps would need to be created separately |
| 106 | + // This is a simplified export - users may need to adjust this |
| 107 | + mcpServer.Spec.PermissionProfile = &v1alpha1.PermissionProfileRef{ |
| 108 | + Type: v1alpha1.PermissionProfileTypeBuiltin, |
| 109 | + Name: "none", // Default to none, user should adjust based on their needs |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Convert OIDC config |
| 114 | + if config.OIDCConfig != nil { |
| 115 | + mcpServer.Spec.OIDCConfig = &v1alpha1.OIDCConfigRef{ |
| 116 | + Type: v1alpha1.OIDCConfigTypeInline, |
| 117 | + Inline: &v1alpha1.InlineOIDCConfig{ |
| 118 | + Issuer: config.OIDCConfig.Issuer, |
| 119 | + Audience: config.OIDCConfig.Audience, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + if config.OIDCConfig.JWKSURL != "" { |
| 124 | + mcpServer.Spec.OIDCConfig.Inline.JWKSURL = config.OIDCConfig.JWKSURL |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Convert authz config |
| 129 | + if config.AuthzConfig != nil && config.AuthzConfig.Cedar != nil && len(config.AuthzConfig.Cedar.Policies) > 0 { |
| 130 | + mcpServer.Spec.AuthzConfig = &v1alpha1.AuthzConfigRef{ |
| 131 | + Type: v1alpha1.AuthzConfigTypeInline, |
| 132 | + Inline: &v1alpha1.InlineAuthzConfig{ |
| 133 | + Policies: config.AuthzConfig.Cedar.Policies, |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + if config.AuthzConfig.Cedar.EntitiesJSON != "" { |
| 138 | + mcpServer.Spec.AuthzConfig.Inline.EntitiesJSON = config.AuthzConfig.Cedar.EntitiesJSON |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Convert audit config - audit is always enabled if config exists |
| 143 | + if config.AuditConfig != nil { |
| 144 | + mcpServer.Spec.Audit = &v1alpha1.AuditConfig{ |
| 145 | + Enabled: true, |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Convert telemetry config |
| 150 | + if config.TelemetryConfig != nil { |
| 151 | + mcpServer.Spec.Telemetry = &v1alpha1.TelemetryConfig{} |
| 152 | + |
| 153 | + if config.TelemetryConfig.Endpoint != "" { |
| 154 | + mcpServer.Spec.Telemetry.OpenTelemetry = &v1alpha1.OpenTelemetryConfig{ |
| 155 | + Enabled: true, |
| 156 | + Endpoint: config.TelemetryConfig.Endpoint, |
| 157 | + Insecure: config.TelemetryConfig.Insecure, |
| 158 | + } |
| 159 | + |
| 160 | + if config.TelemetryConfig.ServiceName != "" { |
| 161 | + mcpServer.Spec.Telemetry.OpenTelemetry.ServiceName = config.TelemetryConfig.ServiceName |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Convert Prometheus metrics path setting |
| 166 | + if config.TelemetryConfig.EnablePrometheusMetricsPath { |
| 167 | + if mcpServer.Spec.Telemetry.Prometheus == nil { |
| 168 | + mcpServer.Spec.Telemetry.Prometheus = &v1alpha1.PrometheusConfig{} |
| 169 | + } |
| 170 | + mcpServer.Spec.Telemetry.Prometheus.Enabled = true |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // Convert tools filter |
| 175 | + if len(config.ToolsFilter) > 0 { |
| 176 | + mcpServer.Spec.ToolsFilter = config.ToolsFilter |
| 177 | + } |
| 178 | + |
| 179 | + return mcpServer, nil |
| 180 | +} |
| 181 | + |
| 182 | +// parseVolumeString parses a volume string in the format "host-path:container-path[:ro]" |
| 183 | +func parseVolumeString(volStr string, index int) (v1alpha1.Volume, error) { |
| 184 | + parts := strings.Split(volStr, ":") |
| 185 | + if len(parts) < 2 { |
| 186 | + return v1alpha1.Volume{}, fmt.Errorf("invalid volume format, expected 'host-path:container-path[:ro]'") |
| 187 | + } |
| 188 | + |
| 189 | + volume := v1alpha1.Volume{ |
| 190 | + Name: fmt.Sprintf("volume-%d", index), |
| 191 | + HostPath: parts[0], |
| 192 | + MountPath: parts[1], |
| 193 | + ReadOnly: false, |
| 194 | + } |
| 195 | + |
| 196 | + // Check for read-only flag |
| 197 | + if len(parts) == 3 && parts[2] == "ro" { |
| 198 | + volume.ReadOnly = true |
| 199 | + } |
| 200 | + |
| 201 | + return volume, nil |
| 202 | +} |
| 203 | + |
| 204 | +// sanitizeK8sName sanitizes a string to be a valid Kubernetes resource name |
| 205 | +// Kubernetes names must be lowercase alphanumeric with hyphens, max 253 chars |
| 206 | +func sanitizeK8sName(name string) string { |
| 207 | + // Convert to lowercase |
| 208 | + name = strings.ToLower(name) |
| 209 | + |
| 210 | + // Replace invalid characters with hyphens |
| 211 | + var result strings.Builder |
| 212 | + for _, r := range name { |
| 213 | + if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-' { |
| 214 | + result.WriteRune(r) |
| 215 | + } else { |
| 216 | + result.WriteRune('-') |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + // Remove leading/trailing hyphens |
| 221 | + sanitized := strings.Trim(result.String(), "-") |
| 222 | + |
| 223 | + // Limit length to 253 characters (Kubernetes limit) |
| 224 | + if len(sanitized) > 253 { |
| 225 | + sanitized = sanitized[:253] |
| 226 | + } |
| 227 | + |
| 228 | + // Ensure we don't end with a hyphen after truncation |
| 229 | + sanitized = strings.TrimRight(sanitized, "-") |
| 230 | + |
| 231 | + return sanitized |
| 232 | +} |
0 commit comments