(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, opts initOptions)
| 67 | } |
| 68 | |
| 69 | func runInit(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, opts initOptions) error { |
| 70 | apiClient := dockerCLI.Client() |
| 71 | |
| 72 | // TODO(thaJeztah): change opts.defaultAddrPools a []netip.Prefix; see https://github.com/docker/cli/pull/6545#discussion_r2420361609 |
| 73 | defaultAddrPool := make([]netip.Prefix, 0, len(opts.defaultAddrPools)) |
| 74 | for _, p := range opts.defaultAddrPools { |
| 75 | if len(p.IP) == 0 { |
| 76 | continue |
| 77 | } |
| 78 | ip := p.IP.To4() |
| 79 | if ip == nil { |
| 80 | ip = p.IP.To16() |
| 81 | } |
| 82 | addr, ok := netip.AddrFromSlice(ip) |
| 83 | if !ok { |
| 84 | return fmt.Errorf("invalid IP address: %s", p.IP) |
| 85 | } |
| 86 | ones, _ := p.Mask.Size() |
| 87 | defaultAddrPool = append(defaultAddrPool, netip.PrefixFrom(addr, ones)) |
| 88 | } |
| 89 | var availability swarm.NodeAvailability |
| 90 | if flags.Changed(flagAvailability) { |
| 91 | switch a := swarm.NodeAvailability(strings.ToLower(opts.availability)); a { |
| 92 | case swarm.NodeAvailabilityActive, swarm.NodeAvailabilityPause, swarm.NodeAvailabilityDrain: |
| 93 | availability = a |
| 94 | default: |
| 95 | return fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | res, err := apiClient.SwarmInit(ctx, client.SwarmInitOptions{ |
| 100 | ListenAddr: opts.listenAddr.String(), |
| 101 | AdvertiseAddr: opts.advertiseAddr, |
| 102 | DataPathAddr: opts.dataPathAddr, |
| 103 | DataPathPort: opts.dataPathPort, |
| 104 | DefaultAddrPool: defaultAddrPool, |
| 105 | ForceNewCluster: opts.forceNewCluster, |
| 106 | Spec: opts.swarmOptions.ToSpec(flags), |
| 107 | AutoLockManagers: opts.swarmOptions.autolock, |
| 108 | Availability: availability, |
| 109 | SubnetSize: opts.DefaultAddrPoolMaskLength, |
| 110 | }) |
| 111 | if err != nil { |
| 112 | if strings.Contains(err.Error(), "could not choose an IP address to advertise") || strings.Contains(err.Error(), "could not find the system's IP address") { |
| 113 | return fmt.Errorf("%w - specify one with --advertise-addr", err) |
| 114 | } |
| 115 | return err |
| 116 | } |
| 117 | |
| 118 | _, _ = fmt.Fprintf(dockerCLI.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", res.NodeID) |
| 119 | |
| 120 | if err := printJoinCommand(ctx, dockerCLI, res.NodeID, true, false); err != nil { |
| 121 | return err |
| 122 | } |
| 123 | |
| 124 | _, _ = fmt.Fprintln(dockerCLI.Out(), "To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.") |
| 125 | |
| 126 | if opts.swarmOptions.autolock { |
no test coverage detected
searching dependent graphs…