1/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package phases
18
19import (
20	"fmt"
21
22	"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
23	"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
24	cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
25	"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
26
27	utilsexec "k8s.io/utils/exec"
28
29	"github.com/pkg/errors"
30)
31
32var (
33	preflightExample = cmdutil.Examples(`
34		# Run pre-flight checks for kubeadm init using a config file.
35		kubeadm init phase preflight --config kubeadm-config.yaml
36		`)
37)
38
39// NewPreflightPhase creates a kubeadm workflow phase that implements preflight checks for a new control-plane node.
40func NewPreflightPhase() workflow.Phase {
41	return workflow.Phase{
42		Name:    "preflight",
43		Short:   "Run pre-flight checks",
44		Long:    "Run pre-flight checks for kubeadm init.",
45		Example: preflightExample,
46		Run:     runPreflight,
47		InheritFlags: []string{
48			options.CfgPath,
49			options.IgnorePreflightErrors,
50		},
51	}
52}
53
54// runPreflight executes preflight checks logic.
55func runPreflight(c workflow.RunData) error {
56	data, ok := c.(InitData)
57	if !ok {
58		return errors.New("preflight phase invoked with an invalid data struct")
59	}
60
61	fmt.Println("[preflight] Running pre-flight checks")
62	if err := preflight.RunInitNodeChecks(utilsexec.New(), data.Cfg(), data.IgnorePreflightErrors(), false, false); err != nil {
63		return err
64	}
65
66	if !data.DryRun() {
67		fmt.Println("[preflight] Pulling images required for setting up a Kubernetes cluster")
68		fmt.Println("[preflight] This might take a minute or two, depending on the speed of your internet connection")
69		fmt.Println("[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'")
70		if err := preflight.RunPullImagesCheck(utilsexec.New(), data.Cfg(), data.IgnorePreflightErrors()); err != nil {
71			return err
72		}
73	} else {
74		fmt.Println("[preflight] Would pull the required images (like 'kubeadm config images pull')")
75	}
76
77	return nil
78}
79