From c23a1f7dbed00bab3f84f282f5b57f80a91f08e9 Mon Sep 17 00:00:00 2001 From: Gunjan Vyas Date: Fri, 1 Jul 2022 15:24:42 +0530 Subject: [PATCH] Added PVC support to volume mounts (#1693) * Added PVC support to volume mounts * Added unit tests for pvc volume type * Added e2e tests --- docs/cmd/kn_container_add.md | 2 +- docs/cmd/kn_service_apply.md | 4 +- docs/cmd/kn_service_create.md | 4 +- docs/cmd/kn_service_update.md | 4 +- docs/cmd/kn_source_container_create.md | 4 +- docs/cmd/kn_source_container_update.md | 4 +- pkg/kn/flags/podspec.go | 8 +-- pkg/kn/flags/podspec_helper.go | 16 +++++- pkg/kn/flags/podspec_helper_test.go | 70 +++++++++++++++++++------- test/e2e/service_test.go | 48 +++++++++++++++++- 10 files changed, 129 insertions(+), 35 deletions(-) diff --git a/docs/cmd/kn_container_add.md b/docs/cmd/kn_container_add.md index 208d47693..a78b1392f 100644 --- a/docs/cmd/kn_container_add.md +++ b/docs/cmd/kn_container_add.md @@ -38,7 +38,7 @@ kn container add NAME -h, --help help for add --image string Image to run. --limit strings The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource limit, append "-" to the resource name, e.g. '--limit memory-'. - --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:)or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. + --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:), a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim) or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. -p, --port string The port where application listens on, in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'. --pull-policy string Image pull policy. Valid values (case insensitive): Always | Never | IfNotPresent --pull-secret string Image pull secret to set. An empty argument ("") clears the pull secret. The referenced secret must exist in the service's namespace. diff --git a/docs/cmd/kn_service_apply.md b/docs/cmd/kn_service_apply.md index 9dde83c6e..526d713db 100644 --- a/docs/cmd/kn_service_apply.md +++ b/docs/cmd/kn_service_apply.md @@ -50,7 +50,7 @@ kn service apply s0 --filename my-svc.yml --label-service stringArray Service label to set. name=value; you may provide this flag any number of times to set multiple labels. To unset, specify the label name followed by a "-" (e.g., name-). This flag takes precedence over the "label" flag. --limit strings The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource limit, append "-" to the resource name, e.g. '--limit memory-'. --lock-to-digest Keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision) (default true) - --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:)or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. + --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:), a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim) or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. -n, --namespace string Specify the namespace to operate in. --no-cluster-local Do not specify that the service be private. (--no-cluster-local will make the service publicly available) (default true) --no-lock-to-digest Do not keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision) @@ -71,7 +71,7 @@ kn service apply s0 --filename my-svc.yml --service-account string Service account name to set. An empty argument ("") clears the service account. The referenced service account must exist in the service's namespace. --timeout int Duration in seconds that the request routing layer will wait for a request delivered to a container to begin replying (default 300) --user int The user ID to run the container (e.g., 1001). - --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), or an EmptyDir (prefix ed: or emptyDir:). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. + --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:) or a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. --wait Wait for 'service apply' operation to be completed. (default true) --wait-timeout int Seconds to wait before giving up on waiting for service to be ready. (default 600) --wait-window int Seconds to wait for service to be ready after a false ready condition is returned (default 2) diff --git a/docs/cmd/kn_service_create.md b/docs/cmd/kn_service_create.md index db5cb8310..cd6eef9a8 100644 --- a/docs/cmd/kn_service_create.md +++ b/docs/cmd/kn_service_create.md @@ -75,7 +75,7 @@ kn service create NAME --image IMAGE --label-service stringArray Service label to set. name=value; you may provide this flag any number of times to set multiple labels. To unset, specify the label name followed by a "-" (e.g., name-). This flag takes precedence over the "label" flag. --limit strings The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource limit, append "-" to the resource name, e.g. '--limit memory-'. --lock-to-digest Keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision) (default true) - --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:)or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. + --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:), a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim) or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. -n, --namespace string Specify the namespace to operate in. --no-cluster-local Do not specify that the service be private. (--no-cluster-local will make the service publicly available) (default true) --no-lock-to-digest Do not keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision) @@ -98,7 +98,7 @@ kn service create NAME --image IMAGE --target string Work on local directory instead of a remote cluster (experimental) --timeout int Duration in seconds that the request routing layer will wait for a request delivered to a container to begin replying (default 300) --user int The user ID to run the container (e.g., 1001). - --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), or an EmptyDir (prefix ed: or emptyDir:). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. + --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:) or a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. --wait Wait for 'service create' operation to be completed. (default true) --wait-timeout int Seconds to wait before giving up on waiting for service to be ready. (default 600) --wait-window int Seconds to wait for service to be ready after a false ready condition is returned (default 2) diff --git a/docs/cmd/kn_service_update.md b/docs/cmd/kn_service_update.md index 8d602b617..9105e41ef 100644 --- a/docs/cmd/kn_service_update.md +++ b/docs/cmd/kn_service_update.md @@ -62,7 +62,7 @@ kn service update NAME --label-service stringArray Service label to set. name=value; you may provide this flag any number of times to set multiple labels. To unset, specify the label name followed by a "-" (e.g., name-). This flag takes precedence over the "label" flag. --limit strings The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource limit, append "-" to the resource name, e.g. '--limit memory-'. --lock-to-digest Keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision) (default true) - --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:)or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. + --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:), a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim) or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. -n, --namespace string Specify the namespace to operate in. --no-cluster-local Do not specify that the service be private. (--no-cluster-local will make the service publicly available) (default true) --no-lock-to-digest Do not keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision) @@ -87,7 +87,7 @@ kn service update NAME --traffic strings Set traffic distribution (format: --traffic revisionRef=percent) where revisionRef can be a revision or a tag or '@latest' string representing latest ready revision. This flag can be given multiple times with percent summing up to 100%. --untag strings Untag revision (format: --untag tagName). This flag can be specified multiple times. --user int The user ID to run the container (e.g., 1001). - --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), or an EmptyDir (prefix ed: or emptyDir:). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. + --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:) or a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. --wait Wait for 'service update' operation to be completed. (default true) --wait-timeout int Seconds to wait before giving up on waiting for service to be ready. (default 600) --wait-window int Seconds to wait for service to be ready after a false ready condition is returned (default 2) diff --git a/docs/cmd/kn_source_container_create.md b/docs/cmd/kn_source_container_create.md index f21e1ce71..c1130f8e6 100644 --- a/docs/cmd/kn_source_container_create.md +++ b/docs/cmd/kn_source_container_create.md @@ -27,7 +27,7 @@ kn source container create NAME --image IMAGE --sink SINK -h, --help help for create --image string Image to run. --limit strings The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource limit, append "-" to the resource name, e.g. '--limit memory-'. - --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:)or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. + --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:), a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim) or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. -n, --namespace string Specify the namespace to operate in. -p, --port string The port where application listens on, in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'. --pull-policy string Image pull policy. Valid values (case insensitive): Always | Never | IfNotPresent @@ -36,7 +36,7 @@ kn source container create NAME --image IMAGE --sink SINK --service-account string Service account name to set. An empty argument ("") clears the service account. The referenced service account must exist in the service's namespace. -s, --sink string Addressable sink for events. You can specify a broker, channel, Knative service or URI. Examples: '--sink broker:nest' for a broker 'nest', '--sink channel:pipe' for a channel 'pipe', '--sink ksvc:mysvc:mynamespace' for a Knative service 'mysvc' in another namespace 'mynamespace', '--sink https://event.receiver.uri' for an URI with an 'http://' or 'https://' schema, '--sink ksvc:receiver' or simply '--sink receiver' for a Knative service 'receiver' in the current namespace. If a prefix is not provided, it is considered as a Knative service in the current namespace. If referring to a Knative service in another namespace, 'ksvc:name:namespace' combination must be provided explicitly. --user int The user ID to run the container (e.g., 1001). - --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), or an EmptyDir (prefix ed: or emptyDir:). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. + --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:) or a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. ``` ### Options inherited from parent commands diff --git a/docs/cmd/kn_source_container_update.md b/docs/cmd/kn_source_container_update.md index d41305d62..2dad84f0f 100644 --- a/docs/cmd/kn_source_container_update.md +++ b/docs/cmd/kn_source_container_update.md @@ -27,7 +27,7 @@ kn source container update NAME --image IMAGE -h, --help help for update --image string Image to run. --limit strings The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource limit, append "-" to the resource name, e.g. '--limit memory-'. - --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:)or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. + --mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:), a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim) or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can specify a volume subpath by following the volume name with slash separated path. Example: --mount /mydir=cm:myconfigmap/subpath/to/be/mounted. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume. -n, --namespace string Specify the namespace to operate in. -p, --port string The port where application listens on, in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'. --pull-policy string Image pull policy. Valid values (case insensitive): Always | Never | IfNotPresent @@ -36,7 +36,7 @@ kn source container update NAME --image IMAGE --service-account string Service account name to set. An empty argument ("") clears the service account. The referenced service account must exist in the service's namespace. -s, --sink string Addressable sink for events. You can specify a broker, channel, Knative service or URI. Examples: '--sink broker:nest' for a broker 'nest', '--sink channel:pipe' for a channel 'pipe', '--sink ksvc:mysvc:mynamespace' for a Knative service 'mysvc' in another namespace 'mynamespace', '--sink https://event.receiver.uri' for an URI with an 'http://' or 'https://' schema, '--sink ksvc:receiver' or simply '--sink receiver' for a Knative service 'receiver' in the current namespace. If a prefix is not provided, it is considered as a Knative service in the current namespace. If referring to a Knative service in another namespace, 'ksvc:name:namespace' combination must be provided explicitly. --user int The user ID to run the container (e.g., 1001). - --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), or an EmptyDir (prefix ed: or emptyDir:). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. + --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:) or a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim). Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. ``` ### Options inherited from parent commands diff --git a/pkg/kn/flags/podspec.go b/pkg/kn/flags/podspec.go index a07f426c4..584c7d4db 100644 --- a/pkg/kn/flags/podspec.go +++ b/pkg/kn/flags/podspec.go @@ -137,8 +137,8 @@ func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet) []string { flagNames = append(flagNames, "env-file") flagset.StringArrayVarP(&p.Mount, "mount", "", []string{}, - "Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:)"+ - "or an existing Volume (without any prefix) on the specified directory. "+ + "Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:), "+ + "a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim) or an existing Volume (without any prefix) on the specified directory. "+ "Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol "+ "or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is "+ "automatically generated. You can specify a volume subpath by following the volume name with slash separated path. "+ @@ -148,8 +148,8 @@ func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet) []string { flagNames = append(flagNames, "mount") flagset.StringArrayVarP(&p.Volume, "volume", "", []string{}, - "Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), or "+ - "an EmptyDir (prefix ed: or emptyDir:). "+ + "Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), "+ + "an EmptyDir (prefix ed: or emptyDir:) or a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim). "+ "Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. "+ "You can use this flag multiple times. "+ "To unset a ConfigMap/Secret reference, append \"-\" to the name, e.g. --volume myvolume-.") diff --git a/pkg/kn/flags/podspec_helper.go b/pkg/kn/flags/podspec_helper.go index c71e1e221..98e4e84bf 100644 --- a/pkg/kn/flags/podspec_helper.go +++ b/pkg/kn/flags/podspec_helper.go @@ -37,6 +37,7 @@ const ( ConfigMapVolumeSourceType VolumeSourceType = iota SecretVolumeSourceType EmptyDirVolumeSourceType + PVCVolumeSourceType PortFormatErr = "the port specification '%s' is not valid. Please provide in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'." ) @@ -470,6 +471,8 @@ func updateVolume(volume *corev1.Volume, info *volumeSourceInfo) error { volume.Secret = &corev1.SecretVolumeSource{SecretName: info.volumeSourceName} case EmptyDirVolumeSourceType: volume.EmptyDir = &corev1.EmptyDirVolumeSource{Medium: corev1.StorageMedium(info.emptyDirMemoryType), SizeLimit: info.emptyDirSize} + case PVCVolumeSourceType: + volume.PersistentVolumeClaim = &corev1.PersistentVolumeClaimVolumeSource{ClaimName: info.volumeSourceName, ReadOnly: true} default: return fmt.Errorf("Invalid VolumeSourceType") } @@ -607,6 +610,8 @@ func newVolumeSourceInfoWithSpecString(spec string) (*volumeSourceInfo, error) { volumeSourceType = SecretVolumeSourceType case "emptyDir", "ed": volumeSourceType = EmptyDirVolumeSourceType + case "persistentVolumeClaim", "pvc": + volumeSourceType = PVCVolumeSourceType default: return nil, fmt.Errorf("unsupported volume source type \"%q\"; supported volume source types are \"config-map\" and \"secret\"", slices[0]) } @@ -622,7 +627,7 @@ func newVolumeSourceInfoWithSpecString(spec string) (*volumeSourceInfo, error) { } else { typeString := strings.TrimSpace(slices[0]) switch typeString { - case "config-map", "cm", "secret", "sc": + case "config-map", "cm", "secret", "sc", "persistentVolumeClaim", "pvc": return nil, fmt.Errorf("incorrect mount details for type %q", typeString) case "emptyDir", "ed": volName := slices[1] @@ -763,6 +768,15 @@ func reviseVolumeInfoAndMountsToUpdate(mountsToUpdate *util.OrderedMap, volumesT }) mountInfo.VolumeName = generatedName mountsToUpdateRevised.Set(path, mountInfo) + case "persistentVolumeClaim", "pvc": + generatedName := util.GenerateVolumeName(path) + mountInfo := getMountInfo(slices[1]) + volumeSourceInfoByName.Set(generatedName, &volumeSourceInfo{ + volumeSourceType: PVCVolumeSourceType, + volumeSourceName: mountInfo.VolumeName, + }) + mountInfo.VolumeName = generatedName + mountsToUpdateRevised.Set(path, mountInfo) default: return nil, nil, fmt.Errorf("unsupported volume type \"%q\"; supported volume types are \"config-map or cm\", \"secret or sc\", \"volume or vo\", and \"emptyDir or ed\"", slices[0]) } diff --git a/pkg/kn/flags/podspec_helper_test.go b/pkg/kn/flags/podspec_helper_test.go index 68acdd38d..4db760f6e 100644 --- a/pkg/kn/flags/podspec_helper_test.go +++ b/pkg/kn/flags/podspec_helper_test.go @@ -292,6 +292,15 @@ func TestUpdateVolumeMountsAndVolumes(t *testing.T) { SizeLimit: &quantity, }, }, + }, + corev1.Volume{ + Name: "new-pvc-volume-name-1", + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ + ClaimName: "pvc1", + ReadOnly: true, + }, + }, }) container.VolumeMounts = append(container.VolumeMounts, @@ -340,6 +349,16 @@ func TestUpdateVolumeMountsAndVolumes(t *testing.T) { ReadOnly: false, MountPath: "/empty-dir-5/mount/path", }, + corev1.VolumeMount{ + Name: "new-pvc-volume-name-1", + ReadOnly: true, + MountPath: "/pvc-1/mount/path", + }, + corev1.VolumeMount{ + Name: "new-pvc-volume-name-2", + ReadOnly: true, + MountPath: "/pvc-2/mount/path", + }, ) err := UpdateVolumeMountsAndVolumes(spec, @@ -406,9 +425,21 @@ func TestUpdateVolumeMountsAndVolumes(t *testing.T) { util.NewOrderedMap(), []string{}) assert.NilError(t, err) + err = UpdateVolumeMountsAndVolumes(spec, + util.NewOrderedMapWithKVStrings([][]string{{"/pvc-1/mount/path", "new-pvc-volume-name-1"}}), + []string{}, + util.NewOrderedMapWithKVStrings([][]string{{"new-pvc-volume-name-1", "pvc:pvc1"}}), + []string{}) + assert.NilError(t, err) + err = UpdateVolumeMountsAndVolumes(spec, + util.NewOrderedMapWithKVStrings([][]string{{"/pvc-2/mount/path", "pvc:pvc2"}}), + []string{}, + util.NewOrderedMap(), + []string{}) + assert.NilError(t, err) - assert.Equal(t, len(spec.Volumes), 9) - assert.Equal(t, len(container.VolumeMounts), 11) + assert.Equal(t, len(spec.Volumes), 11) + assert.Equal(t, len(container.VolumeMounts), 13) assert.Equal(t, spec.Volumes[0].Name, "existing-config-map-volume-name-2") assert.Equal(t, spec.Volumes[0].ConfigMap.Name, "updated-config-map") @@ -424,13 +455,15 @@ func TestUpdateVolumeMountsAndVolumes(t *testing.T) { assert.Equal(t, spec.Volumes[4].EmptyDir.Medium, corev1.StorageMediumMemory) assert.Equal(t, spec.Volumes[5].Name, "new-empty-dir-volume-name-4") assert.DeepEqual(t, spec.Volumes[5].EmptyDir.SizeLimit, &quantity) - assert.Equal(t, spec.Volumes[6].Name, "new-config-map-volume-name") - assert.Equal(t, spec.Volumes[6].ConfigMap.Name, "new-config-map") - assert.Equal(t, spec.Volumes[7].Name, "new-secret-volume-name") - assert.Equal(t, spec.Volumes[7].Secret.SecretName, "new-secret") - assert.Assert(t, strings.Contains(spec.Volumes[8].Name, "empty-dir-5")) - assert.Equal(t, spec.Volumes[8].EmptyDir.Medium, corev1.StorageMediumDefault) - assert.Assert(t, spec.Volumes[8].EmptyDir.SizeLimit == nil) + assert.Equal(t, spec.Volumes[6].Name, "new-pvc-volume-name-1") + assert.Equal(t, spec.Volumes[6].PersistentVolumeClaim.ClaimName, "pvc1") + assert.Equal(t, spec.Volumes[7].Name, "new-config-map-volume-name") + assert.Equal(t, spec.Volumes[7].ConfigMap.Name, "new-config-map") + assert.Equal(t, spec.Volumes[8].Name, "new-secret-volume-name") + assert.Equal(t, spec.Volumes[8].Secret.SecretName, "new-secret") + assert.Assert(t, strings.Contains(spec.Volumes[9].Name, "empty-dir-5")) + assert.Equal(t, spec.Volumes[9].EmptyDir.Medium, corev1.StorageMediumDefault) + assert.Assert(t, spec.Volumes[9].EmptyDir.SizeLimit == nil) assert.Equal(t, container.VolumeMounts[0].Name, "existing-config-map-volume-name-2") assert.Equal(t, container.VolumeMounts[0].MountPath, "/existing-config-map-2/mount/path") @@ -445,14 +478,17 @@ func TestUpdateVolumeMountsAndVolumes(t *testing.T) { assert.Equal(t, container.VolumeMounts[5].Name, "new-empty-dir-volume-name-4") assert.Equal(t, container.VolumeMounts[5].MountPath, "/empty-dir-4/mount/path") assert.Equal(t, container.VolumeMounts[6].MountPath, "/empty-dir-5/mount/path") - assert.Equal(t, container.VolumeMounts[7].Name, "new-config-map-volume-name") - assert.Equal(t, container.VolumeMounts[7].MountPath, "/new-config-map/mount/path") - assert.Equal(t, container.VolumeMounts[8].Name, "existing-config-map-volume-name-2") - assert.Equal(t, container.VolumeMounts[8].MountPath, "/updated-config-map/mount/path") - assert.Equal(t, container.VolumeMounts[9].Name, "new-secret-volume-name") - assert.Equal(t, container.VolumeMounts[9].MountPath, "/new-secret/mount/path") - assert.Equal(t, container.VolumeMounts[10].Name, "existing-secret-volume-name-2") - assert.Equal(t, container.VolumeMounts[10].MountPath, "/updated-secret/mount/path") + assert.Equal(t, container.VolumeMounts[7].Name, "new-pvc-volume-name-1") + assert.Equal(t, container.VolumeMounts[7].MountPath, "/pvc-1/mount/path") + assert.Equal(t, container.VolumeMounts[8].MountPath, "/pvc-2/mount/path") + assert.Equal(t, container.VolumeMounts[9].Name, "new-config-map-volume-name") + assert.Equal(t, container.VolumeMounts[9].MountPath, "/new-config-map/mount/path") + assert.Equal(t, container.VolumeMounts[10].Name, "existing-config-map-volume-name-2") + assert.Equal(t, container.VolumeMounts[10].MountPath, "/updated-config-map/mount/path") + assert.Equal(t, container.VolumeMounts[11].Name, "new-secret-volume-name") + assert.Equal(t, container.VolumeMounts[11].MountPath, "/new-secret/mount/path") + assert.Equal(t, container.VolumeMounts[12].Name, "existing-secret-volume-name-2") + assert.Equal(t, container.VolumeMounts[12].MountPath, "/updated-secret/mount/path") } func TestUpdateContainerImage(t *testing.T) { diff --git a/test/e2e/service_test.go b/test/e2e/service_test.go index 59d14ce3b..f84a77a75 100644 --- a/test/e2e/service_test.go +++ b/test/e2e/service_test.go @@ -20,6 +20,8 @@ package e2e import ( "encoding/json" "fmt" + "io/ioutil" + "os" "strings" "testing" @@ -33,6 +35,20 @@ import ( servingv1 "knative.dev/serving/pkg/apis/serving/v1" ) +const ( + TestPVCSpec = `kind: PersistentVolumeClaim +apiVersion: v1 +metadata: + name: test-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi +` +) + func TestService(t *testing.T) { t.Parallel() it, err := test.NewKnTest() @@ -178,10 +194,16 @@ func serviceDeleteAll(r *test.KnRunResultCollector) { func serviceCreateWithMount(r *test.KnRunResultCollector) { it := r.KnTest() - kubectl := test.NewKubectl(it.Namespace()) + kubectl := test.NewKubectl("knative-serving") + + _, err := kubectl.Run("patch", "cm", "config-features", "--patch={\"data\":{\"kubernetes.podspec-persistent-volume-claim\": \"enabled\", \"kubernetes.podspec-volumes-emptydir\": \"enabled\"}}") + assert.NilError(r.T(), err) + defer kubectl.Run("patch", "cm", "config-features", "--patch={\"data\":{\"kubernetes.podspec-persistent-volume-claim\": \"disabled\", \"kubernetes.podspec-volumes-emptydir\": \"disabled\"}}") + + kubectl = test.NewKubectl(it.Namespace()) r.T().Log("create cm test-cm") - _, err := kubectl.Run("create", "configmap", "test-cm", "--from-literal=key=value") + _, err = kubectl.Run("create", "configmap", "test-cm", "--from-literal=key=value") assert.NilError(r.T(), err) r.T().Log("create service with configmap mounted") @@ -200,6 +222,28 @@ func serviceCreateWithMount(r *test.KnRunResultCollector) { out = r.KnTest().Kn().Run("service", "update", "test-svc", "--mount", "/mydir2=sc:test-sec/key1") r.AssertNoError(out) + r.T().Log("update service with a new emptyDir mount") + out = r.KnTest().Kn().Run("service", "update", "test-svc", "--mount", "/mydir3=ed:myvol") + r.AssertNoError(out) + + r.T().Log("update service with a new emptyDir mount with Memory and dir size") + out = r.KnTest().Kn().Run("service", "update", "test-svc", "--mount", "/mydir4=ed:myvol:type=Memory,size=100Mi") + r.AssertNoError(out) + + r.T().Log("create PVC test-pvc") + fp, err := ioutil.TempFile("", "my-pvc") + assert.NilError(r.T(), err) + fmt.Fprintf(fp, "%s", TestPVCSpec) + defer os.Remove(fp.Name()) + + _, err = kubectl.Run("create", "-f", fp.Name()) + assert.NilError(r.T(), err) + r.AssertNoError(out) + + r.T().Log("update service with a new pvc mount") + out = r.KnTest().Kn().Run("service", "update", "test-svc", "--mount", "/mydir5=pvc:test-pvc") + r.AssertNoError(out) + serviceDescribeMount(r, "test-svc", "/mydir", "key") }