func()

in pkg/controllers/mysqld_statefulset_controller.go [197:258]


func (mssc *mysqldStatefulSetController) reconcileRootUser(ctx context.Context, sc *SyncContext) syncResult {
	mysqldSfset := sc.mysqldSfset
	if mysqldSfset == nil {
		// Nothing to do as the MySQL Servers do not exist
		return continueProcessing()
	}

	// Get the last applied NdbCluster Generation to the Root User
	annotations := mysqldSfset.GetAnnotations()
	var rootUserGen int64
	if genString, exists := annotations[rootUserGeneration]; exists {
		rootUserGen, _ = strconv.ParseInt(genString, 10, 64)
	}

	recentNdbGen := sc.configSummary.NdbClusterGeneration
	if rootUserGen == recentNdbGen {
		// The Root user spec is up-to-date
		return continueProcessing()
	}

	// The root user needs be created or updated
	nc := sc.ndb
	newRootHost := nc.Spec.MysqlNode.RootHost

	// Extract ndb operator mysql user password.
	secretClient := NewMySQLUserPasswordSecretInterface(sc.kubeClientset())
	operatorSecretName := resources.GetMySQLNDBOperatorPasswordSecretName(nc)
	operatorPassword, err := secretClient.ExtractPassword(ctx, mysqldSfset.Namespace, operatorSecretName)
	if err != nil {
		return errorWhileProcessing(err)
	}

	if existingRootHost, exists := annotations[rootHost]; !exists {
		// Root user doesn't exist yet - create it.
		// Extract root user password.
		secretName, _ := resources.GetMySQLRootPasswordSecretName(nc)
		rootPassword, err := secretClient.ExtractPassword(ctx, mysqldSfset.Namespace, secretName)
		if err != nil {
			return errorWhileProcessing(err)
		}

		// Create Root user
		if err = mysqlclient.CreateRootUserIfNotExist(mysqldSfset, newRootHost, rootPassword, operatorPassword); err != nil {
			klog.Errorf("Failed to create root user")
			return errorWhileProcessing(err)
		}
	} else if newRootHost != existingRootHost {
		// Root Host needs to be updated
		if err := mysqlclient.UpdateRootUser(mysqldSfset, existingRootHost, newRootHost, operatorPassword); err != nil {
			klog.Errorf("Failed to update root user")
			return errorWhileProcessing(err)
		}
	}

	// Successfully applied the changes to root user
	// Patch the StatefulSet to mark the changes as done
	updatedMysqldSfset := mysqldSfset.DeepCopy()
	annotations = updatedMysqldSfset.Annotations
	annotations[rootHost] = newRootHost
	annotations[rootUserGeneration] = fmt.Sprintf("%d", recentNdbGen)
	return mssc.patchStatefulSet(ctx, mysqldSfset, updatedMysqldSfset)
}