@@ -27,6 +27,7 @@ import (
27
27
"k8s.io/client-go/rest"
28
28
"k8s.io/client-go/tools/remotecommand"
29
29
"k8s.io/client-go/tools/watch"
30
+ clientconfig "sigs.k8s.io/controller-runtime/pkg/client/config"
30
31
31
32
"github.com/stacklok/toolhive/pkg/container/runtime"
32
33
"github.com/stacklok/toolhive/pkg/logger"
@@ -246,17 +247,30 @@ func (c *Client) DeployWorkload(ctx context.Context,
246
247
}
247
248
248
249
// Ensure the pod template has required configuration (labels, etc.)
249
- podTemplateSpec = ensurePodTemplateConfig (podTemplateSpec , containerLabels )
250
+ isOpenShift := false
251
+ // Get a config to talk to the apiserver
252
+ cfg , err := clientconfig .GetConfig ()
253
+ if err != nil {
254
+ return 0 , fmt .Errorf ("error getting config for APIServer: %w" , err )
255
+ }
256
+
257
+ isOpenShift , err = DetectOpenShiftWith (cfg )
258
+ if err != nil {
259
+ return 0 , fmt .Errorf ("can't determine api server type: %w" , err )
260
+ }
261
+
262
+ podTemplateSpec = ensurePodTemplateConfig (podTemplateSpec , containerLabels , isOpenShift )
250
263
251
264
// Configure the MCP container
252
- err : = configureMCPContainer (
265
+ err = configureMCPContainer (
253
266
podTemplateSpec ,
254
267
image ,
255
268
command ,
256
269
attachStdio ,
257
270
envVarList ,
258
271
transportType ,
259
272
options ,
273
+ isOpenShift ,
260
274
)
261
275
if err != nil {
262
276
return 0 , err
@@ -891,6 +905,7 @@ func createPodTemplateFromPatch(patchJSON string) (*corev1apply.PodTemplateSpecA
891
905
func ensurePodTemplateConfig (
892
906
podTemplateSpec * corev1apply.PodTemplateSpecApplyConfiguration ,
893
907
containerLabels map [string ]string ,
908
+ isOpenShift bool ,
894
909
) * corev1apply.PodTemplateSpecApplyConfiguration {
895
910
podTemplateSpec = ensureObjectMetaApplyConfigurationExists (podTemplateSpec )
896
911
// Ensure the pod template has labels
@@ -940,6 +955,31 @@ func ensurePodTemplateConfig(
940
955
podTemplateSpec .Spec .SecurityContext = podTemplateSpec .Spec .SecurityContext .WithRunAsGroup (int64 (1000 ))
941
956
}
942
957
}
958
+
959
+ if isOpenShift {
960
+ if podTemplateSpec .Spec .SecurityContext .RunAsUser != nil {
961
+ podTemplateSpec .Spec .SecurityContext .RunAsUser = nil
962
+ }
963
+
964
+ if podTemplateSpec .Spec .SecurityContext .RunAsGroup != nil {
965
+ podTemplateSpec .Spec .SecurityContext .RunAsGroup = nil
966
+ }
967
+
968
+ if podTemplateSpec .Spec .SecurityContext .FSGroup != nil {
969
+ podTemplateSpec .Spec .SecurityContext .FSGroup = nil
970
+ }
971
+
972
+ if podTemplateSpec .Spec .SecurityContext .SeccompProfile == nil {
973
+ podTemplateSpec .Spec .SecurityContext .SeccompProfile =
974
+ corev1apply .SeccompProfile ().WithType (
975
+ corev1 .SeccompProfileTypeRuntimeDefault )
976
+ } else {
977
+ podTemplateSpec .Spec .SecurityContext .SeccompProfile =
978
+ podTemplateSpec .Spec .SecurityContext .SeccompProfile .WithType (
979
+ corev1 .SeccompProfileTypeRuntimeDefault )
980
+ }
981
+ }
982
+
943
983
return podTemplateSpec
944
984
}
945
985
@@ -985,7 +1025,18 @@ func configureContainer(
985
1025
command []string ,
986
1026
attachStdio bool ,
987
1027
envVars []* corev1apply.EnvVarApplyConfiguration ,
1028
+ isOpenShift bool ,
988
1029
) {
1030
+ logger .Infof ("Configuring container %s with image %s" , * container .Name , image )
1031
+ logger .Infof ("Command: " )
1032
+ for _ , arg := range command {
1033
+ logger .Infof ("Arg: %s" , arg )
1034
+ }
1035
+ logger .Infof ("AttachStdio: %v" , attachStdio )
1036
+ for _ , envVar := range envVars {
1037
+ logger .Infof ("EnvVar: %s=%s" , * envVar .Name , * envVar .Value )
1038
+ }
1039
+
989
1040
container .WithImage (image ).
990
1041
WithArgs (command ... ).
991
1042
WithStdin (attachStdio ).
@@ -1029,6 +1080,34 @@ func configureContainer(
1029
1080
container .SecurityContext = container .SecurityContext .WithAllowPrivilegeEscalation (false )
1030
1081
}
1031
1082
}
1083
+
1084
+ if isOpenShift {
1085
+ logger .Infof ("Setting OpenShift security context requirements to container %s" , * container .Name )
1086
+
1087
+ if container .SecurityContext .RunAsUser != nil {
1088
+ container .SecurityContext .RunAsUser = nil
1089
+ }
1090
+
1091
+ if container .SecurityContext .RunAsGroup != nil {
1092
+ container .SecurityContext .RunAsGroup = nil
1093
+ }
1094
+
1095
+ if container .SecurityContext .SeccompProfile == nil {
1096
+ container .SecurityContext .SeccompProfile =
1097
+ corev1apply .SeccompProfile ().WithType (
1098
+ corev1 .SeccompProfileTypeRuntimeDefault )
1099
+ } else {
1100
+ container .SecurityContext .SeccompProfile =
1101
+ container .SecurityContext .SeccompProfile .WithType (
1102
+ corev1 .SeccompProfileTypeRuntimeDefault )
1103
+ }
1104
+
1105
+ if container .SecurityContext .Capabilities == nil {
1106
+ container .SecurityContext .Capabilities = & corev1apply.CapabilitiesApplyConfiguration {
1107
+ Drop : []corev1.Capability {"ALL" },
1108
+ }
1109
+ }
1110
+ }
1032
1111
}
1033
1112
1034
1113
// configureMCPContainer configures the MCP container in the pod template
@@ -1040,6 +1119,7 @@ func configureMCPContainer(
1040
1119
envVarList []* corev1apply.EnvVarApplyConfiguration ,
1041
1120
transportType string ,
1042
1121
options * runtime.DeployWorkloadOptions ,
1122
+ isOpenShift bool ,
1043
1123
) error {
1044
1124
// Get the "mcp" container if it exists
1045
1125
mcpContainer := getMCPContainer (podTemplateSpec )
@@ -1049,7 +1129,7 @@ func configureMCPContainer(
1049
1129
mcpContainer = corev1apply .Container ().WithName ("mcp" )
1050
1130
1051
1131
// Configure the container
1052
- configureContainer (mcpContainer , image , command , attachStdio , envVarList )
1132
+ configureContainer (mcpContainer , image , command , attachStdio , envVarList , isOpenShift )
1053
1133
1054
1134
// Configure ports if needed
1055
1135
if options != nil && transportType == string (transtypes .TransportTypeSSE ) {
@@ -1064,7 +1144,7 @@ func configureMCPContainer(
1064
1144
podTemplateSpec .Spec .WithContainers (mcpContainer )
1065
1145
} else {
1066
1146
// Configure the existing container
1067
- configureContainer (mcpContainer , image , command , attachStdio , envVarList )
1147
+ configureContainer (mcpContainer , image , command , attachStdio , envVarList , isOpenShift )
1068
1148
1069
1149
// Configure ports if needed
1070
1150
if options != nil && transportType == string (transtypes .TransportTypeSSE ) {
0 commit comments