-
Notifications
You must be signed in to change notification settings - Fork 5k
Implement schedule stop for unix #9503
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 8 commits
a54ea82
51b9f68
fd425d6
202e9c5
682df50
b9adc95
44f033d
865d3a4
02f03af
a2d00e5
f869ca5
7788cb9
a00b7a3
129f6d4
e050e8c
0ee5a38
c1be325
cf1ee6b
635f2a7
7570cbd
6c48c60
bcf236f
9300331
291e5b6
5569817
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,79 @@ | ||
// +build !windows | ||
|
||
/* | ||
Copyright 2020 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 schedule | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/VividCortex/godaemon" | ||
"github.com/pkg/errors" | ||
"k8s.io/klog/v2" | ||
"k8s.io/minikube/pkg/minikube/localpath" | ||
) | ||
|
||
func killExistingScheduledStops(profiles []string) error { | ||
for _, profile := range profiles { | ||
file := localpath.PID(profile) | ||
f, err := ioutil.ReadFile(file) | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
defer os.Remove(file) | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return errors.Wrapf(err, "reading %s", file) | ||
} | ||
pid, err := strconv.Atoi(string(f)) | ||
if err != nil { | ||
return errors.Wrapf(err, "converting %v to int", string(f)) | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
p, err := os.FindProcess(pid) | ||
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. Please check that the pid is minikube process before killing it. Otherwise this will kill random processes. 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. When I'm not sure how a random process could be killed from this code, could you explain if you still think that's the case? |
||
if err != nil { | ||
return errors.Wrap(err, "finding process") | ||
} | ||
klog.Infof("killing process %v as it is an old scheduled stop", pid) | ||
if err := p.Kill(); err != nil { | ||
return errors.Wrapf(err, "killing %v", pid) | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
return nil | ||
} | ||
|
||
func daemonize(profiles []string, duration time.Duration) error { | ||
_, _, err := godaemon.MakeDaemon(&godaemon.DaemonAttr{}) | ||
if err != nil { | ||
return err | ||
} | ||
// now that this process has daemonized, it has a new PID | ||
pid := os.Getpid() | ||
return savePIDs(pid, profiles) | ||
} | ||
|
||
func savePIDs(pid int, profiles []string) error { | ||
for _, p := range profiles { | ||
file := localpath.PID(p) | ||
if err := ioutil.WriteFile(file, []byte(fmt.Sprintf("%v", pid)), 0644); err != nil { | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return err | ||
} | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// +build windows | ||
|
||
/* | ||
Copyright 2020 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 schedule | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func killExistingScheduledStops(profiles []string) error { | ||
return fmt.Errorf("not yet implemented for windows") | ||
} | ||
|
||
func daemonize(profiles []string, duration time.Duration) error { | ||
return fmt.Errorf("not yet implemented for windows") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright 2020 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 schedule | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/minikube/pkg/minikube/config" | ||
"k8s.io/minikube/pkg/minikube/mustload" | ||
) | ||
|
||
// Daemonize daemonizes minikube so that scheduled stop happens as expected | ||
func Daemonize(profiles []string, duration time.Duration) error { | ||
// save current time and expected duration in config | ||
scheduledStop := &config.ScheduledStopConfig{ | ||
InitiationTime: time.Now().Unix(), | ||
Duration: duration, | ||
} | ||
if err := killExistingScheduledStops(profiles); err != nil { | ||
log.Printf("error killing existing scheduled stops: %v", err) | ||
} | ||
for _, p := range profiles { | ||
_, cc := mustload.Partial(p) | ||
cc.ScheduledStop = scheduledStop | ||
if err := config.SaveProfile(p, cc); err != nil { | ||
return errors.Wrap(err, "saving profile") | ||
} | ||
} | ||
|
||
return daemonize(profiles, duration) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.