Skip to content

Commit

Permalink
Add hetzner provision/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Klar committed Oct 18, 2018
1 parent f7bc2ca commit 279d9d6
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 41 deletions.
21 changes: 10 additions & 11 deletions driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ const (
)

type jsonParameter struct {
FSGroup string `json:"kubernetes.io/fsGroup"`
FSType string `json:"kubernetes.io/fsType"`
PVOrVolumeName string `json:"kubernetes.io/pvOrVolumeName"`
PodName string `json:"kubernetes.io/pod.name"`
PodNamespace string `json:"kubernetes.io/pod.namespace"`
PodUID string `json:"kubernetes.io/pod.uid"`
ReadWrite string `json:"kubernetes.io/readwrite"`
ServiceAccount string `json:"kubernetes.io/serviceAccount.name"`
FSGroup string `json:"kubernetes.io/fsGroup"`
FSType string `json:"kubernetes.io/fsType"`
PVOrVolumeName string `json:"kubernetes.io/pvOrVolumeName"`
PodName string `json:"kubernetes.io/pod.name"`
PodNamespace string `json:"kubernetes.io/pod.namespace"`
PodUID string `json:"kubernetes.io/pod.uid"`
ReadWrite string `json:"kubernetes.io/readwrite"`
ServiceAccount string `json:"kubernetes.io/serviceAccount.name"`
}

func main() {
Expand Down Expand Up @@ -80,12 +80,12 @@ func failure(err error) {
}

func mount(mountDir, jsonOptions string) {
// TODO: To be implemented
// TODO: To be implemented
success()
}

func unmount(mountDir string) {
// TODO: To be implemented
// TODO: To be implemented
success()
}

Expand All @@ -97,4 +97,3 @@ func run(cmd string, args ...string) (string, error) {
}
return string(out), nil
}

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/golang-lru v0.5.0 // indirect
github.com/hetznercloud/hcloud-go v1.10.0
github.com/imdario/mergo v0.3.6 // indirect
github.com/json-iterator/go v1.1.5 // indirect
github.com/kubernetes-incubator/external-storage v5.2.0+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWet
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hetznercloud/hcloud-go v1.10.0 h1:hfJGx6bH8yN1LiDskOiXlJpgbI5Io/27LYuWi5cGEPE=
github.com/hetznercloud/hcloud-go v1.10.0/go.mod h1:g5pff0YNAZywQaivY/CmhUYFVp7oP0nu3MiODC2W4Hw=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
Expand Down
2 changes: 1 addition & 1 deletion provisioner/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
IMAGE = spangenberg/xenserver-provisioner:latest
IMAGE = stevenklar/hetzner-storage-provisioner:latest

all build:
CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o bin/provisioner
Expand Down
14 changes: 12 additions & 2 deletions provisioner/delete.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package main

import (
"context"
"github.com/golang/glog"
"k8s.io/api/core/v1"
)

func (p *hetznerProvisioner) Delete(volume *v1.PersistentVolume) error {
glog.Infof("Delete called for volume: %s", volume.Name)

// TODO: Delete from hetzner
client := p.getClient(p.token)
hetznerVolume, _, err := client.Volume.GetByName(context.Background(), volume.Name)

return nil
if err != nil {
glog.Infof("Delete failed for volume: %s", volume.Name)
return err
}

response, err := client.Volume.Delete(context.Background(), hetznerVolume)
glog.Infoln(response)

return err
}

10 changes: 7 additions & 3 deletions provisioner/deploy/auth/clusterrole.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
apiVersion: v1
kind: rbac.authorization.k8s.io/v1
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: hetzner-provisioner-runner
namespace: hcloud
name: hetzner-provisioner-role
rules:
- apiGroups: [""]
resources: ["events"]
Expand All @@ -15,3 +16,6 @@ rules:
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["create", "update", "get", "list", "watch"]
6 changes: 3 additions & 3 deletions provisioner/deploy/auth/clusterrolebinding.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: run-hetzner-provisioner
name: hetzner-provisioner-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: hetzner-provisioner-runner
name: hetzner-provisioner-role
subjects:
- kind: ServiceAccount
name: hetzner-provisioner
namespace: hetzner-provisioner
namespace: hcloud
17 changes: 0 additions & 17 deletions provisioner/deploy/auth/openshift-clusterrole.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion provisioner/deploy/class.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ kind: StorageClass
metadata:
name: hetzner-cloud-default
parameters:
provisioner: stevenklar/hetzner-provisioner
provisioner: stevenklar/hetzner-storage-provisioner
5 changes: 4 additions & 1 deletion provisioner/deploy/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: hetzner-provisioner
template:
metadata:
labels:
app: hetzner-provisioner
spec:
containers:
- name: provisioner
image: stevenklar/hetzner-provisioner:0.1
image: stevenklar/hetzner-storage-provisioner:latest
env:
- name: HETZNER_TOKEN
valueFrom:
Expand Down
2 changes: 1 addition & 1 deletion provisioner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
var (
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Either this or master needs to be set if the provisioner is being run out of cluster.")
master = flag.String("master", "", "Master URL to build a client config from. Either this or kubeconfig needs to be set if the provisioner is being run out of cluster.")
provisioner = flag.String("provisioner", "spangenberg.io/xenserver-provisioner", "Name of the provisioner. The provisioner will only provision volumes for claims that request a StorageClass with a provisioner field set equal to this name.")
provisioner = flag.String("provisioner", "stevenklar/hetzner-storage-provisioner", "Name of the provisioner. The provisioner will only provision volumes for claims that request a StorageClass with a provisioner field set equal to this name.")
)

func main() {
Expand Down
32 changes: 31 additions & 1 deletion provisioner/provision.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/kubernetes-incubator/external-storage/lib/controller"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -12,7 +15,10 @@ const defaultFSType = "ext4"
func (p *hetznerProvisioner) Provision(options controller.VolumeOptions) (*v1.PersistentVolume, error) {
glog.Infof("Provision called for volume: %s", options.PVName)

// TODO: Provision on Hetzner Cloud
if err := p.provisionOnHetznerCloud(options); err != nil {
glog.Errorf("Failed to provision volume %s, error: %s", options, err.Error())
return nil, err
}

pv := &v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -39,3 +45,27 @@ func (p *hetznerProvisioner) Provision(options controller.VolumeOptions) (*v1.Pe
return pv, nil
}

func (p *hetznerProvisioner) provisionOnHetznerCloud(options controller.VolumeOptions) error {
client := p.getClient(p.token)

capacity, exists := options.PVC.Spec.Resources.Requests[v1.ResourceStorage]
if !exists {
return fmt.Errorf("Capacity was not specified for name label %s", options.PVName)
}

hetznerCapacity := (((int(capacity.Value()) / 1024) / 1024) / 1024) // kuberntes uses bytes, hetzner uses gbytes

glog.Infof("Would create volume with capacity: %s", hetznerCapacity)

// create volume with given volume options
opts := hcloud.VolumeCreateOpts{
Name: options.PVName,
//Size: hetznerCapacity,
Size: 10,
Location: &hcloud.Location{Name: "nbg1"}, // not available in falkenstein yet? - add default option and random picker?
}

_, _, err := client.Volume.Create(context.Background(), opts)

return err
}
6 changes: 6 additions & 0 deletions provisioner/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/kubernetes-incubator/external-storage/lib/controller"
"k8s.io/utils/exec"
"github.com/hetznercloud/hcloud-go/hcloud"
)

type hetznerProvisioner struct {
Expand All @@ -19,3 +20,8 @@ func NewProvisioner() controller.Provisioner {
}
}

func (p *hetznerProvisioner) getClient(token string) *hcloud.Client {
client := hcloud.NewClient(hcloud.WithToken(token))
return client
}

0 comments on commit 279d9d6

Please sign in to comment.