diff --git a/controllers/nginx/nginx.tmpl b/controllers/nginx/nginx.tmpl index 7acf5f736..facbb766e 100644 --- a/controllers/nginx/nginx.tmpl +++ b/controllers/nginx/nginx.tmpl @@ -196,6 +196,18 @@ http { proxy_pass http://{{ $location.Upstream.Name }}; } {{ end }} + + {{ if eq $server.Name "_" }} + # this is required to avoid error if nginx is being monitored + # with an external software (like sysdig) + location /nginx_status { + allow 127.0.0.1; + deny all; + + access_log off; + stub_status on; + } + {{ end }} {{ template "CUSTOM_ERRORS" $cfg }} } {{ end }} diff --git a/controllers/nginx/utils.go b/controllers/nginx/utils.go index eaf5b7423..fe4857684 100644 --- a/controllers/nginx/utils.go +++ b/controllers/nginx/utils.go @@ -25,6 +25,7 @@ import ( "github.com/golang/glog" "k8s.io/kubernetes/pkg/api" + apierrs "k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/client/cache" "k8s.io/kubernetes/pkg/client/unversioned" "k8s.io/kubernetes/pkg/util/wait" @@ -100,7 +101,6 @@ func NewTaskQueue(syncFn func(string)) *taskQueue { // controller or daemonset (namespace and name). // This is required to watch for changes in annotations or configuration (ConfigMap) func getLBDetails(kubeClient *unversioned.Client) (*lbInfo, error) { - podIP := os.Getenv("POD_IP") podName := os.Getenv("POD_NAME") podNs := os.Getenv("POD_NAMESPACE") @@ -109,6 +109,15 @@ func getLBDetails(kubeClient *unversioned.Client) (*lbInfo, error) { return nil, fmt.Errorf("Unable to get POD information") } + if pod.Status.Phase != api.PodRunning { + // we wait up to 30 seconds until the pod is running and + // it is possible to get the IP and name of the node + err := waitForPodRunning(kubeClient, podNs, podName, time.Millisecond*200, time.Second*30) + if err != nil { + return nil, err + } + } + node, err := kubeClient.Nodes().Get(pod.Spec.NodeName) if err != nil { return nil, err @@ -128,6 +137,8 @@ func getLBDetails(kubeClient *unversioned.Client) (*lbInfo, error) { } } + podIP := os.Getenv("POD_IP") + return &lbInfo{ PodIP: podIP, Podname: podName, @@ -195,3 +206,37 @@ func parseNsName(input string) (string, string, error) { return nsName[0], nsName[1], nil } + +func waitForPodRunning(kubeClient *unversioned.Client, ns, podName string, interval, timeout time.Duration) error { + condition := func(pod *api.Pod) (bool, error) { + if pod.Status.Phase == api.PodRunning { + return true, nil + } + return false, nil + } + + return waitForPodCondition(kubeClient, ns, podName, condition, interval, timeout) +} + +// waitForPodCondition waits for a pod in state defined by a condition (func) +func waitForPodCondition(kubeClient *unversioned.Client, ns, podName string, condition func(pod *api.Pod) (bool, error), + interval, timeout time.Duration) error { + return wait.PollImmediate(interval, timeout, func() (bool, error) { + pod, err := kubeClient.Pods(ns).Get(podName) + if err != nil { + if apierrs.IsNotFound(err) { + return false, nil + } + } + + done, err := condition(pod) + if err != nil { + return false, err + } + if done { + return true, nil + } + + return false, nil + }) +}