-
Notifications
You must be signed in to change notification settings - Fork 152
*: add wait-for-kube command #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package waitforkube | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "k8s.io/apimachinery/pkg/util/wait" | ||
| "k8s.io/klog" | ||
| ) | ||
|
|
||
| const ( | ||
| retryDuration = 10 * time.Second | ||
| saTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" | ||
| ) | ||
|
|
||
| type waitForKubeOpts struct { | ||
| errOut io.Writer | ||
| } | ||
|
|
||
| // NewWaitForKubeCommand waits for kube to come up before continuing to start the rest of the containers. | ||
| func NewWaitForKubeCommand(errOut io.Writer) *cobra.Command { | ||
| waitForKubeOpts := &waitForKubeOpts{ | ||
| errOut: errOut, | ||
| } | ||
| cmd := &cobra.Command{ | ||
| Use: "wait-for-kube", | ||
| Short: "wait for kube service account to exist", | ||
| Long: "This command makes sure that the kube is available before starting the rest of the containers in the pod.", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| must := func(fn func() error) { | ||
| if err := fn(); err != nil { | ||
| if cmd.HasParent() { | ||
| klog.Fatal(err) | ||
| fmt.Fprint(waitForKubeOpts.errOut, err.Error()) | ||
| } | ||
| } | ||
| } | ||
| must(waitForKubeOpts.Run) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func (w *waitForKubeOpts) Run() error { | ||
| wait.PollInfinite(retryDuration, func() (bool, error) { | ||
| if _, err := os.Stat(saTokenPath); os.IsNotExist(err) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it sufficient enough to check that the env variables are populated with this? Should we have an equivalent of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the SA token has synced that means kube scheduled staticsync so yes.
Where? we are waiting.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be clear static sync is a daemonset
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. our container will not change its ENV unless it restarts right?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes ideally it will not change, but want to be sure with something like Where inCluster is We can either do it after the wait loop, so the initContainer will crashlooping until the env is populated. if the env is not populated, it is not a bug against us |
||
| klog.Infof("waiting for kube service account resources to sync: %v", err) | ||
| return false, nil | ||
| } | ||
| return true, nil | ||
| }) | ||
| if !inCluster() { | ||
| return fmt.Errorf("kube env not populated") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| //TODO: add to util | ||
| func inCluster() bool { | ||
| if os.Getenv("KUBERNETES_SERVICE_HOST") == "" || os.Getenv("KUBERNETES_SERVICE_PORT") == "" { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was giving a warning obviously not needed cleaned up here.