Skip to content

Commit

Permalink
added code changes for protocol topological awarness
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit Roushan committed Sep 2, 2021
1 parent c78fe52 commit 82d34c0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
80 changes: 59 additions & 21 deletions src/csi/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"utils"
"utils/log"
"utils/k8sutils"
)

const (
Expand Down Expand Up @@ -122,26 +123,10 @@ func newBackend(backendName string, config map[string]interface{}) (*Backend, er
return nil, errors.New("parameters must be configured for backend")
}

supportedTopologies := make([]map[string]string, 0)
if topologies, exist := config[SupportedTopologies]; exist {
topologyArray, ok := topologies.([]interface{})
if !ok {
return nil, errors.New("invalid supported topologies configuration")
}
for _, topologyArrElem := range topologyArray {
topologyMap, ok := topologyArrElem.(map[string]interface{})
if !ok {
return nil, errors.New("invalid supported topologies configuration")
}
tempMap := make(map[string]string, 0)
for topologyKey, value := range topologyMap {
if topologyValue, ok := value.(string); ok {
tempMap[topologyKey] = topologyValue
}
}

supportedTopologies = append(supportedTopologies, tempMap)
}
// Get supported topologies for backend
supportedTopologies, err := getSupportedTopologies(config)
if err != nil {
return nil, err
}

plugin := plugin.GetPlugin(storage)
Expand Down Expand Up @@ -174,6 +159,47 @@ func newBackend(backendName string, config map[string]interface{}) (*Backend, er
}, nil
}

func getSupportedTopologies(config map[string]interface{}) ([]map[string]string, error) {
supportedTopologies := make([]map[string]string, 0)
if topologies, exist := config[SupportedTopologies]; exist {
// populate configured topologies
topologyArray, ok := topologies.([]interface{})
if !ok {
return nil, errors.New("invalid supported topologies configuration")
}
for _, topologyArrElem := range topologyArray {
topologyMap, ok := topologyArrElem.(map[string]interface{})
if !ok {
return nil, errors.New("invalid supported topologies configuration")
}
tempMap := make(map[string]string, 0)
for topologyKey, value := range topologyMap {
if topologyValue, ok := value.(string); ok {
tempMap[topologyKey] = topologyValue
}
}

supportedTopologies = append(supportedTopologies, tempMap)
}
}

return supportedTopologies, nil
}

// addProtocolTopology add up protocol specific topological support
func addProtocolTopology(backend *Backend, driverName string){
proto, protocolAvailable := backend.Parameters["protocol"]
if protocol, isString := proto.(string); protocolAvailable && isString {
backend.SupportedTopologies = append(backend.SupportedTopologies, map[string]string{
k8sutils.TopologyPrefix + "/protocol." + protocol: driverName,
})
return
}

log.Warningf("supported topology for protocol may not work as protocol is miss configured " +
"in backend configuration")
}

func analyzeBackend(config map[string]interface{}) (*Backend, error) {
backendName, exist := config["name"].(string)
if !exist {
Expand Down Expand Up @@ -245,7 +271,7 @@ func updateReplicaBackends() {
}
}

func RegisterBackend(backendConfigs []map[string]interface{}, keepLogin bool) error {
func RegisterBackend(backendConfigs []map[string]interface{}, keepLogin bool, driverName string) error {
for _, i := range backendConfigs {
backend, err := analyzeBackend(i)
if err != nil {
Expand All @@ -259,6 +285,18 @@ func RegisterBackend(backendConfigs []map[string]interface{}, keepLogin bool) er
return err
}

// Note: Protocol is considered as special topological parameter. The protocol topology
// is populated internally by plugin using protocol name.
// If configured protocol for backend is "iscsi", CSI plugin internally add
// topology.kubernetes.io/protocol.iscsi = csi.huawei.com in supportedTopologies.
//
// Now users can opt to provision volumes based on protocol by
// 1. Labeling kubernetes nodes with protocol specific label (ie topology.kubernetes.io/protocol.iscsi = csi.huawei.com)
// 2. Configure topology support in plugin
// 3. Configure protocol topology in allowedTopologies fo Storage class
// addProtocolTopology is called after backend plugin init as init takes care of protocol validation
addProtocolTopology(backend, driverName)

csiBackends[backend.Name] = backend
}

Expand Down
4 changes: 2 additions & 2 deletions src/csi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ func main() {
}()

if *controller || *controllerFlagFile != "" {
err := backend.RegisterBackend(config.Backends, true)
err := backend.RegisterBackend(config.Backends, true, *driverName)
if err != nil {
log.Fatalf("Register backends error: %v", err)
}

go updateBackendCapabilities()
} else {
err := backend.RegisterBackend(config.Backends, false)
err := backend.RegisterBackend(config.Backends, false, *driverName)
if err != nil {
log.Fatalf("Register backends error: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/k8sutils/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
)

const (
topologyRegx = "topology.kubernetes.io/*"
TopologyPrefix = "topology.kubernetes.io"
topologyRegx = TopologyPrefix + "/.*"
)

type Interface interface {
Expand Down

0 comments on commit 82d34c0

Please sign in to comment.