Skip to content

Commit

Permalink
Make BlockAffinity a raw string type (#301)
Browse files Browse the repository at this point in the history
Compatible with old golang
  • Loading branch information
robbrockbank authored Dec 12, 2016
1 parent e44106f commit 9725a75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
22 changes: 18 additions & 4 deletions lib/backend/model/block_affinity.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,24 @@ import (

var (
matchBlockAffinity = regexp.MustCompile("^/?calico/ipam/v2/host/([^/]+)/ipv./block/([^/]+)$")
typeBlockAff = reflect.TypeOf(BlockAffinity{})

// The BlockAffinity is stored as a raw string type. It currently does
// not contain any information since all required information is stored
// in the key (hostname and block CIDR). If we end up needing to store
// data in the BlockAffinity then care will need to be taken to ensure
// existing versions of the value (an empty string) can be successfully
// unmarshalled.
// - The Python version of IPAM wrote an empty string, but can handle
// any value written into the data.
// - The original golang port of IPAM wrote "{}" into the data (the JSON
// value for an empty dict). It was unable to handle reading of an
// empty string written out by the Python IPAM.
// - The current version of the golang port now has the BlockAffinity
// as a raw-string type so that it can handle reading in any value.
// We write in a fixed value of "{}" so that we are compatible with
// both the Python and the original golang port.
BlockAffinityValue = "{}"
typeBlockAff = rawStringType
)

type BlockAffinityKey struct {
Expand Down Expand Up @@ -94,6 +111,3 @@ func (options BlockAffinityListOptions) KeyFromDefaultPath(path string) Key {
}
return BlockAffinityKey{CIDR: *cidr, Host: host}
}

type BlockAffinity struct {
}
5 changes: 3 additions & 2 deletions lib/client/ipam_block_reader_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@ func (rw blockReaderWriter) claimNewAffineBlock(
}

func (rw blockReaderWriter) claimBlockAffinity(subnet cnet.IPNet, host string, config IPAMConfig) error {
// Claim the block affinity for this host.
// Claim the block affinity for this host. See model.BlockAffinityValue
// for details on the hard-coded value that is used.
log.Infof("Host %s claiming block affinity for %s", host, subnet)
obj := model.KVPair{
Key: model.BlockAffinityKey{Host: host, CIDR: subnet},
Value: &model.BlockAffinity{},
Value: model.BlockAffinityValue,
}
_, err := rw.client.backend.Create(&obj)

Expand Down

0 comments on commit 9725a75

Please sign in to comment.