@@ -3,6 +3,8 @@ package kubernetes
3
3
import (
4
4
"bufio"
5
5
"context"
6
+ "encoding/base64"
7
+ "encoding/json"
6
8
"fmt"
7
9
"io"
8
10
"os"
@@ -23,6 +25,7 @@ import (
23
25
24
26
"github.com/kubectyl/kuber/config"
25
27
"github.com/kubectyl/kuber/environment"
28
+ "github.com/kubectyl/kuber/parser"
26
29
"github.com/kubectyl/kuber/system"
27
30
28
31
corev1 "k8s.io/api/core/v1"
@@ -409,45 +412,88 @@ func (e *Environment) Create() error {
409
412
// Create a map to store the file data for the ConfigMap
410
413
fileData := make (map [string ]string )
411
414
415
+ fileReplaceOps := parser.FileReplaceOperations {}
416
+
412
417
for _ , k := range cfs {
413
- // replacement := make(map[string]string)
418
+ fileName := base64 .URLEncoding .EncodeToString ([]byte (k .FileName ))
419
+ fileName = strings .TrimRight (fileName , "=" )
414
420
for _ , t := range k .Replace {
415
- // replacement[t.Match] = t.ReplaceWith.String()
416
- fileData [k .FileName ] += fmt .Sprintf ("%s=%s\n " , t .Match , t .ReplaceWith .String ())
421
+ fileData [fileName ] += fmt .Sprintf ("%s=%s\n " , t .Match , t .ReplaceWith .String ())
417
422
}
418
-
419
- command , err := k . Parse ( "/config/" , "/home/container/" )
420
- if err != nil {
421
- return err
423
+ fileOp := parser. FileReplaceOperation {
424
+ SourceFile : "/config/" + fileName ,
425
+ TargetFile : "/home/container/" + k . FileName ,
426
+ TargetType : k . Parser . String (),
422
427
}
423
-
424
- // Add a new initContainer to the Pod
425
- newInitContainer := corev1.Container {
426
- Name : "configuration-files" ,
427
- Image : "busybox" ,
428
- ImagePullPolicy : corev1 .PullIfNotPresent ,
429
- SecurityContext : & corev1.SecurityContext {
430
- RunAsUser : pointer .Int64 (1000 ),
431
- RunAsNonRoot : pointer .Bool (true ),
428
+ fileReplaceOps .Files = append (fileReplaceOps .Files , fileOp )
429
+ }
430
+ binaryFileOpData , err := json .Marshal (fileReplaceOps )
431
+ binData := make (map [string ][]byte )
432
+ binData ["config.json" ] = binaryFileOpData
433
+ newConfigMap := & corev1.ConfigMap {
434
+ ObjectMeta : metav1.ObjectMeta {
435
+ Name : e .Id + "-config-replace-ops" ,
436
+ Namespace : cfg .Cluster .Namespace ,
437
+ Labels : map [string ]string {
438
+ "Service" : "Kubectyl" ,
439
+ "uuid" : e .Id ,
432
440
},
433
- Command : command ,
434
- Resources : corev1.ResourceRequirements {},
435
- VolumeMounts : []corev1.VolumeMount {
436
- {
437
- Name : "replacement" ,
438
- MountPath : "/config" ,
439
- ReadOnly : true ,
440
- },
441
- {
442
- Name : "storage" ,
443
- MountPath : "/home/container" ,
441
+ },
442
+ BinaryData : binData ,
443
+ }
444
+
445
+ pod .Spec .Volumes = append (pod .Spec .Volumes , corev1.Volume {
446
+ Name : "file-replace-ops" ,
447
+ VolumeSource : corev1.VolumeSource {
448
+ ConfigMap : & corev1.ConfigMapVolumeSource {
449
+ LocalObjectReference : corev1.LocalObjectReference {
450
+ Name : newConfigMap .Name ,
444
451
},
445
452
},
446
- }
453
+ },
454
+ })
447
455
448
- pod .Spec .InitContainers = append (pod .Spec .InitContainers , newInitContainer )
456
+ err = e .CreateOrUpdateConfigMap (newConfigMap )
457
+ if err != nil {
458
+ return err
449
459
}
450
460
461
+ // Add a new initContainer to the Pod
462
+ newInitContainer := corev1.Container {
463
+ Name : "configuration-files" ,
464
+ Image : "inglemr/fileparser:latest" ,
465
+ ImagePullPolicy : corev1 .PullIfNotPresent ,
466
+ SecurityContext : & corev1.SecurityContext {
467
+ RunAsUser : pointer .Int64 (1000 ),
468
+ RunAsNonRoot : pointer .Bool (true ),
469
+ },
470
+ Env : []corev1.EnvVar {
471
+ {
472
+ Name : "CONFIG_LOCATION" ,
473
+ Value : "/fileparserconfig/config.json" ,
474
+ },
475
+ },
476
+ Resources : corev1.ResourceRequirements {},
477
+ VolumeMounts : []corev1.VolumeMount {
478
+ {
479
+ Name : "replacement" ,
480
+ MountPath : "/config" ,
481
+ ReadOnly : true ,
482
+ },
483
+ {
484
+ Name : "storage" ,
485
+ MountPath : "/home/container" ,
486
+ },
487
+ {
488
+ Name : "file-replace-ops" ,
489
+ MountPath : "/fileparserconfig" ,
490
+ ReadOnly : true ,
491
+ },
492
+ },
493
+ }
494
+
495
+ pod .Spec .InitContainers = append (pod .Spec .InitContainers , newInitContainer )
496
+
451
497
pod .Spec .Volumes = append (pod .Spec .Volumes , corev1.Volume {
452
498
Name : "replacement" ,
453
499
VolumeSource : corev1.VolumeSource {
@@ -464,28 +510,17 @@ func (e *Environment) Create() error {
464
510
ObjectMeta : metav1.ObjectMeta {
465
511
Name : e .Id + "-replacement" ,
466
512
Namespace : cfg .Cluster .Namespace ,
513
+ Labels : map [string ]string {
514
+ "Service" : "Kubectyl" ,
515
+ "uuid" : e .Id ,
516
+ },
467
517
},
468
518
Data : fileData ,
469
519
}
470
520
471
- // Check if the ConfigMap already exists
472
- _ , err := e .client .CoreV1 ().ConfigMaps (cfg .Cluster .Namespace ).Get (context .TODO (), e .Id + "-replacement" , metav1.GetOptions {})
521
+ err = e .CreateOrUpdateConfigMap (configMap )
473
522
if err != nil {
474
- if errors .IsNotFound (err ) {
475
- _ , err = e .client .CoreV1 ().ConfigMaps (cfg .Cluster .Namespace ).Create (context .TODO (), configMap , metav1.CreateOptions {})
476
- if err != nil {
477
- return err
478
- }
479
- e .log ().Info ("replacement configmap created successfully" )
480
- } else {
481
- return err
482
- }
483
- } else {
484
- _ , err = e .client .CoreV1 ().ConfigMaps (cfg .Cluster .Namespace ).Update (context .TODO (), configMap , metav1.UpdateOptions {})
485
- if err != nil {
486
- return err
487
- }
488
- e .log ().Info ("replacement configmap updated successfully" )
523
+ return err
489
524
}
490
525
}
491
526
@@ -601,14 +636,12 @@ func (e *Environment) CreateService() error {
601
636
Selector : map [string ]string {
602
637
"uuid" : e .Id ,
603
638
},
604
- Type : corev1 .ServiceType (serviceType ),
605
- ExternalTrafficPolicy : corev1 .ServiceExternalTrafficPolicyType (externalPolicy ),
606
- HealthCheckNodePort : 0 ,
607
- PublishNotReadyAddresses : true ,
608
- AllocateLoadBalancerNodePorts : new (bool ),
639
+ Type : corev1 .ServiceType (serviceType ),
640
+ ExternalTrafficPolicy : corev1 .ServiceExternalTrafficPolicyType (externalPolicy ),
641
+ HealthCheckNodePort : 0 ,
642
+ PublishNotReadyAddresses : true ,
609
643
},
610
644
}
611
-
612
645
udp := & corev1.Service {
613
646
TypeMeta : metav1.TypeMeta {
614
647
Kind : "Service" ,
@@ -626,15 +659,16 @@ func (e *Environment) CreateService() error {
626
659
Selector : map [string ]string {
627
660
"uuid" : e .Id ,
628
661
},
629
- Type : corev1 .ServiceType (serviceType ),
630
- ExternalTrafficPolicy : corev1 .ServiceExternalTrafficPolicyType (externalPolicy ),
631
- HealthCheckNodePort : 0 ,
632
- PublishNotReadyAddresses : true ,
633
- AllocateLoadBalancerNodePorts : new (bool ),
662
+ Type : corev1 .ServiceType (serviceType ),
663
+ ExternalTrafficPolicy : corev1 .ServiceExternalTrafficPolicyType (externalPolicy ),
664
+ HealthCheckNodePort : 0 ,
665
+ PublishNotReadyAddresses : true ,
634
666
},
635
667
}
636
668
637
669
if serviceType == "LoadBalancer" && cfg .Cluster .MetalLBSharedIP {
670
+ udp .Spec .AllocateLoadBalancerNodePorts = new (bool )
671
+ tcp .Spec .AllocateLoadBalancerNodePorts = new (bool )
638
672
tcp .Annotations = map [string ]string {
639
673
"metallb.universe.tf/allow-shared-ip" : e .Id ,
640
674
}
@@ -1062,3 +1096,27 @@ func (e *Environment) convertMounts() ([]corev1.VolumeMount, []corev1.Volume) {
1062
1096
1063
1097
return out , volumes
1064
1098
}
1099
+
1100
+ func (e * Environment ) CreateOrUpdateConfigMap (configMap * corev1.ConfigMap ) error {
1101
+ // Check if the ConfigMap already exists
1102
+ cfg := config .Get ()
1103
+ _ , err := e .client .CoreV1 ().ConfigMaps (cfg .Cluster .Namespace ).Get (context .TODO (), configMap .Name , metav1.GetOptions {})
1104
+ if err != nil {
1105
+ if errors .IsNotFound (err ) {
1106
+ _ , err = e .client .CoreV1 ().ConfigMaps (cfg .Cluster .Namespace ).Create (context .TODO (), configMap , metav1.CreateOptions {})
1107
+ if err != nil {
1108
+ return err
1109
+ }
1110
+ e .log ().Info (configMap .Name + " configmap created successfully" )
1111
+ } else {
1112
+ return err
1113
+ }
1114
+ } else {
1115
+ _ , err = e .client .CoreV1 ().ConfigMaps (cfg .Cluster .Namespace ).Update (context .TODO (), configMap , metav1.UpdateOptions {})
1116
+ if err != nil {
1117
+ return err
1118
+ }
1119
+ e .log ().Info (configMap .Name + " configmap updated successfully" )
1120
+ }
1121
+ return nil
1122
+ }
0 commit comments