2020-01-24 16:26:56 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
keySeparator rune = '/'
|
|
|
|
labelSeparator string = ","
|
|
|
|
)
|
|
|
|
|
|
|
|
func k8sGetNodeLabels(nodeName string) (map[string]string, error) {
|
|
|
|
client := NewK8sClient()
|
|
|
|
node, err := client.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
2020-12-08 14:23:35 +00:00
|
|
|
return nil, fmt.Errorf("failed to get node %q information: %w", nodeName, err)
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return node.GetLabels(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTopologyFromDomainLabels returns the CSI topology map, determined from
|
|
|
|
// the domain labels and their values from the CO system
|
2020-07-19 12:21:03 +00:00
|
|
|
// Expects domainLabels in arg to be in the format "[prefix/]<name>,[prefix/]<name>,...",.
|
2020-01-24 16:26:56 +00:00
|
|
|
func GetTopologyFromDomainLabels(domainLabels, nodeName, driverName string) (map[string]string, error) {
|
|
|
|
if domainLabels == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// size checks on domain label prefix
|
|
|
|
topologyPrefix := strings.ToLower("topology." + driverName)
|
2020-07-21 05:10:13 +00:00
|
|
|
const lenLimit = 63
|
|
|
|
if len(topologyPrefix) > lenLimit {
|
2020-12-08 14:25:10 +00:00
|
|
|
return nil, fmt.Errorf("computed topology label prefix %q for node exceeds length limits", topologyPrefix)
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
// driverName is validated, and we are adding a lowercase "topology." to it, so no validation for conformance
|
|
|
|
|
|
|
|
// Convert passed in labels to a map, and check for uniqueness
|
|
|
|
labelsToRead := strings.SplitN(domainLabels, labelSeparator, -1)
|
2020-12-08 14:25:10 +00:00
|
|
|
DefaultLog("passed in node labels for processing: %+v", labelsToRead)
|
2020-01-24 16:26:56 +00:00
|
|
|
|
|
|
|
labelsIn := make(map[string]bool)
|
|
|
|
labelCount := 0
|
|
|
|
for _, label := range labelsToRead {
|
|
|
|
// as we read the labels from k8s, and check for missing labels,
|
|
|
|
// no label conformance checks here
|
|
|
|
if _, ok := labelsIn[label]; ok {
|
2020-12-08 14:25:10 +00:00
|
|
|
return nil, fmt.Errorf("duplicate label %q found in domain labels", label)
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
labelsIn[label] = true
|
|
|
|
labelCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeLabels, err := k8sGetNodeLabels(nodeName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine values for requested labels from node labels
|
|
|
|
domainMap := make(map[string]string)
|
|
|
|
found := 0
|
|
|
|
for key, value := range nodeLabels {
|
|
|
|
if _, ok := labelsIn[key]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// label found split name component and store value
|
|
|
|
nameIdx := strings.IndexRune(key, keySeparator)
|
|
|
|
domain := key[nameIdx+1:]
|
|
|
|
domainMap[domain] = value
|
|
|
|
labelsIn[key] = false
|
|
|
|
found++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure all labels are found
|
|
|
|
if found != labelCount {
|
|
|
|
missingLabels := []string{}
|
|
|
|
for key, missing := range labelsIn {
|
|
|
|
if missing {
|
|
|
|
missingLabels = append(missingLabels, key)
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 14:25:10 +00:00
|
|
|
return nil, fmt.Errorf("missing domain labels %v on node %q", missingLabels, nodeName)
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 14:25:10 +00:00
|
|
|
DefaultLog("list of domains processed: %+v", domainMap)
|
2020-01-24 16:26:56 +00:00
|
|
|
|
|
|
|
topology := make(map[string]string)
|
|
|
|
for domain, value := range domainMap {
|
|
|
|
topology[topologyPrefix+"/"+domain] = value
|
|
|
|
// TODO: when implementing domain takeover/giveback, enable a domain value that can remain pinned to the node
|
|
|
|
// topology["topology."+driverName+"/"+domain+"-pinned"] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return topology, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type topologySegment struct {
|
|
|
|
DomainLabel string `json:"domainLabel"`
|
|
|
|
DomainValue string `json:"value"`
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// TopologyConstrainedPool stores the pool name and a list of its associated topology domain values.
|
2020-01-24 16:26:56 +00:00
|
|
|
type TopologyConstrainedPool struct {
|
|
|
|
PoolName string `json:"poolName"`
|
2020-04-06 20:19:13 +00:00
|
|
|
DataPoolName string `json:"dataPool"`
|
2020-01-24 16:26:56 +00:00
|
|
|
DomainSegments []topologySegment `json:"domainSegments"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTopologyFromRequest extracts TopologyConstrainedPools and passed in accessibility constraints
|
2020-07-19 12:21:03 +00:00
|
|
|
// from a CSI CreateVolume request.
|
2021-06-25 12:15:08 +00:00
|
|
|
func GetTopologyFromRequest(
|
|
|
|
req *csi.CreateVolumeRequest) (*[]TopologyConstrainedPool, *csi.TopologyRequirement, error) {
|
2021-07-13 12:21:05 +00:00
|
|
|
var topologyPools []TopologyConstrainedPool
|
2020-01-24 16:26:56 +00:00
|
|
|
|
|
|
|
// check if parameters have pool configuration pertaining to topology
|
|
|
|
topologyPoolsStr := req.GetParameters()["topologyConstrainedPools"]
|
|
|
|
if topologyPoolsStr == "" {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if there are any accessibility requirements in the request
|
|
|
|
accessibilityRequirements := req.GetAccessibilityRequirements()
|
|
|
|
if accessibilityRequirements == nil {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract topology based pools configuration
|
|
|
|
err := json.Unmarshal([]byte(strings.Replace(topologyPoolsStr, "\n", " ", -1)), &topologyPools)
|
|
|
|
if err != nil {
|
2021-06-25 12:15:08 +00:00
|
|
|
return nil, nil, fmt.Errorf(
|
2021-07-10 10:45:11 +00:00
|
|
|
"failed to parse JSON encoded topology constrained pools parameter (%s): %w",
|
2021-06-25 12:15:08 +00:00
|
|
|
topologyPoolsStr,
|
|
|
|
err)
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &topologyPools, accessibilityRequirements, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchTopologyForPool returns the topology map, if the passed in pool matches any
|
2020-07-19 12:21:03 +00:00
|
|
|
// passed in accessibility constraints.
|
2020-01-24 16:26:56 +00:00
|
|
|
func MatchTopologyForPool(topologyPools *[]TopologyConstrainedPool,
|
|
|
|
accessibilityRequirements *csi.TopologyRequirement, poolName string) (map[string]string, error) {
|
|
|
|
var topologyPool []TopologyConstrainedPool
|
|
|
|
|
|
|
|
if topologyPools == nil || accessibilityRequirements == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the pool in the list of topology based pools
|
|
|
|
for _, value := range *topologyPools {
|
|
|
|
if value.PoolName == poolName {
|
|
|
|
topologyPool = append(topologyPool, value)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(topologyPool) == 0 {
|
|
|
|
return nil, fmt.Errorf("none of the configured topology pools (%+v) matched passed in pool name (%s)",
|
|
|
|
topologyPools, poolName)
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, topology, err := FindPoolAndTopology(&topologyPool, accessibilityRequirements)
|
2020-01-24 16:26:56 +00:00
|
|
|
|
|
|
|
return topology, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPoolAndTopology loops through passed in "topologyPools" and also related
|
|
|
|
// accessibility requirements, to determine which pool matches the requirement.
|
|
|
|
// The return variables are, image poolname, data poolname, and topology map of
|
2020-07-19 12:21:03 +00:00
|
|
|
// matched requirement.
|
2020-01-24 16:26:56 +00:00
|
|
|
func FindPoolAndTopology(topologyPools *[]TopologyConstrainedPool,
|
2020-04-06 20:19:13 +00:00
|
|
|
accessibilityRequirements *csi.TopologyRequirement) (string, string, map[string]string, error) {
|
2020-01-24 16:26:56 +00:00
|
|
|
if topologyPools == nil || accessibilityRequirements == nil {
|
2020-04-06 20:19:13 +00:00
|
|
|
return "", "", nil, nil
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// select pool that fits first topology constraint preferred requirements
|
|
|
|
for _, topology := range accessibilityRequirements.GetPreferred() {
|
2020-04-06 20:19:13 +00:00
|
|
|
topologyPool := matchPoolToTopology(topologyPools, topology)
|
|
|
|
if topologyPool.PoolName != "" {
|
|
|
|
return topologyPool.PoolName, topologyPool.DataPoolName, topology.GetSegments(), nil
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If preferred mismatches, check requisite for a fit
|
|
|
|
for _, topology := range accessibilityRequirements.GetRequisite() {
|
2020-04-06 20:19:13 +00:00
|
|
|
topologyPool := matchPoolToTopology(topologyPools, topology)
|
|
|
|
if topologyPool.PoolName != "" {
|
|
|
|
return topologyPool.PoolName, topologyPool.DataPoolName, topology.GetSegments(), nil
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
return "", "", nil, fmt.Errorf("none of the topology constrained pools matched requested "+
|
2020-01-24 16:26:56 +00:00
|
|
|
"topology constraints : pools (%+v) requested topology (%+v)",
|
|
|
|
*topologyPools, *accessibilityRequirements)
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchPoolToTopology loops through passed in pools, and for each pool checks if all
|
|
|
|
// requested topology segments are present and match the request, returning the first pool
|
2020-07-19 12:21:03 +00:00
|
|
|
// that hence matches (or an empty string if none match).
|
2020-04-06 20:19:13 +00:00
|
|
|
func matchPoolToTopology(topologyPools *[]TopologyConstrainedPool, topology *csi.Topology) TopologyConstrainedPool {
|
2020-01-24 16:26:56 +00:00
|
|
|
domainMap := extractDomainsFromlabels(topology)
|
|
|
|
|
|
|
|
// check if any pool matches all the domain keys and values
|
|
|
|
for _, topologyPool := range *topologyPools {
|
|
|
|
mismatch := false
|
|
|
|
// match all pool topology labels to requested topology
|
|
|
|
for _, segment := range topologyPool.DomainSegments {
|
|
|
|
if domainValue, ok := domainMap[segment.DomainLabel]; !ok || domainValue != segment.DomainValue {
|
|
|
|
mismatch = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if mismatch {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
return topologyPool
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
return TopologyConstrainedPool{}
|
2020-01-24 16:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extractDomainsFromlabels returns the domain name map, from passed in domain segments,
|
2020-07-19 12:21:03 +00:00
|
|
|
// which is of the form [prefix/]<name>.
|
2020-01-24 16:26:56 +00:00
|
|
|
func extractDomainsFromlabels(topology *csi.Topology) map[string]string {
|
|
|
|
domainMap := make(map[string]string)
|
|
|
|
for domainKey, value := range topology.GetSegments() {
|
|
|
|
domainIdx := strings.IndexRune(domainKey, keySeparator)
|
|
|
|
domain := domainKey[domainIdx+1:]
|
|
|
|
domainMap[domain] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return domainMap
|
|
|
|
}
|