Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Chu Peng 楚鹏
minikube
提交
1d37a912
M
minikube
项目概览
Chu Peng 楚鹏
/
minikube
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
minikube
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
1d37a912
编写于
11月 17, 2020
作者:
P
priyawadhwa
提交者:
GitHub
11月 17, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9689 from priyawadhwa/ss-windows
Implement scheduled stop on windows
上级
2179b048
4cd74633
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
235 addition
and
23 deletion
+235
-23
cmd/minikube/cmd/stop.go
cmd/minikube/cmd/stop.go
+4
-3
deploy/iso/minikube-iso/package/Config.in
deploy/iso/minikube-iso/package/Config.in
+1
-0
deploy/iso/minikube-iso/package/scheduled-stop/Config.in
deploy/iso/minikube-iso/package/scheduled-stop/Config.in
+3
-0
deploy/iso/minikube-iso/package/scheduled-stop/minikube-scheduled-stop
...nikube-iso/package/scheduled-stop/minikube-scheduled-stop
+11
-0
deploy/iso/minikube-iso/package/scheduled-stop/minikube-scheduled-stop.service
...so/package/scheduled-stop/minikube-scheduled-stop.service
+11
-0
deploy/iso/minikube-iso/package/scheduled-stop/scheduled-stop.mk
...iso/minikube-iso/package/scheduled-stop/scheduled-stop.mk
+23
-0
deploy/kicbase/Dockerfile
deploy/kicbase/Dockerfile
+7
-0
deploy/kicbase/scheduled-stop/minikube-scheduled-stop
deploy/kicbase/scheduled-stop/minikube-scheduled-stop
+11
-0
deploy/kicbase/scheduled-stop/minikube-scheduled-stop.service
...oy/kicbase/scheduled-stop/minikube-scheduled-stop.service
+11
-0
pkg/minikube/constants/constants.go
pkg/minikube/constants/constants.go
+4
-0
pkg/minikube/schedule/daemonize_windows.go
pkg/minikube/schedule/daemonize_windows.go
+79
-2
pkg/minikube/schedule/schedule.go
pkg/minikube/schedule/schedule.go
+11
-3
test/integration/scheduled_stop_test.go
test/integration/scheduled_stop_test.go
+59
-15
未找到文件。
cmd/minikube/cmd/stop.go
浏览文件 @
1d37a912
...
...
@@ -99,12 +99,13 @@ func runStop(cmd *cobra.Command, args []string) {
schedule
.
KillExisting
(
profilesToStop
)
if
scheduledStopDuration
!=
0
{
if
runtime
.
GOOS
==
"windows"
{
exit
.
Message
(
reason
.
Usage
,
"the --schedule flag is currently not supported on windows"
)
}
if
err
:=
schedule
.
Daemonize
(
profilesToStop
,
scheduledStopDuration
);
err
!=
nil
{
exit
.
Message
(
reason
.
DaemonizeError
,
"unable to daemonize: {{.err}}"
,
out
.
V
{
"err"
:
err
.
Error
()})
}
// if OS is windows, scheduled stop is now being handled within minikube, so return
if
runtime
.
GOOS
==
"windows"
{
return
}
klog
.
Infof
(
"sleeping %s before completing stop..."
,
scheduledStopDuration
.
String
())
time
.
Sleep
(
scheduledStopDuration
)
}
...
...
deploy/iso/minikube-iso/package/Config.in
浏览文件 @
1d37a912
...
...
@@ -14,4 +14,5 @@ menu "System tools"
source "$BR2_EXTERNAL_MINIKUBE_PATH/package/vbox-guest/Config.in"
source "$BR2_EXTERNAL_MINIKUBE_PATH/package/containerd-bin/Config.in"
source "$BR2_EXTERNAL_MINIKUBE_PATH/package/falco-module/Config.in"
source "$BR2_EXTERNAL_MINIKUBE_PATH/package/scheduled-stop/Config.in"
endmenu
deploy/iso/minikube-iso/package/scheduled-stop/Config.in
0 → 100644
浏览文件 @
1d37a912
config BR2_PACKAGE_SCHEDULED_STOP
bool "scheduled-stop"
default y
deploy/iso/minikube-iso/package/scheduled-stop/minikube-scheduled-stop
0 → 100755
浏览文件 @
1d37a912
#!/bin/bash
set
-x
echo
"running scheduled stop ..."
;
echo
"sleeping %
$SLEEP
seconds..."
sleep
$SLEEP
echo
"running poweroff..."
sudo
systemctl poweroff
deploy/iso/minikube-iso/package/scheduled-stop/minikube-scheduled-stop.service
0 → 100644
浏览文件 @
1d37a912
[Unit]
Description
=
minikube scheduled stop
[Install]
WantedBy
=
multi-user.target
[Service]
Type
=
simple
User
=
root
ExecStart
=
/usr/sbin/minikube-scheduled-stop
EnvironmentFile
=
/var/lib/minikube/scheduled-stop/environment
deploy/iso/minikube-iso/package/scheduled-stop/scheduled-stop.mk
0 → 100644
浏览文件 @
1d37a912
################################################################################
#
# minikube scheduled-stop
#
################################################################################
define
SCHEDULED_STOP_INSTALL_INIT_SYSTEMD
$(INSTALL)
-D
-m
644
\
$(SCHEDULED_STOP_PKGDIR)/minikube-scheduled-stop.service
\
$(TARGET_DIR)/usr/lib/systemd/system/minikube-scheduled-stop.service
mkdir
-p
$(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
ln
-fs
/usr/lib/systemd/system/minikube-scheduled-stop.service
\
$(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/minikube-scheduled-stop.service
endef
define
SCHEDULED_STOP_INSTALL_TARGET_CMDS
$(INSTALL)
-Dm755
\
$(SCHEDULED_STOP_PKGDIR)/minikube-scheduled-stop
\
$(TARGET_DIR)/usr/sbin/minikube-scheduled-stop
endef
$(eval
$(generic-package))
deploy/kicbase/Dockerfile
浏览文件 @
1d37a912
...
...
@@ -130,6 +130,13 @@ COPY automount/minikube-automount.service /usr/lib/systemd/system/minikube-autom
RUN
ln
-fs
/usr/lib/systemd/system/minikube-automount.service
\
/etc/systemd/system/multi-user.target.wants/minikube-automount.service
# scheduled stop service
COPY
scheduled-stop/minikube-scheduled-stop /var/lib/minikube/scheduled-stop/minikube-scheduled-stop
COPY
scheduled-stop/minikube-scheduled-stop.service /usr/lib/systemd/system/minikube-scheduled-stop.service
RUN
ln
-fs
/usr/lib/systemd/system/minikube-scheduled-stop.service
\
/etc/systemd/system/multi-user.target.wants/minikube-scheduled-stop.service
&&
\
chmod
+x /var/lib/minikube/scheduled-stop/minikube-scheduled-stop
# disable non-docker runtimes by default
RUN
systemctl disable containerd
&&
systemctl disable crio
&&
rm
/etc/crictl.yaml
# enable docker which is default
...
...
deploy/kicbase/scheduled-stop/minikube-scheduled-stop
0 → 100644
浏览文件 @
1d37a912
#!/bin/bash
set
-x
echo
"running scheduled stop ..."
;
echo
"sleeping %
$SLEEP
seconds..."
sleep
$SLEEP
echo
"running poweroff..."
sudo
systemctl poweroff
deploy/kicbase/scheduled-stop/minikube-scheduled-stop.service
0 → 100644
浏览文件 @
1d37a912
[Unit]
Description
=
minikube scheduled stop
[Install]
WantedBy
=
multi-user.target
[Service]
Type
=
simple
User
=
root
ExecStart
=
/var/lib/minikube/scheduled-stop/minikube-scheduled-stop
EnvironmentFile
=
/var/lib/minikube/scheduled-stop/environment
pkg/minikube/constants/constants.go
浏览文件 @
1d37a912
...
...
@@ -80,6 +80,10 @@ const (
// TestDiskUsedEnv is used in integration tests for insufficient storage with 'minikube status'
TestDiskUsedEnv
=
"MINIKUBE_TEST_STORAGE_CAPACITY"
// scheduled stop constants
ScheduledStopEnvFile
=
"/var/lib/minikube/scheduled-stop/environment"
ScheduledStopSystemdService
=
"minikube-scheduled-stop"
// MinikubeExistingPrefix is used to save the original environment when executing docker-env
MinikubeExistingPrefix
=
"MINIKUBE_EXISTING_"
...
...
pkg/minikube/schedule/daemonize_windows.go
浏览文件 @
1d37a912
...
...
@@ -20,16 +20,93 @@ package schedule
import
(
"fmt"
"os/exec"
"time"
"github.com/pkg/errors"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/sysinit"
)
// KillExisting will kill existing scheduled stops
func
KillExisting
(
profiles
[]
string
)
{
klog
.
Errorf
(
"not yet implemented for windows"
)
for
_
,
profile
:=
range
profiles
{
if
err
:=
killExisting
(
profile
);
err
!=
nil
{
klog
.
Errorf
(
"error terminating scheduled stop for profile %s: %v"
,
profile
,
err
)
}
}
}
func
killExisting
(
profile
string
)
error
{
klog
.
Infof
(
"trying to kill existing schedule stop for profile %s..."
,
profile
)
api
,
err
:=
machine
.
NewAPIClient
()
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"getting api client for profile %s"
,
profile
)
}
h
,
err
:=
api
.
Load
(
profile
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Error loading existing host. Please try running [minikube delete], then run [minikube start] again."
)
}
runner
,
err
:=
machine
.
CommandRunner
(
h
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"getting command runner"
)
}
// restart scheduled stop service in container
sysManger
:=
sysinit
.
New
(
runner
)
if
err
:=
sysManger
.
Stop
(
constants
.
ScheduledStopSystemdService
);
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"stopping schedule-stop service for profile %s"
,
profile
)
}
return
nil
}
// to daemonize on windows, we schedule the stop within minikube itself
// starting the minikube-scheduled-stop systemd service kicks off the scheduled stop
func
daemonize
(
profiles
[]
string
,
duration
time
.
Duration
)
error
{
return
fmt
.
Errorf
(
"not yet implemented for windows"
)
for
_
,
profile
:=
range
profiles
{
if
err
:=
startSystemdService
(
profile
,
duration
);
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"implementing scheduled stop for %s"
,
profile
)
}
}
return
nil
}
// to start the systemd service, we first have to tell the systemd service how long to sleep for
// before shutting down minikube from within
// we do this by settig the SLEEP environment variable in the environment file to the users
// requested duration
func
startSystemdService
(
profile
string
,
duration
time
.
Duration
)
error
{
// get ssh runner
klog
.
Infof
(
"starting systemd service for profile %s..."
,
profile
)
api
,
err
:=
machine
.
NewAPIClient
()
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"getting api client for profile %s"
,
profile
)
}
h
,
err
:=
api
.
Load
(
profile
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Error loading existing host. Please try running [minikube delete], then run [minikube start] again."
)
}
runner
,
err
:=
machine
.
CommandRunner
(
h
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"getting command runner"
)
}
if
rr
,
err
:=
runner
.
RunCmd
(
exec
.
Command
(
"sudo"
,
"mkdir"
,
"-p"
,
"/var/lib/minikube/scheduled-stop"
));
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"creating dirs: %v"
,
rr
.
Output
())
}
// update environment file to include duration
if
err
:=
runner
.
Copy
(
environmentFile
(
duration
));
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"copying scheduled stop env file"
)
}
// restart scheduled stop service in container
sysManger
:=
sysinit
.
New
(
runner
)
return
sysManger
.
Restart
(
constants
.
ScheduledStopSystemdService
)
}
// return the contents of the environment file for minikube-scheduled-stop systemd service
// should be of the format SLEEP=<scheduled stop requested by user in seconds>
func
environmentFile
(
duration
time
.
Duration
)
assets
.
CopyableFile
{
contents
:=
[]
byte
(
fmt
.
Sprintf
(
"SLEEP=%v"
,
duration
.
Seconds
()))
return
assets
.
NewMemoryAssetTarget
(
contents
,
constants
.
ScheduledStopEnvFile
,
"0644"
)
}
pkg/minikube/schedule/schedule.go
浏览文件 @
1d37a912
...
...
@@ -41,11 +41,19 @@ func Daemonize(profiles []string, duration time.Duration) error {
continue
}
daemonizeProfiles
=
append
(
daemonizeProfiles
,
p
)
}
if
err
:=
daemonize
(
daemonizeProfiles
,
duration
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"daemonizing"
)
}
// save scheduled stop config if daemonize was successful
for
_
,
d
:=
range
daemonizeProfiles
{
_
,
cc
:=
mustload
.
Partial
(
d
)
cc
.
ScheduledStop
=
scheduledStop
if
err
:=
config
.
SaveProfile
(
p
,
cc
);
err
!=
nil
{
if
err
:=
config
.
SaveProfile
(
d
,
cc
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"saving profile"
)
}
}
return
daemonize
(
daemonizeProfiles
,
duration
)
return
nil
}
test/integration/scheduled_stop_test.go
浏览文件 @
1d37a912
...
...
@@ -24,19 +24,58 @@ import (
"io/ioutil"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"syscall"
"testing"
"time"
"github.com/docker/machine/libmachine/state"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/util/retry"
)
func
TestScheduledStop
(
t
*
testing
.
T
)
{
func
TestScheduledStopWindows
(
t
*
testing
.
T
)
{
if
runtime
.
GOOS
!=
"windows"
{
t
.
Skip
(
"test only runs on windows"
)
}
if
NoneDriver
()
{
t
.
Skip
(
"--schedule does not apply to none driver "
)
t
.
Skip
(
"--schedule does not work with the none driver"
)
}
profile
:=
UniqueProfileName
(
"scheduled-stop"
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
Minutes
(
5
))
defer
CleanupWithLogs
(
t
,
profile
,
cancel
)
startMinikube
(
ctx
,
t
,
profile
)
// schedule a stop for 5m from now
scheduledStopMinikube
(
ctx
,
t
,
profile
,
"5m"
)
// make sure the systemd service is running
rr
,
err
:=
Run
(
t
,
exec
.
CommandContext
(
ctx
,
Target
(),
[]
string
{
"ssh"
,
"-p"
,
profile
,
"--"
,
"sudo"
,
"systemctl"
,
"show"
,
constants
.
ScheduledStopSystemdService
,
"--no-page"
}
...
))
if
err
!=
nil
{
t
.
Fatalf
(
"getting minikube-scheduled-stop status: %v
\n
%s"
,
err
,
rr
.
Output
())
}
if
!
strings
.
Contains
(
rr
.
Output
(),
"ActiveState=active"
)
{
t
.
Fatalf
(
"minikube-scheduled-stop is not running: %v"
,
rr
.
Output
())
}
// reschedule stop for 5 seconds from now
scheduledStopMinikube
(
ctx
,
t
,
profile
,
"5s"
)
// sleep for 5 seconds
time
.
Sleep
(
5
*
time
.
Second
)
// make sure minikube status is "Stopped"
ensureMinikubeStatusStopped
(
ctx
,
t
,
profile
)
}
func
TestScheduledStopUnix
(
t
*
testing
.
T
)
{
if
runtime
.
GOOS
==
"windows"
{
t
.
Skip
(
"test only runs on unix"
)
}
if
NoneDriver
()
{
t
.
Skip
(
"--schedule does not work with the none driver"
)
}
profile
:=
UniqueProfileName
(
"scheduled-stop"
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
Minutes
(
5
))
...
...
@@ -56,19 +95,8 @@ func TestScheduledStop(t *testing.T) {
t
.
Fatalf
(
"process %v running but should have been killed on reschedule of stop"
,
pid
)
}
checkPID
(
t
,
profile
)
// wait allotted time to make sure minikube status is "Stopped"
checkStatus
:=
func
()
error
{
ctx
,
cancel
:=
context
.
WithDeadline
(
ctx
,
time
.
Now
()
.
Add
(
10
*
time
.
Second
))
defer
cancel
()
got
:=
Status
(
ctx
,
t
,
Target
(),
profile
,
"Host"
,
profile
)
if
got
!=
state
.
Stopped
.
String
()
{
return
fmt
.
Errorf
(
"expected post-stop host status to be -%q- but got *%q*"
,
state
.
Stopped
,
got
)
}
return
nil
}
if
err
:=
retry
.
Expo
(
checkStatus
,
time
.
Second
,
time
.
Minute
);
err
!=
nil
{
t
.
Fatalf
(
"error %v"
,
err
)
}
// make sure minikube status is "Stopped"
ensureMinikubeStatusStopped
(
ctx
,
t
,
profile
)
}
func
startMinikube
(
ctx
context
.
Context
,
t
*
testing
.
T
,
profile
string
)
{
...
...
@@ -116,3 +144,19 @@ func processRunning(t *testing.T, pid string) bool {
t
.
Log
(
"signal error was: "
,
err
)
return
err
==
nil
}
func
ensureMinikubeStatusStopped
(
ctx
context
.
Context
,
t
*
testing
.
T
,
profile
string
)
{
// wait allotted time to make sure minikube status is "Stopped"
checkStatus
:=
func
()
error
{
ctx
,
cancel
:=
context
.
WithDeadline
(
ctx
,
time
.
Now
()
.
Add
(
10
*
time
.
Second
))
defer
cancel
()
got
:=
Status
(
ctx
,
t
,
Target
(),
profile
,
"Host"
,
profile
)
if
got
!=
state
.
Stopped
.
String
()
{
return
fmt
.
Errorf
(
"expected post-stop host status to be -%q- but got *%q*"
,
state
.
Stopped
,
got
)
}
return
nil
}
if
err
:=
retry
.
Expo
(
checkStatus
,
time
.
Second
,
time
.
Minute
);
err
!=
nil
{
t
.
Fatalf
(
"error %v"
,
err
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录