Skip to content

Commit

Permalink
Read zone information from PowerFlex secret (#810)
Browse files Browse the repository at this point in the history
* start tests

* Implement zone info extraction

* Add new err checks and tests

* Fix lint errors

* Add method description

* Address comments

---------

Co-authored-by: Aaron Tye <[email protected]>
  • Loading branch information
EvgenyUglov and atye authored Dec 5, 2024
1 parent 336e2f0 commit e87631b
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pkg/drivers/powerflex.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,56 @@ func ModifyPowerflexCR(yamlString string, cr csmv1.ContainerStorageModule, fileT
}
return yamlString
}

// ExtractZonesFromSecret - Reads the array config secret and returns the zone label information
func ExtractZonesFromSecret(ctx context.Context, kube client.Client, namespace string, secret string) (map[string]string, error) {
log := logger.GetLogger(ctx)

arraySecret, err := utils.GetSecret(ctx, secret, namespace, kube)
if err != nil {
return nil, fmt.Errorf("reading secret [%s] error [%s]", secret, err)
}

type StorageArrayConfig struct {
SystemID string `json:"systemId"`
Zone struct {
Name string `json:"name"`
LabelKey string `json:"labelKey"`
} `json:"zone"`
}

data := arraySecret.Data
configBytes := data["config"]
zonesMapData := make(map[string]string)

if string(configBytes) != "" {
yamlConfig := make([]StorageArrayConfig, 0)
configs, err := yaml.JSONToYAML(configBytes)
if err != nil {
return nil, fmt.Errorf("unable to parse multi-array configuration[%v]", err)
}
err = yaml.Unmarshal(configs, &yamlConfig)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal multi-array configuration[%v]", err)
}

for _, configParam := range yamlConfig {
if configParam.SystemID == "" {
return nil, fmt.Errorf("invalid value for SystemID")
}
if configParam.Zone.LabelKey != "" {
if configParam.Zone.Name != "" {
zonesMapData[configParam.Zone.LabelKey] = configParam.Zone.Name
log.Infof("Zoning information configured for systemID %s: %v ", configParam.SystemID, zonesMapData)
}
} else {
log.Info("Zoning information not found in the array config. Continue with topology-unaware driver installation mode")
return zonesMapData, nil
}
}
} else {
return nil, fmt.Errorf("Array details are not provided in vxflexos-config secret")
}

return zonesMapData, nil
}
126 changes: 126 additions & 0 deletions pkg/drivers/powerflex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"github.com/dell/csm-operator/tests/shared/crclient"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var (
Expand Down Expand Up @@ -194,3 +196,127 @@ func TestModifyPowerflexCR(t *testing.T) {
})
}
}

func TestExtractZonesFromSecret(t *testing.T) {
emptyConfigData := ``
invalidConfigData := `
- username: "admin"
-
`
dataWithNoSystemID := `
- username: "admin"
password: "password"
endpoint: "https://127.0.0.2"
skipCertificateValidation: true
mdm: "10.0.0.3,10.0.0.4"
`
dataWithZone := `
- username: "admin"
password: "password"
systemID: "2b11bb111111bb1b"
endpoint: "https://127.0.0.2"
skipCertificateValidation: true
mdm: "10.0.0.3,10.0.0.4"
zone:
name: "US-EAST"
labelKey: "zone.csi-vxflexos.dellemc.com"
`
dataWithoutZone := `
- username: "admin"
password: "password"
systemID: "2b11bb111111bb1b"
endpoint: "https://127.0.0.2"
skipCertificateValidation: true
mdm: "10.0.0.3,10.0.0.4"
`
ctx := context.Background()
tests := map[string]func() (client.WithWatch, map[string]string, string, bool){
"success with zone": func() (client.WithWatch, map[string]string, string, bool) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "vxflexos-config",
Namespace: "vxflexos",
},
Data: map[string][]byte{
"config": []byte(dataWithZone),
},
}

client := fake.NewClientBuilder().WithObjects(secret).Build()
return client, map[string]string{"zone.csi-vxflexos.dellemc.com": "US-EAST"}, "vxflexos-config", false
},
"success no zone": func() (client.WithWatch, map[string]string, string, bool) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "vxflexos-config",
Namespace: "vxflexos",
},
Data: map[string][]byte{
"config": []byte(dataWithoutZone),
},
}

client := fake.NewClientBuilder().WithObjects(secret).Build()
return client, map[string]string{}, "vxflexos-config", false
},
"error getting secret": func() (client.WithWatch, map[string]string, string, bool) {
client := fake.NewClientBuilder().Build()
return client, nil, "vxflexos-not-found", true
},
"error parsing empty secret": func() (client.WithWatch, map[string]string, string, bool) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "vxflexos-config",
Namespace: "vxflexos",
},
Data: map[string][]byte{
"config": []byte(emptyConfigData),
},
}

client := fake.NewClientBuilder().WithObjects(secret).Build()
return client, nil, "vxflexos-config", true
},
"error with no system id": func() (client.WithWatch, map[string]string, string, bool) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "vxflexos-config",
Namespace: "vxflexos",
},
Data: map[string][]byte{
"config": []byte(dataWithNoSystemID),
},
}

client := fake.NewClientBuilder().WithObjects(secret).Build()
return client, nil, "vxflexos-config", true
},
"error unmarshaling config": func() (client.WithWatch, map[string]string, string, bool) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "vxflexos-config",
Namespace: "vxflexos",
},
Data: map[string][]byte{
"config": []byte(invalidConfigData),
},
}

client := fake.NewClientBuilder().WithObjects(secret).Build()
return client, nil, "vxflexos-config", true
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
client, wantZones, secret, wantErr := tc()
zones, err := ExtractZonesFromSecret(ctx, client, "vxflexos", secret)
if wantErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, wantZones, zones)
}
})
}
}

0 comments on commit e87631b

Please sign in to comment.