Commit d0c26939 authored by Arie Peterson's avatar Arie Peterson 🐚
Browse files

Upgrade to newer Kubernetes and go

parent 5ce31101
FROM golang:1.12-alpine as buildenv FROM golang:1.13-alpine as buildenv
RUN apk add --no-cache git RUN apk add --no-cache git
RUN go get github.com/golang/dep/cmd/dep
RUN mkdir -p /dist/empty RUN mkdir -p /dist/empty
ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.1/dumb-init_1.2.1_amd64 /dist/dumb-init ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.1/dumb-init_1.2.1_amd64 /dist/dumb-init
RUN chmod 755 /dist/dumb-init RUN chmod 755 /dist/dumb-init
WORKDIR /go/src/ghost WORKDIR /go/src/ghost
COPY Gopkg.toml ./
COPY go.mod ./
RUN go mod download
COPY cmd ./cmd COPY cmd ./cmd
COPY ghost ./ghost COPY ghost ./ghost
RUN dep ensure
RUN CGO_ENABLED=0 go build -a -ldflags '-extldflags "-static"' -o provisioner ./cmd/provisioner RUN CGO_ENABLED=0 go build -a -ldflags '-extldflags "-static"' -o provisioner ./cmd/provisioner
RUN cp /go/src/ghost/provisioner /dist/provisioner RUN cp /go/src/ghost/provisioner /dist/provisioner
......
[[constraint]]
name = "github.com/golang/glog"
branch = "master"
[[constraint]]
name = "github.com/kubernetes-sigs/sig-storage-lib-external-provisioner"
version = "v2.1.0"
[[constraint]]
name = "k8s.io/api"
version = "kubernetes-1.13.0"
[[constraint]]
name = "k8s.io/apimachinery"
version = "kubernetes-1.13.0"
[[constraint]]
name = "k8s.io/client-go"
version = "10.0.0"
package main package main
import ( import (
"ghost/ghost" "greenhost.net/kubernetes-ghost/provisioner/ghost"
"context"
"flag" "flag"
"os" "os"
"syscall" "syscall"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
"github.com/golang/glog" "k8s.io/klog"
"github.com/kubernetes-sigs/sig-storage-lib-external-provisioner/controller" "sigs.k8s.io/sig-storage-lib-external-provisioner/v6/controller"
) )
const ( const (
...@@ -38,12 +38,12 @@ var _ controller.Provisioner = &GreenhostProvisioner{} ...@@ -38,12 +38,12 @@ var _ controller.Provisioner = &GreenhostProvisioner{}
func NewGreenhostProvisioner() controller.Provisioner { func NewGreenhostProvisioner() controller.Provisioner {
cc, err := ghost.NewCosmosClient() cc, err := ghost.NewCosmosClient()
if err != nil { if err != nil {
glog.Fatalf("failed to create CosmosClient: %v", err) klog.Fatalf("failed to create CosmosClient: %v", err)
} }
projectID_, err := cc.GetInstanceData() projectID_, err := cc.GetInstanceData()
project := "" project := ""
if err != nil { if err != nil {
glog.Fatalf("failed getting project ID: %s", err) klog.Fatalf("failed getting project ID: %s", err)
} else { } else {
project = *projectID_ project = *projectID_
} }
...@@ -55,16 +55,16 @@ func NewGreenhostProvisioner() controller.Provisioner { ...@@ -55,16 +55,16 @@ func NewGreenhostProvisioner() controller.Provisioner {
} }
// Provision creates a storage asset and returns a pv object representing it. // Provision creates a storage asset and returns a pv object representing it.
func (p *GreenhostProvisioner) Provision(options controller.VolumeOptions) (*v1.PersistentVolume, error) { func (p *GreenhostProvisioner) Provision(ctx context.Context, options controller.ProvisionOptions) (*v1.PersistentVolume, controller.ProvisioningState, error) {
pvName := options.PVC.ObjectMeta.Name pvName := options.PVC.ObjectMeta.Name
name := "kubernetes-ghost:" + pvName name := "kubernetes-ghost:" + pvName
region := os.Getenv("GHOST_CLOUD_REGION") region := os.Getenv("GHOST_CLOUD_REGION")
backup := os.Getenv("GHOST_IMAGE_BACKUP") backup := os.Getenv("GHOST_IMAGE_BACKUP")
backupBool := backup == "true" backupBool := backup == "true"
if (backupBool) { if (backupBool) {
glog.Info("disk image backups on") klog.Info("disk image backups on")
} else { } else {
glog.Info("disk image backups off") klog.Info("disk image backups off")
} }
requestedSize := options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)] requestedSize := options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
// Round the requested size up to nearest integer number of gigabytes. // Round the requested size up to nearest integer number of gigabytes.
...@@ -79,11 +79,11 @@ func (p *GreenhostProvisioner) Provision(options controller.VolumeOptions) (*v1. ...@@ -79,11 +79,11 @@ func (p *GreenhostProvisioner) Provision(options controller.VolumeOptions) (*v1.
Backup: backupBool, Backup: backupBool,
} }
// The Cosmos API returns a description of the created disk image. // The Cosmos API returns a description of the created disk image.
glog.Infof("Creating new disk image with specs %v", specs) klog.Infof("Creating new disk image with specs %v", specs)
diskImage, err := p.cosmosClient.CreateDiskImage(&specs) diskImage, err := p.cosmosClient.CreateDiskImage(&specs)
if err != nil { if err != nil {
glog.Warningf("Error in creating new disk image %v", err) klog.Warningf("Error in creating new disk image %v", err)
return nil, err return nil, controller.ProvisioningFinished, err
} }
pv := &v1.PersistentVolume{ pv := &v1.PersistentVolume{
...@@ -94,7 +94,7 @@ func (p *GreenhostProvisioner) Provision(options controller.VolumeOptions) (*v1. ...@@ -94,7 +94,7 @@ func (p *GreenhostProvisioner) Provision(options controller.VolumeOptions) (*v1.
}, },
}, },
Spec: v1.PersistentVolumeSpec{ Spec: v1.PersistentVolumeSpec{
PersistentVolumeReclaimPolicy: options.PersistentVolumeReclaimPolicy, PersistentVolumeReclaimPolicy: *options.StorageClass.ReclaimPolicy,
AccessModes: options.PVC.Spec.AccessModes, AccessModes: options.PVC.Spec.AccessModes,
Capacity: v1.ResourceList{ Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): requestedSize, v1.ResourceName(v1.ResourceStorage): requestedSize,
...@@ -112,13 +112,13 @@ func (p *GreenhostProvisioner) Provision(options controller.VolumeOptions) (*v1. ...@@ -112,13 +112,13 @@ func (p *GreenhostProvisioner) Provision(options controller.VolumeOptions) (*v1.
}, },
} }
return pv, nil return pv, controller.ProvisioningFinished, nil
} }
func (p *GreenhostProvisioner) Delete(volume *v1.PersistentVolume) error { func (p *GreenhostProvisioner) Delete(ctx context.Context, volume *v1.PersistentVolume) error {
pvName := volume.Name pvName := volume.Name
ghostID := volume.Annotations[idAnnotation] ghostID := volume.Annotations[idAnnotation]
glog.Infof("Deleting pv with name %s and cosmos-id %s", pvName, ghostID) klog.Infof("Deleting pv with name %s and cosmos-id %s", pvName, ghostID)
p.cosmosClient.DeleteDiskImage(ghostID) p.cosmosClient.DeleteDiskImage(ghostID)
return nil return nil
} }
...@@ -129,7 +129,7 @@ func main() { ...@@ -129,7 +129,7 @@ func main() {
flag.Parse() flag.Parse()
flag.Set("logtostderr", "true") flag.Set("logtostderr", "true")
glog.Info("Starting kubernetes-ghost provisioner.") klog.Info("Starting kubernetes-ghost provisioner.")
// Do not restrict permissions on created files and directories. // Do not restrict permissions on created files and directories.
syscall.Umask(0) syscall.Umask(0)
...@@ -137,19 +137,19 @@ func main() { ...@@ -137,19 +137,19 @@ func main() {
// Create an InClusterConfig. // Create an InClusterConfig.
config, err := rest.InClusterConfig() config, err := rest.InClusterConfig()
if err != nil { if err != nil {
glog.Fatalf("Error creating InClusterConfig: %v", err) klog.Fatalf("Error creating InClusterConfig: %v", err)
} }
// Create a Kubernetes client. // Create a Kubernetes client.
clientSet, err := kubernetes.NewForConfig(config) clientSet, err := kubernetes.NewForConfig(config)
if err != nil { if err != nil {
glog.Fatalf("Error creating Kubernetes client: %v", err) klog.Fatalf("Error creating Kubernetes client: %v", err)
} }
// Get the Kubernetes server version. // Get the Kubernetes server version.
serverVersion, err := clientSet.Discovery().ServerVersion() serverVersion, err := clientSet.Discovery().ServerVersion()
if err != nil { if err != nil {
glog.Fatalf("Error getting Kubernetes server version: %v", err) klog.Fatalf("Error getting Kubernetes server version: %v", err)
} }
// Create the provisioner. // Create the provisioner.
...@@ -164,5 +164,5 @@ func main() { ...@@ -164,5 +164,5 @@ func main() {
) )
// Run the provision controller. // Run the provision controller.
pc.Run(wait.NeverStop) pc.Run(context.Background())
} }
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"strings" "strings"
"github.com/golang/glog" "k8s.io/klog"
) )
// Read this machine's instance ID and API token as passed on the // Read this machine's instance ID and API token as passed on the
...@@ -21,7 +21,7 @@ func getKernelParams() (string, string, error) { ...@@ -21,7 +21,7 @@ func getKernelParams() (string, string, error) {
words := strings.Split(strings.TrimRight(string(data), "\x00"), " ") words := strings.Split(strings.TrimRight(string(data), "\x00"), " ")
var instanceID, apiToken string var instanceID, apiToken string
for _, word := range words { for _, word := range words {
glog.Infof("cmdline word: %s", word) klog.Infof("cmdline word: %s", word)
// Split word into parts before and after "=". // Split word into parts before and after "=".
components := strings.SplitN(word, "=", 2) components := strings.SplitN(word, "=", 2)
// Check whether this is the argument we're looking for. // Check whether this is the argument we're looking for.
......
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
"net/http" "net/http"
"os" "os"
"github.com/golang/glog" "k8s.io/klog"
) )
type CosmosClient struct { type CosmosClient struct {
...@@ -27,7 +27,7 @@ func NewCosmosClient() (*CosmosClient, error) { ...@@ -27,7 +27,7 @@ func NewCosmosClient() (*CosmosClient, error) {
func (cc *CosmosClient) ApiCall(method string, endPoint string, data []byte) (*http.Response, error) { func (cc *CosmosClient) ApiCall(method string, endPoint string, data []byte) (*http.Response, error) {
url := os.Getenv("GHOST_COSMOS_API_URL") + endPoint url := os.Getenv("GHOST_COSMOS_API_URL") + endPoint
glog.Infof("calling url: %v", url) klog.Infof("calling url: %v", url)
// Prepare the body, if present. // Prepare the body, if present.
var body io.Reader var body io.Reader
...@@ -35,7 +35,7 @@ func (cc *CosmosClient) ApiCall(method string, endPoint string, data []byte) (*h ...@@ -35,7 +35,7 @@ func (cc *CosmosClient) ApiCall(method string, endPoint string, data []byte) (*h
body = nil body = nil
} else { } else {
body = bytes.NewReader(data) body = bytes.NewReader(data)
glog.Infof("with data: %v", string(data)) klog.Infof("with data: %v", string(data))
} }
// Create the HTTP request. // Create the HTTP request.
...@@ -50,7 +50,7 @@ func (cc *CosmosClient) ApiCall(method string, endPoint string, data []byte) (*h ...@@ -50,7 +50,7 @@ func (cc *CosmosClient) ApiCall(method string, endPoint string, data []byte) (*h
// Send the HTTP request. // Send the HTTP request.
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
glog.Fatalf("Error in http request: %v", err) klog.Fatalf("Error in http request: %v", err)
} }
return resp, err return resp, err
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"github.com/golang/glog" "k8s.io/klog"
) )
// Specifications of a disk to be created by Cosmos2. // Specifications of a disk to be created by Cosmos2.
...@@ -46,7 +46,7 @@ func (cc *CosmosClient) CreateDiskImage(specs *Specs) (*DiskImage, error) { ...@@ -46,7 +46,7 @@ func (cc *CosmosClient) CreateDiskImage(specs *Specs) (*DiskImage, error) {
var diskResponse diskResponse var diskResponse diskResponse
json.NewDecoder(resp.Body).Decode(&diskResponse) json.NewDecoder(resp.Body).Decode(&diskResponse)
diskImage := diskResponse.DiskImage diskImage := diskResponse.DiskImage
glog.Infof("Created DiskImage object: %+v", diskImage) klog.Infof("Created DiskImage object: %+v", diskImage)
return &diskImage, nil return &diskImage, nil
} }
...@@ -65,7 +65,7 @@ func (cc *CosmosClient) detachDiskImage(id string) bool { ...@@ -65,7 +65,7 @@ func (cc *CosmosClient) detachDiskImage(id string) bool {
resp, err := cc.ApiCall("POST", "/disks/"+id+"/actions", postData) resp, err := cc.ApiCall("POST", "/disks/"+id+"/actions", postData)
if err != nil { if err != nil {
glog.Warningf("Error performing API call to Cosmos2: %v", err) klog.Warningf("Error performing API call to Cosmos2: %v", err)
return false return false
} }
...@@ -74,10 +74,10 @@ func (cc *CosmosClient) detachDiskImage(id string) bool { ...@@ -74,10 +74,10 @@ func (cc *CosmosClient) detachDiskImage(id string) bool {
} else { } else {
responseData, err := ioutil.ReadAll(resp.Body) responseData, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
glog.Warningf("Error reading Cosmos2 response: %v", err) klog.Warningf("Error reading Cosmos2 response: %v", err)
return false return false
} }
glog.Warningf("Cosmos2 failed to detach disk: %d %s", resp.StatusCode, responseData) klog.Warningf("Cosmos2 failed to detach disk: %d %s", resp.StatusCode, responseData)
return false return false
} }
} }
...@@ -90,7 +90,7 @@ func (cc *CosmosClient) DeleteDiskImage(id string) bool { ...@@ -90,7 +90,7 @@ func (cc *CosmosClient) DeleteDiskImage(id string) bool {
resp, err := cc.ApiCall("DELETE", "/disks/"+id, nil) resp, err := cc.ApiCall("DELETE", "/disks/"+id, nil)
if err != nil { if err != nil {
glog.Warningf("Error performing API call to Cosmos2: %v", err) klog.Warningf("Error performing API call to Cosmos2: %v", err)
return false return false
} }
...@@ -99,10 +99,10 @@ func (cc *CosmosClient) DeleteDiskImage(id string) bool { ...@@ -99,10 +99,10 @@ func (cc *CosmosClient) DeleteDiskImage(id string) bool {
} else { } else {
responseData, err := ioutil.ReadAll(resp.Body) responseData, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
glog.Warningf("Error reading Cosmos2 response: %v", err) klog.Warningf("Error reading Cosmos2 response: %v", err)
return false return false
} }
glog.Warningf("Cosmos2 failed to delete disk with ID %s returning: %d %s", id, resp.StatusCode, responseData) klog.Warningf("Cosmos2 failed to delete disk with ID %s returning: %d %s", id, resp.StatusCode, responseData)
return false return false
} }
} }
...@@ -144,7 +144,7 @@ func (cc *CosmosClient) GetInstanceData() (*string, error) { ...@@ -144,7 +144,7 @@ func (cc *CosmosClient) GetInstanceData() (*string, error) {
if projectID == nil { if projectID == nil {
return nil, errors.New("project_id is not set for this instance") return nil, errors.New("project_id is not set for this instance")
} else { } else {
glog.Infof("Project ID is %s", *projectID) klog.Infof("Project ID is %s", *projectID)
return projectID, nil return projectID, nil
} }
} }
module greenhost.net/kubernetes-ghost/provisioner
go 1.13
require (
k8s.io/api v0.18.0
k8s.io/apimachinery v0.18.0
k8s.io/client-go v0.18.0
k8s.io/klog v1.0.0
sigs.k8s.io/sig-storage-lib-external-provisioner/v6 v6.0.0
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment