github.com/kubeshop/testkube@v1.17.23/pkg/tcl/mapperstcl/testworkflows/kube_openapi.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 testworkflows
    10  
    11  import (
    12  	corev1 "k8s.io/api/core/v1"
    13  	"k8s.io/apimachinery/pkg/api/resource"
    14  	"k8s.io/apimachinery/pkg/util/intstr"
    15  
    16  	testsv3 "github.com/kubeshop/testkube-operator/api/tests/v3"
    17  	testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
    18  
    19  	"github.com/kubeshop/testkube/internal/common"
    20  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    21  )
    22  
    23  func MapIntOrStringToString(i intstr.IntOrString) string {
    24  	return i.String()
    25  }
    26  
    27  func MapIntOrStringPtrToStringPtr(i *intstr.IntOrString) *string {
    28  	if i == nil {
    29  		return nil
    30  	}
    31  	return common.Ptr(MapIntOrStringToString(*i))
    32  }
    33  
    34  func MapIntOrStringToBoxedString(v *intstr.IntOrString) *testkube.BoxedString {
    35  	if v == nil {
    36  		return nil
    37  	}
    38  	return MapStringToBoxedString(common.Ptr(v.String()))
    39  }
    40  
    41  func MapStringToBoxedString(v *string) *testkube.BoxedString {
    42  	if v == nil {
    43  		return nil
    44  	}
    45  	return &testkube.BoxedString{Value: *v}
    46  }
    47  
    48  func MapStringTypeToBoxedString[T ~string](v *T) *testkube.BoxedString {
    49  	if v == nil {
    50  		return nil
    51  	}
    52  	return &testkube.BoxedString{Value: string(*v)}
    53  }
    54  
    55  func MapBoolToBoxedBoolean(v *bool) *testkube.BoxedBoolean {
    56  	if v == nil {
    57  		return nil
    58  	}
    59  	return &testkube.BoxedBoolean{Value: *v}
    60  }
    61  
    62  func MapStringSliceToBoxedStringList(v *[]string) *testkube.BoxedStringList {
    63  	if v == nil {
    64  		return nil
    65  	}
    66  	return &testkube.BoxedStringList{Value: *v}
    67  }
    68  
    69  func MapInt64ToBoxedInteger(v *int64) *testkube.BoxedInteger {
    70  	if v == nil {
    71  		return MapInt32ToBoxedInteger(nil)
    72  	}
    73  	return MapInt32ToBoxedInteger(common.Ptr(int32(*v)))
    74  }
    75  
    76  func MapInt32ToBoxedInteger(v *int32) *testkube.BoxedInteger {
    77  	if v == nil {
    78  		return nil
    79  	}
    80  	return &testkube.BoxedInteger{Value: *v}
    81  }
    82  
    83  func MapQuantityToBoxedString(v *resource.Quantity) *testkube.BoxedString {
    84  	if v == nil {
    85  		return nil
    86  	}
    87  	return &testkube.BoxedString{Value: v.String()}
    88  }
    89  
    90  func MapDynamicListMapKubeToAPI(v map[string]testworkflowsv1.DynamicList) map[string]interface{} {
    91  	if len(v) == 0 {
    92  		return nil
    93  	}
    94  	result := make(map[string]interface{}, len(v))
    95  	for k := range v {
    96  		if v[k].Dynamic {
    97  			result[k] = v[k].Dynamic
    98  		} else {
    99  			result[k] = v[k].Static
   100  		}
   101  	}
   102  	return result
   103  }
   104  
   105  func MapHostPathVolumeSourceKubeToAPI(v corev1.HostPathVolumeSource) testkube.HostPathVolumeSource {
   106  	return testkube.HostPathVolumeSource{
   107  		Path:  v.Path,
   108  		Type_: MapStringTypeToBoxedString[corev1.HostPathType](v.Type),
   109  	}
   110  }
   111  
   112  func MapEmptyDirVolumeSourceKubeToAPI(v corev1.EmptyDirVolumeSource) testkube.EmptyDirVolumeSource {
   113  	return testkube.EmptyDirVolumeSource{
   114  		Medium:    string(v.Medium),
   115  		SizeLimit: MapQuantityToBoxedString(v.SizeLimit),
   116  	}
   117  }
   118  
   119  func MapGCEPersistentDiskVolumeSourceKubeToAPI(v corev1.GCEPersistentDiskVolumeSource) testkube.GcePersistentDiskVolumeSource {
   120  	return testkube.GcePersistentDiskVolumeSource{
   121  		PdName:    v.PDName,
   122  		FsType:    v.FSType,
   123  		Partition: v.Partition,
   124  		ReadOnly:  v.ReadOnly,
   125  	}
   126  }
   127  
   128  func MapAWSElasticBlockStoreVolumeSourceKubeToAPI(v corev1.AWSElasticBlockStoreVolumeSource) testkube.AwsElasticBlockStoreVolumeSource {
   129  	return testkube.AwsElasticBlockStoreVolumeSource{
   130  		VolumeID:  v.VolumeID,
   131  		FsType:    v.FSType,
   132  		Partition: v.Partition,
   133  		ReadOnly:  v.ReadOnly,
   134  	}
   135  }
   136  
   137  func MapKeyToPathKubeToAPI(v corev1.KeyToPath) testkube.SecretVolumeSourceItems {
   138  	return testkube.SecretVolumeSourceItems{
   139  		Key:  v.Key,
   140  		Path: v.Path,
   141  		Mode: MapInt32ToBoxedInteger(v.Mode),
   142  	}
   143  }
   144  
   145  func MapSecretVolumeSourceKubeToAPI(v corev1.SecretVolumeSource) testkube.SecretVolumeSource {
   146  	return testkube.SecretVolumeSource{
   147  		SecretName:  v.SecretName,
   148  		Items:       common.MapSlice(v.Items, MapKeyToPathKubeToAPI),
   149  		DefaultMode: MapInt32ToBoxedInteger(v.DefaultMode),
   150  		Optional:    common.ResolvePtr(v.Optional, false),
   151  	}
   152  }
   153  
   154  func MapNFSVolumeSourceKubeToAPI(v corev1.NFSVolumeSource) testkube.NfsVolumeSource {
   155  	return testkube.NfsVolumeSource{
   156  		Server:   v.Server,
   157  		Path:     v.Path,
   158  		ReadOnly: v.ReadOnly,
   159  	}
   160  }
   161  
   162  func MapPersistentVolumeClaimVolumeSourceKubeToAPI(v corev1.PersistentVolumeClaimVolumeSource) testkube.PersistentVolumeClaimVolumeSource {
   163  	return testkube.PersistentVolumeClaimVolumeSource{
   164  		ClaimName: v.ClaimName,
   165  		ReadOnly:  v.ReadOnly,
   166  	}
   167  }
   168  
   169  func MapCephFSVolumeSourceKubeToAPI(v corev1.CephFSVolumeSource) testkube.CephFsVolumeSource {
   170  	return testkube.CephFsVolumeSource{
   171  		Monitors:   v.Monitors,
   172  		Path:       v.Path,
   173  		User:       v.User,
   174  		SecretFile: v.SecretFile,
   175  		SecretRef:  common.MapPtr(v.SecretRef, MapLocalObjectReferenceKubeToAPI),
   176  		ReadOnly:   v.ReadOnly,
   177  	}
   178  }
   179  
   180  func MapAzureFileVolumeSourceKubeToAPI(v corev1.AzureFileVolumeSource) testkube.AzureFileVolumeSource {
   181  	return testkube.AzureFileVolumeSource{
   182  		SecretName: v.SecretName,
   183  		ShareName:  v.ShareName,
   184  		ReadOnly:   v.ReadOnly,
   185  	}
   186  }
   187  
   188  func MapConfigMapVolumeSourceKubeToAPI(v corev1.ConfigMapVolumeSource) testkube.ConfigMapVolumeSource {
   189  	return testkube.ConfigMapVolumeSource{
   190  		Name:        v.Name,
   191  		Items:       common.MapSlice(v.Items, MapKeyToPathKubeToAPI),
   192  		DefaultMode: MapInt32ToBoxedInteger(v.DefaultMode),
   193  		Optional:    common.ResolvePtr(v.Optional, false),
   194  	}
   195  }
   196  
   197  func MapAzureDiskVolumeSourceKubeToAPI(v corev1.AzureDiskVolumeSource) testkube.AzureDiskVolumeSource {
   198  	return testkube.AzureDiskVolumeSource{
   199  		DiskName:    v.DiskName,
   200  		DiskURI:     v.DataDiskURI,
   201  		CachingMode: MapStringTypeToBoxedString[corev1.AzureDataDiskCachingMode](v.CachingMode),
   202  		FsType:      MapStringToBoxedString(v.FSType),
   203  		ReadOnly:    common.ResolvePtr(v.ReadOnly, false),
   204  		Kind:        MapStringTypeToBoxedString[corev1.AzureDataDiskKind](v.Kind),
   205  	}
   206  }
   207  
   208  func MapVolumeKubeToAPI(v corev1.Volume) testkube.Volume {
   209  	// TODO: Add rest of VolumeSource types in future,
   210  	//       so they will be recognized by JSON API and persisted with Execution.
   211  	return testkube.Volume{
   212  		Name:                  v.Name,
   213  		HostPath:              common.MapPtr(v.HostPath, MapHostPathVolumeSourceKubeToAPI),
   214  		EmptyDir:              common.MapPtr(v.EmptyDir, MapEmptyDirVolumeSourceKubeToAPI),
   215  		GcePersistentDisk:     common.MapPtr(v.GCEPersistentDisk, MapGCEPersistentDiskVolumeSourceKubeToAPI),
   216  		AwsElasticBlockStore:  common.MapPtr(v.AWSElasticBlockStore, MapAWSElasticBlockStoreVolumeSourceKubeToAPI),
   217  		Secret:                common.MapPtr(v.Secret, MapSecretVolumeSourceKubeToAPI),
   218  		Nfs:                   common.MapPtr(v.NFS, MapNFSVolumeSourceKubeToAPI),
   219  		PersistentVolumeClaim: common.MapPtr(v.PersistentVolumeClaim, MapPersistentVolumeClaimVolumeSourceKubeToAPI),
   220  		Cephfs:                common.MapPtr(v.CephFS, MapCephFSVolumeSourceKubeToAPI),
   221  		AzureFile:             common.MapPtr(v.AzureFile, MapAzureFileVolumeSourceKubeToAPI),
   222  		ConfigMap:             common.MapPtr(v.ConfigMap, MapConfigMapVolumeSourceKubeToAPI),
   223  		AzureDisk:             common.MapPtr(v.AzureDisk, MapAzureDiskVolumeSourceKubeToAPI),
   224  	}
   225  }
   226  
   227  func MapEnvVarKubeToAPI(v corev1.EnvVar) testkube.EnvVar {
   228  	return testkube.EnvVar{
   229  		Name:      v.Name,
   230  		Value:     v.Value,
   231  		ValueFrom: common.MapPtr(v.ValueFrom, MapEnvVarSourceKubeToAPI),
   232  	}
   233  }
   234  
   235  func MapConfigMapKeyRefKubeToAPI(v *corev1.ConfigMapKeySelector) *testkube.EnvVarSourceConfigMapKeyRef {
   236  	if v == nil {
   237  		return nil
   238  	}
   239  	return &testkube.EnvVarSourceConfigMapKeyRef{
   240  		Key:      v.Key,
   241  		Name:     v.Name,
   242  		Optional: common.ResolvePtr(v.Optional, false),
   243  	}
   244  }
   245  
   246  func MapFieldRefKubeToAPI(v *corev1.ObjectFieldSelector) *testkube.EnvVarSourceFieldRef {
   247  	if v == nil {
   248  		return nil
   249  	}
   250  	return &testkube.EnvVarSourceFieldRef{
   251  		ApiVersion: v.APIVersion,
   252  		FieldPath:  v.FieldPath,
   253  	}
   254  }
   255  
   256  func MapResourceFieldRefKubeToAPI(v *corev1.ResourceFieldSelector) *testkube.EnvVarSourceResourceFieldRef {
   257  	if v == nil {
   258  		return nil
   259  	}
   260  	divisor := ""
   261  	if !v.Divisor.IsZero() {
   262  		divisor = v.Divisor.String()
   263  	}
   264  	return &testkube.EnvVarSourceResourceFieldRef{
   265  		ContainerName: v.ContainerName,
   266  		Divisor:       divisor,
   267  		Resource:      v.Resource,
   268  	}
   269  }
   270  
   271  func MapSecretKeyRefKubeToAPI(v *corev1.SecretKeySelector) *testkube.EnvVarSourceSecretKeyRef {
   272  	if v == nil {
   273  		return nil
   274  	}
   275  	return &testkube.EnvVarSourceSecretKeyRef{
   276  		Key:      v.Key,
   277  		Name:     v.Name,
   278  		Optional: common.ResolvePtr(v.Optional, false),
   279  	}
   280  }
   281  
   282  func MapEnvVarSourceKubeToAPI(v corev1.EnvVarSource) testkube.EnvVarSource {
   283  	return testkube.EnvVarSource{
   284  		ConfigMapKeyRef:  MapConfigMapKeyRefKubeToAPI(v.ConfigMapKeyRef),
   285  		FieldRef:         MapFieldRefKubeToAPI(v.FieldRef),
   286  		ResourceFieldRef: MapResourceFieldRefKubeToAPI(v.ResourceFieldRef),
   287  		SecretKeyRef:     MapSecretKeyRefKubeToAPI(v.SecretKeyRef),
   288  	}
   289  }
   290  
   291  func MapConfigMapEnvSourceKubeToAPI(v *corev1.ConfigMapEnvSource) *testkube.ConfigMapEnvSource {
   292  	if v == nil {
   293  		return nil
   294  	}
   295  	return &testkube.ConfigMapEnvSource{
   296  		Name:     v.Name,
   297  		Optional: common.ResolvePtr(v.Optional, false),
   298  	}
   299  }
   300  
   301  func MapSecretEnvSourceKubeToAPI(v *corev1.SecretEnvSource) *testkube.SecretEnvSource {
   302  	if v == nil {
   303  		return nil
   304  	}
   305  	return &testkube.SecretEnvSource{
   306  		Name:     v.Name,
   307  		Optional: common.ResolvePtr(v.Optional, false),
   308  	}
   309  }
   310  
   311  func MapEnvFromSourceKubeToAPI(v corev1.EnvFromSource) testkube.EnvFromSource {
   312  	return testkube.EnvFromSource{
   313  		Prefix:       v.Prefix,
   314  		ConfigMapRef: MapConfigMapEnvSourceKubeToAPI(v.ConfigMapRef),
   315  		SecretRef:    MapSecretEnvSourceKubeToAPI(v.SecretRef),
   316  	}
   317  }
   318  
   319  func MapSecurityContextKubeToAPI(v *corev1.SecurityContext) *testkube.SecurityContext {
   320  	if v == nil {
   321  		return nil
   322  	}
   323  	return &testkube.SecurityContext{
   324  		Privileged:               MapBoolToBoxedBoolean(v.Privileged),
   325  		RunAsUser:                MapInt64ToBoxedInteger(v.RunAsUser),
   326  		RunAsGroup:               MapInt64ToBoxedInteger(v.RunAsGroup),
   327  		RunAsNonRoot:             MapBoolToBoxedBoolean(v.RunAsNonRoot),
   328  		ReadOnlyRootFilesystem:   MapBoolToBoxedBoolean(v.ReadOnlyRootFilesystem),
   329  		AllowPrivilegeEscalation: MapBoolToBoxedBoolean(v.AllowPrivilegeEscalation),
   330  	}
   331  }
   332  
   333  func MapLocalObjectReferenceKubeToAPI(v corev1.LocalObjectReference) testkube.LocalObjectReference {
   334  	return testkube.LocalObjectReference{Name: v.Name}
   335  }
   336  
   337  func MapConfigValueKubeToAPI(v map[string]intstr.IntOrString) map[string]string {
   338  	return common.MapMap(v, MapIntOrStringToString)
   339  }
   340  
   341  func MapParameterTypeKubeToAPI(v testworkflowsv1.ParameterType) *testkube.TestWorkflowParameterType {
   342  	if v == "" {
   343  		return nil
   344  	}
   345  	return common.Ptr(testkube.TestWorkflowParameterType(v))
   346  }
   347  
   348  func MapGitAuthTypeKubeToAPI(v testsv3.GitAuthType) *testkube.ContentGitAuthType {
   349  	if v == "" {
   350  		return nil
   351  	}
   352  	return common.Ptr(testkube.ContentGitAuthType(v))
   353  }
   354  
   355  func MapImagePullPolicyKubeToAPI(v corev1.PullPolicy) *testkube.ImagePullPolicy {
   356  	if v == "" {
   357  		return nil
   358  	}
   359  	return common.Ptr(testkube.ImagePullPolicy(v))
   360  }
   361  
   362  func MapParameterSchemaKubeToAPI(v testworkflowsv1.ParameterSchema) testkube.TestWorkflowParameterSchema {
   363  	return testkube.TestWorkflowParameterSchema{
   364  		Description:      v.Description,
   365  		Type_:            MapParameterTypeKubeToAPI(v.Type),
   366  		Enum:             v.Enum,
   367  		Example:          common.ResolvePtr(common.MapPtr(v.Example, MapIntOrStringToString), ""),
   368  		Default_:         MapStringToBoxedString(MapIntOrStringPtrToStringPtr(v.Default)),
   369  		Format:           v.Format,
   370  		Pattern:          v.Pattern,
   371  		MinLength:        MapInt64ToBoxedInteger(v.MinLength),
   372  		MaxLength:        MapInt64ToBoxedInteger(v.MaxLength),
   373  		Minimum:          MapInt64ToBoxedInteger(v.Minimum),
   374  		Maximum:          MapInt64ToBoxedInteger(v.Maximum),
   375  		ExclusiveMinimum: MapInt64ToBoxedInteger(v.ExclusiveMinimum),
   376  		ExclusiveMaximum: MapInt64ToBoxedInteger(v.ExclusiveMaximum),
   377  		MultipleOf:       MapInt64ToBoxedInteger(v.MultipleOf),
   378  	}
   379  }
   380  
   381  func MapTemplateRefKubeToAPI(v testworkflowsv1.TemplateRef) testkube.TestWorkflowTemplateRef {
   382  	return testkube.TestWorkflowTemplateRef{
   383  		Name:   v.Name,
   384  		Config: MapConfigValueKubeToAPI(v.Config),
   385  	}
   386  }
   387  
   388  func MapContentGitKubeToAPI(v testworkflowsv1.ContentGit) testkube.TestWorkflowContentGit {
   389  	return testkube.TestWorkflowContentGit{
   390  		Uri:          v.Uri,
   391  		Revision:     v.Revision,
   392  		Username:     v.Username,
   393  		UsernameFrom: common.MapPtr(v.UsernameFrom, MapEnvVarSourceKubeToAPI),
   394  		Token:        v.Token,
   395  		TokenFrom:    common.MapPtr(v.TokenFrom, MapEnvVarSourceKubeToAPI),
   396  		AuthType:     MapGitAuthTypeKubeToAPI(v.AuthType),
   397  		MountPath:    v.MountPath,
   398  		Paths:        v.Paths,
   399  	}
   400  }
   401  
   402  func MapContentKubeToAPI(v testworkflowsv1.Content) testkube.TestWorkflowContent {
   403  	return testkube.TestWorkflowContent{
   404  		Git:   common.MapPtr(v.Git, MapContentGitKubeToAPI),
   405  		Files: common.MapSlice(v.Files, MapContentFileKubeToAPI),
   406  	}
   407  }
   408  
   409  func MapContentFileKubeToAPI(v testworkflowsv1.ContentFile) testkube.TestWorkflowContentFile {
   410  	return testkube.TestWorkflowContentFile{
   411  		Path:        v.Path,
   412  		Content:     v.Content,
   413  		ContentFrom: common.MapPtr(v.ContentFrom, MapEnvVarSourceKubeToAPI),
   414  		Mode:        MapInt32ToBoxedInteger(v.Mode),
   415  	}
   416  }
   417  
   418  func MapResourcesListKubeToAPI(v map[corev1.ResourceName]intstr.IntOrString) *testkube.TestWorkflowResourcesList {
   419  	if len(v) == 0 {
   420  		return nil
   421  	}
   422  	empty := intstr.IntOrString{Type: intstr.String, StrVal: ""}
   423  	return &testkube.TestWorkflowResourcesList{
   424  		Cpu:              MapIntOrStringToString(common.GetMapValue(v, corev1.ResourceCPU, empty)),
   425  		Memory:           MapIntOrStringToString(common.GetMapValue(v, corev1.ResourceMemory, empty)),
   426  		Storage:          MapIntOrStringToString(common.GetMapValue(v, corev1.ResourceStorage, empty)),
   427  		EphemeralStorage: MapIntOrStringToString(common.GetMapValue(v, corev1.ResourceEphemeralStorage, empty)),
   428  	}
   429  }
   430  
   431  func MapResourcesKubeToAPI(v testworkflowsv1.Resources) testkube.TestWorkflowResources {
   432  	requests := MapResourcesListKubeToAPI(v.Requests)
   433  	limits := MapResourcesListKubeToAPI(v.Limits)
   434  	return testkube.TestWorkflowResources{
   435  		Limits:   limits,
   436  		Requests: requests,
   437  	}
   438  }
   439  
   440  func MapJobConfigKubeToAPI(v testworkflowsv1.JobConfig) testkube.TestWorkflowJobConfig {
   441  	return testkube.TestWorkflowJobConfig{
   442  		Labels:      v.Labels,
   443  		Annotations: v.Annotations,
   444  	}
   445  }
   446  
   447  func MapEventKubeToAPI(v testworkflowsv1.Event) testkube.TestWorkflowEvent {
   448  	return testkube.TestWorkflowEvent{
   449  		Cronjob: common.MapPtr(v.Cronjob, MapCronJobConfigKubeToAPI),
   450  	}
   451  }
   452  
   453  func MapCronJobConfigKubeToAPI(v testworkflowsv1.CronJobConfig) testkube.TestWorkflowCronJobConfig {
   454  	return testkube.TestWorkflowCronJobConfig{
   455  		Cron:        v.Cron,
   456  		Labels:      v.Labels,
   457  		Annotations: v.Annotations,
   458  	}
   459  }
   460  
   461  func MapPodConfigKubeToAPI(v testworkflowsv1.PodConfig) testkube.TestWorkflowPodConfig {
   462  	return testkube.TestWorkflowPodConfig{
   463  		ServiceAccountName: v.ServiceAccountName,
   464  		ImagePullSecrets:   common.MapSlice(v.ImagePullSecrets, MapLocalObjectReferenceKubeToAPI),
   465  		NodeSelector:       v.NodeSelector,
   466  		Labels:             v.Labels,
   467  		Annotations:        v.Annotations,
   468  		Volumes:            common.MapSlice(v.Volumes, MapVolumeKubeToAPI),
   469  	}
   470  }
   471  
   472  func MapVolumeMountKubeToAPI(v corev1.VolumeMount) testkube.VolumeMount {
   473  	return testkube.VolumeMount{
   474  		Name:             v.Name,
   475  		ReadOnly:         v.ReadOnly,
   476  		MountPath:        v.MountPath,
   477  		SubPath:          v.SubPath,
   478  		MountPropagation: MapStringTypeToBoxedString[corev1.MountPropagationMode](v.MountPropagation),
   479  		SubPathExpr:      v.SubPathExpr,
   480  	}
   481  }
   482  
   483  func MapContainerConfigKubeToAPI(v testworkflowsv1.ContainerConfig) testkube.TestWorkflowContainerConfig {
   484  	return testkube.TestWorkflowContainerConfig{
   485  		WorkingDir:      MapStringToBoxedString(v.WorkingDir),
   486  		Image:           v.Image,
   487  		ImagePullPolicy: MapImagePullPolicyKubeToAPI(v.ImagePullPolicy),
   488  		Env:             common.MapSlice(v.Env, MapEnvVarKubeToAPI),
   489  		EnvFrom:         common.MapSlice(v.EnvFrom, MapEnvFromSourceKubeToAPI),
   490  		Command:         MapStringSliceToBoxedStringList(v.Command),
   491  		Args:            MapStringSliceToBoxedStringList(v.Args),
   492  		Resources:       common.MapPtr(v.Resources, MapResourcesKubeToAPI),
   493  		SecurityContext: MapSecurityContextKubeToAPI(v.SecurityContext),
   494  		VolumeMounts:    common.MapSlice(v.VolumeMounts, MapVolumeMountKubeToAPI),
   495  	}
   496  }
   497  
   498  func MapStepRunKubeToAPI(v testworkflowsv1.StepRun) testkube.TestWorkflowStepRun {
   499  	return testkube.TestWorkflowStepRun{
   500  		WorkingDir:      MapStringToBoxedString(v.WorkingDir),
   501  		Image:           v.Image,
   502  		ImagePullPolicy: MapImagePullPolicyKubeToAPI(v.ImagePullPolicy),
   503  		Env:             common.MapSlice(v.Env, MapEnvVarKubeToAPI),
   504  		EnvFrom:         common.MapSlice(v.EnvFrom, MapEnvFromSourceKubeToAPI),
   505  		Command:         MapStringSliceToBoxedStringList(v.Command),
   506  		Args:            MapStringSliceToBoxedStringList(v.Args),
   507  		Shell:           MapStringToBoxedString(v.Shell),
   508  		Resources:       common.MapPtr(v.Resources, MapResourcesKubeToAPI),
   509  		SecurityContext: MapSecurityContextKubeToAPI(v.SecurityContext),
   510  		VolumeMounts:    common.MapSlice(v.VolumeMounts, MapVolumeMountKubeToAPI),
   511  	}
   512  }
   513  
   514  func MapTestVariableKubeToAPI(v testsv3.Variable) testkube.Variable {
   515  	var configMapRef *testkube.ConfigMapRef
   516  	if v.ValueFrom.ConfigMapKeyRef != nil {
   517  		configMapRef = &testkube.ConfigMapRef{
   518  			Name: v.ValueFrom.ConfigMapKeyRef.Name,
   519  			Key:  v.ValueFrom.ConfigMapKeyRef.Key,
   520  		}
   521  	}
   522  	var secretRef *testkube.SecretRef
   523  	if v.ValueFrom.SecretKeyRef != nil {
   524  		secretRef = &testkube.SecretRef{
   525  			Name: v.ValueFrom.SecretKeyRef.Name,
   526  			Key:  v.ValueFrom.SecretKeyRef.Key,
   527  		}
   528  	}
   529  	return testkube.Variable{
   530  		Type_:        common.PtrOrNil(testkube.VariableType(v.Type_)),
   531  		Name:         v.Name,
   532  		Value:        v.Value,
   533  		SecretRef:    secretRef,
   534  		ConfigMapRef: configMapRef,
   535  	}
   536  }
   537  
   538  func MapTestArtifactRequestKubeToAPI(v testsv3.ArtifactRequest) testkube.ArtifactRequest {
   539  	return testkube.ArtifactRequest{
   540  		StorageClassName:           v.StorageClassName,
   541  		VolumeMountPath:            v.VolumeMountPath,
   542  		Dirs:                       v.Dirs,
   543  		Masks:                      v.Masks,
   544  		StorageBucket:              v.StorageBucket,
   545  		OmitFolderPerExecution:     v.OmitFolderPerExecution,
   546  		SharedBetweenPods:          v.SharedBetweenPods,
   547  		UseDefaultStorageClassName: v.UseDefaultStorageClassName,
   548  	}
   549  }
   550  
   551  func MapTestEnvReferenceKubeToAPI(v testsv3.EnvReference) testkube.EnvReference {
   552  	return testkube.EnvReference{
   553  		Reference:      common.PtrOrNil(testkube.LocalObjectReference{Name: v.Name}),
   554  		Mount:          v.Mount,
   555  		MountPath:      v.MountPath,
   556  		MapToVariables: v.MapToVariables,
   557  	}
   558  }
   559  
   560  func MapStepExecuteTestExecutionRequestKubeToAPI(v testworkflowsv1.TestExecutionRequest) testkube.TestWorkflowStepExecuteTestExecutionRequest {
   561  	return testkube.TestWorkflowStepExecuteTestExecutionRequest{
   562  		Name:                               v.Name,
   563  		ExecutionLabels:                    v.ExecutionLabels,
   564  		VariablesFile:                      v.VariablesFile,
   565  		IsVariablesFileUploaded:            v.IsVariablesFileUploaded,
   566  		Variables:                          common.MapMap(v.Variables, MapTestVariableKubeToAPI),
   567  		TestSecretUUID:                     v.TestSecretUUID,
   568  		Args:                               v.Args,
   569  		ArgsMode:                           string(v.ArgsMode),
   570  		Command:                            v.Command,
   571  		Image:                              v.Image,
   572  		ImagePullSecrets:                   common.MapSlice(v.ImagePullSecrets, MapLocalObjectReferenceKubeToAPI),
   573  		Sync:                               v.Sync,
   574  		HttpProxy:                          v.HttpProxy,
   575  		HttpsProxy:                         v.HttpsProxy,
   576  		NegativeTest:                       v.NegativeTest,
   577  		ActiveDeadlineSeconds:              v.ActiveDeadlineSeconds,
   578  		ArtifactRequest:                    common.MapPtr(v.ArtifactRequest, MapTestArtifactRequestKubeToAPI),
   579  		JobTemplate:                        v.JobTemplate,
   580  		CronJobTemplate:                    v.CronJobTemplate,
   581  		PreRunScript:                       v.PreRunScript,
   582  		PostRunScript:                      v.PostRunScript,
   583  		ExecutePostRunScriptBeforeScraping: v.ExecutePostRunScriptBeforeScraping,
   584  		SourceScripts:                      v.SourceScripts,
   585  		ScraperTemplate:                    v.ScraperTemplate,
   586  		EnvConfigMaps:                      common.MapSlice(v.EnvConfigMaps, MapTestEnvReferenceKubeToAPI),
   587  		EnvSecrets:                         common.MapSlice(v.EnvSecrets, MapTestEnvReferenceKubeToAPI),
   588  		ExecutionNamespace:                 v.ExecutionNamespace,
   589  	}
   590  }
   591  func MapStepExecuteTestKubeToAPI(v testworkflowsv1.StepExecuteTest) testkube.TestWorkflowStepExecuteTestRef {
   592  	return testkube.TestWorkflowStepExecuteTestRef{
   593  		Name:             v.Name,
   594  		Description:      v.Description,
   595  		ExecutionRequest: common.MapPtr(v.ExecutionRequest, MapStepExecuteTestExecutionRequestKubeToAPI),
   596  		Count:            MapIntOrStringToBoxedString(v.Count),
   597  		MaxCount:         MapIntOrStringToBoxedString(v.MaxCount),
   598  		Matrix:           MapDynamicListMapKubeToAPI(v.Matrix),
   599  		Shards:           MapDynamicListMapKubeToAPI(v.Shards),
   600  	}
   601  }
   602  
   603  func MapStepExecuteTestWorkflowKubeToAPI(v testworkflowsv1.StepExecuteWorkflow) testkube.TestWorkflowStepExecuteTestWorkflowRef {
   604  	return testkube.TestWorkflowStepExecuteTestWorkflowRef{
   605  		Name:          v.Name,
   606  		Description:   v.Description,
   607  		ExecutionName: v.ExecutionName,
   608  		Config:        MapConfigValueKubeToAPI(v.Config),
   609  		Count:         MapIntOrStringToBoxedString(v.Count),
   610  		MaxCount:      MapIntOrStringToBoxedString(v.MaxCount),
   611  		Matrix:        MapDynamicListMapKubeToAPI(v.Matrix),
   612  		Shards:        MapDynamicListMapKubeToAPI(v.Shards),
   613  	}
   614  }
   615  
   616  func MapStepExecuteKubeToAPI(v testworkflowsv1.StepExecute) testkube.TestWorkflowStepExecute {
   617  	return testkube.TestWorkflowStepExecute{
   618  		Parallelism: v.Parallelism,
   619  		Async:       v.Async,
   620  		Tests:       common.MapSlice(v.Tests, MapStepExecuteTestKubeToAPI),
   621  		Workflows:   common.MapSlice(v.Workflows, MapStepExecuteTestWorkflowKubeToAPI),
   622  	}
   623  }
   624  
   625  func MapStepArtifactsCompressionKubeToAPI(v testworkflowsv1.ArtifactCompression) testkube.TestWorkflowStepArtifactsCompression {
   626  	return testkube.TestWorkflowStepArtifactsCompression{
   627  		Name: v.Name,
   628  	}
   629  }
   630  
   631  func MapStepArtifactsKubeToAPI(v testworkflowsv1.StepArtifacts) testkube.TestWorkflowStepArtifacts {
   632  	return testkube.TestWorkflowStepArtifacts{
   633  		WorkingDir: MapStringToBoxedString(v.WorkingDir),
   634  		Compress:   common.MapPtr(v.Compress, MapStepArtifactsCompressionKubeToAPI),
   635  		Paths:      v.Paths,
   636  	}
   637  }
   638  
   639  func MapRetryPolicyKubeToAPI(v testworkflowsv1.RetryPolicy) testkube.TestWorkflowRetryPolicy {
   640  	return testkube.TestWorkflowRetryPolicy{
   641  		Count: v.Count,
   642  		Until: v.Until,
   643  	}
   644  }
   645  
   646  func MapStepKubeToAPI(v testworkflowsv1.Step) testkube.TestWorkflowStep {
   647  	return testkube.TestWorkflowStep{
   648  		Name:       v.Name,
   649  		Condition:  v.Condition,
   650  		Negative:   v.Negative,
   651  		Optional:   v.Optional,
   652  		Use:        common.MapSlice(v.Use, MapTemplateRefKubeToAPI),
   653  		Template:   common.MapPtr(v.Template, MapTemplateRefKubeToAPI),
   654  		Retry:      common.MapPtr(v.Retry, MapRetryPolicyKubeToAPI),
   655  		Timeout:    v.Timeout,
   656  		Delay:      v.Delay,
   657  		Content:    common.MapPtr(v.Content, MapContentKubeToAPI),
   658  		Shell:      v.Shell,
   659  		Run:        common.MapPtr(v.Run, MapStepRunKubeToAPI),
   660  		WorkingDir: MapStringToBoxedString(v.WorkingDir),
   661  		Container:  common.MapPtr(v.Container, MapContainerConfigKubeToAPI),
   662  		Execute:    common.MapPtr(v.Execute, MapStepExecuteKubeToAPI),
   663  		Artifacts:  common.MapPtr(v.Artifacts, MapStepArtifactsKubeToAPI),
   664  		Setup:      common.MapSlice(v.Setup, MapStepKubeToAPI),
   665  		Steps:      common.MapSlice(v.Steps, MapStepKubeToAPI),
   666  	}
   667  }
   668  
   669  func MapIndependentStepKubeToAPI(v testworkflowsv1.IndependentStep) testkube.TestWorkflowIndependentStep {
   670  	return testkube.TestWorkflowIndependentStep{
   671  		Name:       v.Name,
   672  		Condition:  v.Condition,
   673  		Negative:   v.Negative,
   674  		Optional:   v.Optional,
   675  		Retry:      common.MapPtr(v.Retry, MapRetryPolicyKubeToAPI),
   676  		Timeout:    v.Timeout,
   677  		Delay:      v.Delay,
   678  		Content:    common.MapPtr(v.Content, MapContentKubeToAPI),
   679  		Shell:      v.Shell,
   680  		Run:        common.MapPtr(v.Run, MapStepRunKubeToAPI),
   681  		WorkingDir: MapStringToBoxedString(v.WorkingDir),
   682  		Container:  common.MapPtr(v.Container, MapContainerConfigKubeToAPI),
   683  		Execute:    common.MapPtr(v.Execute, MapStepExecuteKubeToAPI),
   684  		Artifacts:  common.MapPtr(v.Artifacts, MapStepArtifactsKubeToAPI),
   685  		Setup:      common.MapSlice(v.Setup, MapIndependentStepKubeToAPI),
   686  		Steps:      common.MapSlice(v.Steps, MapIndependentStepKubeToAPI),
   687  	}
   688  }
   689  
   690  func MapSpecKubeToAPI(v testworkflowsv1.TestWorkflowSpec) testkube.TestWorkflowSpec {
   691  	return testkube.TestWorkflowSpec{
   692  		Use:       common.MapSlice(v.Use, MapTemplateRefKubeToAPI),
   693  		Config:    common.MapMap(v.Config, MapParameterSchemaKubeToAPI),
   694  		Content:   common.MapPtr(v.Content, MapContentKubeToAPI),
   695  		Container: common.MapPtr(v.Container, MapContainerConfigKubeToAPI),
   696  		Job:       common.MapPtr(v.Job, MapJobConfigKubeToAPI),
   697  		Pod:       common.MapPtr(v.Pod, MapPodConfigKubeToAPI),
   698  		Setup:     common.MapSlice(v.Setup, MapStepKubeToAPI),
   699  		Steps:     common.MapSlice(v.Steps, MapStepKubeToAPI),
   700  		After:     common.MapSlice(v.After, MapStepKubeToAPI),
   701  		Events:    common.MapSlice(v.Events, MapEventKubeToAPI),
   702  	}
   703  }
   704  
   705  func MapTemplateSpecKubeToAPI(v testworkflowsv1.TestWorkflowTemplateSpec) testkube.TestWorkflowTemplateSpec {
   706  	return testkube.TestWorkflowTemplateSpec{
   707  		Config:    common.MapMap(v.Config, MapParameterSchemaKubeToAPI),
   708  		Content:   common.MapPtr(v.Content, MapContentKubeToAPI),
   709  		Container: common.MapPtr(v.Container, MapContainerConfigKubeToAPI),
   710  		Job:       common.MapPtr(v.Job, MapJobConfigKubeToAPI),
   711  		Pod:       common.MapPtr(v.Pod, MapPodConfigKubeToAPI),
   712  		Setup:     common.MapSlice(v.Setup, MapIndependentStepKubeToAPI),
   713  		Steps:     common.MapSlice(v.Steps, MapIndependentStepKubeToAPI),
   714  		After:     common.MapSlice(v.After, MapIndependentStepKubeToAPI),
   715  		Events:    common.MapSlice(v.Events, MapEventKubeToAPI),
   716  	}
   717  }
   718  
   719  func MapTestWorkflowKubeToAPI(w testworkflowsv1.TestWorkflow) testkube.TestWorkflow {
   720  	return testkube.TestWorkflow{
   721  		Name:        w.Name,
   722  		Namespace:   w.Namespace,
   723  		Labels:      w.Labels,
   724  		Annotations: w.Annotations,
   725  		Created:     w.CreationTimestamp.Time,
   726  		Description: w.Description,
   727  		Spec:        common.Ptr(MapSpecKubeToAPI(w.Spec)),
   728  	}
   729  }
   730  
   731  func MapTestWorkflowTemplateKubeToAPI(w testworkflowsv1.TestWorkflowTemplate) testkube.TestWorkflowTemplate {
   732  	return testkube.TestWorkflowTemplate{
   733  		Name:        w.Name,
   734  		Namespace:   w.Namespace,
   735  		Labels:      w.Labels,
   736  		Annotations: w.Annotations,
   737  		Created:     w.CreationTimestamp.Time,
   738  		Description: w.Description,
   739  		Spec:        common.Ptr(MapTemplateSpecKubeToAPI(w.Spec)),
   740  	}
   741  }
   742  
   743  func MapTemplateKubeToAPI(w *testworkflowsv1.TestWorkflowTemplate) *testkube.TestWorkflowTemplate {
   744  	return common.MapPtr(w, MapTestWorkflowTemplateKubeToAPI)
   745  }
   746  
   747  func MapKubeToAPI(w *testworkflowsv1.TestWorkflow) *testkube.TestWorkflow {
   748  	return common.MapPtr(w, MapTestWorkflowKubeToAPI)
   749  }
   750  
   751  func MapListKubeToAPI(v *testworkflowsv1.TestWorkflowList) []testkube.TestWorkflow {
   752  	workflows := make([]testkube.TestWorkflow, len(v.Items))
   753  	for i, item := range v.Items {
   754  		workflows[i] = MapTestWorkflowKubeToAPI(item)
   755  	}
   756  	return workflows
   757  }
   758  
   759  func MapTemplateListKubeToAPI(v *testworkflowsv1.TestWorkflowTemplateList) []testkube.TestWorkflowTemplate {
   760  	workflows := make([]testkube.TestWorkflowTemplate, len(v.Items))
   761  	for i, item := range v.Items {
   762  		workflows[i] = MapTestWorkflowTemplateKubeToAPI(item)
   763  	}
   764  	return workflows
   765  }