Search code examples
gocontainers

Golang function to delay sidecar pod


I'm trying to delay a sidecar container through a golang function. As far as I know there is no native k8s tool for that.

So I just want to check if the main container is serving the URL as expected before executing the code that process some data in the sidecar container.

This is the function:

func checkifhostalive() {

    seconds := 35
    host := "containername"
    port := "6062"
    timeOut := time.Duration(seconds) * time.Second

    _, err := net.DialTimeout("tcp", host+":"+port, timeOut)

    if err != nil {
        fmt.Println(err)
        return
    }

}

The container just serves an application dashboard on that port.

Am I correct if i specify "containername:6062" for the netDialTimeout?

Basically I want that rest of the code only is executed when the application is ready. So before the rest of the code i placed that function.

I'm not sure if that´s totally correct or not. The function returns an error that the main container refused the connection.

Maybe I would like something more simple like a ping that waits till the container responds and returns a true if responding correctly so I can start running the rest of the code in the main function.


Solution

  • Am I correct if i specify "containername:6062" for the netDialTimeout?

    All containers in a pod share the same network stack. So they can communicate via localhost. Container names do not work.

    So, use localhost:6062 instead.