From ed4bf6d880079ef8f3eeb5bad50f36fc38a8daa2 Mon Sep 17 00:00:00 2001 From: Ciprian Hacman Date: Mon, 30 May 2022 15:40:51 +0300 Subject: [PATCH] Use build tags for Protokube --- Makefile | 9 +- .../weaveworks/mesh/peer_name_hash.go | 2 +- .../weaveworks/mesh/peer_name_mac.go | 110 ++++++++++++++++++ 3 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 vendor/github.com/weaveworks/mesh/peer_name_mac.go diff --git a/Makefile b/Makefile index 6f1ef99896..7bd92593c8 100644 --- a/Makefile +++ b/Makefile @@ -213,7 +213,7 @@ crossbuild-nodeup: nodeup-amd64 nodeup-arm64 .PHONY: protokube-amd64 protokube-arm64 protokube-amd64 protokube-arm64: protokube-%: mkdir -p ${DIST}/linux/$* - GOOS=linux GOARCH=$* go build ${GCFLAGS} ${BUILDFLAGS} ${EXTRA_BUILDFLAGS} -o ${DIST}/linux/$*/protokube ${LDFLAGS}"${EXTRA_LDFLAGS} -X k8s.io/kops.Version=${VERSION} -X k8s.io/kops.GitVersion=${GITSHA}" k8s.io/kops/protokube/cmd/protokube + GOOS=linux GOARCH=$* go build -tags=peer_name_alternative,peer_name_hash ${GCFLAGS} ${BUILDFLAGS} ${EXTRA_BUILDFLAGS} -o ${DIST}/linux/$*/protokube ${LDFLAGS}"${EXTRA_LDFLAGS} -X k8s.io/kops.Version=${VERSION} -X k8s.io/kops.GitVersion=${GITSHA}" k8s.io/kops/protokube/cmd/protokube .PHONY: protokube protokube: protokube-amd64 @@ -296,7 +296,7 @@ dns-controller-push: ko-dns-controller-push .PHONY: ko-dns-controller-push ko-dns-controller-push: ko - KO_DOCKER_REPO="${DOCKER_REGISTRY}/${DOCKER_IMAGE_PREFIX}dns-controller" ko build --tags ${DNS_CONTROLLER_PUSH_TAG} --platform=linux/amd64,linux/arm64 --bare ./dns-controller/cmd/dns-controller/ + KO_DOCKER_REPO="${DOCKER_REGISTRY}/${DOCKER_IMAGE_PREFIX}dns-controller" GOFLAGS="-tags=peer_name_alternative,peer_name_hash" ko build --tags ${DNS_CONTROLLER_PUSH_TAG} --platform=linux/amd64,linux/arm64 --bare ./dns-controller/cmd/dns-controller/ # -------------------------------------------------- # development targets @@ -305,9 +305,6 @@ ko-dns-controller-push: ko gomod: go mod tidy go mod vendor - # Switch weavemesh to use peer_name_hash - bazel rule-go doesn't support build tags yet - rm vendor/github.com/weaveworks/mesh/peer_name_mac.go - sed -i.bak -e 's/peer_name_hash/!peer_name_mac/g' vendor/github.com/weaveworks/mesh/peer_name_hash.go cd tests/e2e; go mod tidy cd hack; go mod tidy @@ -531,7 +528,7 @@ ko-kube-apiserver-healthcheck-export: ko-kube-apiserver-healthcheck-export-linux .PHONY: ko-dns-controller-export-linux-amd64 ko-dns-controller-export-linux-arm64 ko-dns-controller-export-linux-amd64 ko-dns-controller-export-linux-arm64: ko-dns-controller-export-linux-%: ko mkdir -p ${IMAGES} - KO_DOCKER_REPO="registry.k8s.io/kops" ko build --tags ${DNS_CONTROLLER_TAG} --platform=linux/$* -B --push=false --tarball=${IMAGES}/dns-controller-$*.tar ./dns-controller/cmd/dns-controller + KO_DOCKER_REPO="registry.k8s.io/kops" GOFLAGS="-tags=peer_name_alternative,peer_name_hash" ko build --tags ${DNS_CONTROLLER_TAG} --platform=linux/$* -B --push=false --tarball=${IMAGES}/dns-controller-$*.tar ./dns-controller/cmd/dns-controller gzip -f ${IMAGES}/dns-controller-$*.tar tools/sha256 ${IMAGES}/dns-controller-$*.tar.gz ${IMAGES}/dns-controller-$*.tar.gz.sha256 diff --git a/vendor/github.com/weaveworks/mesh/peer_name_hash.go b/vendor/github.com/weaveworks/mesh/peer_name_hash.go index dc3b6cc916..6aa6e59295 100644 --- a/vendor/github.com/weaveworks/mesh/peer_name_hash.go +++ b/vendor/github.com/weaveworks/mesh/peer_name_hash.go @@ -1,4 +1,4 @@ -// +build !peer_name_mac +// +build peer_name_hash package mesh diff --git a/vendor/github.com/weaveworks/mesh/peer_name_mac.go b/vendor/github.com/weaveworks/mesh/peer_name_mac.go new file mode 100644 index 0000000000..42ac3ba060 --- /dev/null +++ b/vendor/github.com/weaveworks/mesh/peer_name_mac.go @@ -0,0 +1,110 @@ +// +build peer_name_mac !peer_name_alternative + +package mesh + +// The !peer_name_alternative effectively makes this the default, +// i.e. to choose an alternative, run +// +// go build -tags 'peer_name_alternative peer_name_hash' +// +// Let peer names be MACs... +// +// MACs need to be unique across our network, or bad things will +// happen anyway. So they make pretty good candidates for peer +// names. And doing so is pretty efficient both computationally and +// network overhead wise. +// +// Note that we do not mandate *what* MAC should be used as the peer +// name. In particular it doesn't actually have to be the MAC of, say, +// the network interface the peer is sniffing on. + +import ( + "fmt" + "net" +) + +// PeerName is used as a map key. Since net.HardwareAddr isn't suitable for +// that - it's a slice, and slices can't be map keys - we convert that to/from +// uint64. +type PeerName uint64 + +const ( + // PeerNameFlavour is the type of peer names we use. + PeerNameFlavour = "mac" + + // NameSize is the number of bytes in a peer name. + NameSize = 6 + + // UnknownPeerName is used as a sentinel value. + UnknownPeerName = PeerName(0) +) + +// PeerNameFromUserInput parses PeerName from a user-provided string. +func PeerNameFromUserInput(userInput string) (PeerName, error) { + return PeerNameFromString(userInput) +} + +// PeerNameFromString parses PeerName from a generic string. +func PeerNameFromString(nameStr string) (PeerName, error) { + var a, b, c, d, e, f uint64 + + match := func(format string, args ...interface{}) bool { + a, b, c, d, e, f = 0, 0, 0, 0, 0, 0 + n, err := fmt.Sscanf(nameStr+"\000", format+"\000", args...) + return err == nil && n == len(args) + } + + switch { + case match("%2x:%2x:%2x:%2x:%2x:%2x", &a, &b, &c, &d, &e, &f): + case match("::%2x:%2x:%2x:%2x", &c, &d, &e, &f): + case match("%2x::%2x:%2x:%2x", &a, &d, &e, &f): + case match("%2x:%2x::%2x:%2x", &a, &b, &e, &f): + case match("%2x:%2x:%2x::%2x", &a, &b, &c, &f): + case match("%2x:%2x:%2x:%2x::", &a, &b, &c, &d): + case match("::%2x:%2x:%2x", &d, &e, &f): + case match("%2x::%2x:%2x", &a, &e, &f): + case match("%2x:%2x::%2x", &a, &b, &f): + case match("%2x:%2x:%2x::", &a, &b, &c): + case match("::%2x:%2x", &e, &f): + case match("%2x::%2x", &a, &f): + case match("%2x:%2x::", &a, &b): + case match("::%2x", &f): + case match("%2x::", &a): + default: + return UnknownPeerName, fmt.Errorf("invalid peer name format: %q", nameStr) + } + + return PeerName(a<<40 | b<<32 | c<<24 | d<<16 | e<<8 | f), nil +} + +// PeerNameFromBin parses PeerName from a byte slice. +func PeerNameFromBin(nameByte []byte) PeerName { + return PeerName(macint(net.HardwareAddr(nameByte))) +} + +// bytes encodes PeerName as a byte slice. +func (name PeerName) bytes() []byte { + return intmac(uint64(name)) +} + +// String encodes PeerName as a string. +func (name PeerName) String() string { + return intmac(uint64(name)).String() +} + +func macint(mac net.HardwareAddr) (r uint64) { + for _, b := range mac { + r <<= 8 + r |= uint64(b) + } + return +} + +func intmac(key uint64) (r net.HardwareAddr) { + r = make([]byte, 6) + for i := 5; i >= 0; i-- { + r[i] = byte(key) + key >>= 8 + } + return +}