Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit a743cc0

Browse files
committed
start: add --required-pods flag
1 parent 22d32ca commit a743cc0

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

cmd/bootkube/start.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"errors"
5+
"fmt"
6+
"strings"
57

68
"github.com/spf13/cobra"
79

@@ -22,21 +24,31 @@ var (
2224
assetDir string
2325
podManifestPath string
2426
strict bool
27+
requiredPods []string
2528
}
2629
)
2730

31+
var defaultRequiredPods = []string{
32+
"kube-system/pod-checkpointer",
33+
"kube-system/kube-apiserver",
34+
"kube-system/kube-scheduler",
35+
"kube-system/kube-controller-manager",
36+
}
37+
2838
func init() {
2939
cmdRoot.AddCommand(cmdStart)
3040
cmdStart.Flags().StringVar(&startOpts.assetDir, "asset-dir", "", "Path to the cluster asset directory. Expected layout genereted by the `bootkube render` command.")
3141
cmdStart.Flags().StringVar(&startOpts.podManifestPath, "pod-manifest-path", "/etc/kubernetes/manifests", "The location where the kubelet is configured to look for static pod manifests.")
3242
cmdStart.Flags().BoolVar(&startOpts.strict, "strict", false, "Strict mode will cause bootkube to exit early if any manifests in the asset directory cannot be created.")
43+
cmdStart.Flags().StringSliceVar(&startOpts.requiredPods, "required-pods", defaultRequiredPods, "List of pods with their namespace (written as <namespace>/<pod-name>) that are required to be running before the start command does the pivot.")
3344
}
3445

3546
func runCmdStart(cmd *cobra.Command, args []string) error {
3647
bk, err := bootkube.NewBootkube(bootkube.Config{
3748
AssetDir: startOpts.assetDir,
3849
PodManifestPath: startOpts.podManifestPath,
3950
Strict: startOpts.strict,
51+
RequiredPods: startOpts.requiredPods,
4052
})
4153
if err != nil {
4254
return err
@@ -57,5 +69,10 @@ func validateStartOpts(cmd *cobra.Command, args []string) error {
5769
if startOpts.assetDir == "" {
5870
return errors.New("missing required flag: --asset-dir")
5971
}
72+
for _, nsPod := range startOpts.requiredPods {
73+
if len(strings.Split(nsPod, "/")) != 2 {
74+
return fmt.Errorf("invalid required pod: expected %q to be of shape <namespace>/<pod-name>", nsPod)
75+
}
76+
}
6077
return nil
6178
}

pkg/bootkube/bootkube.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,26 @@ import (
1212

1313
const assetTimeout = 20 * time.Minute
1414

15-
var requiredPods = []string{
16-
"pod-checkpointer",
17-
"kube-apiserver",
18-
"kube-scheduler",
19-
"kube-controller-manager",
20-
}
21-
2215
type Config struct {
2316
AssetDir string
2417
PodManifestPath string
2518
Strict bool
19+
RequiredPods []string
2620
}
2721

2822
type bootkube struct {
2923
podManifestPath string
3024
assetDir string
3125
strict bool
26+
requiredPods []string
3227
}
3328

3429
func NewBootkube(config Config) (*bootkube, error) {
3530
return &bootkube{
3631
assetDir: config.AssetDir,
3732
podManifestPath: config.PodManifestPath,
3833
strict: config.Strict,
34+
requiredPods: config.RequiredPods,
3935
}, nil
4036
}
4137

@@ -71,7 +67,7 @@ func (b *bootkube) Run() error {
7167
return err
7268
}
7369

74-
if err = WaitUntilPodsRunning(kubeConfig, requiredPods, assetTimeout); err != nil {
70+
if err = WaitUntilPodsRunning(kubeConfig, b.requiredPods, assetTimeout); err != nil {
7571
return err
7672
}
7773

pkg/bootkube/status.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package bootkube
22

33
import (
44
"fmt"
5-
"path"
65
"reflect"
76
"strings"
87
"time"
@@ -65,10 +64,10 @@ func (s *statusController) Run() {
6564
podStore, podController := cache.NewInformer(
6665
&cache.ListWatch{
6766
ListFunc: func(lo metav1.ListOptions) (runtime.Object, error) {
68-
return s.client.Core().Pods("kube-system").List(options)
67+
return s.client.Core().Pods("").List(options)
6968
},
7069
WatchFunc: func(lo metav1.ListOptions) (watch.Interface, error) {
71-
return s.client.Core().Pods("kube-system").Watch(options)
70+
return s.client.Core().Pods("").Watch(options)
7271
},
7372
},
7473
&v1.Pod{},
@@ -110,24 +109,24 @@ func (s *statusController) PodStatus() (map[string]v1.PodPhase, error) {
110109
status := make(map[string]v1.PodPhase)
111110

112111
podNames := s.podStore.ListKeys()
113-
for _, pod := range s.watchPods {
112+
for _, watchedPod := range s.watchPods {
114113
// Pod names are suffixed with random data. Match on prefix
115-
podName := pod
116114
for _, pn := range podNames {
117-
if strings.HasPrefix(pn, path.Join("kube-system", pod)) {
118-
podName = pn
115+
if strings.HasPrefix(pn, watchedPod) {
116+
watchedPod = pn
117+
break
119118
}
120119
}
121-
p, exists, err := s.podStore.GetByKey(podName)
120+
p, exists, err := s.podStore.GetByKey(watchedPod)
122121
if err != nil {
123122
return nil, err
124123
}
125124
if !exists {
126-
status[pod] = doesNotExist
125+
status[watchedPod] = doesNotExist
127126
continue
128127
}
129128
if p, ok := p.(*v1.Pod); ok {
130-
status[pod] = p.Status.Phase
129+
status[watchedPod] = p.Status.Phase
131130
}
132131
}
133132
return status, nil

0 commit comments

Comments
 (0)