From 50d9aa7bbe1b56e42d1046eefde1f68a15e33bad Mon Sep 17 00:00:00 2001
From: hasheddan <georgedanielmangum@gmail.com>
Date: Tue, 30 Aug 2022 09:34:45 -0400
Subject: [PATCH 1/2] Add machinery for building and publishing bundled
 provider packages

Adds an xpkg.mk library for building and publishing bundled provider
packages.

Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
---
 makelib/xpkg.mk | 115 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
 create mode 100644 makelib/xpkg.mk

diff --git a/makelib/xpkg.mk b/makelib/xpkg.mk
new file mode 100644
index 0000000..af679d0
--- /dev/null
+++ b/makelib/xpkg.mk
@@ -0,0 +1,115 @@
+# Copyright 2022 The Upbound Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# ====================================================================================
+# Options
+
+ifeq ($(origin XPKG_DIR),undefined)
+XPKG_DIR := $(ROOT_DIR)/package
+endif
+
+ifeq ($(origin XPKG_EXAMPLES_DIR),undefined)
+XPKG_EXAMPLES_DIR := $(ROOT_DIR)/examples
+endif
+
+ifeq ($(origin XPKG_IGNORE),undefined)
+XPKG_IGNORE := ''
+endif
+
+ifeq ($(origin XPKG_OUTPUT_DIR),undefined)
+XPKG_OUTPUT_DIR := $(OUTPUT_DIR)/xpkg
+endif
+
+# a registry that is scoped to the current build tree on this host. this enables
+# us to have isolation between concurrent builds on the same system, as in the case
+# of multiple working directories or on a CI system with multiple executors. All images
+# tagged with this build registry can safely be untagged/removed at the end of the build.
+ifeq ($(origin BUILD_REGISTRY), undefined)
+BUILD_REGISTRY := build-$(shell echo $(HOSTNAME)-$(ROOT_DIR) | shasum -a 256 | cut -c1-8)
+endif
+
+XPKG_REG_ORGS ?= xpkg.upbound.io/crossplane
+XPKG_REG_ORGS_NO_PROMOTE ?= xpkg.upbound.io/crossplane
+XPKG_LINUX_PLATFORMS := $(filter linux_%,$(PLATFORMS))
+XPKG_ARCHS := $(subst linux_,,$(filter linux_%,$(PLATFORMS)))
+XPKG_PLATFORMS := $(subst _,/,$(subst $(SPACE),$(COMMA),$(filter linux_%,$(PLATFORMS))))
+XPKG_PLATFORMS_LIST := $(subst _,/,$(filter linux_%,$(PLATFORMS)))
+XPKG_PLATFORM := $(subst _,/,$(PLATFORM))
+
+UP ?= up
+
+# =====================================================================================
+# XPKG Targets
+
+# 1: xpkg
+define xpkg.build.targets
+xpkg.build.$(1):
+	@$(INFO) Building package $(1)-$(VERSION).xpkg for $(PLATFORM)
+	@mkdir -p $(OUTPUT_DIR)/xpkg/$(PLATFORM)
+	@$(UP) xpkg build \
+		--controller $(BUILD_REGISTRY)/$(1)-$(ARCH) \
+		--package-root $(XPKG_DIR) \
+		--examples-root $(XPKG_EXAMPLES_DIR) \
+		--ignore $(XPKG_IGNORE) \
+		--output $(XPKG_OUTPUT_DIR)/$(PLATFORM)/$(1)-$(VERSION).xpkg || $(FAIL)
+	@$(OK) Built package $(1)-$(VERSION).xpkg for $(PLATFORM)
+xpkg.build: xpkg.build.$(1)
+endef
+$(foreach x,$(XPKGS),$(eval $(call xpkg.build.targets,$(x))))
+
+# 1: registry/org 2: repo
+define xpkg.release.targets
+xpkg.release.publish.$(1).$(2):
+	@$(INFO) Pushing package $(1)/$(2):$(VERSION)
+	@$(UP) xpkg push \
+		$(foreach p,$(XPKG_LINUX_PLATFORMS),--package $(XPKG_OUTPUT_DIR)/$(p)/$(2)-$(VERSION).xpkg ) \
+		$(1)/$(2):$(VERSION) || $(FAIL)
+	@$(OK) Pushed package $(1)/$(2):$(VERSION)
+xpkg.release.publish: xpkg.release.publish.$(1).$(2)
+
+xpkg.release.promote.$(1).$(2):
+	@$(INFO) Promoting package from $(1)/$(2):$(VERSION) to $(1)/$(2):$(CHANNEL)
+	@docker buildx imagetools create -t $(1)/$(2):$(CHANNEL) $(1)/$(2):$(VERSION)
+	@[ "$(CHANNEL)" = "master" ] || docker buildx imagetools create -t $(1)/$(2):$(VERSION)-$(CHANNEL) $(1)/$(2):$(VERSION)
+	@$(OK) Promoted package from $(1)/$(2):$(VERSION) to $(1)/$(2):$(CHANNEL)
+xpkg.release.promote: xpkg.release.promote.$(1).$(2)
+endef
+$(foreach r,$(XPKG_REG_ORGS), $(foreach x,$(XPKGS),$(eval $(call xpkg.release.targets,$(r),$(x)))))
+
+# ====================================================================================
+# Common Targets
+
+do.build.xpkgs: $(foreach i,$(XPKGS),xpkg.build.$(i))
+do.skip.xpkgs:
+	@$(OK) Skipping xpkg build for unsupported platform $(IMAGE_PLATFORM)
+
+ifneq ($(filter $(XPKG_PLATFORM),$(XPKG_PLATFORMS_LIST)),)
+build.artifacts.platform: do.build.xpkgs
+else
+build.artifacts.platform: do.skip.xpkgs
+endif
+
+# only publish package for main / master and release branches
+# TODO(hasheddan): remove master and support overriding
+ifneq ($(filter main master release-%,$(BRANCH_NAME)),)
+publish.artifacts: $(foreach r,$(XPKG_REG_ORGS), $(foreach x,$(XPKGS),xpkg.release.publish.$(r).$(x)))
+endif
+
+# NOTE(hasheddan): promotion fails using buildx imagetools create with some
+# registries, so a NO_PROMOTE list is supported here. Additionally, channels may
+# not be used on some registries that infer vanity tags.
+# https://github.com/containerd/containerd/issues/5978
+# https://github.com/estesp/manifest-tool/issues/122
+# https://github.com/moby/buildkit/issues/2438
+promote.artifacts: $(foreach r,$(filter-out $(XPKG_REG_ORGS_NO_PROMOTE),$(XPKG_REG_ORGS)), $(foreach x,$(XPKGS),xpkg.release.promote.$(r).$(x)))

From e848cbdde8c762ddc634aeaf682223f92fd8e2e5 Mon Sep 17 00:00:00 2001
From: hasheddan <georgedanielmangum@gmail.com>
Date: Tue, 30 Aug 2022 12:30:26 -0400
Subject: [PATCH 2/2] Use shasum fallback in xpkg.mk

Updates xpkg.mk to incorporate the shasum fallback logic.

Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
---
 makelib/xpkg.mk | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/makelib/xpkg.mk b/makelib/xpkg.mk
index af679d0..448e949 100644
--- a/makelib/xpkg.mk
+++ b/makelib/xpkg.mk
@@ -31,12 +31,19 @@ ifeq ($(origin XPKG_OUTPUT_DIR),undefined)
 XPKG_OUTPUT_DIR := $(OUTPUT_DIR)/xpkg
 endif
 
+# shasum is not available on all systems. In that case, fall back to sha256sum.
+ifneq ($(shell type shasum 2>/dev/null),)
+SHA256SUM := shasum -a 256
+else
+SHA256SUM := sha256sum
+endif
+
 # a registry that is scoped to the current build tree on this host. this enables
 # us to have isolation between concurrent builds on the same system, as in the case
 # of multiple working directories or on a CI system with multiple executors. All images
 # tagged with this build registry can safely be untagged/removed at the end of the build.
 ifeq ($(origin BUILD_REGISTRY), undefined)
-BUILD_REGISTRY := build-$(shell echo $(HOSTNAME)-$(ROOT_DIR) | shasum -a 256 | cut -c1-8)
+BUILD_REGISTRY := build-$(shell echo $(HOSTNAME)-$(ROOT_DIR) | $(SHA256SUM) | cut -c1-8)
 endif
 
 XPKG_REG_ORGS ?= xpkg.upbound.io/crossplane