@@ -77,12 +77,13 @@ func parseKubernetesOptions(options map[string]string) (KubernetesOptions, error
7777
7878type KubernetesCM struct {
7979 * types.Logger
80- config * types.ServerConfig
81- clientSet * kubernetes.Clientset
82- restConfig * rest.Config
83- appConfig * types.AppConfig
84- appRunDir string
85- appId types.AppId
80+ appNamespace string
81+ config * types.ServerConfig
82+ clientSet * kubernetes.Clientset
83+ restConfig * rest.Config
84+ appConfig * types.AppConfig
85+ appRunDir string
86+ appId types.AppId
8687}
8788
8889func sanitizeContainerName (name string ) string {
@@ -126,13 +127,14 @@ func NewKubernetesCM(logger *types.Logger, config *types.ServerConfig, appConfig
126127 }
127128
128129 return & KubernetesCM {
129- Logger : logger ,
130- config : config ,
131- restConfig : cfg ,
132- clientSet : clientSet ,
133- appConfig : appConfig ,
134- appRunDir : appRunDir ,
135- appId : appId ,
130+ Logger : logger ,
131+ appNamespace : config .Kubernetes .Namespace + "-apps" ,
132+ config : config ,
133+ restConfig : cfg ,
134+ clientSet : clientSet ,
135+ appConfig : appConfig ,
136+ appRunDir : appRunDir ,
137+ appId : appId ,
136138 }, nil
137139}
138140
@@ -208,7 +210,7 @@ func (k *KubernetesCM) BuildImage(ctx context.Context, imgName ImageName, source
208210
209211 appId , _ , _ := strings .Cut (string (imgName ), ":" )
210212 kanikoBuild := KanikoBuild {
211- Namespace : k .config . Kubernetes . Namespace ,
213+ Namespace : k .appNamespace ,
212214 JobName : fmt .Sprintf ("%s-builder-%d" , appId , time .Now ().Unix ()),
213215 Image : k .config .Builder .KanikoImage ,
214216 SourceDir : sourceUrl ,
@@ -223,16 +225,16 @@ func (k *KubernetesCM) BuildImage(ctx context.Context, imgName ImageName, source
223225func (k * KubernetesCM ) GetContainerState (ctx context.Context , name ContainerName , expectHash string ) (string , bool , error ) {
224226 name = ContainerName (sanitizeContainerName (string (name )))
225227 svc , err := k .clientSet .CoreV1 ().
226- Services (k .config . Kubernetes . Namespace ).
228+ Services (k .appNamespace ).
227229 Get (ctx , string (name ), meta.GetOptions {})
228230 if err != nil {
229231 if apierrors .IsNotFound (err ) {
230232 return "" , false , nil
231233 }
232- return "" , false , fmt .Errorf ("get service %s/%s: %w" , k .config . Kubernetes . Namespace , string (name ), err )
234+ return "" , false , fmt .Errorf ("get service %s/%s: %w" , k .appNamespace , string (name ), err )
233235 }
234236 if len (svc .Spec .Ports ) == 0 {
235- return "" , false , fmt .Errorf ("service %s/%s has no ports" , k .config . Kubernetes . Namespace , string (name ))
237+ return "" , false , fmt .Errorf ("service %s/%s has no ports" , k .appNamespace , string (name ))
236238 }
237239
238240 svcPort := svc .Spec .Ports [0 ].Port
@@ -242,10 +244,10 @@ func (k *KubernetesCM) GetContainerState(ctx context.Context, name ContainerName
242244 }
243245
244246 dep , err := k .clientSet .AppsV1 ().
245- Deployments (k .config . Kubernetes . Namespace ).
247+ Deployments (k .appNamespace ).
246248 Get (ctx , string (name ), meta.GetOptions {})
247249 if err != nil {
248- return "" , false , fmt .Errorf ("get deployment %s/%s: %w" , k .config . Kubernetes . Namespace , string (name ), err )
250+ return "" , false , fmt .Errorf ("get deployment %s/%s: %w" , k .appNamespace , string (name ), err )
249251 }
250252
251253 if expectHash != "" && dep .Spec .Template .Labels [VERSION_HASH_LABEL ] != TrimLabelValue (expectHash ) {
@@ -269,11 +271,11 @@ func (k *KubernetesCM) GetContainerState(ctx context.Context, name ContainerName
269271 }
270272
271273 // Get the pods which are part of this deployment
272- pods , err := k .clientSet .CoreV1 ().Pods (k .config . Kubernetes . Namespace ).List (ctx , meta.ListOptions {
274+ pods , err := k .clientSet .CoreV1 ().Pods (k .appNamespace ).List (ctx , meta.ListOptions {
273275 LabelSelector : fmt .Sprintf ("app=%s" , string (name )),
274276 })
275277 if err != nil {
276- return "" , false , fmt .Errorf ("list pods for deployment %s/%s: %w" , k .config . Kubernetes . Namespace , string (name ), err )
278+ return "" , false , fmt .Errorf ("list pods for deployment %s/%s: %w" , k .appNamespace , string (name ), err )
277279 }
278280
279281 runningCount := 0
@@ -290,25 +292,25 @@ func (k *KubernetesCM) GetContainerState(ctx context.Context, name ContainerName
290292func (k * KubernetesCM ) StartContainer (ctx context.Context , name ContainerName ) error {
291293 name = ContainerName (sanitizeContainerName (string (name )))
292294 return retry .RetryOnConflict (retry .DefaultRetry , func () error {
293- scale , err := k .clientSet .AppsV1 ().Deployments (k .config . Kubernetes . Namespace ).GetScale (ctx , string (name ), meta.GetOptions {})
295+ scale , err := k .clientSet .AppsV1 ().Deployments (k .appNamespace ).GetScale (ctx , string (name ), meta.GetOptions {})
294296 if err != nil {
295297 return err
296298 }
297299 scale .Spec .Replicas = 1
298- _ , err = k .clientSet .AppsV1 ().Deployments (k .config . Kubernetes . Namespace ).UpdateScale (ctx , string (name ), scale , meta.UpdateOptions {})
300+ _ , err = k .clientSet .AppsV1 ().Deployments (k .appNamespace ).UpdateScale (ctx , string (name ), scale , meta.UpdateOptions {})
299301 return err
300302 })
301303}
302304
303305func (k * KubernetesCM ) StopContainer (ctx context.Context , name ContainerName ) error {
304306 name = ContainerName (sanitizeContainerName (string (name )))
305307 return retry .RetryOnConflict (retry .DefaultRetry , func () error {
306- scale , err := k .clientSet .AppsV1 ().Deployments (k .config . Kubernetes . Namespace ).GetScale (ctx , string (name ), meta.GetOptions {})
308+ scale , err := k .clientSet .AppsV1 ().Deployments (k .appNamespace ).GetScale (ctx , string (name ), meta.GetOptions {})
307309 if err != nil {
308310 return err
309311 }
310312 scale .Spec .Replicas = 0 // scale down to zero
311- _ , err = k .clientSet .AppsV1 ().Deployments (k .config . Kubernetes . Namespace ).UpdateScale (ctx , string (name ), scale , meta.UpdateOptions {})
313+ _ , err = k .clientSet .AppsV1 ().Deployments (k .appNamespace ).UpdateScale (ctx , string (name ), scale , meta.UpdateOptions {})
312314 return err
313315 })
314316}
@@ -337,7 +339,7 @@ func (k *KubernetesCM) GetContainerLogs(ctx context.Context, name ContainerName,
337339 name = ContainerName (sanitizeContainerName (string (name )))
338340
339341 // List pods with the matching label
340- pods , err := k .clientSet .CoreV1 ().Pods (k .config . Kubernetes . Namespace ).List (ctx , meta.ListOptions {
342+ pods , err := k .clientSet .CoreV1 ().Pods (k .appNamespace ).List (ctx , meta.ListOptions {
341343 LabelSelector : fmt .Sprintf ("app=%s" , string (name )),
342344 })
343345 if err != nil {
@@ -362,7 +364,7 @@ func (k *KubernetesCM) GetContainerLogs(ctx context.Context, name ContainerName,
362364 TailLines : & tailLines ,
363365 }
364366
365- req := k .clientSet .CoreV1 ().Pods (k .config . Kubernetes . Namespace ).GetLogs (pod .Name , logOptions )
367+ req := k .clientSet .CoreV1 ().Pods (k .appNamespace ).GetLogs (pod .Name , logOptions )
366368 logStream , err := req .Stream (ctx )
367369 if err != nil {
368370 return "" , fmt .Errorf ("get logs for pod %s container %s: %w" , pod .Name , containerName , err )
@@ -380,7 +382,7 @@ func (k *KubernetesCM) GetContainerLogs(ctx context.Context, name ContainerName,
380382func (k * KubernetesCM ) VolumeExists (ctx context.Context , name VolumeName ) bool {
381383 pvcName := sanitizeContainerName (string (name ))
382384 _ , err := k .clientSet .CoreV1 ().
383- PersistentVolumeClaims (k .config . Kubernetes . Namespace ).
385+ PersistentVolumeClaims (k .appNamespace ).
384386 Get (ctx , pvcName , meta.GetOptions {})
385387 if err != nil {
386388 if apierrors .IsNotFound (err ) {
@@ -398,7 +400,7 @@ func (k *KubernetesCM) VolumeCreate(ctx context.Context, name VolumeName) error
398400 return fmt .Errorf ("error parsing default volume size %s: %w" , k .appConfig .Kubernetes .DefaultVolumeSize , err )
399401 }
400402 pvcName := sanitizeContainerName (string (name ))
401- pvc := corev1apply .PersistentVolumeClaim (pvcName , k .config . Kubernetes . Namespace ).
403+ pvc := corev1apply .PersistentVolumeClaim (pvcName , k .appNamespace ).
402404 WithSpec (corev1apply .PersistentVolumeClaimSpec ().
403405 WithAccessModes (core .ReadWriteOnce ). // TODO: support other access modes
404406 WithResources (corev1apply .VolumeResourceRequirements ().
@@ -407,7 +409,7 @@ func (k *KubernetesCM) VolumeCreate(ctx context.Context, name VolumeName) error
407409 })))
408410
409411 _ , err = k .clientSet .CoreV1 ().
410- PersistentVolumeClaims (k .config . Kubernetes . Namespace ).
412+ PersistentVolumeClaims (k .appNamespace ).
411413 Apply (ctx , pvc , meta.ApplyOptions {FieldManager : OPENRUN_FIELD_MANAGER })
412414 if err != nil {
413415 return fmt .Errorf ("apply PersistentVolumeClaim %s: %w" , pvcName , err )
@@ -447,10 +449,10 @@ func (k *KubernetesCM) processVolumes(ctx context.Context, name string, volumes
447449 }
448450
449451 fileName := filepath .Base (vol .TargetPath )
450- secretApply := corev1apply .Secret (secretName , k .config . Kubernetes . Namespace ).
452+ secretApply := corev1apply .Secret (secretName , k .appNamespace ).
451453 WithData (map [string ][]byte {fileName : secretData })
452454
453- if _ , err := k .clientSet .CoreV1 ().Secrets (k .config . Kubernetes . Namespace ).Apply (
455+ if _ , err := k .clientSet .CoreV1 ().Secrets (k .appNamespace ).Apply (
454456 ctx , secretApply , meta.ApplyOptions {FieldManager : OPENRUN_FIELD_MANAGER }); err != nil {
455457 return nil , nil , fmt .Errorf ("apply secret %s: %w" , secretName , err )
456458 }
@@ -480,10 +482,10 @@ func (k *KubernetesCM) processVolumes(ctx context.Context, name string, volumes
480482 }
481483
482484 fileName := filepath .Base (vol .TargetPath )
483- configMapApply := corev1apply .ConfigMap (configMapName , k .config . Kubernetes . Namespace ).
485+ configMapApply := corev1apply .ConfigMap (configMapName , k .appNamespace ).
484486 WithData (map [string ]string {fileName : string (data )})
485487
486- if _ , err := k .clientSet .CoreV1 ().ConfigMaps (k .config . Kubernetes . Namespace ).Apply (
488+ if _ , err := k .clientSet .CoreV1 ().ConfigMaps (k .appNamespace ).Apply (
487489 ctx , configMapApply , meta.ApplyOptions {FieldManager : OPENRUN_FIELD_MANAGER }); err != nil {
488490 return nil , nil , fmt .Errorf ("apply configmap %s: %w" , configMapName , err )
489491 }
@@ -623,7 +625,7 @@ func (k *KubernetesCM) createDeployment(ctx context.Context, name, image string,
623625 //WithType(appsv1.RecreateDeploymentStrategyType)
624626 WithType (appsv1 .RollingUpdateDeploymentStrategyType )
625627
626- dep := appsv1apply .Deployment (name , k .config . Kubernetes . Namespace ).
628+ dep := appsv1apply .Deployment (name , k .appNamespace ).
627629 WithLabels (labels ).
628630 WithSpec (appsv1apply .DeploymentSpec ().
629631 WithReplicas (replicas ).
@@ -634,7 +636,7 @@ func (k *KubernetesCM) createDeployment(ctx context.Context, name, image string,
634636 WithLabels (metadata ).
635637 WithSpec (podSpec )))
636638
637- if _ , err := k .clientSet .AppsV1 ().Deployments (k .config . Kubernetes . Namespace ).Apply (ctx , dep , meta.ApplyOptions {FieldManager : OPENRUN_FIELD_MANAGER }); err != nil {
639+ if _ , err := k .clientSet .AppsV1 ().Deployments (k .appNamespace ).Apply (ctx , dep , meta.ApplyOptions {FieldManager : OPENRUN_FIELD_MANAGER }); err != nil {
638640 return "" , fmt .Errorf ("apply deployment: %w" , err )
639641 }
640642
@@ -644,7 +646,7 @@ func (k *KubernetesCM) createDeployment(ctx context.Context, name, image string,
644646 if minReplicas < 1 {
645647 minReplicas = 1
646648 }
647- hpa := autoscalingv2apply .HorizontalPodAutoscaler (name , k .config . Kubernetes . Namespace ).
649+ hpa := autoscalingv2apply .HorizontalPodAutoscaler (name , k .appNamespace ).
648650 WithLabels (labels ).
649651 WithSpec (autoscalingv2apply .HorizontalPodAutoscalerSpec ().
650652 WithScaleTargetRef (autoscalingv2apply .CrossVersionObjectReference ().
@@ -661,7 +663,7 @@ func (k *KubernetesCM) createDeployment(ctx context.Context, name, image string,
661663 WithType (autoscalingv2 .UtilizationMetricType ).
662664 WithAverageUtilization (k .appConfig .Kubernetes .ScalingThresholdCPU )))))
663665
664- if _ , err := k .clientSet .AutoscalingV2 ().HorizontalPodAutoscalers (k .config . Kubernetes . Namespace ).Apply (
666+ if _ , err := k .clientSet .AutoscalingV2 ().HorizontalPodAutoscalers (k .appNamespace ).Apply (
665667 ctx , hpa , meta.ApplyOptions {FieldManager : OPENRUN_FIELD_MANAGER }); err != nil {
666668 return "" , fmt .Errorf ("apply hpa: %w" , err )
667669 }
@@ -672,7 +674,7 @@ func (k *KubernetesCM) createDeployment(ctx context.Context, name, image string,
672674 if k .config .Kubernetes .UseNodePort {
673675 serviceType = core .ServiceTypeNodePort
674676 }
675- svcApply := corev1apply .Service (name , k .config . Kubernetes . Namespace ).
677+ svcApply := corev1apply .Service (name , k .appNamespace ).
676678 WithLabels (metadata ).
677679 WithSpec (corev1apply .ServiceSpec ().
678680 WithType (serviceType ).
@@ -684,7 +686,7 @@ func (k *KubernetesCM) createDeployment(ctx context.Context, name, image string,
684686 WithProtocol (protocol )))
685687
686688 svc , err := k .clientSet .CoreV1 ().Services (
687- k .config . Kubernetes . Namespace ).Apply (ctx , svcApply ,
689+ k .appNamespace ).Apply (ctx , svcApply ,
688690 meta.ApplyOptions {FieldManager : OPENRUN_FIELD_MANAGER })
689691 if err != nil {
690692 return "" , fmt .Errorf ("apply service: %w" , err )
0 commit comments