Skip to content
Open
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
28 changes: 16 additions & 12 deletions cmd/stackit-csi-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/component-base/cli"
"k8s.io/klog/v2"

"github.com/stackitcloud/cloud-provider-stackit/pkg/csi"
"github.com/stackitcloud/cloud-provider-stackit/pkg/csi/blockstorage"
"github.com/stackitcloud/cloud-provider-stackit/pkg/csi/util/mount"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
"github.com/stackitcloud/cloud-provider-stackit/pkg/metrics"
stackitclient "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/client"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/metadata"
"github.com/stackitcloud/cloud-provider-stackit/pkg/version"
sdkconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"k8s.io/component-base/cli"
"k8s.io/klog/v2"
)

var (
Expand Down Expand Up @@ -73,7 +74,7 @@ func main() {
cmd.PersistentFlags().BoolVar(&provideNodeService, "provide-node-service", true,
"If set to true then the CSI driver does provide the node service (default: true)")

stackit.AddExtraFlags(pflag.CommandLine)
stackitclient.AddExtraFlags(pflag.CommandLine)

code := cli.Run(cmd)
os.Exit(code)
Expand All @@ -89,29 +90,32 @@ func handle() {

if provideControllerService {
var err error
cfg, err := stackit.GetConfigFromFile(cloudConfig)
cfg, err := stackitclient.GetConfigFromFile(cloudConfig)
if err != nil {
klog.Fatal(err)
}

iaasClient, err := stackit.CreateIaaSClient(&cfg)
if err != nil {
klog.Fatalf("Failed to create IaaS client: %v", err)
iaasOpts := []sdkconfig.ConfigurationOption{
sdkconfig.WithHTTPClient(metrics.NewInstrumentedHTTPClient()),
}

if cfg.Global.APIEndpoints.IaasAPI != "" {
iaasOpts = append(iaasOpts, sdkconfig.WithEndpoint(cfg.Global.APIEndpoints.IaasAPI))
}

stackitProvider, err := stackit.CreateSTACKITProvider(iaasClient, &cfg)
iaasClient, err := stackitclient.New(cfg.Global.Region, cfg.Global.ProjectID).IaaS(iaasOpts)
if err != nil {
klog.Fatalf("Failed to create STACKIT provider: %v", err)
}

d.SetupControllerService(stackitProvider)
d.SetupControllerService(iaasClient)
}

if provideNodeService {
// Initialize mount
mountProvider := mount.GetMountProvider()

cfg, err := stackit.GetConfigFromFile(cloudConfig)
cfg, err := stackitclient.GetConfigFromFile(cloudConfig)
if err != nil {
klog.Fatal(err)
}
Expand Down
109 changes: 54 additions & 55 deletions pkg/csi/blockstorage/controllerserver.go

Large diffs are not rendered by default.

156 changes: 85 additions & 71 deletions pkg/csi/blockstorage/controllerserver_test.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/csi/blockstorage/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
stackitclient "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/client"
stackitconfig "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/config"
corev1 "k8s.io/client-go/listers/core/v1"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -134,7 +134,7 @@ func (d *Driver) AddNodeServiceCapabilities(nl []csi.NodeServiceCapability_RPC_T
return nil
}

func (d *Driver) SetupControllerService(instance stackit.IaasClient) {
func (d *Driver) SetupControllerService(instance stackitclient.IaaSClient) {
klog.Info("Providing controller service")
d.cs = NewControllerServer(d, instance)
}
Expand Down
41 changes: 20 additions & 21 deletions pkg/csi/blockstorage/sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ import (
"github.com/google/uuid"
"github.com/kubernetes-csi/csi-test/v5/pkg/sanity"
. "github.com/onsi/ginkgo/v2"
stackitconfig "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/config"
mountutils "k8s.io/mount-utils"
exec "k8s.io/utils/exec/testing"

"github.com/stackitcloud/cloud-provider-stackit/pkg/csi/util/mount"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
stackitclient "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/client"
stackitclientmock "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/client/mock"
stackitconfig "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/config"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/metadata"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api"
"go.uber.org/mock/gomock"
mountutils "k8s.io/mount-utils"
exec "k8s.io/utils/exec/testing"
)

var _ = Describe("CSI sanity test", Ordered, func() {
Context("Base config", func() {
var (
driver *Driver
opts *DriverOpts
iaasClient *stackit.MockIaasClient
iaasClient *stackitclientmock.MockIaaSClient
mountMock *mount.MockIMount
metadataMock *metadata.MockIMetadata
FakeEndpoint string
Expand Down Expand Up @@ -57,7 +57,7 @@ var _ = Describe("CSI sanity test", Ordered, func() {
})

// --- Initialize Mocks ---
iaasClient = stackit.NewMockIaasClient(ctrl)
iaasClient = stackitclientmock.NewMockIaaSClient(ctrl)
mountMock = mount.NewMockIMount(ctrl)
metadataMock = metadata.NewMockIMetadata(ctrl)

Expand Down Expand Up @@ -90,7 +90,7 @@ var _ = Describe("CSI sanity test", Ordered, func() {
Id: new(uuid.New().String()), // Create a random ID
Name: opts.Name,
Size: size,
Status: new(stackit.VolumeAvailableStatus),
Status: new(stackitclient.VolumeAvailableStatus),
AvailabilityZone: opts.AvailabilityZone,
Source: opts.Source,
}
Expand Down Expand Up @@ -161,23 +161,21 @@ var _ = Describe("CSI sanity test", Ordered, func() {

iaasClient.EXPECT().CreateSnapshot(
gomock.Any(), // context
gomock.Any(), // name
gomock.Any(), // volID
gomock.Any(), // tags
).DoAndReturn(func(_ context.Context, name string, volID string, _ map[string]string) (*iaas.Snapshot, error) {
gomock.Any(), // payload
).DoAndReturn(func(_ context.Context, payload *iaas.CreateSnapshotPayload) (*iaas.Snapshot, error) {
newSnap := &iaas.Snapshot{
Id: new(uuid.New().String()),
Name: new(name),
Status: new(stackit.SnapshotReadyStatus),
Name: payload.Name,
Status: new(stackitclient.SnapshotReadyStatus),
CreatedAt: new(time.Now()),
Size: new(int64(10)), // 10 GiB
VolumeId: volID,
VolumeId: payload.VolumeId,
}
createdSnapshots[*newSnap.Id] = newSnap
return newSnap, nil
}).AnyTimes()

iaasClient.EXPECT().GetSnapshotByID(
iaasClient.EXPECT().GetSnapshot(
gomock.Any(), // context
gomock.Any(), // snapshotID
).DoAndReturn(func(_ context.Context, snapshotID string) (*iaas.Snapshot, error) {
Expand Down Expand Up @@ -249,7 +247,7 @@ var _ = Describe("CSI sanity test", Ordered, func() {
gomock.Any(), // context
gomock.Any(), // snapshotID
).Return(
new(string(stackit.SnapshotReadyStatus)),
new(stackitclient.SnapshotReadyStatus),
nil,
).AnyTimes()

Expand All @@ -274,7 +272,7 @@ var _ = Describe("CSI sanity test", Ordered, func() {
return newBackup, nil
}).AnyTimes()

iaasClient.EXPECT().GetBackupByID(
iaasClient.EXPECT().GetBackup(
gomock.Any(), // context
gomock.Any(), // backupID
).DoAndReturn(func(_ context.Context, backupID string) (*iaas.Backup, error) {
Expand Down Expand Up @@ -306,7 +304,7 @@ var _ = Describe("CSI sanity test", Ordered, func() {

// --- 4. Mock IaaS Client (Instances & Attach/Detach) ---

iaasClient.EXPECT().GetInstanceByID(
iaasClient.EXPECT().GetServer(
gomock.Any(), // context
gomock.Any(), // instanceID
).DoAndReturn(func(_ context.Context, instanceID string) (*iaas.Server, error) {
Expand All @@ -324,7 +322,8 @@ var _ = Describe("CSI sanity test", Ordered, func() {
gomock.Any(), // context
gomock.Any(), // instanceID
gomock.Any(), // volumeID
).DoAndReturn(func(_ context.Context, instanceID string, volumeID string) (string, error) {
gomock.Any(), // payload
).DoAndReturn(func(_ context.Context, instanceID string, volumeID string, _ iaas.AddVolumeToServerPayload) (string, error) {
vol, ok := createdVolumes[volumeID]
if !ok {
return "", &oapierror.GenericOpenAPIError{StatusCode: http.StatusNotFound}
Expand Down Expand Up @@ -458,4 +457,4 @@ var _ = Describe("CSI sanity test", Ordered, func() {
})

})
})
})
4 changes: 2 additions & 2 deletions pkg/csi/blockstorage/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync/atomic"

"github.com/stackitcloud/cloud-provider-stackit/pkg/csi/util/mount"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
stackitclient "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/client"
stackitconfig "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/config"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/metadata"

Expand Down Expand Up @@ -44,7 +44,7 @@ func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *c
}

//revive:disable:unexported-return
func NewControllerServer(d *Driver, instance stackit.IaasClient) *controllerServer {
func NewControllerServer(d *Driver, instance stackitclient.IaaSClient) *controllerServer {
return &controllerServer{
Driver: d,
Instance: instance,
Expand Down
57 changes: 55 additions & 2 deletions pkg/stackit/client/factory.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
package client

import sdkconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
import (
"io"
"os"

"github.com/spf13/pflag"
stackitconfig "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/config"
sdkconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"gopkg.in/yaml.v3"
"k8s.io/klog/v2"
)

func AddExtraFlags(fs *pflag.FlagSet) {
fs.StringArrayVar(&userAgentData, "user-agent", nil, "Extra data to add to STACKIT SDK user-agent. Use multiple times to add more than one component.")
}

const (
UserAgent = "cloud-provider-stackit"
)

// userAgentData is used to add extra information to the STACKIT SDK user-agent
var (
userAgentData []string
)

// Factory produces clients for various STACKIT services.
type Factory interface {
// LoadBalancing returns a STACKIT load balancing service client.
LoadBalancing(options []sdkconfig.ConfigurationOption) (LoadBalancingClient, error)
LoadBalancing(ptions []sdkconfig.ConfigurationOption) (LoadBalancingClient, error)

// IaaS returns a STACKIT IaaS service client.
IaaS(options []sdkconfig.ConfigurationOption) (IaaSClient, error)
Expand All @@ -30,3 +52,34 @@ func (f factory) LoadBalancing(options []sdkconfig.ConfigurationOption) (LoadBal
func (f factory) IaaS(options []sdkconfig.ConfigurationOption) (IaaSClient, error) {
return NewIaaSClient(f.StackitRegion, f.StackitProjectID, options)
}

func GetConfigFromFile(path string) (stackitconfig.CSIConfig, error) {
var cfg stackitconfig.CSIConfig

config, err := os.Open(path)
if err != nil {
klog.ErrorS(err, "Failed to open stackitconfig file", "path", path)
return cfg, err
}
defer config.Close()

return GetConfig(config)
}

func GetConfig(reader io.Reader) (stackitconfig.CSIConfig, error) {
var cfg stackitconfig.CSIConfig

content, err := io.ReadAll(reader)
if err != nil {
klog.ErrorS(err, "Failed to read config content")
return cfg, err
}

err = yaml.Unmarshal(content, &cfg)
if err != nil {
klog.ErrorS(err, "Failed to parse config as YAML")
return cfg, err
}

return cfg, nil
}
Loading