Skip to content

Commit e3ff49a

Browse files
authored
Merge pull request #21021 from Miciah/release-3.11-restore-graceful-shutdown-of-dns-server
[release-3.11] Restore graceful shutdown of DNS server
2 parents a8ab70e + 4c6c0eb commit e3ff49a

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

pkg/cmd/server/start/start_network.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ import (
77
"os"
88
"path/filepath"
99
"strings"
10+
"time"
1011

1112
"github.com/coreos/go-systemd/daemon"
1213
"github.com/golang/glog"
1314
"github.com/openshift/origin/pkg/cmd/server/origin"
1415
"github.com/spf13/cobra"
1516

1617
kerrors "k8s.io/apimachinery/pkg/api/errors"
17-
"k8s.io/apimachinery/pkg/util/wait"
1818
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1919
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2020
"k8s.io/kubernetes/pkg/master/ports"
21+
"k8s.io/kubernetes/pkg/util/interrupt"
2122

2223
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
2324
configapilatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
@@ -57,7 +58,16 @@ func NewCommandStartNetwork(basename string, out, errout io.Writer) (*cobra.Comm
5758
Short: "Launch node network",
5859
Long: fmt.Sprintf(networkLong, basename),
5960
Run: func(c *cobra.Command, args []string) {
60-
options.Run(c, errout, args, wait.NeverStop)
61+
ch := make(chan struct{})
62+
interrupt.New(func(s os.Signal) {
63+
close(ch)
64+
fmt.Fprintf(errout, "interrupt: Gracefully shutting down ...\n")
65+
time.Sleep(200 * time.Millisecond)
66+
os.Exit(1)
67+
}).Run(func() error {
68+
options.Run(c, errout, args, ch)
69+
return nil
70+
})
6171
},
6272
}
6373

@@ -112,9 +122,10 @@ func (o NetworkOptions) Complete(cmd *cobra.Command) error {
112122
return nil
113123
}
114124

115-
// StartNode calls RunNode and then waits forever
125+
// StartNetwork starts the networking processes and then waits until the stop
126+
// channel receives a message or is closed.
116127
func (o NetworkOptions) StartNetwork(stopCh <-chan struct{}) error {
117-
if err := o.RunNetwork(); err != nil {
128+
if err := o.RunNetwork(stopCh); err != nil {
118129
return err
119130
}
120131

@@ -123,10 +134,10 @@ func (o NetworkOptions) StartNetwork(stopCh <-chan struct{}) error {
123134
return nil
124135
}
125136

126-
// RunNetwork takes the options and:
127-
// 1. Reads fully specified node config
128-
// 2. Starts the node based on the fully specified config
129-
func (o NetworkOptions) RunNetwork() error {
137+
// RunNetwork takes the network options and does the following:
138+
// 1. Reads the fully specified node config.
139+
// 2. Starts the node networking based on the fully specified config.
140+
func (o NetworkOptions) RunNetwork(stopCh <-chan struct{}) error {
130141
nodeConfig, configFile, err := o.resolveNodeConfig()
131142
if err != nil {
132143
return err
@@ -175,7 +186,7 @@ func (o NetworkOptions) RunNetwork() error {
175186
return err
176187
}
177188

178-
return StartNetwork(*nodeConfig, o.NodeArgs.Components)
189+
return StartNetwork(*nodeConfig, o.NodeArgs.Components, stopCh)
179190
}
180191

181192
// resolveNodeConfig creates a new configuration on disk by reading from the master, reads
@@ -191,8 +202,8 @@ func (o NetworkOptions) resolveNodeConfig() (*configapi.NodeConfig, string, erro
191202
return cfg, o.ConfigFile, err
192203
}
193204

194-
// StartNetwork launches the node processes.
195-
func StartNetwork(nodeConfig configapi.NodeConfig, components *utilflags.ComponentFlag) error {
205+
// StartNetwork launches the node networking processes.
206+
func StartNetwork(nodeConfig configapi.NodeConfig, components *utilflags.ComponentFlag, stopCh <-chan struct{}) error {
196207
glog.Infof("Starting node networking %s (%s)", nodeConfig.NodeName, version.Get().String())
197208

198209
proxyConfig, err := networkoptions.Build(nodeConfig)
@@ -217,12 +228,12 @@ func StartNetwork(nodeConfig configapi.NodeConfig, components *utilflags.Compone
217228
networkConfig.RunProxy()
218229
}
219230
if components.Enabled(ComponentDNS) && networkConfig.DNSServer != nil {
220-
networkConfig.RunDNS(wait.NeverStop)
231+
networkConfig.RunDNS(stopCh)
221232
}
222233

223-
networkConfig.InternalKubeInformers.Start(wait.NeverStop)
234+
networkConfig.InternalKubeInformers.Start(stopCh)
224235
if networkConfig.NetworkInformers != nil {
225-
networkConfig.NetworkInformers.Start(wait.NeverStop)
236+
networkConfig.NetworkInformers.Start(stopCh)
226237
}
227238

228239
return nil

0 commit comments

Comments
 (0)