diff --git a/cmd/convert.go b/cmd/convert.go index a4f11608..a1257a3d 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -33,6 +33,7 @@ var ( ConvertBuildBranch string ConvertBuild string ConvertVolumes string + ConvertPVCRequestSize string ConvertChart bool ConvertDeployment bool ConvertDaemonSet bool @@ -90,6 +91,7 @@ var convertCmd = &cobra.Command{ CreateDeploymentConfig: ConvertDeploymentConfig, EmptyVols: ConvertEmptyVols, Volumes: ConvertVolumes, + PVCRequestSize: ConvertPVCRequestSize, InsecureRepository: ConvertInsecureRepo, IsDeploymentFlag: cmd.Flags().Lookup("deployment").Changed, IsDaemonSetFlag: cmd.Flags().Lookup("daemon-set").Changed, @@ -153,6 +155,7 @@ func init() { convertCmd.Flags().StringVarP(&ConvertOut, "out", "o", "", "Specify a file name or directory to save objects to (if path does not exist, a file will be created)") convertCmd.Flags().IntVar(&ConvertReplicas, "replicas", 1, "Specify the number of replicas in the generated resource spec") convertCmd.Flags().StringVar(&ConvertVolumes, "volumes", "persistentVolumeClaim", `Volumes to be generated ("persistentVolumeClaim"|"emptyDir"|"hostPath" | "configMap")`) + convertCmd.Flags().StringVar(&ConvertPVCRequestSize, "pvc-request-size", "", `Specify the size of pvc storage requests in the generated resource spec`) convertCmd.Flags().BoolVar(&WithKomposeAnnotation, "with-kompose-annotation", true, "Add kompose annotations to generated resource") diff --git a/docs/user-guide.md b/docs/user-guide.md index 62818ea1..da203e37 100755 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -257,7 +257,8 @@ services: kompose.image-pull-secret: "example-kubernetes-secret" ``` -- `kompose.volume.size` defines the requests storage's size in the PersistentVolumeClaim +- `kompose.volume.size` defines the requests storage's size in the PersistentVolumeClaim, or you can use command line parameter `--pvc-request-size`. +The priority follow label (kompose.volume.size) > command parameter(--pvc-request-size) > defaultSize (100Mi) For example: diff --git a/pkg/kobject/kobject.go b/pkg/kobject/kobject.go index 50b19392..2878db2b 100644 --- a/pkg/kobject/kobject.go +++ b/pkg/kobject/kobject.go @@ -58,6 +58,7 @@ type ConvertOptions struct { StoreManifest bool EmptyVols bool Volumes string + PVCRequestSize string InsecureRepository bool Replicas int InputFiles []string diff --git a/pkg/transformer/kubernetes/kubernetes.go b/pkg/transformer/kubernetes/kubernetes.go index 465082e4..dbde49be 100644 --- a/pkg/transformer/kubernetes/kubernetes.go +++ b/pkg/transformer/kubernetes/kubernetes.go @@ -845,7 +845,9 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) ( volsource = k.ConfigPVCVolumeSource(volumeName, readonly) if volume.VFrom == "" { defaultSize := PVCRequestSize - + if k.Opt.PVCRequestSize != "" { + defaultSize = k.Opt.PVCRequestSize + } if len(volume.PVCSize) > 0 { defaultSize = volume.PVCSize } else { diff --git a/script/test/cmd/tests_new.sh b/script/test/cmd/tests_new.sh index a731901c..a96a032f 100755 --- a/script/test/cmd/tests_new.sh +++ b/script/test/cmd/tests_new.sh @@ -78,3 +78,7 @@ dst=$TEMP_DIR/output_dir/ convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $dst -j" "${dst}redis-deployment.json" "${dst}redis-service.json" "${dst}web-deployment.json" "${dst}web-service.json" # Behavior with -o / convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $TEMP_DIR/output_dir2/output_file -j" "$TEMP_DIR/output_dir2/output_file" + +#TEST the pvc-request-size command parameter +convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/pvc-request-size/docker-compose.yml convert -o $TEMP_DIR/output_dir2/output-k8s.json -j --pvc-request-size=300Mi" "$TEMP_DIR/output_dir2/output-k8s.json" +convert::check_artifacts_generated "kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/pvc-request-size/docker-compose.yml convert -o $TEMP_DIR/output_dir2/output-os.json -j --pvc-request-size=300Mi" "$TEMP_DIR/output_dir2/output-os.json" diff --git a/script/test/fixtures/pvc-request-size/docker-compose.yml b/script/test/fixtures/pvc-request-size/docker-compose.yml new file mode 100644 index 00000000..2b35488f --- /dev/null +++ b/script/test/fixtures/pvc-request-size/docker-compose.yml @@ -0,0 +1,27 @@ +version: '2' + +services: + redis: + restart: always + image: sameersbn/redis:latest + command: + - --loglevel warning + volumes: + - /srv/docker/gitlab/redis:/var/lib/redis + ports: + - "6379" + + postgresql: + restart: always + image: sameersbn/postgresql:9.5-3 + volumes: + - /srv/docker/gitlab/postgresql:/var/lib/postgresql + environment: + - DB_USER=gitlab + - DB_PASS=password + - DB_NAME=gitlabhq_production + - DB_EXTENSION=pg_trgm + ports: + - "5432" + labels: + kompose.volume.size: 200Mi diff --git a/script/test/fixtures/pvc-request-size/output-k8s.json b/script/test/fixtures/pvc-request-size/output-k8s.json new file mode 100644 index 00000000..33e0747e --- /dev/null +++ b/script/test/fixtures/pvc-request-size/output-k8s.json @@ -0,0 +1,271 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "postgresql", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "postgresql" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%", + "kompose.volume.size": "200Mi" + } + }, + "spec": { + "ports": [ + { + "name": "5432", + "port": 5432, + "targetPort": 5432 + } + ], + "selector": { + "io.kompose.service": "postgresql" + } + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "redis", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "redis" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "ports": [ + { + "name": "6379", + "port": 6379, + "targetPort": 6379 + } + ], + "selector": { + "io.kompose.service": "redis" + } + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "Deployment", + "apiVersion": "apps/v1", + "metadata": { + "name": "postgresql", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "postgresql" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%", + "kompose.volume.size": "200Mi" + } + }, + "spec": { + "replicas": 1, + "selector": { + "matchLabels": { + "io.kompose.service": "postgresql" + } + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "postgresql" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%", + "kompose.volume.size": "200Mi" + } + }, + "spec": { + "volumes": [ + { + "name": "postgresql-claim0", + "persistentVolumeClaim": { + "claimName": "postgresql-claim0" + } + } + ], + "containers": [ + { + "name": "postgresql", + "image": "sameersbn/postgresql:9.5-3", + "ports": [ + { + "containerPort": 5432 + } + ], + "env": [ + { + "name": "DB_EXTENSION", + "value": "pg_trgm" + }, + { + "name": "DB_NAME", + "value": "gitlabhq_production" + }, + { + "name": "DB_PASS", + "value": "password" + }, + { + "name": "DB_USER", + "value": "gitlab" + } + ], + "resources": {}, + "volumeMounts": [ + { + "name": "postgresql-claim0", + "mountPath": "/var/lib/postgresql" + } + ] + } + ], + "restartPolicy": "Always" + } + }, + "strategy": { + "type": "Recreate" + } + }, + "status": {} + }, + { + "kind": "PersistentVolumeClaim", + "apiVersion": "v1", + "metadata": { + "name": "postgresql-claim0", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "postgresql-claim0" + } + }, + "spec": { + "accessModes": [ + "ReadWriteOnce" + ], + "resources": { + "requests": { + "storage": "200Mi" + } + } + }, + "status": {} + }, + { + "kind": "Deployment", + "apiVersion": "apps/v1", + "metadata": { + "name": "redis", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "redis" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "replicas": 1, + "selector": { + "matchLabels": { + "io.kompose.service": "redis" + } + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "redis" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "volumes": [ + { + "name": "redis-claim0", + "persistentVolumeClaim": { + "claimName": "redis-claim0" + } + } + ], + "containers": [ + { + "name": "redis", + "image": "sameersbn/redis:latest", + "args": [ + "--loglevel warning" + ], + "ports": [ + { + "containerPort": 6379 + } + ], + "resources": {}, + "volumeMounts": [ + { + "name": "redis-claim0", + "mountPath": "/var/lib/redis" + } + ] + } + ], + "restartPolicy": "Always" + } + }, + "strategy": { + "type": "Recreate" + } + }, + "status": {} + }, + { + "kind": "PersistentVolumeClaim", + "apiVersion": "v1", + "metadata": { + "name": "redis-claim0", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "redis-claim0" + } + }, + "spec": { + "accessModes": [ + "ReadWriteOnce" + ], + "resources": { + "requests": { + "storage": "300Mi" + } + } + }, + "status": {} + } + ] +} diff --git a/script/test/fixtures/pvc-request-size/output-os.json b/script/test/fixtures/pvc-request-size/output-os.json new file mode 100644 index 00000000..6defe69d --- /dev/null +++ b/script/test/fixtures/pvc-request-size/output-os.json @@ -0,0 +1,368 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "postgresql", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "postgresql" + }, + "annotations": { + "kompose.volume.size": "200Mi" + } + }, + "spec": { + "ports": [ + { + "name": "5432", + "port": 5432, + "targetPort": 5432 + } + ], + "selector": { + "io.kompose.service": "postgresql" + } + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "redis", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "redis" + } + }, + "spec": { + "ports": [ + { + "name": "6379", + "port": 6379, + "targetPort": 6379 + } + ], + "selector": { + "io.kompose.service": "redis" + } + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "DeploymentConfig", + "apiVersion": "v1", + "metadata": { + "name": "postgresql", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "postgresql" + }, + "annotations": { + "kompose.volume.size": "200Mi" + } + }, + "spec": { + "strategy": { + "type": "Recreate", + "resources": {} + }, + "triggers": [ + { + "type": "ConfigChange" + }, + { + "type": "ImageChange", + "imageChangeParams": { + "automatic": true, + "containerNames": [ + "postgresql" + ], + "from": { + "kind": "ImageStreamTag", + "name": "postgresql:9.5-3" + } + } + } + ], + "replicas": 1, + "test": false, + "selector": { + "io.kompose.service": "postgresql" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "postgresql" + } + }, + "spec": { + "volumes": [ + { + "name": "postgresql-claim0", + "persistentVolumeClaim": { + "claimName": "postgresql-claim0" + } + } + ], + "containers": [ + { + "name": "postgresql", + "image": " ", + "ports": [ + { + "containerPort": 5432 + } + ], + "env": [ + { + "name": "DB_EXTENSION", + "value": "pg_trgm" + }, + { + "name": "DB_NAME", + "value": "gitlabhq_production" + }, + { + "name": "DB_PASS", + "value": "password" + }, + { + "name": "DB_USER", + "value": "gitlab" + } + ], + "resources": {}, + "volumeMounts": [ + { + "name": "postgresql-claim0", + "mountPath": "/var/lib/postgresql" + } + ] + } + ], + "restartPolicy": "Always" + } + } + }, + "status": { + "latestVersion": 0, + "observedGeneration": 0, + "replicas": 0, + "updatedReplicas": 0, + "availableReplicas": 0, + "unavailableReplicas": 0 + } + }, + { + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "postgresql", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "postgresql" + } + }, + "spec": { + "lookupPolicy": { + "local": false + }, + "tags": [ + { + "name": "", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "sameersbn/postgresql:9.5-3" + }, + "generation": null, + "importPolicy": {}, + "referencePolicy": { + "type": "" + } + } + ] + }, + "status": { + "dockerImageRepository": "" + } + }, + { + "kind": "PersistentVolumeClaim", + "apiVersion": "v1", + "metadata": { + "name": "postgresql-claim0", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "postgresql-claim0" + } + }, + "spec": { + "accessModes": [ + "ReadWriteOnce" + ], + "resources": { + "requests": { + "storage": "200Mi" + } + } + }, + "status": {} + }, + { + "kind": "DeploymentConfig", + "apiVersion": "v1", + "metadata": { + "name": "redis", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "redis" + } + }, + "spec": { + "strategy": { + "type": "Recreate", + "resources": {} + }, + "triggers": [ + { + "type": "ConfigChange" + }, + { + "type": "ImageChange", + "imageChangeParams": { + "automatic": true, + "containerNames": [ + "redis" + ], + "from": { + "kind": "ImageStreamTag", + "name": "redis:latest" + } + } + } + ], + "replicas": 1, + "test": false, + "selector": { + "io.kompose.service": "redis" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "redis" + } + }, + "spec": { + "volumes": [ + { + "name": "redis-claim0", + "persistentVolumeClaim": { + "claimName": "redis-claim0" + } + } + ], + "containers": [ + { + "name": "redis", + "image": " ", + "args": [ + "--loglevel warning" + ], + "ports": [ + { + "containerPort": 6379 + } + ], + "resources": {}, + "volumeMounts": [ + { + "name": "redis-claim0", + "mountPath": "/var/lib/redis" + } + ] + } + ], + "restartPolicy": "Always" + } + } + }, + "status": { + "latestVersion": 0, + "observedGeneration": 0, + "replicas": 0, + "updatedReplicas": 0, + "availableReplicas": 0, + "unavailableReplicas": 0 + } + }, + { + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "redis", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "redis" + } + }, + "spec": { + "lookupPolicy": { + "local": false + }, + "tags": [ + { + "name": "", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "sameersbn/redis:latest" + }, + "generation": null, + "importPolicy": {}, + "referencePolicy": { + "type": "" + } + } + ] + }, + "status": { + "dockerImageRepository": "" + } + }, + { + "kind": "PersistentVolumeClaim", + "apiVersion": "v1", + "metadata": { + "name": "redis-claim0", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "redis-claim0" + } + }, + "spec": { + "accessModes": [ + "ReadWriteOnce" + ], + "resources": { + "requests": { + "storage": "300Mi" + } + } + }, + "status": {} + } + ] +}