Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
implement storageclass
Browse files Browse the repository at this point in the history
fix fmt
  • Loading branch information
zetaab committed Dec 11, 2018
1 parent a0aa036 commit e8488b0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion engine/kube/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 17 additions & 10 deletions engine/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
30 changes: 21 additions & 9 deletions engine/kube/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions engine/kube/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit e8488b0

Please sign in to comment.