Merge pull request #957 from hangyan/support-hostpath-volume

Support hostpath volume
This commit is contained in:
Charlie Drage 2018-03-19 11:47:00 -04:00 committed by GitHub
commit 442caa4359
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 296 additions and 5 deletions

View File

@ -135,7 +135,7 @@ func init() {
convertCmd.Flags().BoolVar(&ConvertStdout, "stdout", false, "Print converted objects to stdout") convertCmd.Flags().BoolVar(&ConvertStdout, "stdout", false, "Print converted objects to stdout")
convertCmd.Flags().StringVarP(&ConvertOut, "out", "o", "", "Specify a file name to save objects to") convertCmd.Flags().StringVarP(&ConvertOut, "out", "o", "", "Specify a file name to save objects to")
convertCmd.Flags().IntVar(&ConvertReplicas, "replicas", 1, "Specify the number of replicas in the generated resource spec") 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")`) convertCmd.Flags().StringVar(&ConvertVolumes, "volumes", "persistentVolumeClaim", `Volumes to be generated ("persistentVolumeClaim"|"emptyDir"|"hostPath")`)
// Deprecated commands // Deprecated commands
convertCmd.Flags().BoolVar(&ConvertEmptyVols, "emptyvols", false, "Use Empty Volumes. Do not generate PVCs") convertCmd.Flags().BoolVar(&ConvertEmptyVols, "emptyvols", false, "Use Empty Volumes. Do not generate PVCs")

View File

@ -76,7 +76,7 @@ var upCmd = &cobra.Command{
func init() { func init() {
upCmd.Flags().IntVar(&UpReplicas, "replicas", 1, "Specify the number of replicas generated") upCmd.Flags().IntVar(&UpReplicas, "replicas", 1, "Specify the number of replicas generated")
upCmd.Flags().StringVar(&UpVolumes, "volumes", "persistentVolumeClaim", `Volumes to be generated ("persistentVolumeClaim"|"emptyDir")`) upCmd.Flags().StringVar(&UpVolumes, "volumes", "persistentVolumeClaim", `Volumes to be generated ("persistentVolumeClaim"|"emptyDir"|"hostPath")`)
upCmd.Flags().BoolVar(&UpInsecureRepo, "insecure-repository", false, "Use an insecure Docker repository for OpenShift ImageStream") upCmd.Flags().BoolVar(&UpInsecureRepo, "insecure-repository", false, "Use an insecure Docker repository for OpenShift ImageStream")
upCmd.Flags().StringVar(&UpNamespace, "namespace", "default", "Specify Namespace to deploy your application") upCmd.Flags().StringVar(&UpNamespace, "namespace", "default", "Specify Namespace to deploy your application")
upCmd.Flags().StringVar(&UpBuild, "build", "local", `Set the type of build ("local"|"build-config" (OpenShift only)|"none")`) upCmd.Flags().StringVar(&UpBuild, "build", "local", `Set the type of build ("local"|"build-config" (OpenShift only)|"none")`)

View File

@ -149,7 +149,7 @@ func ValidateFlags(bundle string, args []string, cmd *cobra.Command, opt *kobjec
log.Fatalf("YAML and JSON format cannot be provided at the same time") log.Fatalf("YAML and JSON format cannot be provided at the same time")
} }
if opt.Volumes != "persistentVolumeClaim" && opt.Volumes != "emptyDir" { if opt.Volumes != "persistentVolumeClaim" && opt.Volumes != "emptyDir" && opt.Volumes != "hostPath" {
log.Fatal("Unknown Volume type: ", opt.Volumes, ", possible values are: persistentVolumeClaim and emptyDir") log.Fatal("Unknown Volume type: ", opt.Volumes, ", possible values are: persistentVolumeClaim and emptyDir")
} }
} }

View File

@ -51,6 +51,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/labels" "k8s.io/kubernetes/pkg/labels"
"path/filepath"
) )
// Kubernetes implements Transformer interface and represents Kubernetes transformer // Kubernetes implements Transformer interface and represents Kubernetes transformer
@ -431,11 +432,16 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
// Set a var based on if the user wants to use empty volumes // Set a var based on if the user wants to use empty volumes
// as opposed to persistent volumes and volume claims // as opposed to persistent volumes and volume claims
useEmptyVolumes := k.Opt.EmptyVols useEmptyVolumes := k.Opt.EmptyVols
useHostPath := false
if k.Opt.Volumes == "emptyDir" { if k.Opt.Volumes == "emptyDir" {
useEmptyVolumes = true useEmptyVolumes = true
} }
if k.Opt.Volumes == "hostPath" {
useHostPath = true
}
var count int var count int
//interating over array of `Vols` struct as it contains all necessary information about volumes //interating over array of `Vols` struct as it contains all necessary information about volumes
for _, volume := range service.Volumes { for _, volume := range service.Volumes {
@ -446,6 +452,8 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
if volume.VolumeName == "" { if volume.VolumeName == "" {
if useEmptyVolumes { if useEmptyVolumes {
volumeName = strings.Replace(volume.PVCName, "claim", "empty", 1) volumeName = strings.Replace(volume.PVCName, "claim", "empty", 1)
} else if useHostPath {
volumeName = strings.Replace(volume.PVCName, "claim", "hostpath", 1)
} else { } else {
volumeName = volume.PVCName volumeName = volume.PVCName
} }
@ -465,8 +473,13 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
if useEmptyVolumes { if useEmptyVolumes {
volsource = k.ConfigEmptyVolumeSource("volume") volsource = k.ConfigEmptyVolumeSource("volume")
} else if useHostPath {
source, err := k.ConfigHostPathVolumeSource(volume.Host)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "k.ConfigHostPathVolumeSource failed")
}
volsource = source
} else { } else {
volsource = k.ConfigPVCVolumeSource(volumeName, readonly) volsource = k.ConfigPVCVolumeSource(volumeName, readonly)
if volume.VFrom == "" { if volume.VFrom == "" {
defaultSize := PVCRequestSize defaultSize := PVCRequestSize
@ -485,6 +498,7 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
PVCs = append(PVCs, createdPVC) PVCs = append(PVCs, createdPVC)
} }
} }
// create a new volume object using the volsource and add to list // create a new volume object using the volsource and add to list
@ -494,7 +508,7 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
} }
volumes = append(volumes, vol) volumes = append(volumes, vol)
if len(volume.Host) > 0 { if len(volume.Host) > 0 && !useHostPath {
log.Warningf("Volume mount on the host %q isn't supported - ignoring path on the host", volume.Host) log.Warningf("Volume mount on the host %q isn't supported - ignoring path on the host", volume.Host)
} }
@ -521,6 +535,18 @@ func (k *Kubernetes) ConfigEmptyVolumeSource(key string) *api.VolumeSource {
} }
// ConfigHostPathVolumeSource is a helper function to create a HostPath api.VolumeSource
func (k *Kubernetes) ConfigHostPathVolumeSource(path string) (*api.VolumeSource, error) {
dir, err := transformer.GetComposeFileDir(k.Opt.InputFiles)
if err != nil {
return nil, err
}
absPath := filepath.Join(dir, path)
return &api.VolumeSource{
HostPath: &api.HostPathVolumeSource{Path: absPath},
}, nil
}
// ConfigPVCVolumeSource is helper function to create an api.VolumeSource with a PVC // ConfigPVCVolumeSource is helper function to create an api.VolumeSource with a PVC
func (k *Kubernetes) ConfigPVCVolumeSource(name string, readonly bool) *api.VolumeSource { func (k *Kubernetes) ConfigPVCVolumeSource(name string, readonly bool) *api.VolumeSource {
return &api.VolumeSource{ return &api.VolumeSource{

View File

@ -175,6 +175,20 @@ sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/f
convert::expect_success "kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/volume-mounts/named-volume/docker-compose.yml convert --stdout -j" "/tmp/output-os.json" convert::expect_success "kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/volume-mounts/named-volume/docker-compose.yml convert --stdout -j" "/tmp/output-os.json"
######
# Tests related to docker-compose file in /script/test/fixtures/volume-mounts/hostpath
# kubernetes test
cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/volume-mounts/hostpath/docker-compose.yml convert --stdout -j --volumes hostPath"
hostpath=$KOMPOSE_ROOT/script/test/fixtures/volume-mounts/hostpath/data
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" -e "s;%HOSTPATH%;$hostpath;g" $KOMPOSE_ROOT/script/test/fixtures/volume-mounts/hostpath/output-k8s-template.json > /tmp/output-k8s.json
convert::expect_success "$cmd" "/tmp/output-k8s.json"
# openshift test
cmd="kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/volume-mounts/hostpath/docker-compose.yml convert --stdout -j --volumes hostPath"
hostpath=$KOMPOSE_ROOT/script/test/fixtures/volume-mounts/hostpath/data
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" -e "s;%HOSTPATH%;$hostpath;g" $KOMPOSE_ROOT/script/test/fixtures/volume-mounts/hostpath/output-os-template.json > /tmp/output-os.json
convert::expect_success "$cmd" "/tmp/output-os.json"
###### ######
# Tests related to docker-compose file in /script/test/fixtures/volume-mounts/volumes-from # Tests related to docker-compose file in /script/test/fixtures/volume-mounts/volumes-from
# kubernetes test # kubernetes test

View File

@ -0,0 +1,8 @@
version: '2'
services:
db:
image: postgres:10.1
ports:
- "5432"
volumes:
- ./data:/var/lib/postgresql/data

View File

@ -0,0 +1,96 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "db",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"ports": [
{
"name": "5432",
"port": 5432,
"targetPort": 5432
}
],
"selector": {
"io.kompose.service": "db"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "db",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
}
},
"spec": {
"volumes": [
{
"name": "db-hostpath0",
"hostPath": {
"path": "%HOSTPATH%"
}
}
],
"containers": [
{
"name": "db",
"image": "postgres:10.1",
"ports": [
{
"containerPort": 5432
}
],
"resources": {},
"volumeMounts": [
{
"name": "db-hostpath0",
"mountPath": "/var/lib/postgresql/data"
}
]
}
],
"restartPolicy": "Always"
}
},
"strategy": {
"type": "Recreate"
}
},
"status": {}
}
]
}

View File

@ -0,0 +1,147 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "db",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"ports": [
{
"name": "5432",
"port": 5432,
"targetPort": 5432
}
],
"selector": {
"io.kompose.service": "db"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "db",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"strategy": {
"type": "Recreate",
"resources": {}
},
"triggers": [
{
"type": "ConfigChange"
},
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"db"
],
"from": {
"kind": "ImageStreamTag",
"name": "db:10.1"
}
}
}
],
"replicas": 1,
"test": false,
"selector": {
"io.kompose.service": "db"
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
}
},
"spec": {
"volumes": [
{
"name": "db-hostpath0",
"hostPath": {
"path": "%HOSTPATH%"
}
}
],
"containers": [
{
"name": "db",
"image": " ",
"ports": [
{
"containerPort": 5432
}
],
"resources": {},
"volumeMounts": [
{
"name": "db-hostpath0",
"mountPath": "/var/lib/postgresql/data"
}
]
}
],
"restartPolicy": "Always"
}
}
},
"status": {}
},
{
"kind": "ImageStream",
"apiVersion": "v1",
"metadata": {
"name": "db",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "db"
}
},
"spec": {
"tags": [
{
"name": "10.1",
"annotations": null,
"from": {
"kind": "DockerImage",
"name": "postgres:10.1"
},
"generation": null,
"importPolicy": {}
}
]
},
"status": {
"dockerImageRepository": ""
}
}
]
}