diff --git a/engine/kube/debug.go b/engine/kube/debug.go index c951992..cc0b2bb 100644 --- a/engine/kube/debug.go +++ b/engine/kube/debug.go @@ -59,7 +59,7 @@ func Print(spec *engine.Spec) string { for _, step := range spec.Steps { buf.WriteString(documentBegin) - res := toPod(spec, step) + res := toPod(spec, step, false) res.Namespace = spec.Metadata.Namespace res.Kind = "Pod" raw, _ := yaml.Marshal(res) diff --git a/engine/kube/kube.go b/engine/kube/kube.go index 8023724..586a43f 100644 --- a/engine/kube/kube.go +++ b/engine/kube/kube.go @@ -21,13 +21,14 @@ import ( ) type kubeEngine struct { - client *kubernetes.Clientset - node string + client *kubernetes.Clientset + node string + storageClass string } // NewFile returns a new Kubernetes engine from a // Kubernetes configuration file (~/.kube/config). -func NewFile(url, path, node string) (engine.Engine, error) { +func NewFile(url, path, node string, storageClass string) (engine.Engine, error) { config, err := clientcmd.BuildConfigFromFlags(url, path) if err != nil { return nil, err @@ -36,7 +37,7 @@ func NewFile(url, path, node string) (engine.Engine, error) { if err != nil { return nil, err } - return &kubeEngine{client: client, node: node}, nil + return &kubeEngine{client: client, node: node, storageClass: storageClass}, nil } func (e *kubeEngine) Setup(ctx context.Context, spec *engine.Spec) error { @@ -105,11 +106,13 @@ func (e *kubeEngine) Setup(ctx context.Context, spec *engine.Spec) error { // return err // } - // pvc := toPersistentVolumeClaim(spec.Metadata.Namespace, spec.Metadata.Namespace) - // _, err = e.client.CoreV1().PersistentVolumeClaims(spec.Metadata.Namespace).Create(pvc) - // if err != nil { - // return err - // } + if e.storageClass != "" { + pvc := toPersistentVolumeClaim(spec.Metadata.Namespace, spec.Metadata.Namespace, e.storageClass) + _, err = e.client.CoreV1().PersistentVolumeClaims(spec.Metadata.Namespace).Create(pvc) + if err != nil { + return err + } + } return nil } @@ -120,7 +123,11 @@ func (e *kubeEngine) Create(_ context.Context, _ *engine.Spec, _ *engine.Step) e } func (e *kubeEngine) Start(ctx context.Context, spec *engine.Spec, step *engine.Step) error { - pod := toPod(spec, step) + usePVC := false + if e.storageClass != "" { + usePVC = true + } + pod := toPod(spec, step, usePVC) if len(step.Docker.Ports) != 0 { service := toService(spec, step) _, err := e.client.CoreV1().Services(spec.Metadata.Namespace).Create(service) diff --git a/engine/kube/util.go b/engine/kube/util.go index 3c65602..9d02fe1 100644 --- a/engine/kube/util.go +++ b/engine/kube/util.go @@ -129,7 +129,7 @@ func toConfigMounts(spec *engine.Spec, step *engine.Step) []v1.VolumeMount { return to } -func toVolumes(spec *engine.Spec, step *engine.Step) []v1.Volume { +func toVolumes(spec *engine.Spec, step *engine.Step, usePVC bool) []v1.Volume { var to []v1.Volume for _, mount := range step.Volumes { vol, ok := engine.LookupVolume(spec, mount.Name) @@ -139,9 +139,15 @@ func toVolumes(spec *engine.Spec, step *engine.Step) []v1.Volume { volume := v1.Volume{Name: vol.Metadata.UID} source := v1.HostPathDirectoryOrCreate if vol.HostPath != nil { - volume.HostPath = &v1.HostPathVolumeSource{ - Path: vol.HostPath.Path, - Type: &source, + if usePVC { + volume.PersistentVolumeClaim = &v1.PersistentVolumeClaimVolumeSource{ + ClaimName: spec.Metadata.Namespace, + } + } else { + volume.HostPath = &v1.HostPathVolumeSource{ + Path: vol.HostPath.Path, + Type: &source, + } } } if vol.EmptyDir != nil { @@ -152,9 +158,15 @@ func toVolumes(spec *engine.Spec, step *engine.Step) []v1.Volume { // directory on the host machine that can be shared // between pods. This means we are responsible for deleting // these directories. - volume.HostPath = &v1.HostPathVolumeSource{ - Path: filepath.Join("/tmp", "drone", spec.Metadata.Namespace, vol.Metadata.Namespace), - Type: &source, + if usePVC { + volume.PersistentVolumeClaim = &v1.PersistentVolumeClaimVolumeSource{ + ClaimName: spec.Metadata.Namespace, + } + } else { + volume.HostPath = &v1.HostPathVolumeSource{ + Path: filepath.Join("/tmp", "drone", spec.Metadata.Namespace, vol.Metadata.Namespace), + Type: &source, + } } } to = append(to, volume) @@ -226,9 +238,9 @@ func toResources(step *engine.Step) v1.ResourceRequirements { // helper function returns a kubernetes pod for the // given step and specification. -func toPod(spec *engine.Spec, step *engine.Step) *v1.Pod { +func toPod(spec *engine.Spec, step *engine.Step, usePVC bool) *v1.Pod { var volumes []v1.Volume - volumes = append(volumes, toVolumes(spec, step)...) + volumes = append(volumes, toVolumes(spec, step, usePVC)...) volumes = append(volumes, toConfigVolumes(spec, step)...) var mounts []v1.VolumeMount diff --git a/engine/kube/volume.go b/engine/kube/volume.go index febe20f..dd112d9 100644 --- a/engine/kube/volume.go +++ b/engine/kube/volume.go @@ -41,8 +41,7 @@ func toPersistentVolume(node, namespace, name, path string) *v1.PersistentVolume } } -func toPersistentVolumeClaim(namespace, name string) *v1.PersistentVolumeClaim { - localStorageClass := "local-storage" +func toPersistentVolumeClaim(namespace, name string, storageClassName string) *v1.PersistentVolumeClaim { return &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ @@ -51,7 +50,7 @@ func toPersistentVolumeClaim(namespace, name string) *v1.PersistentVolumeClaim { }, Spec: v1.PersistentVolumeClaimSpec{ AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteMany}, - StorageClassName: &localStorageClass, + StorageClassName: &storageClassName, Resources: v1.ResourceRequirements{ Requests: v1.ResourceList{ v1.ResourceStorage: defaultVolumeSize, diff --git a/main.go b/main.go index fc3eb79..d104556 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ func main() { u := flag.String("kube-url", "", "") n := flag.String("kube-node", "", "") d := flag.Bool("kube-debug", false, "") + s := flag.String("kube-storageclass", "", "") t := flag.Duration("timeout", time.Hour, "") h := flag.Bool("help", false, "") @@ -69,7 +70,7 @@ func main() { log.Fatalln(err) } } else { - engine, err = kube.NewFile(*u, *k, *n) + engine, err = kube.NewFile(*u, *k, *n, *s) if err != nil { log.Fatalln(err) }