Skip to content

Commit

Permalink
auto-pause: restart service after configuration (kubernetes#19900)
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeProgrammer authored Dec 11, 2024
1 parent 8d3ab40 commit 52cd866
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
62 changes: 58 additions & 4 deletions cmd/minikube/cmd/config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ import (
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/addons"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/service"
"k8s.io/minikube/pkg/minikube/style"
"k8s.io/minikube/pkg/minikube/sysinit"
)

var posResponses = []string{"yes", "y"}
Expand Down Expand Up @@ -255,7 +259,7 @@ var addonsConfigureCmd = &cobra.Command{
}
}
case "auto-pause":
_, cfg := mustload.Partial(profile)
lapi, cfg := mustload.Partial(profile)
intervalInput := AskForStaticValue("-- Enter interval time of auto-pause-interval (ex. 1m0s): ")
intervalTime, err := time.ParseDuration(intervalInput)
if err != nil {
Expand All @@ -270,9 +274,25 @@ var addonsConfigureCmd = &cobra.Command{
}
addon := assets.Addons["auto-pause"]
if addon.IsEnabled(cfg) {
// Re-enable auto-pause addon in order to update interval time
if err := addons.EnableOrDisableAddon(cfg, "auto-pause", "true"); err != nil {
out.ErrT(style.Fatal, "Failed to configure auto-pause {{.profile}}", out.V{"profile": profile})

// see #17945: restart auto-pause service
p, err := config.LoadProfile(profile)
if err != nil {
out.ErrT(style.Fatal, "failed to load profile: {{.error}}", out.V{"error": err})
}
if profileStatus(p, lapi).StatusCode/100 == 2 { // 2xx code
co := mustload.Running(profile)
// first unpause all nodes cluster immediately
unpauseWholeCluster(co)
// Re-enable auto-pause addon in order to update interval time
if err := addons.EnableOrDisableAddon(cfg, "auto-pause", "true"); err != nil {
out.ErrT(style.Fatal, "Failed to configure auto-pause {{.profile}}", out.V{"profile": profile})
}
// restart auto-pause service
if err := sysinit.New(co.CP.Runner).Restart("auto-pause"); err != nil {
out.ErrT(style.Fatal, "failed to restart auto-pause: {{.error}}", out.V{"error": err})
}

}
}
default:
Expand All @@ -284,6 +304,40 @@ var addonsConfigureCmd = &cobra.Command{
},
}

func unpauseWholeCluster(co mustload.ClusterController) {
for _, n := range co.Config.Nodes {

// Use node-name if available, falling back to cluster name
name := n.Name
if n.Name == "" {
name = co.Config.Name
}

out.Step(style.Pause, "Unpausing node {{.name}} ... ", out.V{"name": name})

machineName := config.MachineName(*co.Config, n)
host, err := machine.LoadHost(co.API, machineName)
if err != nil {
exit.Error(reason.GuestLoadHost, "Error getting host", err)
}

r, err := machine.CommandRunner(host)
if err != nil {
exit.Error(reason.InternalCommandRunner, "Failed to get command runner", err)
}

cr, err := cruntime.New(cruntime.Config{Type: co.Config.KubernetesConfig.ContainerRuntime, Runner: r})
if err != nil {
exit.Error(reason.InternalNewRuntime, "Failed runtime", err)
}

_, err = cluster.Unpause(cr, r, nil) // nil means all namespaces
if err != nil {
exit.Error(reason.GuestUnpause, "Pause", err)
}
}
}

func init() {
AddonsCmd.AddCommand(addonsConfigureCmd)
}
13 changes: 9 additions & 4 deletions cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,28 @@ func updateProfilesStatus(profiles []*config.Profile) {
defer api.Close()

for _, p := range profiles {
p.Status = profileStatus(p, api)
p.Status = profileStatus(p, api).StatusName
}
}

func profileStatus(p *config.Profile, api libmachine.API) string {
func profileStatus(p *config.Profile, api libmachine.API) cluster.State {
cps := config.ControlPlanes(*p.Config)
if len(cps) == 0 {
exit.Message(reason.GuestCpConfig, "No control-plane nodes found.")
}
statuses, err := cluster.GetStatus(api, p.Config)
if err != nil {
klog.Errorf("error getting statuses: %v", err)
return "Unknown"
return cluster.State{
BaseState: cluster.BaseState{
Name: "Unknown",
StatusCode: 520,
},
}
}
clusterStatus := cluster.GetState(statuses, ClusterFlagValue(), p.Config)

return clusterStatus.StatusName
return clusterStatus
}

func renderProfilesTable(ps [][]string) {
Expand Down

0 comments on commit 52cd866

Please sign in to comment.