Skip to content

Commit ab02cda

Browse files
committed
make error consistent
1 parent a02fe24 commit ab02cda

File tree

5 files changed

+49
-61
lines changed

5 files changed

+49
-61
lines changed

cmd/kubeadm/app/cmd/phases/init/waitcontrolplane.go

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ package phases
1919
import (
2020
"fmt"
2121
"io"
22-
"text/template"
2322
"time"
2423

25-
"github.com/lithammer/dedent"
2624
"github.com/pkg/errors"
2725

2826
v1 "k8s.io/api/core/v1"
@@ -38,28 +36,6 @@ import (
3836
staticpodutil "k8s.io/kubernetes/cmd/kubeadm/app/util/staticpod"
3937
)
4038

41-
var (
42-
kubeletFailTempl = template.Must(template.New("init").Parse(dedent.Dedent(`
43-
Unfortunately, an error has occurred:
44-
{{ .Error }}
45-
46-
This error is likely caused by:
47-
- The kubelet is not running
48-
- The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
49-
50-
If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
51-
- 'systemctl status kubelet'
52-
- 'journalctl -xeu kubelet'
53-
54-
Additionally, a control plane component may have crashed or exited when started by the container runtime.
55-
To troubleshoot, list all containers using your preferred container runtimes CLI.
56-
Here is one example how you may list all running Kubernetes containers by using crictl:
57-
- 'crictl --runtime-endpoint {{ .Socket }} ps -a | grep kube | grep -v pause'
58-
Once you have found the failing container, you can inspect its logs with:
59-
- 'crictl --runtime-endpoint {{ .Socket }} logs CONTAINERID'
60-
`)))
61-
)
62-
6339
// NewWaitControlPlanePhase is a hidden phase that runs after the control-plane and etcd phases
6440
func NewWaitControlPlanePhase() workflow.Phase {
6541
phase := workflow.Phase{
@@ -102,27 +78,15 @@ func runWaitControlPlanePhase(c workflow.RunData) error {
10278
" from directory %q\n",
10379
data.ManifestDir())
10480

105-
handleError := func(err error) error {
106-
context := struct {
107-
Error string
108-
Socket string
109-
}{
110-
Error: fmt.Sprintf("%v", err),
111-
Socket: data.Cfg().NodeRegistration.CRISocket,
112-
}
113-
114-
kubeletFailTempl.Execute(data.OutputWriter(), context)
115-
return errors.New("could not initialize a Kubernetes cluster")
116-
}
117-
11881
waiter.SetTimeout(data.Cfg().Timeouts.KubeletHealthCheck.Duration)
11982
kubeletConfig := data.Cfg().ClusterConfiguration.ComponentConfigs[componentconfigs.KubeletGroup].Get()
12083
kubeletConfigTyped, ok := kubeletConfig.(*kubeletconfig.KubeletConfiguration)
12184
if !ok {
12285
return errors.New("could not convert the KubeletConfiguration to a typed object")
12386
}
12487
if err := waiter.WaitForKubelet(kubeletConfigTyped.HealthzBindAddress, *kubeletConfigTyped.HealthzPort); err != nil {
125-
return handleError(err)
88+
apiclient.PrintKubeletErrorHelpScreen(data.OutputWriter())
89+
return errors.Wrap(err, "failed while waiting for the kubelet to start")
12690
}
12791

12892
var podMap map[string]*v1.Pod
@@ -138,7 +102,8 @@ func runWaitControlPlanePhase(c workflow.RunData) error {
138102
err = waiter.WaitForAPI()
139103
}
140104
if err != nil {
141-
return handleError(err)
105+
apiclient.PrintControlPlaneErrorHelpScreen(data.OutputWriter(), data.Cfg().NodeRegistration.CRISocket)
106+
return errors.Wrap(err, "failed while waiting for the control plane to start")
142107
}
143108

144109
return nil

cmd/kubeadm/app/cmd/phases/join/kubelet.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"path/filepath"
2424
"time"
2525

26-
"github.com/lithammer/dedent"
2726
"github.com/pkg/errors"
2827

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

53-
var (
54-
kubeadmJoinFailMsg = dedent.Dedent(`
55-
Unfortunately, an error has occurred:
56-
%v
57-
58-
This error is likely caused by:
59-
- The kubelet is not running
60-
- The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
61-
62-
If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
63-
- 'systemctl status kubelet'
64-
- 'journalctl -xeu kubelet'
65-
`)
66-
)
67-
6852
// NewKubeletStartPhase creates a kubeadm workflow phase that start kubelet on a node.
6953
func NewKubeletStartPhase() workflow.Phase {
7054
return workflow.Phase{
@@ -328,13 +312,13 @@ func runKubeletWaitBootstrapPhase(c workflow.RunData) (returnErr error) {
328312
return errors.New("could not convert the KubeletConfiguration to a typed object")
329313
}
330314
if err := waiter.WaitForKubelet(kubeletConfigTyped.HealthzBindAddress, *kubeletConfigTyped.HealthzPort); err != nil {
331-
fmt.Printf(kubeadmJoinFailMsg, err)
332-
return err
315+
apiclient.PrintKubeletErrorHelpScreen(data.OutputWriter())
316+
return errors.Wrap(err, "failed while waiting for the kubelet to start")
333317
}
334318

335319
if err := waitForTLSBootstrappedClient(cfg.Timeouts.TLSBootstrap.Duration); err != nil {
336-
fmt.Printf(kubeadmJoinFailMsg, err)
337-
return err
320+
apiclient.PrintKubeletErrorHelpScreen(data.OutputWriter())
321+
return errors.Wrap(err, "failed while waiting for TLS bootstrap")
338322
}
339323

340324
// When we know the /etc/kubernetes/kubelet.conf file is available, get the client

cmd/kubeadm/app/cmd/phases/join/waitcontrolplane.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func runWaitControlPlanePhase(c workflow.RunData) error {
8080
}
8181
if err = waiter.WaitForControlPlaneComponents(pods,
8282
data.Cfg().ControlPlane.LocalAPIEndpoint.AdvertiseAddress); err != nil {
83-
return err
83+
apiclient.PrintControlPlaneErrorHelpScreen(data.OutputWriter(), data.Cfg().NodeRegistration.CRISocket)
84+
return errors.Wrap(err, "failed while waiting for the control plane to start")
8485
}
8586

8687
return nil

cmd/kubeadm/app/phases/kubelet/kubelet.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package kubelet
1818

1919
import (
2020
"fmt"
21-
2221
"k8s.io/klog/v2"
2322

2423
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"

cmd/kubeadm/app/util/apiclient/wait.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import (
2424
"net"
2525
"net/http"
2626
"strings"
27+
"text/template"
2728
"time"
2829

30+
"github.com/lithammer/dedent"
2931
"github.com/pkg/errors"
3032

3133
v1 "k8s.io/api/core/v1"
@@ -52,6 +54,27 @@ const (
5254
argAdvertiseAddress = "advertise-address"
5355
)
5456

57+
var (
58+
controlPlaneFailTempl = template.Must(template.New("init").Parse(dedent.Dedent(`
59+
A control plane component may have crashed or exited when started by the container runtime.
60+
To troubleshoot, list all containers using your preferred container runtimes CLI.
61+
Here is one example how you may list all running Kubernetes containers by using crictl:
62+
- 'crictl --runtime-endpoint {{ .Socket }} ps -a | grep kube | grep -v pause'
63+
Once you have found the failing container, you can inspect its logs with:
64+
- 'crictl --runtime-endpoint {{ .Socket }} logs CONTAINERID'
65+
`)))
66+
67+
kubeletFailMsg = dedent.Dedent(`
68+
Unfortunately, an error has occurred, likely caused by:
69+
- The kubelet is not running
70+
- The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
71+
72+
If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
73+
- 'systemctl status kubelet'
74+
- 'journalctl -xeu kubelet'
75+
`)
76+
)
77+
5578
// Waiter is an interface for waiting for criteria in Kubernetes to happen
5679
type Waiter interface {
5780
// WaitForControlPlaneComponents waits for all control plane components to be ready.
@@ -496,3 +519,19 @@ func getStaticPodSingleHash(client clientset.Interface, nodeName string, compone
496519
staticPodHash := staticPod.Annotations["kubernetes.io/config.hash"]
497520
return staticPodHash, nil
498521
}
522+
523+
// PrintControlPlaneErrorHelpScreen prints help text on wait ControlPlane components errors.
524+
func PrintControlPlaneErrorHelpScreen(outputWriter io.Writer, criSocket string) {
525+
context := struct {
526+
Socket string
527+
}{
528+
Socket: criSocket,
529+
}
530+
_ = controlPlaneFailTempl.Execute(outputWriter, context)
531+
fmt.Println("")
532+
}
533+
534+
// PrintKubeletErrorHelpScreen prints help text on kubelet errors.
535+
func PrintKubeletErrorHelpScreen(outputWriter io.Writer) {
536+
fmt.Fprintln(outputWriter, kubeletFailMsg)
537+
}

0 commit comments

Comments
 (0)