~wallyworld/juju-core/1304742-backport-1.18

« back to all changes in this revision

Viewing changes to worker/firewaller/firewaller.go

Merge prev pipe.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
        "launchpad.net/juju-core/environs"
9
9
        "launchpad.net/juju-core/environs/config"
10
10
        "launchpad.net/juju-core/errors"
 
11
        "launchpad.net/juju-core/instance"
11
12
        "launchpad.net/juju-core/log"
12
13
        "launchpad.net/juju-core/state"
13
 
        "launchpad.net/juju-core/state/api/params"
14
14
        "launchpad.net/juju-core/state/watcher"
15
15
        "launchpad.net/juju-core/worker"
16
16
        "launchpad.net/tomb"
31
31
        serviceds       map[string]*serviceData
32
32
        exposedChange   chan *exposedChange
33
33
        globalMode      bool
34
 
        globalPortRef   map[params.Port]int
 
34
        globalPortRef   map[instance.Port]int
35
35
}
36
36
 
37
37
// NewFirewaller returns a new Firewaller.
66
66
        }
67
67
        if fw.environ.Config().FirewallMode() == config.FwGlobal {
68
68
                fw.globalMode = true
69
 
                fw.globalPortRef = make(map[params.Port]int)
 
69
                fw.globalPortRef = make(map[instance.Port]int)
70
70
        }
71
71
        for {
72
72
                select {
135
135
                fw:     fw,
136
136
                id:     id,
137
137
                unitds: make(map[string]*unitData),
138
 
                ports:  make([]params.Port, 0),
 
138
                ports:  make([]instance.Port, 0),
139
139
        }
140
140
        m, err := machined.machine()
141
141
        if errors.IsNotFoundError(err) {
194
194
        unitd.serviced = fw.serviceds[serviceName]
195
195
        unitd.serviced.unitds[unitName] = unitd
196
196
 
197
 
        ports := make([]params.Port, len(unitd.ports))
 
197
        ports := make([]instance.Port, len(unitd.ports))
198
198
        copy(ports, unitd.ports)
199
199
 
200
200
        go unitd.watchLoop(ports)
223
223
        if err != nil {
224
224
                return err
225
225
        }
226
 
        collector := make(map[params.Port]bool)
 
226
        collector := make(map[instance.Port]bool)
227
227
        for _, unitd := range fw.unitds {
228
228
                if unitd.serviced.exposed {
229
229
                        for _, port := range unitd.ports {
231
231
                        }
232
232
                }
233
233
        }
234
 
        wantedPorts := []params.Port{}
 
234
        wantedPorts := []instance.Port{}
235
235
        for port := range collector {
236
236
                wantedPorts = append(wantedPorts, port)
237
237
        }
273
273
                if !ok {
274
274
                        return errors.NotFoundf("instance id for %v", m)
275
275
                }
276
 
                instances, err := fw.environ.Instances([]state.InstanceId{instanceId})
 
276
                instances, err := fw.environ.Instances([]instance.Id{instanceId})
277
277
                if err == environs.ErrNoInstances {
278
278
                        return nil
279
279
                } else if err != nil {
364
364
// flushMachine opens and closes ports for the passed machine.
365
365
func (fw *Firewaller) flushMachine(machined *machineData) error {
366
366
        // Gather ports to open and close.
367
 
        ports := map[params.Port]bool{}
 
367
        ports := map[instance.Port]bool{}
368
368
        for _, unitd := range machined.unitds {
369
369
                if unitd.serviced.exposed {
370
370
                        for _, port := range unitd.ports {
372
372
                        }
373
373
                }
374
374
        }
375
 
        want := []params.Port{}
 
375
        want := []instance.Port{}
376
376
        for port := range ports {
377
377
                want = append(want, port)
378
378
        }
388
388
// flushGlobalPorts opens and closes global ports in the environment.
389
389
// It keeps a reference count for ports so that only 0-to-1 and 1-to-0 events
390
390
// modify the environment.
391
 
func (fw *Firewaller) flushGlobalPorts(rawOpen, rawClose []params.Port) error {
 
391
func (fw *Firewaller) flushGlobalPorts(rawOpen, rawClose []instance.Port) error {
392
392
        // Filter which ports are really to open or close.
393
 
        var toOpen, toClose []params.Port
 
393
        var toOpen, toClose []instance.Port
394
394
        for _, port := range rawOpen {
395
395
                if fw.globalPortRef[port] == 0 {
396
396
                        toOpen = append(toOpen, port)
425
425
}
426
426
 
427
427
// flushGlobalPorts opens and closes ports global on the machine.
428
 
func (fw *Firewaller) flushInstancePorts(machined *machineData, toOpen, toClose []params.Port) error {
 
428
func (fw *Firewaller) flushInstancePorts(machined *machineData, toOpen, toClose []instance.Port) error {
429
429
        // If there's nothing to do, do nothing.
430
430
        // This is important because when a machine is first created,
431
431
        // it will have no instance id but also no open ports -
444
444
        if !ok {
445
445
                return errors.NotFoundf("instance id for %v", m)
446
446
        }
447
 
        instances, err := fw.environ.Instances([]state.InstanceId{instanceId})
 
447
        instances, err := fw.environ.Instances([]instance.Id{instanceId})
448
448
        if err != nil {
449
449
                return err
450
450
        }
582
582
        fw     *Firewaller
583
583
        id     string
584
584
        unitds map[string]*unitData
585
 
        ports  []params.Port
 
585
        ports  []instance.Port
586
586
}
587
587
 
588
588
func (md *machineData) machine() (*state.Machine, error) {
623
623
// portsChange contains the changed ports for one specific unit.
624
624
type portsChange struct {
625
625
        unitd *unitData
626
 
        ports []params.Port
 
626
        ports []instance.Port
627
627
}
628
628
 
629
629
// unitData holds unit details and watches port changes.
633
633
        unit     *state.Unit
634
634
        serviced *serviceData
635
635
        machined *machineData
636
 
        ports    []params.Port
 
636
        ports    []instance.Port
637
637
}
638
638
 
639
639
// watchLoop watches the unit for port changes.
640
 
func (ud *unitData) watchLoop(latestPorts []params.Port) {
 
640
func (ud *unitData) watchLoop(latestPorts []instance.Port) {
641
641
        defer ud.tomb.Done()
642
642
        w := ud.unit.Watch()
643
643
        defer watcher.Stop(w, &ud.tomb)
672
672
 
673
673
// samePorts returns whether old and new contain the same set of ports.
674
674
// Both old and new must be sorted.
675
 
func samePorts(old, new []params.Port) bool {
 
675
func samePorts(old, new []instance.Port) bool {
676
676
        if len(old) != len(new) {
677
677
                return false
678
678
        }
746
746
}
747
747
 
748
748
// diff returns all the ports that exist in A but not B.
749
 
func diff(A, B []params.Port) (missing []params.Port) {
 
749
func diff(A, B []instance.Port) (missing []instance.Port) {
750
750
next:
751
751
        for _, a := range A {
752
752
                for _, b := range B {