Skip to content

Commit

Permalink
Minor ADS changes to enable Astra reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
clintonk committed Mar 21, 2022
1 parent 58b6b0c commit ddc922e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 40 deletions.
47 changes: 46 additions & 1 deletion mocks/mock_storage_drivers/mock_astrads/mock_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions storage_drivers/astrads/api/astrads.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package api

import (
"context"
"encoding/base64"
"fmt"
"time"

Expand Down Expand Up @@ -88,17 +87,14 @@ func NewClient() *Clients {
return &Clients{}
}

func (c *Clients) Init(ctx context.Context, namespace, kubeConfig, cluster string) (*Cluster, string, error) {
func (c *Clients) Init(
ctx context.Context, namespace, cluster string, kubeConfigBytes []byte,
) (*Cluster, string, error) {

var err error

c.namespace = namespace

kubeConfigBytes, err := base64.StdEncoding.DecodeString(kubeConfig)
if err != nil {
return nil, "", fmt.Errorf("kubeconfig is not base64 encoded: %v", err)
}

c.restConfig, err = clientcmd.RESTConfigFromKubeConfig(kubeConfigBytes)
if err != nil {
return nil, "", fmt.Errorf("could not create REST config from kubeconfig; %v", err)
Expand All @@ -125,11 +121,17 @@ func (c *Clients) Init(ctx context.Context, namespace, kubeConfig, cluster strin
}).Info("Created Kubernetes clients.")

// Discover the cluster
var adsCluster *Cluster
if adsCluster, err = c.getCluster(ctx, cluster); err != nil {
return nil, "", err
if cluster != "" {
if c.cluster, err = c.Cluster(ctx, cluster); err != nil {
return nil, "", err
}

log.WithFields(log.Fields{
"cluster": c.cluster.Name,
"status": c.cluster.Status,
"version": c.cluster.Version,
}).Info("Discovered AstraDS cluster.")
}
c.cluster = adsCluster

// Discover the kube-system namespace UUID
var ns *v1.Namespace
Expand All @@ -138,15 +140,13 @@ func (c *Clients) Init(ctx context.Context, namespace, kubeConfig, cluster strin
}
c.kubeSystemUUID = string(ns.UID)

log.WithFields(log.Fields{
"cluster": c.cluster.Name,
"status": c.cluster.Status,
"version": c.cluster.Version,
}).Info("Discovered AstraDS cluster.")

return c.cluster, c.kubeSystemUUID, nil
}

func (c *Clients) KubeClient() *kubernetes.Clientset {
return c.kubeClient
}

// //////////////////////////////////////////////////////////////////////////
// Cluster operations BEGIN

Expand All @@ -163,8 +163,8 @@ func (c *Clients) getClusterFromAstraDSCluster(ftc *v1alpha1.AstraDSCluster) *Cl
}
}

// getCluster returns the named AstraDS cluster.
func (c *Clients) getCluster(ctx context.Context, name string) (*Cluster, error) {
// Cluster returns the named AstraDS cluster.
func (c *Clients) Cluster(ctx context.Context, name string) (*Cluster, error) {

unstructuredCluster, err := c.dynamicClient.Resource(clustersGVR).Namespace(AstraDSNamespace).Get(ctx, name, getOpts)
if err != nil {
Expand All @@ -180,8 +180,8 @@ func (c *Clients) getCluster(ctx context.Context, name string) (*Cluster, error)
return c.getClusterFromAstraDSCluster(&astraDSCluster), nil
}

// getClusters discovers and returns all AstraDS clusters.
func (c *Clients) getClusters(ctx context.Context) ([]*Cluster, error) {
// Clusters discovers and returns all AstraDS clusters.
func (c *Clients) Clusters(ctx context.Context) ([]*Cluster, error) {

unstructuredList, err := c.dynamicClient.Resource(clustersGVR).Namespace(AstraDSNamespace).List(ctx, listOpts)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion storage_drivers/astrads/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import (
"time"

"github.com/RoaringBitmap/roaring"
"k8s.io/client-go/kubernetes"
)

//go:generate mockgen -destination=../../../mocks/mock_storage_drivers/mock_astrads/mock_api.go github.com/netapp/trident/storage_drivers/astrads/api AstraDS

type AstraDS interface {
Init(context.Context, string, string, string) (*Cluster, string, error)
Init(context.Context, string, string, []byte) (*Cluster, string, error)
KubeClient() *kubernetes.Clientset
Cluster(context.Context, string) (*Cluster, error)
Clusters(context.Context) ([]*Cluster, error)

Volumes(context.Context) ([]*Volume, error)
Volume(context.Context, string) (*Volume, error)
Expand Down
8 changes: 7 additions & 1 deletion storage_drivers/astrads/astrads.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package astrads

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -165,10 +166,15 @@ func (d *StorageDriver) Initialize(
return errors.New("cluster not specified in backend config")
}

kubeConfigBytes, err := base64.StdEncoding.DecodeString(d.Config.Kubeconfig)
if err != nil {
return fmt.Errorf("kubeconfig is not base64 encoded: %v", err)
}

if d.API == nil {
d.API = api.NewClient()
}
d.cluster, d.kubeSystemUUID, err = d.API.Init(ctx, d.Config.Namespace, d.Config.Kubeconfig, d.Config.Cluster)
d.cluster, d.kubeSystemUUID, err = d.API.Init(ctx, d.Config.Namespace, d.Config.Cluster, kubeConfigBytes)
if err != nil {
return fmt.Errorf("error initializing %s API client; %v", d.Name(), err)
}
Expand Down
33 changes: 17 additions & 16 deletions storage_drivers/astrads/astrads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (

const (
BackendName = "myADSBackend"
Kubeconfig = "ZmFrZS1rdWJlY29uZmln"
Kubeconfig = "fake-kubeconfig"
KubeconfigB64 = "ZmFrZS1rdWJlY29uZmln"
Cluster = "fake-cluster"
ClusterUUID = "deadbeef-d086-46cf-b08f-945945386d2a"
Namespace = "fake-namespace"
Expand Down Expand Up @@ -118,7 +119,7 @@ func newTestAstraDSDriver(mockAPI api.AstraDS) *StorageDriver {

config := drivers.AstraDSStorageDriverConfig{
CommonStorageDriverConfig: mutableCommonConfig,
Kubeconfig: Kubeconfig,
Kubeconfig: KubeconfigB64,
Cluster: Cluster,
Namespace: Namespace,
NfsMountOptions: "vers=4.1",
Expand Down Expand Up @@ -308,7 +309,7 @@ func TestInitialize_WithoutSecrets(t *testing.T) {
API: mockAPI,
}

mockAPI.EXPECT().Init(ctx, Namespace, Kubeconfig, Cluster).Return(adsCluster, KubeSystemUUID, nil).Times(1)
mockAPI.EXPECT().Init(ctx, Namespace, Cluster, []byte(Kubeconfig)).Return(adsCluster, KubeSystemUUID, nil).Times(1)
mockAPI.EXPECT().QosPolicies(ctx).Return(qosPolicies, nil).Times(1)
mockAPI.EXPECT().ExportPolicyExists(ctx, FakeExportPolicy).Return(true, staticExportPolicy, nil).Times(1)

Expand Down Expand Up @@ -345,7 +346,7 @@ func TestInitialize_WithSecrets(t *testing.T) {
}`

secrets := map[string]string{
"kubeconfig": Kubeconfig,
"kubeconfig": KubeconfigB64,
}

mockCtrl := gomock.NewController(t)
Expand All @@ -355,7 +356,7 @@ func TestInitialize_WithSecrets(t *testing.T) {
API: mockAPI,
}

mockAPI.EXPECT().Init(ctx, Namespace, Kubeconfig, Cluster).Return(adsCluster, KubeSystemUUID, nil).Times(1)
mockAPI.EXPECT().Init(ctx, Namespace, Cluster, []byte(Kubeconfig)).Return(adsCluster, KubeSystemUUID, nil).Times(1)
mockAPI.EXPECT().QosPolicies(ctx).Return(qosPolicies, nil).Times(1)
mockAPI.EXPECT().ExportPolicyExists(ctx, FakeExportPolicy).Return(true, staticExportPolicy, nil).Times(1)

Expand All @@ -364,7 +365,7 @@ func TestInitialize_WithSecrets(t *testing.T) {
assert.NoError(t, result, "initialize failed")
assert.NotNil(t, driver.Config, "config is nil")
assert.Equal(t, adsCluster, driver.cluster)
assert.Equal(t, Kubeconfig, driver.Config.Kubeconfig)
assert.Equal(t, KubeconfigB64, driver.Config.Kubeconfig)
assert.Equal(t, KubeSystemUUID, driver.kubeSystemUUID)
assert.Equal(t, ClusterUUID, driver.Config.ClusterUUID)
assert.Equal(t, KubeSystemUUID, driver.Config.KubeSystemUUID)
Expand Down Expand Up @@ -393,7 +394,7 @@ func TestInitialize_WithInvalidSecrets(t *testing.T) {
}`

secrets := map[string]string{
"KubeConfig": Kubeconfig, // valid key is "kubeconfig"
"KubeConfig": KubeconfigB64, // valid key is "kubeconfig"
}

mockCtrl := gomock.NewController(t)
Expand Down Expand Up @@ -555,7 +556,7 @@ func TestInitialize_APIInitError(t *testing.T) {
API: mockAPI,
}

mockAPI.EXPECT().Init(ctx, Namespace, Kubeconfig, Cluster).Return(nil, "", errFailed).Times(1)
mockAPI.EXPECT().Init(ctx, Namespace, Cluster, []byte(Kubeconfig)).Return(nil, "", errFailed).Times(1)

result := driver.Initialize(ctx, tridentconfig.ContextCSI, configJSON, commonConfig, map[string]string{}, BackendUUID)

Expand Down Expand Up @@ -599,7 +600,7 @@ func TestInitialize_InvalidStoragePrefix(t *testing.T) {
API: mockAPI,
}

mockAPI.EXPECT().Init(ctx, Namespace, Kubeconfig, Cluster).Return(adsCluster, KubeSystemUUID, nil).Times(1)
mockAPI.EXPECT().Init(ctx, Namespace, Cluster, []byte(Kubeconfig)).Return(adsCluster, KubeSystemUUID, nil).Times(1)

result := driver.Initialize(ctx, tridentconfig.ContextCSI, configJSON, badCommonConfig, map[string]string{}, BackendUUID)

Expand Down Expand Up @@ -641,7 +642,7 @@ func TestInitialize_InvalidExportPolicyMode(t *testing.T) {
API: mockAPI,
}

mockAPI.EXPECT().Init(ctx, Namespace, Kubeconfig, Cluster).Return(adsCluster, KubeSystemUUID, nil).Times(1)
mockAPI.EXPECT().Init(ctx, Namespace, Cluster, []byte(Kubeconfig)).Return(adsCluster, KubeSystemUUID, nil).Times(1)

result := driver.Initialize(ctx, tridentconfig.ContextDocker, configJSON, badCommonConfig, map[string]string{}, BackendUUID)

Expand Down Expand Up @@ -692,7 +693,7 @@ func TestPopulateConfigurationDefaults_NoneSet(t *testing.T) {

config := &drivers.AstraDSStorageDriverConfig{
CommonStorageDriverConfig: commonConfig,
Kubeconfig: Kubeconfig,
Kubeconfig: KubeconfigB64,
Cluster: Cluster,
Namespace: Namespace,
AutoExportPolicy: false,
Expand Down Expand Up @@ -728,7 +729,7 @@ func TestPopulateConfigurationDefaults_AllSet(t *testing.T) {

config := &drivers.AstraDSStorageDriverConfig{
CommonStorageDriverConfig: commonConfig,
Kubeconfig: Kubeconfig,
Kubeconfig: KubeconfigB64,
Cluster: Cluster,
Namespace: Namespace,
NfsMountOptions: "vers=3",
Expand Down Expand Up @@ -776,7 +777,7 @@ func TestInitializeStoragePools_NoVirtualPools(t *testing.T) {
DebugTraceFlags: debugTraceFlags,
LimitVolumeSize: "123456789000",
},
Kubeconfig: Kubeconfig,
Kubeconfig: KubeconfigB64,
Cluster: Cluster,
Namespace: Namespace,
NfsMountOptions: "nfsvers=4.1",
Expand Down Expand Up @@ -846,7 +847,7 @@ func TestInitializeStoragePools_VirtualPools(t *testing.T) {
DebugTraceFlags: debugTraceFlags,
LimitVolumeSize: "123456789000",
},
Kubeconfig: Kubeconfig,
Kubeconfig: KubeconfigB64,
Cluster: Cluster,
Namespace: Namespace,
NfsMountOptions: "nfsvers=4.1",
Expand Down Expand Up @@ -2891,7 +2892,7 @@ func TestDestroyExportPolicy_SharedPoolPolicy(t *testing.T) {
DebugTraceFlags: debugTraceFlags,
LimitVolumeSize: "123456789000",
},
Kubeconfig: Kubeconfig,
Kubeconfig: KubeconfigB64,
Cluster: Cluster,
Namespace: Namespace,
NfsMountOptions: "nfsvers=4.1",
Expand Down Expand Up @@ -4633,7 +4634,7 @@ func TestStringAndGoString(t *testing.T) {
assert.Contains(t, result, "<REDACTED>", "ADS driver does not contain <REDACTED>")
assert.Contains(t, result, "API:<REDACTED>", "ADS driver does not redact API information")
assert.Contains(t, result, "Kubeconfig:<REDACTED>", "ADS driver does not redact Kubeconfig")
assert.NotContains(t, result, Kubeconfig, "ADS driver contains Kubeconfig")
assert.NotContains(t, result, KubeconfigB64, "ADS driver contains Kubeconfig")
}
}

Expand Down

0 comments on commit ddc922e

Please sign in to comment.