@@ -33,7 +33,7 @@ import (
33
33
"time"
34
34
35
35
"github.com/Masterminds/semver"
36
- homedir "github.com/mitchellh/go-homedir"
36
+ "github.com/mitchellh/go-homedir"
37
37
consolev1typedclient "github.com/openshift/client-go/console/clientset/versioned/typed/console/v1"
38
38
consolev1alpha1typedclient "github.com/openshift/client-go/console/clientset/versioned/typed/console/v1alpha1"
39
39
operatorv1typedclient "github.com/openshift/client-go/operator/clientset/versioned/typed/operator/v1"
@@ -56,7 +56,6 @@ type containerEngineInterface interface {
56
56
pullImage (imageName string ) error
57
57
putFileToMount (filename string , content []byte ) error
58
58
stopContainer (containerName string ) error
59
- // some container engines have special handlings for different types of containers
60
59
runConsoleContainer (containerName string , port string , consoleArgs []string , envVars []envVar ) error
61
60
runMonitorPlugin (containerName string , consoleContainerName string , nginxConf string , pluginArgs []string ) error
62
61
containerIsExist (containerName string ) (bool , error )
@@ -70,7 +69,7 @@ type dockerLinux struct{}
70
69
type dockerMac struct {}
71
70
72
71
type execActionOnTermInterface interface {
73
- execActionOnTerminationFunction (action postTerminationAction ) error
72
+ execActionOnTerminationFunction (action postTerminateFunc ) error
74
73
}
75
74
76
75
type execActionOnTermStruct struct {}
@@ -101,7 +100,7 @@ const (
101
100
PODMAN = "podman"
102
101
// Linux name in runtime.GOOS
103
102
LINUX = "linux"
104
- // MacOS name in runtime.GOOS
103
+ // MACOS name in runtime.GOOS
105
104
MACOS = "darwin"
106
105
107
106
// Environment variable that indicates if open by browser is set as default
@@ -137,12 +136,12 @@ var (
137
136
138
137
// Pull Secret saving directory
139
138
pullSecretConfigDirectory string
140
-
141
- ctx , cancel = context .WithCancel (context .Background ())
142
139
)
143
140
144
141
func newConsoleOptions () * consoleOptions {
145
- return & consoleOptions {terminationFunction : & execActionOnTermStruct {}}
142
+ return & consoleOptions {
143
+ terminationFunction : & execActionOnTermStruct {},
144
+ }
146
145
}
147
146
148
147
func NewConsoleCmd () * cobra.Command {
@@ -157,7 +156,9 @@ func NewConsoleCmd() *cobra.Command {
157
156
You can specify container engine with -c. If not specified, it will lookup the PATH in the order of podman and docker.
158
157
` ,
159
158
SilenceUsage : true ,
160
- RunE : ops .run ,
159
+ RunE : func (cmd * cobra.Command , args []string ) error {
160
+ return ops .run ()
161
+ },
161
162
}
162
163
163
164
flags := consoleCmd .Flags ()
@@ -205,7 +206,7 @@ func NewConsoleCmd() *cobra.Command {
205
206
return consoleCmd
206
207
}
207
208
208
- func (o * consoleOptions ) run (cmd * cobra. Command , argv [] string ) error {
209
+ func (o * consoleOptions ) run () error {
209
210
err := o .determineOpenBrowser ()
210
211
if err != nil {
211
212
return err
@@ -222,7 +223,6 @@ func (o *consoleOptions) run(cmd *cobra.Command, argv []string) error {
222
223
if err != nil {
223
224
return err
224
225
}
225
- // finialize the console image url
226
226
err = o .determineImage (kubeconfig )
227
227
if err != nil {
228
228
return err
@@ -249,16 +249,14 @@ func (o *consoleOptions) run(cmd *cobra.Command, argv []string) error {
249
249
if err != nil {
250
250
return err
251
251
}
252
- //Perform a cleanup before starting a new console
252
+ // Perform a cleanup before starting a new console
253
253
err = o .beforeStartCleanUp (ce )
254
254
if err != nil {
255
255
return err
256
256
}
257
- done := make (chan bool )
258
- errs := make (chan error )
259
-
260
- go o .runContainers (ce , done , errs )
261
257
258
+ errs := make (chan error )
259
+ go o .runContainers (ce , errs )
262
260
if len (errs ) != 0 {
263
261
err := <- errs
264
262
return err
@@ -272,9 +270,8 @@ func (o *consoleOptions) run(cmd *cobra.Command, argv []string) error {
272
270
return nil
273
271
}
274
272
275
- func (o * consoleOptions ) runContainers (ce containerEngineInterface , done chan bool , errs chan <- error ) {
273
+ func (o * consoleOptions ) runContainers (ce containerEngineInterface , errs chan <- error ) {
276
274
if err := o .runConsoleContainer (ce ); err != nil {
277
-
278
275
errs <- err
279
276
return
280
277
}
@@ -286,8 +283,6 @@ func (o *consoleOptions) runContainers(ce containerEngineInterface, done chan bo
286
283
if err := o .printURL (); err != nil {
287
284
errs <- err
288
285
}
289
-
290
- close (done )
291
286
}
292
287
293
288
// Parse environment variables
@@ -374,7 +369,6 @@ func (o *consoleOptions) getContainerEngineImpl() (containerEngineInterface, err
374
369
} else if runtime .GOOS == LINUX && containerEngine == DOCKER {
375
370
containerEngineImpl = & dockerLinux {}
376
371
} else if runtime .GOOS == MACOS && containerEngine == DOCKER {
377
- logger .Warnln ("Docker on MacOS is not tested" )
378
372
containerEngineImpl = & dockerMac {}
379
373
}
380
374
return containerEngineImpl , nil
@@ -555,7 +549,7 @@ func (o *consoleOptions) runConsoleContainer(ce containerEngineInterface) error
555
549
556
550
bridgeListen := fmt .Sprintf ("http://0.0.0.0:%s" , o .port )
557
551
558
- envVars := []envVar {}
552
+ var envVars []envVar
559
553
// Set proxy URL to the container
560
554
proxyURL , err := getProxyURL ()
561
555
if err != nil {
@@ -677,17 +671,19 @@ func (o *consoleOptions) beforeStartCleanUp(ce containerEngineInterface) error {
677
671
return nil
678
672
}
679
673
674
+ // cleanUp will first populate the containers needed to clean up, then call the blocking function
675
+ // o.terminateFunction, which will block until a system signal is received.
680
676
func (o * consoleOptions ) cleanUp (ce containerEngineInterface ) error {
681
677
clusterID , err := getClusterID ()
682
678
if err != nil {
683
679
return err
684
680
}
685
681
686
- containersToCleanUp := []string {}
682
+ var containersToCleanUp []string
687
683
688
684
// forcing order of removal as the order is not deterministic between container engines
689
685
if o .needMonitorPlugin {
690
- logger .Debugln ("monitoring plugin is needed, need to clean up monitoring plugin first " )
686
+ logger .Debugln ("adding monitoring plugin to containers for cleanup " )
691
687
containersToCleanUp = append (containersToCleanUp , fmt .Sprintf ("monitoring-plugin-%s" , clusterID ))
692
688
}
693
689
containersToCleanUp = append (containersToCleanUp , fmt .Sprintf ("console-%s" , clusterID ))
@@ -698,7 +694,6 @@ func (o *consoleOptions) cleanUp(ce containerEngineInterface) error {
698
694
o .terminationFunction = & execActionOnTermStruct {}
699
695
}
700
696
701
- <- ctx .Done ()
702
697
err = o .terminationFunction .execActionOnTerminationFunction (func () error {
703
698
for _ , c := range containersToCleanUp {
704
699
exist , err := ce .containerIsExist (c )
@@ -911,9 +906,6 @@ func GetConfigDirectory() (string, error) {
911
906
912
907
// Update config directory default path
913
908
pullSecretConfigDirectory = filepath .Join (home , ".kube/ocm-pull-secret" )
914
- if err != nil {
915
- return "" , fmt .Errorf ("can't modify config directory. Error: %s" , err .Error ())
916
- }
917
909
}
918
910
919
911
return pullSecretConfigDirectory , nil
@@ -979,24 +971,19 @@ func replaceConsoleURL(containerURL string, userProvidedURL string) (string, err
979
971
return u .String (), nil
980
972
}
981
973
982
- type postTerminationAction func () error
974
+ type postTerminateFunc func () error
983
975
984
976
// keep the program running in frontend and wait for ctrl-c
985
- func (e * execActionOnTermStruct ) execActionOnTerminationFunction (action postTerminationAction ) error {
977
+ func (e * execActionOnTermStruct ) execActionOnTerminationFunction (action postTerminateFunc ) error {
986
978
sigs := make (chan os.Signal , 1 )
987
979
signal .Notify (sigs , syscall .SIGINT , syscall .SIGTERM )
988
- done := make (chan bool , 1 )
989
980
sig := <- sigs
990
- fmt .Println (sig )
991
- done <- true
992
- err := action ()
993
- if err != nil {
994
- return err
995
- }
996
- return nil
981
+ fmt .Printf ("System signal '%v' received, cleaning up containers and exiting...\n " , sig )
982
+
983
+ return action ()
997
984
}
998
985
999
- // ---- Contianer Engine Implementations ---- //
986
+ // ---- Container Engine Implementations ---- //
1000
987
1001
988
// podman pull - pull the image to local from registry
1002
989
// this action is OS independent
@@ -1070,7 +1057,7 @@ func (ce *dockerMac) pullImage(imageName string) error {
1070
1057
return generalDockerPullImage (imageName )
1071
1058
}
1072
1059
1073
- // the shared function for podman to run console container for both linux and macos
1060
+ // the shared function for podman to run console container for both linux and macOS
1074
1061
func podmanRunConsoleContainer (containerName string , port string , consoleArgs []string , envVars []envVar ) error {
1075
1062
_ , authFilename , err := fetchPullSecretIfNotExist ()
1076
1063
if err != nil {
@@ -1093,15 +1080,11 @@ func podmanRunConsoleContainer(containerName string, port string, consoleArgs []
1093
1080
engRunArgs = append (engRunArgs , consoleArgs ... )
1094
1081
logger .WithField ("Command" , fmt .Sprintf ("`%s %s`" , PODMAN , strings .Join (engRunArgs , " " ))).Infoln ("Running container" )
1095
1082
1096
- runCmd := exec .CommandContext (ctx , PODMAN , engRunArgs ... )
1097
- defer cancel ()
1083
+ runCmd := createCommand (PODMAN , engRunArgs ... )
1098
1084
runCmd .Stderr = os .Stderr
1099
1085
runCmd .Stdout = nil
1100
- err = runCmd .Run ()
1101
- if err != nil {
1102
- return err
1103
- }
1104
- return nil
1086
+
1087
+ return runCmd .Run ()
1105
1088
}
1106
1089
1107
1090
func (ce * podmanMac ) runConsoleContainer (containerName string , port string , consoleArgs []string , envVars []envVar ) error {
@@ -1112,7 +1095,7 @@ func (ce *podmanLinux) runConsoleContainer(containerName string, port string, co
1112
1095
return podmanRunConsoleContainer (containerName , port , consoleArgs , envVars )
1113
1096
}
1114
1097
1115
- // the shared function for docker to run console container for both linux and macos
1098
+ // the shared function for docker to run console container for both linux and macOS
1116
1099
func dockerRunConsoleContainer (containerName string , port string , consoleArgs []string , envVars []envVar ) error {
1117
1100
configDirectory , _ , err := fetchPullSecretIfNotExist ()
1118
1101
if err != nil {
@@ -1138,15 +1121,12 @@ func dockerRunConsoleContainer(containerName string, port string, consoleArgs []
1138
1121
}
1139
1122
engRunArgs = append (engRunArgs , consoleArgs ... )
1140
1123
logger .WithField ("Command" , fmt .Sprintf ("`%s %s`" , DOCKER , strings .Join (engRunArgs , " " ))).Infoln ("Running container" )
1124
+
1141
1125
runCmd := createCommand (DOCKER , engRunArgs ... )
1142
1126
runCmd .Stderr = os .Stderr
1143
1127
runCmd .Stdout = nil
1144
1128
1145
- err = runCmd .Run ()
1146
- if err != nil {
1147
- return err
1148
- }
1149
- return nil
1129
+ return runCmd .Run ()
1150
1130
}
1151
1131
1152
1132
func (ce * dockerMac ) runConsoleContainer (containerName string , port string , consoleArgs []string , envVars []envVar ) error {
@@ -1157,7 +1137,7 @@ func (ce *dockerLinux) runConsoleContainer(containerName string, port string, co
1157
1137
return dockerRunConsoleContainer (containerName , port , consoleArgs , envVars )
1158
1138
}
1159
1139
1160
- // the shared function for podman to run monitoring plugin for both linux and macos
1140
+ // the shared function for podman to run monitoring plugin for both linux and macOS
1161
1141
func podmanRunMonitorPlugin (containerName string , consoleContainerName string , nginxConfPath string , pluginArgs []string ) error {
1162
1142
_ , authFilename , err := fetchPullSecretIfNotExist ()
1163
1143
if err != nil {
@@ -1180,11 +1160,7 @@ func podmanRunMonitorPlugin(containerName string, consoleContainerName string, n
1180
1160
runCmd .Stderr = os .Stderr
1181
1161
runCmd .Stdout = nil
1182
1162
1183
- err = runCmd .Run ()
1184
- if err != nil {
1185
- return err
1186
- }
1187
- return nil
1163
+ return runCmd .Run ()
1188
1164
}
1189
1165
1190
1166
func (ce * podmanMac ) runMonitorPlugin (containerName string , consoleContainerName string , nginxConf string , pluginArgs []string ) error {
@@ -1197,8 +1173,8 @@ func (ce *podmanLinux) runMonitorPlugin(containerName string, consoleContainerNa
1197
1173
return podmanRunMonitorPlugin (containerName , consoleContainerName , nginxConfPath , pluginArgs )
1198
1174
}
1199
1175
1200
- // the shared function for docker to run monitoring plugin for both linux and macos
1201
- func dockerRunMonitorPlugin (containerName string , consoleContainerName string , nginxConfPath string , pluginArgs []string ) error {
1176
+ // the shared function for docker to run monitoring plugin for both linux and macOS
1177
+ func dockerRunMonitorPlugin (containerName string , _ string , nginxConfPath string , pluginArgs []string ) error {
1202
1178
configDirectory , _ , err := fetchPullSecretIfNotExist ()
1203
1179
if err != nil {
1204
1180
return err
@@ -1220,11 +1196,7 @@ func dockerRunMonitorPlugin(containerName string, consoleContainerName string, n
1220
1196
runCmd .Stderr = os .Stderr
1221
1197
runCmd .Stdout = nil
1222
1198
1223
- err = runCmd .Run ()
1224
- if err != nil {
1225
- return err
1226
- }
1227
- return nil
1199
+ return runCmd .Run ()
1228
1200
}
1229
1201
1230
1202
func (ce * dockerLinux ) runMonitorPlugin (containerName string , consoleContainerName string , nginxConf string , pluginArgs []string ) error {
@@ -1385,7 +1357,7 @@ func (ce *podmanLinux) stopContainer(containerName string) error {
1385
1357
return generalStopContainer (PODMAN , containerName )
1386
1358
}
1387
1359
1388
- // podman-stop for MacOS
1360
+ // podman-stop for macOS
1389
1361
func (ce * podmanMac ) stopContainer (containerName string ) error {
1390
1362
return generalStopContainer (PODMAN , containerName )
1391
1363
}
@@ -1395,7 +1367,7 @@ func (ce *dockerLinux) stopContainer(containerName string) error {
1395
1367
return generalStopContainer (DOCKER , containerName )
1396
1368
}
1397
1369
1398
- // docker-stop for MacOS
1370
+ // docker-stop for macOS
1399
1371
func (ce * dockerMac ) stopContainer (containerName string ) error {
1400
1372
return generalStopContainer (DOCKER , containerName )
1401
1373
}
@@ -1405,7 +1377,7 @@ func (ce *podmanLinux) containerIsExist(containerName string) (bool, error) {
1405
1377
return generalContainerIsExist (PODMAN , containerName )
1406
1378
}
1407
1379
1408
- // podman-exist for MacOS
1380
+ // podman-exist for macOS
1409
1381
func (ce * podmanMac ) containerIsExist (containerName string ) (bool , error ) {
1410
1382
return generalContainerIsExist (PODMAN , containerName )
1411
1383
}
@@ -1415,7 +1387,7 @@ func (ce *dockerLinux) containerIsExist(containerName string) (bool, error) {
1415
1387
return generalContainerIsExist (DOCKER , containerName )
1416
1388
}
1417
1389
1418
- // docker-exist for MacOS
1390
+ // docker-exist for macOS
1419
1391
func (ce * dockerMac ) containerIsExist (containerName string ) (bool , error ) {
1420
1392
return generalContainerIsExist (DOCKER , containerName )
1421
1393
}
0 commit comments