@@ -41,6 +41,7 @@ func (n *NetworkCommand) Init(c *Cli) {
4141 c .AddCommand (n , & NetworkRemoveCommand {})
4242 c .AddCommand (n , & NetworkInspectCommand {})
4343 c .AddCommand (n , & NetworkListCommand {})
44+ c .AddCommand (n , & NetworkConnectCommand {})
4445 c .AddCommand (n , & NetworkDisconnectCommand {})
4546}
4647
@@ -377,6 +378,91 @@ e495f50913 net1 bridge
377378`
378379}
379380
381+ // networkConnectDescription is used to describe network connect command in detail and auto generate command doc.
382+ var networkConnectDescription = "Connect a container to a network in pouchd. " +
383+ "It must specify network's name and container's name."
384+
385+ // NetworkConnectCommand is used to implement 'network connect' command.
386+ type NetworkConnectCommand struct {
387+ baseCommand
388+
389+ ipAddress string
390+ ipv6Address string
391+ links []string
392+ aliases []string
393+ linklocalips []string
394+ }
395+
396+ // Init initializes NetworkConnectCommand command.
397+ func (n * NetworkConnectCommand ) Init (c * Cli ) {
398+ n .cli = c
399+
400+ n .cmd = & cobra.Command {
401+ Use : "connect [OPTIONS] NETWORK CONTAINER" ,
402+ Short : "Connect a container to a network" ,
403+ Long : networkConnectDescription ,
404+ Args : cobra .ExactArgs (2 ),
405+ RunE : func (cmd * cobra.Command , args []string ) error {
406+ return n .runNetworkConnect (args )
407+ },
408+ Example : networkConnectExample (),
409+ }
410+
411+ n .addFlags ()
412+ }
413+
414+ // addFlags adds flags for specific command.
415+ func (n * NetworkConnectCommand ) addFlags () {
416+ flagSet := n .cmd .Flags ()
417+
418+ flagSet .StringVar (& n .ipAddress , "ip" , "" , "IP Address" )
419+ flagSet .StringVar (& n .ipv6Address , "ip6" , "" , "IPv6 Address" )
420+ flagSet .StringSliceVar (& n .links , "link" , []string {}, "Add link to another container" )
421+ flagSet .StringSliceVar (& n .aliases , "alias" , []string {}, "Add network-scoped alias for the container" )
422+ flagSet .StringSliceVar (& n .linklocalips , "link-local-ip" , []string {}, "Add a link-local address for the container" )
423+ }
424+
425+ // runNetworkConnect is the entry of NetworkConnectCommand command.
426+ func (n * NetworkConnectCommand ) runNetworkConnect (args []string ) error {
427+ network := args [0 ]
428+ container := args [1 ]
429+ if network == "" {
430+ return fmt .Errorf ("network name cannot be empty" )
431+ }
432+ if container == "" {
433+ return fmt .Errorf ("container name cannot be empty" )
434+ }
435+
436+ networkReq := & types.NetworkConnect {
437+ Container : container ,
438+ EndpointConfig : & types.EndpointSettings {
439+ IPAMConfig : & types.EndpointIPAMConfig {
440+ IPV4Address : n .ipAddress ,
441+ IPV6Address : n .ipv6Address ,
442+ LinkLocalIps : n .linklocalips ,
443+ },
444+ Links : n .links ,
445+ Aliases : n .aliases ,
446+ },
447+ }
448+
449+ ctx := context .Background ()
450+ apiClient := n .cli .Client ()
451+ err := apiClient .NetworkConnect (ctx , network , networkReq )
452+ if err != nil {
453+ return err
454+ }
455+ fmt .Printf ("container %s is connected to network %s\n " , container , network )
456+
457+ return nil
458+ }
459+
460+ // networkConnectExample shows examples in network connect command, and is used in auto-generated cli docs.
461+ func networkConnectExample () string {
462+ return `$ pouch network connect net1 container1
463+ container container1 is connected to network net1`
464+ }
465+
380466// networkDisconnectDescription is used to describe network disconnect command in detail and auto generate comand doc.
381467var networkDisconnectDescription = "Disconnect a container from a network"
382468
0 commit comments