github.com/kubeshop/testkube@v1.17.23/pkg/tcl/testworkflowstcl/testworkflowresolver/merge.go (about)

     1  // Copyright 2024 Testkube.
     2  //
     3  // Licensed as a Testkube Pro file under the Testkube Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //     https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt
     8  
     9  package testworkflowresolver
    10  
    11  import (
    12  	"maps"
    13  
    14  	corev1 "k8s.io/api/core/v1"
    15  
    16  	testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
    17  	"github.com/kubeshop/testkube/internal/common"
    18  )
    19  
    20  func MergePodConfig(dst, include *testworkflowsv1.PodConfig) *testworkflowsv1.PodConfig {
    21  	if dst == nil {
    22  		return include
    23  	} else if include == nil {
    24  		return dst
    25  	}
    26  	dst.Labels = MergeMap(dst.Labels, include.Labels)
    27  	dst.Annotations = MergeMap(dst.Annotations, include.Annotations)
    28  	dst.NodeSelector = MergeMap(dst.NodeSelector, include.NodeSelector)
    29  	dst.Volumes = append(dst.Volumes, include.Volumes...)
    30  	dst.ImagePullSecrets = append(dst.ImagePullSecrets, include.ImagePullSecrets...)
    31  	if include.ServiceAccountName != "" {
    32  		dst.ServiceAccountName = include.ServiceAccountName
    33  	}
    34  	return dst
    35  }
    36  
    37  func MergeJobConfig(dst, include *testworkflowsv1.JobConfig) *testworkflowsv1.JobConfig {
    38  	if dst == nil {
    39  		return include
    40  	} else if include == nil {
    41  		return dst
    42  	}
    43  	dst.Labels = MergeMap(dst.Labels, include.Labels)
    44  	dst.Annotations = MergeMap(dst.Annotations, include.Annotations)
    45  	return dst
    46  }
    47  
    48  func MergeContentGit(dst, include *testworkflowsv1.ContentGit) *testworkflowsv1.ContentGit {
    49  	if dst == nil {
    50  		return include
    51  	} else if include == nil {
    52  		return dst
    53  	}
    54  	return include
    55  }
    56  
    57  func MergeSecurityContext(dst, include *corev1.SecurityContext) *corev1.SecurityContext {
    58  	if dst == nil {
    59  		return include
    60  	} else if include == nil {
    61  		return dst
    62  	}
    63  	return include
    64  }
    65  
    66  func MergeContent(dst, include *testworkflowsv1.Content) *testworkflowsv1.Content {
    67  	if dst == nil {
    68  		return include
    69  	} else if include == nil {
    70  		return dst
    71  	}
    72  	dst.Files = append(dst.Files, include.Files...)
    73  	dst.Git = MergeContentGit(dst.Git, include.Git)
    74  	return dst
    75  }
    76  
    77  func MergeResources(dst, include *testworkflowsv1.Resources) *testworkflowsv1.Resources {
    78  	if dst == nil {
    79  		return include
    80  	} else if include == nil {
    81  		return dst
    82  	}
    83  	dst.Requests = MergeMap(dst.Requests, include.Requests)
    84  	dst.Limits = MergeMap(dst.Limits, include.Limits)
    85  	return dst
    86  }
    87  
    88  func MergeContainerConfig(dst, include *testworkflowsv1.ContainerConfig) *testworkflowsv1.ContainerConfig {
    89  	if dst == nil {
    90  		return include
    91  	} else if include == nil {
    92  		return dst
    93  	}
    94  	if include.WorkingDir != nil {
    95  		dst.WorkingDir = include.WorkingDir
    96  	}
    97  	if include.ImagePullPolicy != "" {
    98  		dst.ImagePullPolicy = include.ImagePullPolicy
    99  	}
   100  	dst.Env = append(dst.Env, include.Env...)
   101  	dst.EnvFrom = append(dst.EnvFrom, include.EnvFrom...)
   102  	dst.VolumeMounts = append(dst.VolumeMounts, include.VolumeMounts...)
   103  	if include.Image != "" {
   104  		dst.Image = include.Image
   105  		dst.Command = include.Command
   106  		dst.Args = include.Args
   107  	} else if include.Command != nil {
   108  		dst.Command = include.Command
   109  		dst.Args = include.Args
   110  	} else if include.Args != nil {
   111  		dst.Args = include.Args
   112  	}
   113  	dst.Resources = MergeResources(dst.Resources, include.Resources)
   114  	dst.SecurityContext = MergeSecurityContext(dst.SecurityContext, include.SecurityContext)
   115  	return dst
   116  }
   117  
   118  func MergeMap[T comparable, U any](dst, include map[T]U) map[T]U {
   119  	if include == nil {
   120  		return dst
   121  	} else if dst == nil {
   122  		return include
   123  	}
   124  	maps.Copy(dst, include)
   125  	return dst
   126  }
   127  
   128  func ConvertIndependentStepToStep(step testworkflowsv1.IndependentStep) (res testworkflowsv1.Step) {
   129  	res.StepBase = step.StepBase
   130  	res.Setup = common.MapSlice(step.Setup, ConvertIndependentStepToStep)
   131  	res.Steps = common.MapSlice(step.Steps, ConvertIndependentStepToStep)
   132  	return res
   133  }