Skip to content

Commit

Permalink
Added timeout to Moodle Operator controller
Browse files Browse the repository at this point in the history
Partially-Fixes: #12

The timeout value needs to be made configurable.
  • Loading branch information
devdattakulkarni committed Feb 20, 2019
1 parent a979423 commit 2af9f6a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
12 changes: 9 additions & 3 deletions moodle/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ func (c *Controller) syncHandler(key string) error {
var supportedPlugins, unsupportedPlugins []string
initialDeployment := c.isInitialDeployment(foo)

if foo.Status.Status != "" && foo.Status.Status == "Moodle Pod Timeout" {
return nil
}

if initialDeployment {

MOODLE_PORT = MOODLE_PORT_BASE
Expand All @@ -327,15 +331,16 @@ func (c *Controller) syncHandler(key string) error {

serviceURL, podName, secretName, unsupportedPlugins, erredPlugins, err := c.deployMoodle(foo)

var correctlyInstalledPlugins []string
if err != nil {
status = "Error"
status = err.Error()
} else {
status = "Ready"
url = "http://" + serviceURL
fmt.Printf("Moodle URL:%s\n", url)
correctlyInstalledPlugins = c.getDiff(plugins, erredPlugins)
}

correctlyInstalledPlugins := c.getDiff(plugins, erredPlugins)
c.updateMoodleStatus(foo, podName, secretName, status, url, &correctlyInstalledPlugins, &unsupportedPlugins)
c.recorder.Event(foo, corev1.EventTypeNormal, SuccessSynced, MessageResourceSynced)
} else {
Expand All @@ -355,7 +360,8 @@ func (c *Controller) syncHandler(key string) error {
fmt.Printf("Moodle custom resource %s did not change. No plugin installed.\n", moodleName)
}
}
return err
// Returning nil so that the controller does not try to sync the same Moodle instance.
return nil
}

func appendList(source, destination []string) []string {
Expand Down
29 changes: 23 additions & 6 deletions moodle/moodle.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strings"
"time"
"math/rand"
"errors"
)

var (
Expand Down Expand Up @@ -49,7 +50,6 @@ func (c *Controller) deployMoodle(foo *operatorv1.Moodle) (string, string, strin
err, moodlePodName, secretName := c.createDeployment(foo)

if err != nil {
panic(err)
return serviceURIToReturn, moodlePodName, secretName, unsupportedPlugins, erredPlugins, err
}

Expand Down Expand Up @@ -564,12 +564,18 @@ func (c *Controller) createDeployment(foo *operatorv1.Moodle) (error, string, st
result, err := deploymentsClient.Create(deployment)
if err != nil {
panic(err)
return err, "", ""
}
fmt.Printf("Created deployment %q.\n", result.GetObjectMeta().GetName())

moodlePodName := c.waitForPod(foo)
moodlePodName, podReady := c.waitForPod(foo)

return nil, moodlePodName, secretName
if podReady {
return nil, moodlePodName, secretName
} else {
err1 := errors.New("Moodle Pod Timeout")
return err1, moodlePodName, secretName
}
}

func (c *Controller) createSecret(foo *operatorv1.Moodle, adminPassword string) string {
Expand Down Expand Up @@ -747,12 +753,14 @@ func (c *Controller) isInitialDeployment(foo *operatorv1.Moodle) bool {
}
}

func (c *Controller) waitForPod(foo *operatorv1.Moodle) string {
func (c *Controller) waitForPod(foo *operatorv1.Moodle) (string, bool) {
var podName string
deploymentName := foo.Name
namespace := getNamespace(foo)
// Check if Postgres Pod is ready or not
podReady := false
podTimeoutCount := 0
TIMEOUT_COUNT := 150 // 10 minutes; this should be made configurable
for {
pods := c.getPods(namespace, deploymentName)
for _, d := range pods.Items {
Expand Down Expand Up @@ -783,10 +791,19 @@ func (c *Controller) waitForPod(foo *operatorv1.Moodle) string {
} else {
fmt.Println("Waiting for Moodle Pod to get ready.")
time.Sleep(time.Second * 4)
podTimeoutCount = podTimeoutCount + 1
if podTimeoutCount >= TIMEOUT_COUNT {
podReady = false
break
}
}
}
fmt.Println("Pod is ready.")
return podName
if podReady {
fmt.Println("Pod is ready.")
} else {
fmt.Println("Pod timeout")
}
return podName, podReady
}

func (c *Controller) getPods(namespace, deploymentName string) *apiv1.PodList {
Expand Down

0 comments on commit 2af9f6a

Please sign in to comment.