Skip to content

Commit 8615304

Browse files
S-mishinayokawasa
andauthored
Support Custom labels and annotations to be added to Pod template of a Gatling runner job (#63)
* Add label to Gatling job manifest * delete commentout code * delete Unnecessary code * Add annotation to Gatling job manifest * add labels & annotation Report Available * fix code * add runner label & reporter label * fix code * fix delete space * Added behavior when label does not exist * Add label to type even when label does not exist * Move Add_labels_pods function to utils.go * fix code * fix code * fix code * add map linkage * fix comment * fix code * fix ci test * Update pkg/utils/utils.go fix Streamlining the code Co-authored-by: Yoichi Kawasaki <yokawasa@gmail.com> * fix parameters Co-authored-by: Yoichi Kawasaki <yokawasa@gmail.com>
1 parent 574bb8f commit 8615304

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

controllers/gatling_controller.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (r *GatlingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
8181
log.Info("Reconciling Gatling")
8282
// r.dumpGatlingStatus(gatling, log)
8383
if r.isGatlingCompleted(gatling) {
84-
log.Info("Gatling job has completed!", "name", gatling.ObjectMeta.Name, "namespace", gatling.ObjectMeta.Namespace)
84+
log.Info("Gatling job has completed!", "name", r.getObjectMeta(gatling).Name, "namespace", r.getObjectMeta(gatling).Namespace)
8585

8686
// Clean up Job resources if neccessary
8787
if gatling.Spec.CleanupAfterJobDone {
@@ -458,6 +458,11 @@ func (r *GatlingReconciler) newGatlingRunnerJobForCR(gatling *gatlingv1alpha1.Ga
458458
Parallelism: r.getGatlingRunnerJobParallelism(gatling),
459459
Completions: r.getGatlingRunnerJobParallelism(gatling),
460460
Template: corev1.PodTemplateSpec{
461+
ObjectMeta: metav1.ObjectMeta{
462+
Name: r.getObjectMeta(gatling).Name,
463+
Labels: utils.AddMapValue("type", "runner", r.getObjectMeta(gatling).Labels, true),
464+
Annotations: r.getObjectMeta(gatling).Annotations,
465+
},
461466
Spec: corev1.PodSpec{
462467
Affinity: r.getPodAffinity(gatling),
463468
Tolerations: r.getPodTolerations(gatling),
@@ -519,6 +524,11 @@ func (r *GatlingReconciler) newGatlingRunnerJobForCR(gatling *gatlingv1alpha1.Ga
519524
Parallelism: &gatling.Spec.TestScenarioSpec.Parallelism,
520525
Completions: &gatling.Spec.TestScenarioSpec.Parallelism,
521526
Template: corev1.PodTemplateSpec{
527+
ObjectMeta: metav1.ObjectMeta{
528+
Name: r.getObjectMeta(gatling).Name,
529+
Labels: utils.AddMapValue("type", "runner", r.getObjectMeta(gatling).Labels, true),
530+
Annotations: r.getObjectMeta(gatling).Annotations,
531+
},
522532
Spec: corev1.PodSpec{
523533
Affinity: r.getPodAffinity(gatling),
524534
Tolerations: r.getPodTolerations(gatling),
@@ -588,6 +598,11 @@ func (r *GatlingReconciler) newGatlingReporterJobForCR(gatling *gatlingv1alpha1.
588598
},
589599
Spec: batchv1.JobSpec{
590600
Template: corev1.PodTemplateSpec{
601+
ObjectMeta: metav1.ObjectMeta{
602+
Name: r.getObjectMeta(gatling).Name,
603+
Labels: utils.AddMapValue("type", "reporter", r.getObjectMeta(gatling).Labels, true),
604+
Annotations: r.getObjectMeta(gatling).Annotations,
605+
},
591606
Spec: corev1.PodSpec{
592607
Affinity: r.getPodAffinity(gatling),
593608
Tolerations: r.getPodTolerations(gatling),
@@ -922,6 +937,14 @@ func (r *GatlingReconciler) getPodResources(gatling *gatlingv1alpha1.Gatling) co
922937
return resources
923938
}
924939

940+
func (r *GatlingReconciler) getObjectMeta(gatling *gatlingv1alpha1.Gatling) *metav1.ObjectMeta {
941+
objectmeta := metav1.ObjectMeta{}
942+
if &gatling != nil && &gatling.ObjectMeta != nil {
943+
objectmeta = gatling.ObjectMeta
944+
}
945+
return &objectmeta
946+
}
947+
925948
func (r *GatlingReconciler) getPodAffinity(gatling *gatlingv1alpha1.Gatling) *corev1.Affinity {
926949
affinity := corev1.Affinity{}
927950
if &gatling.Spec.PodSpec != nil && &gatling.Spec.PodSpec.Affinity != nil {

pkg/utils/utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,16 @@ func GetMapValue(key string, dataMap map[string][]byte) (string, bool) {
2323
}
2424
return "", false
2525
}
26+
27+
// Add key-value to map
28+
func AddMapValue(key string, value string, dataMap map[string]string, overwrite bool) map[string]string {
29+
if _, ok := dataMap[key]; ok && !overwrite {
30+
return dataMap
31+
}
32+
if dataMap == nil {
33+
dataMap = map[string]string{}
34+
}
35+
dataMap[key] = value
36+
return dataMap
37+
38+
}

0 commit comments

Comments
 (0)