Skip to content

Commit 7fd3db2

Browse files
fix: restart vCluster if k8s exits (#2647) (#2653)
(cherry picked from commit b2d8b7d) Co-authored-by: Fabian Kramm <fab.kramm@googlemail.com>
1 parent 22a00a7 commit 7fd3db2

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

pkg/k8s/k8s.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/loft-sh/vcluster/pkg/etcd"
1818
"github.com/loft-sh/vcluster/pkg/pro"
1919
"github.com/loft-sh/vcluster/pkg/util/commandwriter"
20-
"golang.org/x/sync/errgroup"
2120
"k8s.io/apimachinery/pkg/util/wait"
2221
"k8s.io/klog/v2"
2322
)
@@ -30,7 +29,7 @@ func StartK8S(
3029
scheduler vclusterconfig.DistroContainer,
3130
vConfig *config.VirtualClusterConfig,
3231
) error {
33-
eg := &errgroup.Group{}
32+
errChan := make(chan error, 1)
3433

3534
// start the backing store
3635
etcdEndpoints, etcdCertificates, err := StartBackingStore(ctx, vConfig)
@@ -40,7 +39,7 @@ func StartK8S(
4039

4140
// start api server first
4241
if apiServer.Enabled {
43-
eg.Go(func() error {
42+
go func() {
4443
// build flags
4544
issuer := "https://kubernetes.default.svc.cluster.local"
4645

@@ -89,12 +88,13 @@ func StartK8S(
8988
// wait until etcd is up and running
9089
err := etcd.WaitForEtcd(ctx, etcdCertificates, etcdEndpoints)
9190
if err != nil {
92-
return err
91+
errChan <- err
92+
return
9393
}
9494

9595
// now start the api server
96-
return RunCommand(ctx, args, "apiserver")
97-
})
96+
errChan <- RunCommand(ctx, args, "apiserver")
97+
}()
9898
}
9999

100100
// wait for api server to be up as otherwise controller and scheduler might fail
@@ -105,7 +105,7 @@ func StartK8S(
105105

106106
// start controller command
107107
if controllerManager.Enabled {
108-
eg.Go(func() error {
108+
go func() {
109109
// build flags
110110
args := []string{}
111111
if len(controllerManager.Command) > 0 {
@@ -145,13 +145,13 @@ func StartK8S(
145145

146146
// add extra args
147147
args = append(args, controllerManager.ExtraArgs...)
148-
return RunCommand(ctx, args, "controller-manager")
149-
})
148+
errChan <- RunCommand(ctx, args, "controller-manager")
149+
}()
150150
}
151151

152152
// start scheduler command
153153
if vConfig.ControlPlane.Advanced.VirtualScheduler.Enabled {
154-
eg.Go(func() error {
154+
go func() {
155155
// build flags
156156
args := []string{}
157157
if len(scheduler.Command) > 0 {
@@ -171,18 +171,11 @@ func StartK8S(
171171

172172
// add extra args
173173
args = append(args, scheduler.ExtraArgs...)
174-
return RunCommand(ctx, args, "scheduler")
175-
})
174+
errChan <- RunCommand(ctx, args, "scheduler")
175+
}()
176176
}
177177

178-
// regular stop case, will return as soon as a component returns an error.
179-
// we don't expect the components to stop by themselves since they're supposed
180-
// to run until killed or until they fail
181-
err = eg.Wait()
182-
if err == nil || err.Error() == "signal: killed" {
183-
return nil
184-
}
185-
return err
178+
return <-errChan
186179
}
187180

188181
func StartKine(ctx context.Context, dataSource, listenAddress string, certificates *etcd.Certificates) {
@@ -300,7 +293,7 @@ func RunCommand(ctx context.Context, command []string, component string) error {
300293

301294
// make sure we wait for scanner to be done
302295
writer.CloseAndWait(ctx, err)
303-
return err
296+
return fmt.Errorf("error running command %s: %w", command[0], err)
304297
}
305298

306299
// waits for the api to be up, ignoring certs and calling it

0 commit comments

Comments
 (0)