minikube/cmd/auto-pause/auto-pause.go

124 lines
3.0 KiB
Go
Raw Normal View History

2021-02-16 02:46:54 +00:00
/*
Copyright 2021 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"log"
"net/http"
2021-02-16 18:06:47 +00:00
"sync"
2021-02-16 04:13:02 +00:00
"time"
2021-02-16 02:46:54 +00:00
2021-02-16 18:06:47 +00:00
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/style"
)
2021-02-16 04:13:02 +00:00
2021-02-24 21:07:25 +00:00
var unpauseRequests = make(chan struct{})
2021-02-16 18:06:47 +00:00
var done = make(chan struct{})
var mu sync.Mutex
2021-02-16 23:01:38 +00:00
2021-02-21 20:37:00 +00:00
// TODO: initialize with current state (handle the case that user enables auto-pause after it is already paused)
2021-02-16 23:01:38 +00:00
var runtimePaused = false
var version = "0.0.1"
2021-02-24 21:15:36 +00:00
// TODO: #10597 make this configurable to support containerd/cri-o
2021-02-16 23:01:38 +00:00
var runtime = "docker"
2021-02-16 04:13:02 +00:00
2021-02-16 02:46:54 +00:00
func main() {
2021-02-24 21:15:36 +00:00
// TODO: #10595 make this configurable
2021-02-16 23:01:38 +00:00
const interval = time.Minute * 1
2021-02-16 18:06:47 +00:00
// channel for incoming messages
go func() {
for {
// On each iteration new timer is created
select {
2021-02-24 21:15:36 +00:00
// TODO: #10596 make it memory-leak proof
2021-02-16 18:06:47 +00:00
case <-time.After(interval):
runPause()
2021-02-24 21:07:25 +00:00
case <-unpauseRequests:
2021-02-16 23:01:38 +00:00
fmt.Printf("Got request\n")
if runtimePaused {
runUnpause()
}
2021-02-16 18:06:47 +00:00
done <- struct{}{}
}
}
}()
2021-02-16 02:46:54 +00:00
http.HandleFunc("/", handler) // each request calls handler
2021-02-16 23:01:38 +00:00
fmt.Printf("Starting auto-pause server %s at port 8080 \n", version)
2021-02-16 18:06:47 +00:00
log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
2021-02-16 02:46:54 +00:00
}
// handler echoes the Path component of the requested URL.
func handler(w http.ResponseWriter, r *http.Request) {
2021-02-24 21:07:25 +00:00
unpauseRequests <- struct{}{}
2021-02-16 18:06:47 +00:00
<-done
2021-02-16 02:46:54 +00:00
fmt.Fprintf(w, "allow")
}
2021-02-16 04:13:02 +00:00
2021-02-16 18:06:47 +00:00
func runPause() {
mu.Lock()
defer mu.Unlock()
2021-02-16 23:01:38 +00:00
if runtimePaused {
2021-02-16 18:06:47 +00:00
return
2021-02-16 04:13:02 +00:00
}
2021-02-16 18:06:47 +00:00
r := command.NewExecRunner(true)
2021-02-16 23:01:38 +00:00
cr, err := cruntime.New(cruntime.Config{Type: runtime, Runner: r})
2021-02-16 18:06:47 +00:00
if err != nil {
exit.Error(reason.InternalNewRuntime, "Failed runtime", err)
}
2021-02-16 23:01:38 +00:00
uids, err := cluster.Pause(cr, r, []string{"kube-system"})
2021-02-16 18:06:47 +00:00
if err != nil {
exit.Error(reason.GuestPause, "Pause", err)
}
2021-02-16 04:13:02 +00:00
2021-02-16 23:01:38 +00:00
runtimePaused = true
2021-02-16 18:06:47 +00:00
2021-02-24 21:07:25 +00:00
out.Step(style.Unpause, "Paused {{.count}} containers", out.V{"count": len(uids)})
2021-02-16 04:13:02 +00:00
}
2021-02-16 18:06:47 +00:00
func runUnpause() {
2021-02-16 23:01:38 +00:00
fmt.Println("unpausing...")
2021-02-16 18:06:47 +00:00
mu.Lock()
defer mu.Unlock()
r := command.NewExecRunner(true)
2021-02-16 23:01:38 +00:00
cr, err := cruntime.New(cruntime.Config{Type: runtime, Runner: r})
2021-02-16 18:06:47 +00:00
if err != nil {
exit.Error(reason.InternalNewRuntime, "Failed runtime", err)
}
uids, err := cluster.Unpause(cr, r, nil)
if err != nil {
exit.Error(reason.GuestUnpause, "Unpause", err)
}
2021-02-16 23:01:38 +00:00
runtimePaused = false
2021-02-16 18:06:47 +00:00
2021-02-24 21:07:25 +00:00
out.Step(style.Unpause, "Unpaused {{.count}} containers", out.V{"count": len(uids)})
2021-02-16 04:13:02 +00:00
}