Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set User agent for csi #10

Merged
merged 3 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/driver.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ jobs:
with:
file: Dockerfile
push: true
build-args: |
VERSION=${{ steps.tag.outputs.tag }}
tags: |
civo/csi:${{ steps.tag.outputs.tag }}
civo/csi:latest
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
################################
FROM golang:alpine AS builder

ARG VERSION=dev

# Install git - required for fetching the dependencies
RUN apk add --update --no-cache ca-certificates git
WORKDIR /app
Expand All @@ -15,7 +17,7 @@ RUN go mod verify
RUN find .

# Build the binary.
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /app/civo-csi
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s -X github.com/civo/civo-csi/driver.CSIVersion=${VERSION}" -o /app/civo-csi


############################
Expand Down
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION?="0.0.1"
VERSION?="dev"

generate:
go generate ./...
Expand All @@ -9,7 +9,12 @@ protobuf:
fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"

docker: fmtcheck
docker build .
docker: fmtcheck buildprep
docker build --build-arg=VERSION=$(VERSION) .

buildprep:
git fetch --tags -f
mkdir -p dest
$(eval VERSION=$(shell git describe --tags | cut -d "v" -f 2 | cut -d "-" -f 1))

.PHONY: fmtcheck generate protobuf docker
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/civo/civo-csi

require (
github.com/BurntSushi/toml v0.3.1
github.com/civo/civogo v0.3.3
github.com/civo/civogo v0.3.13
github.com/container-storage-interface/spec v1.6.0
github.com/joho/godotenv v1.4.0
github.com/kubernetes-csi/csi-test/v4 v4.4.0
Expand Down
39 changes: 3 additions & 36 deletions go.sum

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions pkg/driver/controller_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// BytesInGigabyte describes how many bytes are in a gigabyte
const BytesInGigabyte int64 = 1024 * 1024 * 1024

// CivoVolumeAvailableRetries is the number of times we will retry to check if a volume is available
const CivoVolumeAvailableRetries int = 20

var supportedAccessModes = []csi.VolumeCapability_AccessMode_Mode{
Expand Down Expand Up @@ -294,9 +295,8 @@ func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.Control
if strings.Contains(err.Error(), "DatabaseVolumeNotFoundError") || strings.Contains(err.Error(), "ZeroMatchesError") {
log.Info().Str("volume_id", req.VolumeId).Msg("Volume already deleted from Civo API, pretend it's unmounted")
return &csi.ControllerUnpublishVolumeResponse{}, nil
} else {
log.Debug().Str("message", err.Error()).Msg("Error didn't match DatabaseVolumeNotFoundError")
}
log.Debug().Str("message", err.Error()).Msg("Error didn't match DatabaseVolumeNotFoundError")

log.Error().Err(err).Msg("Unable to find volume for unpublishing in Civo API")
return nil, err
Expand Down Expand Up @@ -333,16 +333,16 @@ func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.Control

// ControllerExpandVolume allows for offline expansion of Volumes
func (d *Driver) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
volId := req.GetVolumeId()
volID := req.GetVolumeId()

log.Info().Str("volume_id", volId).Msg("Request: ControllerExpandVolume")
log.Info().Str("volume_id", volID).Msg("Request: ControllerExpandVolume")

if volId == "" {
if volID == "" {
return nil, status.Error(codes.InvalidArgument, "must provide a VolumeId to ControllerExpandVolume")
}

// Get the volume from the Civo API
volume, err := d.CivoClient.GetVolume(volId)
volume, err := d.CivoClient.GetVolume(volID)
if err != nil {
return nil, status.Errorf(codes.Internal, "ControllerExpandVolume could not retrieve existing volume: %v", err)
}
Expand All @@ -365,16 +365,16 @@ func (d *Driver) ControllerExpandVolume(ctx context.Context, req *csi.Controller
}

if desiredSize <= int64(volume.SizeGigabytes) {
log.Info().Str("volume_id", volId).Msg("Volume is currently larger that desired Size")
log.Info().Str("volume_id", volID).Msg("Volume is currently larger that desired Size")
return &csi.ControllerExpandVolumeResponse{CapacityBytes: int64(volume.SizeGigabytes) * BytesInGigabyte, NodeExpansionRequired: true}, nil
}

if volume.Status != "available" {
return nil, status.Error(codes.FailedPrecondition, "volume is not in an availble state for OFFLINE expansion")
}

log.Info().Int64("size_gb", desiredSize).Str("volume_id", volId).Msg("Volume resize request sent")
d.CivoClient.ResizeVolume(volId, int(desiredSize))
log.Info().Int64("size_gb", desiredSize).Str("volume_id", volID).Msg("Volume resize request sent")
d.CivoClient.ResizeVolume(volID, int(desiredSize))

// Resizes can take a while, double the number of normal retries
available, err := d.waitForVolumeStatus(volume, "available", CivoVolumeAvailableRetries*2)
Expand All @@ -387,8 +387,8 @@ func (d *Driver) ControllerExpandVolume(ctx context.Context, req *csi.Controller
return nil, status.Error(codes.Internal, "failed to wait for volume to be in an available state")
}

volume, _ = d.CivoClient.GetVolume(volId)
log.Info().Int64("size_gb", int64(volume.SizeGigabytes)).Str("volume_id", volId).Msg("Volume succesfully resized")
volume, _ = d.CivoClient.GetVolume(volID)
log.Info().Int64("size_gb", int64(volume.SizeGigabytes)).Str("volume_id", volID).Msg("Volume succesfully resized")
return &csi.ControllerExpandVolumeResponse{
CapacityBytes: int64(volume.SizeGigabytes) * BytesInGigabyte,
NodeExpansionRequired: true,
Expand Down
17 changes: 14 additions & 3 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"google.golang.org/grpc"
)

// CSIVersion is the version of the csi to set in the User-Agent header
var CSIVersion = "dev"

// Name is the name of the driver
const Name string = "Civo CSI Driver"

Expand Down Expand Up @@ -44,7 +47,7 @@ type Driver struct {
}

// NewDriver returns a CSI driver that implements gRPC endpoints for CSI
func NewDriver(apiURL, apiKey, region, namespace, cluster_id string) (*Driver, error) {
func NewDriver(apiURL, apiKey, region, namespace, clusterID string) (*Driver, error) {
var client *civogo.Client
var err error

Expand All @@ -55,18 +58,26 @@ func NewDriver(apiURL, apiKey, region, namespace, cluster_id string) (*Driver, e
}
}

userAgent := &civogo.Component{
RealHarshThakur marked this conversation as resolved.
Show resolved Hide resolved
ID: clusterID,
Name: "civo-csi",
Version: Version,
}

client.SetUserAgent(userAgent)

socketFilename := os.Getenv("CSI_ENDPOINT")
if socketFilename == "" {
socketFilename = DefaultSocketFilename
}

log.Info().Str("api_url", apiURL).Str("region", region).Str("namespace", namespace).Str("cluster_id", cluster_id).Str("socketFilename", socketFilename).Msg("Created a new driver")
log.Info().Str("api_url", apiURL).Str("region", region).Str("namespace", namespace).Str("cluster_id", clusterID).Str("socketFilename", socketFilename).Str("user_agent", userAgent.Name).Msg("Created a new driver")

return &Driver{
CivoClient: client,
Region: region,
Namespace: namespace,
ClusterID: cluster_id,
ClusterID: clusterID,
DiskHotPlugger: &RealDiskHotPlugger{},
controller: (apiKey != ""),
SocketFilename: socketFilename,
Expand Down
8 changes: 7 additions & 1 deletion pkg/driver/hotplug_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (

const BlikidNotFound int = 2

// VolumeStatistics represents the statistics of a volume
type VolumeStatistics struct {
AvailableBytes, TotalBytes, UsedBytes int64
AvailableInodes, TotalInodes, UsedInodes int64
}

// DiskHotPlugger is an interface for hotplugging disks
type DiskHotPlugger interface {
// PathForVolume returns the path of the hotplugged disk
PathForVolume(volumeID string) string
Expand Down Expand Up @@ -58,7 +60,7 @@ func (p *RealDiskHotPlugger) PathForVolume(volumeID string) string {
return ""
}

// ExpandFilesytem expands the existing file system at the given path
// ExpandFilesystem expands the existing file system at the given path
func (p *RealDiskHotPlugger) ExpandFilesystem(path string) error {
log.Debug().Str("path", path).Msg("Resizing")

Expand Down Expand Up @@ -243,6 +245,7 @@ func (p *RealDiskHotPlugger) IsMounted(path string) (bool, error) {
return true, nil
}

// GetStatistics returns the statistics for a given volume path.
func (p *RealDiskHotPlugger) GetStatistics(volumePath string) (VolumeStatistics, error) {
var statfs unix.Statfs_t
// See http://man7.org/linux/man-pages/man2/statfs.2.html for details.
Expand All @@ -264,6 +267,7 @@ func (p *RealDiskHotPlugger) GetStatistics(volumePath string) (VolumeStatistics,
return volStats, nil
}

// FakeDiskHotPlugger is a fake implementation of RealDiskHotPlugger
type FakeDiskHotPlugger struct {
DiskAttachmentMissing bool
Filesystem string
Expand Down Expand Up @@ -293,6 +297,7 @@ func (p *FakeDiskHotPlugger) Format(path, filesystem string) error {
return nil
}

// ExpandFilesystem expands the existing file system at the given path
func (p *FakeDiskHotPlugger) ExpandFilesystem(path string) error {
if !p.Formatted {
return fmt.Errorf("disk must be formatted before being expanded")
Expand Down Expand Up @@ -332,6 +337,7 @@ func (p *FakeDiskHotPlugger) IsMounted(target string) (bool, error) {
return p.Mounted, nil
}

// GetStatistics returns the statistics for the given volume path
func (p *FakeDiskHotPlugger) GetStatistics(volumePath string) (VolumeStatistics, error) {
return VolumeStatistics{
AvailableBytes: 3 * BytesInGigabyte,
Expand Down