Skip to content

Commit

Permalink
make error consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
HirazawaUi committed Feb 8, 2025
1 parent a02fe24 commit ae78ec1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 60 deletions.
44 changes: 5 additions & 39 deletions cmd/kubeadm/app/cmd/phases/init/waitcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ package phases
import (
"fmt"
"io"
"text/template"
"time"

"github.com/lithammer/dedent"
"github.com/pkg/errors"

v1 "k8s.io/api/core/v1"
Expand All @@ -33,33 +31,12 @@ import (
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/features"
"k8s.io/kubernetes/cmd/kubeadm/app/phases/kubelet"
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
dryrunutil "k8s.io/kubernetes/cmd/kubeadm/app/util/dryrun"
staticpodutil "k8s.io/kubernetes/cmd/kubeadm/app/util/staticpod"
)

var (
kubeletFailTempl = template.Must(template.New("init").Parse(dedent.Dedent(`
Unfortunately, an error has occurred:
{{ .Error }}
This error is likely caused by:
- The kubelet is not running
- The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
- 'systemctl status kubelet'
- 'journalctl -xeu kubelet'
Additionally, a control plane component may have crashed or exited when started by the container runtime.
To troubleshoot, list all containers using your preferred container runtimes CLI.
Here is one example how you may list all running Kubernetes containers by using crictl:
- 'crictl --runtime-endpoint {{ .Socket }} ps -a | grep kube | grep -v pause'
Once you have found the failing container, you can inspect its logs with:
- 'crictl --runtime-endpoint {{ .Socket }} logs CONTAINERID'
`)))
)

// NewWaitControlPlanePhase is a hidden phase that runs after the control-plane and etcd phases
func NewWaitControlPlanePhase() workflow.Phase {
phase := workflow.Phase{
Expand Down Expand Up @@ -102,27 +79,15 @@ func runWaitControlPlanePhase(c workflow.RunData) error {
" from directory %q\n",
data.ManifestDir())

handleError := func(err error) error {
context := struct {
Error string
Socket string
}{
Error: fmt.Sprintf("%v", err),
Socket: data.Cfg().NodeRegistration.CRISocket,
}

kubeletFailTempl.Execute(data.OutputWriter(), context)
return errors.New("could not initialize a Kubernetes cluster")
}

waiter.SetTimeout(data.Cfg().Timeouts.KubeletHealthCheck.Duration)
kubeletConfig := data.Cfg().ClusterConfiguration.ComponentConfigs[componentconfigs.KubeletGroup].Get()
kubeletConfigTyped, ok := kubeletConfig.(*kubeletconfig.KubeletConfiguration)
if !ok {
return errors.New("could not convert the KubeletConfiguration to a typed object")
}
if err := waiter.WaitForKubelet(kubeletConfigTyped.HealthzBindAddress, *kubeletConfigTyped.HealthzPort); err != nil {
return handleError(err)
kubelet.PrintKubeletErrorHelpScreen(data.OutputWriter(), data.Cfg().NodeRegistration.CRISocket, false)
return errors.Wrap(err, "failed while waiting for the kubelet to start")
}

var podMap map[string]*v1.Pod
Expand All @@ -138,7 +103,8 @@ func runWaitControlPlanePhase(c workflow.RunData) error {
err = waiter.WaitForAPI()
}
if err != nil {
return handleError(err)
kubelet.PrintKubeletErrorHelpScreen(data.OutputWriter(), data.Cfg().NodeRegistration.CRISocket, true)
return errors.Wrap(err, "failed while waiting for the control plane to start")
}

return nil
Expand Down
24 changes: 4 additions & 20 deletions cmd/kubeadm/app/cmd/phases/join/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"path/filepath"
"time"

"github.com/lithammer/dedent"
"github.com/pkg/errors"

v1 "k8s.io/api/core/v1"
Expand All @@ -50,21 +49,6 @@ import (
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
)

var (
kubeadmJoinFailMsg = dedent.Dedent(`
Unfortunately, an error has occurred:
%v
This error is likely caused by:
- The kubelet is not running
- The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
- 'systemctl status kubelet'
- 'journalctl -xeu kubelet'
`)
)

// NewKubeletStartPhase creates a kubeadm workflow phase that start kubelet on a node.
func NewKubeletStartPhase() workflow.Phase {
return workflow.Phase{
Expand Down Expand Up @@ -328,13 +312,13 @@ func runKubeletWaitBootstrapPhase(c workflow.RunData) (returnErr error) {
return errors.New("could not convert the KubeletConfiguration to a typed object")
}
if err := waiter.WaitForKubelet(kubeletConfigTyped.HealthzBindAddress, *kubeletConfigTyped.HealthzPort); err != nil {
fmt.Printf(kubeadmJoinFailMsg, err)
return err
kubeletphase.PrintKubeletErrorHelpScreen(data.OutputWriter(), data.Cfg().NodeRegistration.CRISocket, false)
return errors.Wrap(err, "failed while waiting for the kubelet to start")
}

if err := waitForTLSBootstrappedClient(cfg.Timeouts.TLSBootstrap.Duration); err != nil {
fmt.Printf(kubeadmJoinFailMsg, err)
return err
kubeletphase.PrintKubeletErrorHelpScreen(data.OutputWriter(), data.Cfg().NodeRegistration.CRISocket, false)
return errors.Wrap(err, "failed while waiting for TLS bootstrap")
}

// When we know the /etc/kubernetes/kubelet.conf file is available, get the client
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubeadm/app/cmd/phases/join/waitcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/features"
"k8s.io/kubernetes/cmd/kubeadm/app/phases/kubelet"
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
dryrunutil "k8s.io/kubernetes/cmd/kubeadm/app/util/dryrun"
staticpodutil "k8s.io/kubernetes/cmd/kubeadm/app/util/staticpod"
Expand Down Expand Up @@ -80,7 +81,8 @@ func runWaitControlPlanePhase(c workflow.RunData) error {
}
if err = waiter.WaitForControlPlaneComponents(pods,
data.Cfg().ControlPlane.LocalAPIEndpoint.AdvertiseAddress); err != nil {
return err
kubelet.PrintKubeletErrorHelpScreen(data.OutputWriter(), data.Cfg().NodeRegistration.CRISocket, true)
return errors.Wrap(err, "failed while waiting for the control plane to start")
}

return nil
Expand Down
39 changes: 39 additions & 0 deletions cmd/kubeadm/app/phases/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,37 @@ package kubelet

import (
"fmt"
"io"
"text/template"

"github.com/lithammer/dedent"
"k8s.io/klog/v2"

kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/util/initsystem"
)

var (
kubeletFailMsg = dedent.Dedent(`
Unfortunately, an error has occurred, likely caused by:
- The kubelet is not running
- The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
- 'systemctl status kubelet'
- 'journalctl -xeu kubelet'
`)

controlPlaneFailTempl = template.Must(template.New("init").Parse(dedent.Dedent(`
Additionally, a control plane component may have crashed or exited when started by the container runtime.
To troubleshoot, list all containers using your preferred container runtimes CLI.
Here is one example how you may list all running Kubernetes containers by using crictl:
- 'crictl --runtime-endpoint {{ .Socket }} ps -a | grep kube | grep -v pause'
Once you have found the failing container, you can inspect its logs with:
- 'crictl --runtime-endpoint {{ .Socket }} logs CONTAINERID'
`)))
)

// TryStartKubelet attempts to bring up kubelet service
func TryStartKubelet() {
// If we notice that the kubelet service is inactive, try to start it
Expand All @@ -45,6 +69,21 @@ func TryStartKubelet() {
}
}

// PrintKubeletErrorHelpScreen prints help text on kubelet errors.
func PrintKubeletErrorHelpScreen(outputWriter io.Writer, criSocket string, waitControlPlaneComponents bool) {
context := struct {
Socket string
}{
Socket: criSocket,
}

fmt.Fprintln(outputWriter, kubeletFailMsg)
if waitControlPlaneComponents {
_ = controlPlaneFailTempl.Execute(outputWriter, context)
}
fmt.Println("")
}

// TryStopKubelet attempts to bring down the kubelet service momentarily
func TryStopKubelet() {
// If we notice that the kubelet service is inactive, try to start it
Expand Down

0 comments on commit ae78ec1

Please sign in to comment.