Skip to content

Commit

Permalink
Merge pull request #957 from hangyan/support-hostpath-volume
Browse files Browse the repository at this point in the history
Support hostpath volume
  • Loading branch information
cdrage authored Mar 19, 2018
2 parents 90f6d89 + cf833c8 commit 442caa4
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func init() {
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().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
convertCmd.Flags().BoolVar(&ConvertEmptyVols, "emptyvols", false, "Use Empty Volumes. Do not generate PVCs")
Expand Down
2 changes: 1 addition & 1 deletion cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var upCmd = &cobra.Command{

func init() {
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().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")`)
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

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")
}
}
Expand Down
30 changes: 28 additions & 2 deletions pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/pkg/errors"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/labels"
"path/filepath"
)

// Kubernetes implements Transformer interface and represents Kubernetes transformer
Expand Down Expand Up @@ -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
// as opposed to persistent volumes and volume claims
useEmptyVolumes := k.Opt.EmptyVols
useHostPath := false

if k.Opt.Volumes == "emptyDir" {
useEmptyVolumes = true
}

if k.Opt.Volumes == "hostPath" {
useHostPath = true
}

var count int
//interating over array of `Vols` struct as it contains all necessary information about volumes
for _, volume := range service.Volumes {
Expand All @@ -446,6 +452,8 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
if volume.VolumeName == "" {
if useEmptyVolumes {
volumeName = strings.Replace(volume.PVCName, "claim", "empty", 1)
} else if useHostPath {
volumeName = strings.Replace(volume.PVCName, "claim", "hostpath", 1)
} else {
volumeName = volume.PVCName
}
Expand All @@ -465,8 +473,13 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (

if useEmptyVolumes {
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 {

volsource = k.ConfigPVCVolumeSource(volumeName, readonly)
if volume.VFrom == "" {
defaultSize := PVCRequestSize
Expand All @@ -485,6 +498,7 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (

PVCs = append(PVCs, createdPVC)
}

}

// create a new volume object using the volsource and add to list
Expand All @@ -494,7 +508,7 @@ func (k *Kubernetes) ConfigVolumes(name string, service kobject.ServiceConfig) (
}
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)
}

Expand All @@ -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
func (k *Kubernetes) ConfigPVCVolumeSource(name string, readonly bool) *api.VolumeSource {
return &api.VolumeSource{
Expand Down
14 changes: 14 additions & 0 deletions script/test/cmd/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"


######
# 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
# kubernetes test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '2'
services:
db:
image: postgres:10.1
ports:
- "5432"
volumes:
- ./data:/var/lib/postgresql/data
Original file line number Diff line number Diff line change
@@ -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": {}
}
]
}
147 changes: 147 additions & 0 deletions script/test/fixtures/volume-mounts/hostpath/output-os-template.json
Original file line number Diff line number Diff line change
@@ -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": ""
}
}
]
}

0 comments on commit 442caa4

Please sign in to comment.