func GetConfigString()

in pkg/ndbconfig/config_generators.go [91:199]


func GetConfigString(ndb *v1.NdbCluster, oldConfigSummary *ConfigSummary) (string, error) {

	var (
		// Variables that keep track of the first free mgmd/data node id and api nodeId
		ndbdMgmdStartNodeId = 1
		apiStartNodeId      = constants.NdbNodeTypeAPIStartNodeId

		// newDataNodeStartId tracks the starting nodeId of the new
		// data nodes during an online add data node scenario
		newDataNodeStartId = 0
	)

	if oldConfigSummary != nil && oldConfigSummary.NumOfDataNodes < ndb.Spec.DataNode.NodeCount {
		// Data Nodes are being added to the configuration.
		newDataNodeStartId = int(ndb.GetManagementNodeCount() + oldConfigSummary.NumOfDataNodes + 1)
	}

	tmpl := template.New("config.ini")
	tmpl.Funcs(template.FuncMap{
		// GetNodeIds returns an array of node ids for the given node type
		"GetNodeIds": func(nodeType string) []int {
			var startNodeId *int
			var numberOfNodes int32
			switch nodeType {
			case constants.NdbNodeTypeMgmd:
				startNodeId = &ndbdMgmdStartNodeId
				numberOfNodes = ndb.GetManagementNodeCount()
			case constants.NdbNodeTypeNdbmtd:
				startNodeId = &ndbdMgmdStartNodeId
				numberOfNodes = ndb.Spec.DataNode.NodeCount
			case constants.NdbNodeTypeMySQLD:
				startNodeId = &apiStartNodeId
				numberOfNodes = GetNumOfSectionsRequiredForMySQLServers(ndb)
			case constants.NdbNodeTypeAPI:
				startNodeId = &apiStartNodeId
				numberOfNodes = ndb.Spec.FreeAPISlots
			default:
				panic("Unrecognised node type")
			}
			// generate nodeIds based on start node id and number of nodes
			nodeIds := make([]int, numberOfNodes)
			for i := range nodeIds {
				nodeIds[i] = *startNodeId
				*startNodeId++
			}
			return nodeIds
		},
		"GetMySQLServerNodeIds": func() map[int]int {
			startNodeId := &apiStartNodeId
			numberOfNodeIds := GetNumOfSectionsRequiredForMySQLServers(ndb)
			nodeIdToPodIdx := make(map[int]int)
			ndbConnectionPoolSize := ndb.GetMySQLServerConnectionPoolSize()
			podIdx := 0
			for i := 0; int32(i) < numberOfNodeIds; i = i + int(ndbConnectionPoolSize) {
				for j := 0; j < int(ndbConnectionPoolSize); j++ {
					nodeIdToPodIdx[*startNodeId] = podIdx
					*startNodeId++
				}
				podIdx++
			}

			return nodeIdToPodIdx
		},
		"GetDataDir": func() string { return constants.DataDir + "/data" },
		"IsNewDataNode": func(nodeId int) bool {
			return newDataNodeStartId != 0 && nodeId >= newDataNodeStartId
		},
		"GetConfigVersion": func() int32 {
			if oldConfigSummary == nil {
				// First version of the management config based on newly added NdbCluster spec.
				return 1
			} else {
				// Bump up the config version for every change.
				return oldConfigSummary.MySQLClusterConfigVersion + 1
			}
		},
		"GetHostnameSuffix": func() string {
			// If the K8s Cluster domain can be found, generate the hostname suffix of form :
			// '<namespace>.svc.<k8s-cluster-domain>' or else, simply use the namespace as the suffix.
			// Deduce K8s cluster domain suffix by looking up the kubernetes server's CNAME.
			if k8sCname, err := net.LookupCNAME("kubernetes.default.svc"); err != nil {
				klog.Warning("K8s Cluster domain lookup failed :", err.Error())
				klog.Warning("Using partial subdomain as Hostnames in management configuration")
				return ndb.Namespace
			} else {
				// Found the FQDN of form "kubernetes.default.svc.<k8s-cluster-domain>."
				// Extract the required parts and append them to the suffix
				return ndb.Namespace + k8sCname[len("kubernetes.default"):len(k8sCname)-1]
			}
		},
		"NdbNodeTypeMgmd":               func() string { return constants.NdbNodeTypeMgmd },
		"NdbNodeTypeNdbmtd":             func() string { return constants.NdbNodeTypeNdbmtd },
		"NdbNodeTypeMySQLD":             func() string { return constants.NdbNodeTypeMySQLD },
		"NdbNodeTypeAPI":                func() string { return constants.NdbNodeTypeAPI },
		"NdbOperatorDedicatedAPINodeId": func() int { return constants.NdbOperatorDedicatedAPINodeId },
	})

	if _, err := tmpl.Parse(mgmtConfigTmpl); err != nil {
		// panic to discover any parsing errors during development
		panic("Failed to parse mgmt config template : " + err.Error())
	}

	var configIni bytes.Buffer
	if err := tmpl.Execute(&configIni, ndb); err != nil {
		return "", err
	}

	return configIni.String(), nil
}