@@ -55,18 +55,23 @@ func (cmd *agentCommand) Bind(parser parser) {
5555 parser .Flag ("host-interface" , "Network interface for pods to configure IPTables." ).Default ("docker0" ).StringVar (& cmd .hostInterface )
5656}
5757
58- func (opts * agentCommand ) Run () {
58+ // run is the actual run implementation.
59+ func (opts * agentCommand ) run () error {
5960 opts .configureLogger ()
6061
6162 if opts .iptables {
6263 log .Infof ("configuring iptables" )
6364 rules := newIPTablesRules (opts .hostIP , opts .ListenPort , opts .hostInterface )
6465 err := rules .Add ()
6566 if err != nil {
66- log .Fatal ("error configuring iptables:" , err .Error ())
67+ log .Error ("error configuring iptables: %s" , err .Error ())
68+ return err
6769 }
6870 if opts .iptablesRemove {
69- defer rules .Remove ()
71+ defer func () {
72+ log .Infof ("undoing iptables changes" )
73+ rules .Remove ()
74+ }()
7075 }
7176 }
7277
@@ -75,27 +80,46 @@ func (opts *agentCommand) Run() {
7580
7681 go opts .telemetryOptions .start (ctx , "agent" )
7782
78- stopChan := make (chan os.Signal )
79- signal .Notify (stopChan , os .Interrupt )
80- signal .Notify (stopChan , syscall .SIGTERM )
83+ stopChan := make (chan os.Signal , 8 )
84+ signal .Notify (stopChan , os .Interrupt , syscall .SIGTERM )
8185
8286 ctxGateway , cancelCtxGateway := context .WithTimeout (context .Background (), opts .timeoutKiamGateway )
8387 defer cancelCtxGateway ()
8488
8589 gateway , err := kiamserver .NewGateway (ctxGateway , opts .serverAddress , opts .caPath , opts .certificatePath , opts .keyPath )
8690 if err != nil {
87- log .Fatalf ("error creating server gateway: %s" , err .Error ())
91+ log .Errorf ("error creating server gateway: %s" , err .Error ())
92+ return err
8893 }
8994 defer gateway .Close ()
9095
9196 server , err := http .NewWebServer (opts .ServerOptions , gateway )
9297 if err != nil {
93- log .Fatalf ("error creating agent http server: %s" , err .Error ())
98+ log .Errorf ("error creating agent http server: %s" , err .Error ())
99+ return err
94100 }
95101
96- go server .Serve ()
102+ errCh := make (chan error , 1 )
103+ go func () {
104+ errCh <- server .Serve ()
105+ }()
97106 defer server .Stop (ctx )
98107
99- <- stopChan
108+ select {
109+ case err := <- errCh :
110+ if err != nil {
111+ log .Error ("error running server: %s" , err .Error ())
112+ return err
113+ }
114+ case sig := <- stopChan :
115+ log .Infof ("received signal (%s) stopping now" , sig .String ())
116+ }
100117 log .Infoln ("stopped" )
118+ return nil
119+ }
120+
121+ func (opts * agentCommand ) Run () {
122+ if err := opts .run (); err != nil {
123+ log .Fatalf ("fatal error: %s" , err .Error ())
124+ }
101125}
0 commit comments