github.com/oam-dev/kubevela@v1.9.11/vela-templates/definitions/internal/trait/storage.cue (about)

     1  storage: {
     2  	type: "trait"
     3  	annotations: {}
     4  	labels: {}
     5  	description: "Add storages on K8s pod for your workload which follows the pod spec in path 'spec.template'."
     6  	attributes: {
     7  		appliesToWorkloads: ["deployments.apps", "statefulsets.apps", "daemonsets.apps", "jobs.batch"]
     8  		podDisruptive: true
     9  	}
    10  }
    11  template: {
    12  	volumesList: [
    13  		if parameter.pvc != _|_ for v in parameter.pvc {
    14  			{
    15  				name: "pvc-" + v.name
    16  				persistentVolumeClaim: claimName: v.name
    17  			}
    18  		},
    19  		if parameter.configMap != _|_ for v in parameter.configMap if v.mountPath != _|_ {
    20  			{
    21  				name: "configmap-" + v.name
    22  				configMap: {
    23  					defaultMode: v.defaultMode
    24  					name:        v.name
    25  					if v.items != _|_ {
    26  						items: v.items
    27  					}
    28  				}
    29  			}
    30  		},
    31  		if parameter.secret != _|_ for v in parameter.secret if v.mountPath != _|_ {
    32  			{
    33  				name: "secret-" + v.name
    34  				secret: {
    35  					defaultMode: v.defaultMode
    36  					secretName:  v.name
    37  					if v.items != _|_ {
    38  						items: v.items
    39  					}
    40  				}
    41  			}
    42  		},
    43  		if parameter.emptyDir != _|_ for v in parameter.emptyDir {
    44  			{
    45  				name: "emptydir-" + v.name
    46  				emptyDir: {
    47  					medium: v.medium
    48  				}
    49  			}
    50  		},
    51  		if parameter.hostPath != _|_ for v in parameter.hostPath {
    52  			{
    53  				name: "hostpath-" + v.name
    54  				path: v.path
    55  			}
    56  		},
    57  	]
    58  
    59  	volumeMountsList: [
    60  		if parameter.pvc != _|_ for v in parameter.pvc {
    61  			if v.volumeMode == "Filesystem" {
    62  				{
    63  					name:      "pvc-" + v.name
    64  					mountPath: v.mountPath
    65  					if v.subPath != _|_ {
    66  						subPath: v.subPath
    67  					}
    68  				}
    69  			}
    70  		},
    71  		if parameter.configMap != _|_ for v in parameter.configMap if v.mountPath != _|_ {
    72  			{
    73  				name:      "configmap-" + v.name
    74  				mountPath: v.mountPath
    75  				if v.subPath != _|_ {
    76  					subPath: v.subPath
    77  				}
    78  			}
    79  		},
    80  		if parameter.secret != _|_ for v in parameter.secret if v.mountPath != _|_ {
    81  			{
    82  				name:      "secret-" + v.name
    83  				mountPath: v.mountPath
    84  				if v.subPath != _|_ {
    85  					subPath: v.subPath
    86  				}
    87  			}
    88  		},
    89  		if parameter.emptyDir != _|_ for v in parameter.emptyDir {
    90  			{
    91  				name:      "emptydir-" + v.name
    92  				mountPath: v.mountPath
    93  				if v.subPath != _|_ {
    94  					subPath: v.subPath
    95  				}
    96  			}
    97  		},
    98  		if parameter.hostPath != _|_ for v in parameter.hostPath {
    99  			{
   100  				name:      "hostpath-" + v.name
   101  				mountPath: v.mountPath
   102  			}
   103  		},
   104  	]
   105  
   106  	envList: [
   107  		if parameter.configMap != _|_ for v in parameter.configMap if v.mountToEnv != _|_ {
   108  			{
   109  				name: v.mountToEnv.envName
   110  				valueFrom: configMapKeyRef: {
   111  					name: v.name
   112  					key:  v.mountToEnv.configMapKey
   113  				}
   114  			}
   115  		},
   116  		if parameter.configMap != _|_ for v in parameter.configMap if v.mountToEnvs != _|_ for k in v.mountToEnvs {
   117  			{
   118  				name: k.envName
   119  				valueFrom: configMapKeyRef: {
   120  					name: v.name
   121  					key:  k.configMapKey
   122  				}
   123  			}
   124  		},
   125  		if parameter.secret != _|_ for v in parameter.secret if v.mountToEnv != _|_ {
   126  			{
   127  				name: v.mountToEnv.envName
   128  				valueFrom: secretKeyRef: {
   129  					name: v.name
   130  					key:  v.mountToEnv.secretKey
   131  				}
   132  			}
   133  		},
   134  		if parameter.secret != _|_ for v in parameter.secret if v.mountToEnvs != _|_ for k in v.mountToEnvs {
   135  			{
   136  				name: k.envName
   137  				valueFrom: secretKeyRef: {
   138  					name: v.name
   139  					key:  k.secretKey
   140  				}
   141  			}
   142  		},
   143  	]
   144  
   145  	volumeDevicesList: *[
   146  				for v in parameter.pvc if v.volumeMode == "Block" {
   147  			{
   148  				name:       "pvc-" + v.name
   149  				devicePath: v.mountPath
   150  				if v.subPath != _|_ {
   151  					subPath: v.subPath
   152  				}
   153  			}
   154  		},
   155  	] | []
   156  
   157  	deDupVolumesArray: [
   158  		for val in [
   159  			for i, vi in volumesList {
   160  				for j, vj in volumesList if j < i && vi.name == vj.name {
   161  					_ignore: true
   162  				}
   163  				vi
   164  			},
   165  		] if val._ignore == _|_ {
   166  			val
   167  		},
   168  	]
   169  
   170  	patch: spec: template: spec: {
   171  		// +patchKey=name
   172  		volumes: deDupVolumesArray
   173  
   174  		containers: [{
   175  			// +patchKey=name
   176  			env: envList
   177  			// +patchKey=name
   178  			volumeDevices: volumeDevicesList
   179  			// +patchKey=name
   180  			volumeMounts: volumeMountsList
   181  		}, ...]
   182  
   183  	}
   184  
   185  	outputs: {
   186  		for v in parameter.pvc {
   187  			if v.mountOnly == false {
   188  				"pvc-\(v.name)": {
   189  					apiVersion: "v1"
   190  					kind:       "PersistentVolumeClaim"
   191  					metadata: {
   192  						name: v.name
   193  					}
   194  					spec: {
   195  						accessModes: v.accessModes
   196  						volumeMode:  v.volumeMode
   197  						if v.volumeName != _|_ {
   198  							volumeName: v.volumeName
   199  						}
   200  						if v.storageClassName != _|_ {
   201  							storageClassName: v.storageClassName
   202  						}
   203  
   204  						if v.resources.requests.storage == _|_ {
   205  							resources: requests: storage: "8Gi"
   206  						}
   207  						if v.resources.requests.storage != _|_ {
   208  							resources: requests: storage: v.resources.requests.storage
   209  						}
   210  						if v.resources.limits.storage != _|_ {
   211  							resources: limits: storage: v.resources.limits.storage
   212  						}
   213  						if v.dataSourceRef != _|_ {
   214  							dataSourceRef: v.dataSourceRef
   215  						}
   216  						if v.dataSource != _|_ {
   217  							dataSource: v.dataSource
   218  						}
   219  						if v.selector != _|_ {
   220  							dataSource: v.selector
   221  						}
   222  					}
   223  				}
   224  			}
   225  		}
   226  
   227  		for v in parameter.configMap {
   228  			if v.mountOnly == false {
   229  				"configmap-\(v.name)": {
   230  					apiVersion: "v1"
   231  					kind:       "ConfigMap"
   232  					metadata: name: v.name
   233  					if v.data != _|_ {
   234  						data: v.data
   235  					}
   236  				}
   237  			}
   238  		}
   239  
   240  		for v in parameter.secret {
   241  			if v.mountOnly == false {
   242  				"secret-\(v.name)": {
   243  					apiVersion: "v1"
   244  					kind:       "Secret"
   245  					metadata: name: v.name
   246  					if v.data != _|_ {
   247  						data: v.data
   248  					}
   249  					if v.stringData != _|_ {
   250  						stringData: v.stringData
   251  					}
   252  				}
   253  			}
   254  		}
   255  
   256  	}
   257  
   258  	parameter: {
   259  		// +usage=Declare pvc type storage
   260  		pvc?: [...{
   261  			name:              string
   262  			mountOnly:         *false | bool
   263  			mountPath:         string
   264  			subPath?:          string
   265  			volumeMode:        *"Filesystem" | string
   266  			volumeName?:       string
   267  			accessModes:       *["ReadWriteOnce"] | [...string]
   268  			storageClassName?: string
   269  			resources?: {
   270  				requests: storage: =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$"
   271  				limits?: storage:  =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$"
   272  			}
   273  			dataSourceRef?: {
   274  				name:     string
   275  				kind:     string
   276  				apiGroup: string
   277  			}
   278  			dataSource?: {
   279  				name:     string
   280  				kind:     string
   281  				apiGroup: string
   282  			}
   283  			selector?: {
   284  				matchLabels?: [string]: string
   285  				matchExpressions?: {
   286  					key: string
   287  					values: [...string]
   288  					operator: string
   289  				}
   290  			}
   291  		}]
   292  
   293  		// +usage=Declare config map type storage
   294  		configMap?: [...{
   295  			name:      string
   296  			mountOnly: *false | bool
   297  			mountToEnv?: {
   298  				envName:      string
   299  				configMapKey: string
   300  			}
   301  			mountToEnvs?: [...{
   302  				envName:      string
   303  				configMapKey: string
   304  			}]
   305  			mountPath?:  string
   306  			subPath?:    string
   307  			defaultMode: *420 | int
   308  			readOnly:    *false | bool
   309  			data?: {...}
   310  			items?: [...{
   311  				key:  string
   312  				path: string
   313  				mode: *511 | int
   314  			}]
   315  		}]
   316  
   317  		// +usage=Declare secret type storage
   318  		secret?: [...{
   319  			name:      string
   320  			mountOnly: *false | bool
   321  			mountToEnv?: {
   322  				envName:   string
   323  				secretKey: string
   324  			}
   325  			mountToEnvs?: [...{
   326  				envName:   string
   327  				secretKey: string
   328  			}]
   329  			mountPath:   string
   330  			subPath?:    string
   331  			defaultMode: *420 | int
   332  			readOnly:    *false | bool
   333  			stringData?: {...}
   334  			data?: {...}
   335  			items?: [...{
   336  				key:  string
   337  				path: string
   338  				mode: *511 | int
   339  			}]
   340  		}]
   341  
   342  		// +usage=Declare empty dir type storage
   343  		emptyDir?: [...{
   344  			name:      string
   345  			mountPath: string
   346  			subPath?:  string
   347  			medium:    *"" | "Memory"
   348  		}]
   349  
   350  		// +usage=Declare host path type storage
   351  		hostPath?: [...{
   352  			name:      string
   353  			path:      string
   354  			mountPath: string
   355  			type:      *"Directory" | "DirectoryOrCreate" | "FileOrCreate" | "File" | "Socket" | "CharDevice" | "BlockDevice"
   356  		}]
   357  	}
   358  
   359  }