2016-07-25 20:00:28 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-08-02 05:53:12 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
2016-08-02 05:53:12 +00:00
|
|
|
// gc begins the gc process for Kubernetes.
|
|
|
|
func (cli *client) gc(ctx context.Context, every time.Duration) {
|
|
|
|
handleErr := func(err error) { log.Println(err.Error()) }
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2016-08-02 05:53:12 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(every):
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2016-08-02 05:53:12 +00:00
|
|
|
// TODO(ericchiang): On failures, run garbage collection more often.
|
|
|
|
log.Println("kubernetes: running garbage collection")
|
|
|
|
cli.gcAuthRequests(handleErr)
|
|
|
|
cli.gcAuthCodes(handleErr)
|
|
|
|
log.Printf("kubernetes: garbage collection finished, next run at %s", cli.now().Add(every))
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 05:53:12 +00:00
|
|
|
func (cli *client) gcAuthRequests(handleErr func(error)) {
|
2016-07-25 20:00:28 +00:00
|
|
|
var authRequests AuthRequestList
|
|
|
|
if err := cli.list(resourceAuthRequest, &authRequests); err != nil {
|
2016-08-02 05:53:12 +00:00
|
|
|
handleErr(fmt.Errorf("failed to list auth requests: %v", err))
|
|
|
|
return
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
for _, authRequest := range authRequests.AuthRequests {
|
|
|
|
if cli.now().After(authRequest.Expiry) {
|
|
|
|
if err := cli.delete(resourceAuthRequest, authRequest.ObjectMeta.Name); err != nil {
|
2016-08-02 05:53:12 +00:00
|
|
|
handleErr(fmt.Errorf("failed to detele auth request: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) gcAuthCodes(handleErr func(error)) {
|
|
|
|
var authCodes AuthCodeList
|
|
|
|
if err := cli.list(resourceAuthCode, &authCodes); err != nil {
|
|
|
|
handleErr(fmt.Errorf("failed to list auth codes: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, authCode := range authCodes.AuthCodes {
|
|
|
|
if cli.now().After(authCode.Expiry) {
|
|
|
|
if err := cli.delete(resourceAuthCode, authCode.ObjectMeta.Name); err != nil {
|
|
|
|
handleErr(fmt.Errorf("failed to delete auth code: %v", err))
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|