Skip to content

Commit

Permalink
Update pkg/minikube/cluster/status.go
Browse files Browse the repository at this point in the history
Co-authored-by: Steven Powell <44844360+spowelljr@users.noreply.github.com>
  • Loading branch information
ComradeProgrammer and spowelljr committed Jul 14, 2024
1 parent 06b019f commit dcc9ad4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func profileStatus(p *config.Profile, api libmachine.API) string {
klog.Errorf("error getting statuses: %v", err)
return "Unknown"
}
clusterStatus := cluster.GetClusterState(statuses, ClusterFlagValue(), p.Config)
clusterStatus := cluster.GetState(statuses, ClusterFlagValue(), p.Config)

return clusterStatus.StatusName
}
Expand Down
20 changes: 4 additions & 16 deletions cmd/minikube/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,6 @@ var (
watch time.Duration
)

// Additional legacy states
const (
// Configured means configured
Configured = "Configured" // ~state.Saved
// Misconfigured means misconfigured
Misconfigured = "Misconfigured" // ~state.Error
// Nonexistent means the resource does not exist
Nonexistent = "Nonexistent" // ~state.None
// Irrelevant is used for statuses that aren't meaningful for worker nodes
Irrelevant = "Irrelevant"
)

const (
minikubeNotRunningStatusFlag = 1 << 0
clusterNotRunningStatusFlag = 1 << 1
Expand Down Expand Up @@ -175,10 +163,10 @@ func exitCode(statuses []*cluster.Status) int {
if st.Host != state.Running.String() {
c |= minikubeNotRunningStatusFlag
}
if (st.APIServer != state.Running.String() && st.APIServer != Irrelevant) || st.Kubelet != state.Running.String() {
if (st.APIServer != state.Running.String() && st.APIServer != cluster.Irrelevant) || st.Kubelet != state.Running.String() {
c |= clusterNotRunningStatusFlag
}
if st.Kubeconfig != Configured && st.Kubeconfig != Irrelevant {
if st.Kubeconfig != cluster.Configured && st.Kubeconfig != cluster.Irrelevant {
c |= k8sNotRunningStatusFlag
}
}
Expand Down Expand Up @@ -209,7 +197,7 @@ func statusText(st *cluster.Status, w io.Writer) error {
if err := tmpl.Execute(w, st); err != nil {
return err
}
if st.Kubeconfig == Misconfigured {
if st.Kubeconfig == cluster.Misconfigured {
_, err := w.Write([]byte("\nWARNING: Your kubectl is pointing to stale minikube-vm.\nTo fix the kubectl context, run `minikube update-context`\n"))
return err
}
Expand All @@ -233,7 +221,7 @@ func statusJSON(st []*cluster.Status, w io.Writer) error {
}

func clusterStatusJSON(statuses []*cluster.Status, w io.Writer, cc *config.ClusterConfig) error {
cs := cluster.GetClusterState(statuses, ClusterFlagValue(), cc)
cs := cluster.GetState(statuses, ClusterFlagValue(), cc)

bs, err := json.Marshal(cs)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions cmd/minikube/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func TestExitCode(t *testing.T) {
want int
state *cluster.Status
}{
{"ok", 0, &cluster.Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured}},
{"paused", 2, &cluster.Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured}},
{"down", 7, &cluster.Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured}},
{"ok", 0, &cluster.Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: cluster.Configured}},
{"paused", 2, &cluster.Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: cluster.Configured}},
{"down", 7, &cluster.Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: cluster.Misconfigured}},
{"missing", 7, &cluster.Status{Host: "Nonexistent", Kubelet: "Nonexistent", APIServer: "Nonexistent", Kubeconfig: "Nonexistent"}},
}
for _, tc := range tests {
Expand All @@ -53,17 +53,17 @@ func TestStatusText(t *testing.T) {
}{
{
name: "ok",
state: &cluster.Status{Name: "minikube", Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured, TimeToStop: "10m"},
state: &cluster.Status{Name: "minikube", Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: cluster.Configured, TimeToStop: "10m"},
want: "minikube\ntype: Control Plane\nhost: Running\nkubelet: Running\napiserver: Running\nkubeconfig: Configured\ntimeToStop: 10m\n\n",
},
{
name: "paused",
state: &cluster.Status{Name: "minikube", Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured},
state: &cluster.Status{Name: "minikube", Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: cluster.Configured},
want: "minikube\ntype: Control Plane\nhost: Running\nkubelet: Stopped\napiserver: Paused\nkubeconfig: Configured\n\n",
},
{
name: "down",
state: &cluster.Status{Name: "minikube", Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured},
state: &cluster.Status{Name: "minikube", Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: cluster.Misconfigured},
want: "minikube\ntype: Control Plane\nhost: Stopped\nkubelet: Stopped\napiserver: Stopped\nkubeconfig: Misconfigured\n\n\nWARNING: Your kubectl is pointing to stale minikube-vm.\nTo fix the kubectl context, run `minikube update-context`\n",
},
}
Expand All @@ -88,9 +88,9 @@ func TestStatusJSON(t *testing.T) {
name string
state *cluster.Status
}{
{"ok", &cluster.Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: Configured, TimeToStop: "10m"}},
{"paused", &cluster.Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: Configured}},
{"down", &cluster.Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: Misconfigured}},
{"ok", &cluster.Status{Host: "Running", Kubelet: "Running", APIServer: "Running", Kubeconfig: cluster.Configured, TimeToStop: "10m"}},
{"paused", &cluster.Status{Host: "Running", Kubelet: "Stopped", APIServer: "Paused", Kubeconfig: cluster.Configured}},
{"down", &cluster.Status{Host: "Stopped", Kubelet: "Stopped", APIServer: "Stopped", Kubeconfig: cluster.Misconfigured}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down
24 changes: 13 additions & 11 deletions pkg/minikube/cluster/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ type Status struct {
PodManEnv string `json:",omitempty"`
}

// ClusterState holds a cluster state representation
// State holds a cluster state representation
//
//nolint:revive
type ClusterState struct {
type State struct {
BaseState

BinaryVersion string
Expand Down Expand Up @@ -166,6 +166,7 @@ type BaseState struct {
StepDetail string `json:",omitempty"`
}

// GetStatus returns the statuses of each node
func GetStatus(api libmachine.API, cc *config.ClusterConfig) ([]*Status, error) {
var statuses []*Status
for _, n := range cc.Nodes {
Expand All @@ -188,17 +189,20 @@ func GetStatus(api libmachine.API, cc *config.ClusterConfig) ([]*Status, error)
return statuses, nil
}

// clusterState converts Status structs into a ClusterState struct
// GetState converts Status structs into a State struct
//
//nolint:gocyclo
func GetClusterState(sts []*Status, profile string, cc *config.ClusterConfig) ClusterState {
statusName := sts[0].APIServer
if sts[0].Host == codeNames[InsufficientStorage] {
statusName = sts[0].Host
func GetState(sts []*Status, profile string, cc *config.ClusterConfig) State {
statusName := ""
if len(sts) > 0 {
statusName = sts[0].APIServer
if sts[0].Host == codeNames[InsufficientStorage] {
statusName = sts[0].Host
}
}
sc := statusCode(statusName)

cs := ClusterState{
cs := State{
BinaryVersion: version.GetVersion(),

BaseState: BaseState{
Expand Down Expand Up @@ -260,7 +264,6 @@ func GetClusterState(sts []*Status, profile string, cc *config.ClusterConfig) Cl
var finalStep map[string]string

for _, ev := range evs {
// klog.Infof("read event: %+v", ev)
if ev.Type() == "io.k8s.sigs.minikube.step" {
var data map[string]string
err := ev.DataAs(&data)
Expand Down Expand Up @@ -326,7 +329,6 @@ func GetClusterState(sts []*Status, profile string, cc *config.ClusterConfig) Cl
}
}

// if it is
if config.IsHA(*cc) && started {
switch {
case healthyCPs < 2:
Expand All @@ -344,7 +346,7 @@ func GetClusterState(sts []*Status, profile string, cc *config.ClusterConfig) Cl
return cs
}

// nodeStatus looks up the status of a node
// NodeStatus looks up the status of a node
func NodeStatus(api libmachine.API, cc config.ClusterConfig, n config.Node) (*Status, error) {
controlPlane := n.ControlPlane
name := config.MachineName(cc, n)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/pause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func validateStatus(ctx context.Context, t *testing.T, profile string) {
defer PostMortemLogs(t, profile)

statusOutput := runStatusCmd(ctx, t, profile, false)
var cs cluster.ClusterState
var cs cluster.State
if err := json.Unmarshal(statusOutput, &cs); err != nil {
t.Fatalf("unmarshalling: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func runStatusCmd(ctx context.Context, t *testing.T, profile string, increaseEnv
}

func verifyClusterState(t *testing.T, contents []byte) {
var cs cluster.ClusterState
var cs cluster.State
if err := json.Unmarshal(contents, &cs); err != nil {
t.Fatalf("unmarshalling: %v", err)
}
Expand Down

0 comments on commit dcc9ad4

Please sign in to comment.