// handleReplsetInit runs the k8s-mongodb-initiator from within the first running pod's mongod container.
// This must be ran from within the running container to utilise the MongoDB Localhost Exeception.
//
// See: https://docs.mongodb.com/manual/core/security-users/#localhost-exception
//
handleReplsetInit runs the k8s-mongodb-initiator from within the first running pod's mongod container.
This must be ran from within the running container to utilise the MongoDB Localhost Exeception.

docs.mongodb.com/manual/core/security-users/#localhost-exception
// handleReplsetInit runs the k8s - mongodb - initiator from within the first running pod ' s mongod container . // This must be ran from within the running container to utilise the MongoDB Localhost Exeception . // // See : https : // docs . mongodb . com / manual / core / security - users / #localhost - exception //
func (r *ReconcilePerconaServerMongoDB) handleReplsetInit(m *api.PerconaServerMongoDB, replset *api.ReplsetSpec, pods []corev1.Pod) error {
	for _, pod := range pods {
		if !isMongodPod(pod) || !isContainerAndPodRunning(pod, "mongod") || !isPodReady(pod) {
			continue
		}
		log.Info("Initiating replset", "replset", replset.Name, "pod", pod.Name)
		host, err := psmdb.MongoHost(r.client, m, replset, pod)
		if err != nil {
			return fmt.Errorf("get host for the pod %s: %v", pod.Name, err)
		}
		cmd := []string{
			"sh", "-c",
			fmt.Sprintf(
				`
				cat <<-EOF | mongo 
				rs.initiate(
					{
						_id: '%s',
						version: 1,
						members: [
							{ _id: 0, host: "%s" },
						]
					}
				)
				EOF
			`, replset.Name, host),
		}
		var errb, outb bytes.Buffer
		err = r.clientcmd.Exec(&pod, "mongod", cmd, nil, &outb, &errb, false)
		if err != nil {
			return fmt.Errorf("exec rs.initiate: %v / %s / %s", err, outb.String(), errb.String())
		}
		time.Sleep(time.Second * 5)
		cmd[2] = fmt.Sprintf(
			`
			cat <<-EOF | mongo 
			%s
			EOF
			`, mongoInitAdminUser)
		errb.Reset()
		outb.Reset()
		err = r.clientcmd.Exec(&pod, "mongod", cmd, nil, &outb, &errb, false)
		if err != nil {
			return fmt.Errorf("exec add admin user: %v / %s / %s", err, outb.String(), errb.String())
		}
		cmd[2] = fmt.Sprintf(
			`
			cat <<-EOF | mongo "mongodb://${MONGODB_USER_ADMIN_USER}:${MONGODB_USER_ADMIN_PASSWORD}@%s/?replicaSet=%s"
			%s
			EOF
			`, host, replset.Name, mongoInitUsers)
		errb.Reset()
		outb.Reset()
		err = r.clientcmd.Exec(&pod, "mongod", cmd, nil, &outb, &errb, false)
		if err != nil {
			return fmt.Errorf("exec add users: %v / %s / %s", err, outb.String(), errb.String())
		}
		return nil
	}
	return errNoRunningMongodContainers
}
func ( r * ReconcilePerconaServerMongoDB ) handleReplsetInit ( m * api . PerconaServerMongoDB , replset * api . ReplsetSpec , pods [ ] corev1 . Pod ) error { for _ , pod := range pods { if ! isMongodPod ( pod ) || ! isContainerAndPodRunning ( pod , " " ) || ! isPodReady ( pod ) { continue } log . Info ( " " , " " , replset . Name , " " , pod . Name ) host , err := psmdb . MongoHost ( r . client , m , replset , pod ) if err != nil { return fmt . Errorf ( " " , pod . Name , err ) } cmd := [ ] string { " " , " " , fmt . Sprintf ( `
				cat <<-EOF | mongo 
				rs.initiate(
					{
						_id: '%s',
						version: 1,
						members: [
							{ _id: 0, host: "%s" },
						]
					}
				)
				EOF
			` , replset . Name , host ) , } var errb , outb bytes . Buffer err = r . clientcmd . Exec ( & pod , " " , cmd , nil , & outb , & errb , false ) if err != nil { return fmt . Errorf ( " " , err , outb . String ( ) , errb . String ( ) ) } time . Sleep ( time . Second * 5 ) cmd [ 2 ] = fmt . Sprintf ( `
			cat <<-EOF | mongo 
			%s
			EOF
			` , mongoInitAdminUser ) errb . Reset ( ) outb . Reset ( ) err = r . clientcmd . Exec ( & pod , " " , cmd , nil , & outb , & errb , false ) if err != nil { return fmt . Errorf ( " " , err , outb . String ( ) , errb . String ( ) ) } cmd [ 2 ] = fmt . Sprintf ( `
			cat <<-EOF | mongo "mongodb://${MONGODB_USER_ADMIN_USER}:${MONGODB_USER_ADMIN_PASSWORD}@%s/?replicaSet=%s"
			%s
			EOF
			` , host , replset . Name , mongoInitUsers ) errb . Reset ( ) outb . Reset ( ) err = r . clientcmd . Exec ( & pod , " " , cmd , nil , & outb , & errb , false ) if err != nil { return fmt . Errorf ( " " , err , outb . String ( ) , errb . String ( ) ) } return nil } return errNoRunningMongodContainers }
handleReplsetInit runs the k8s-mongodb-initiator from within the first running pod's mongod container.
handleReplsetInit runs the k8s - mongodb - initiator from within the first running pod ' s mongod container .