Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update plugin-tooling.mk, Update tanzu builder init to include plugin-tooling.mk to build and publish plugins #139

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/plugin/builder/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
// NewInitCmd initializes a repository.
func NewInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init PLUGIN_NAME",
Use: "init REPO_NAME",
Short: "Initialize a new plugin repository",
Long: desc,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
78 changes: 5 additions & 73 deletions cmd/plugin/builder/template/plugintemplates/Makefile.tmpl
Original file line number Diff line number Diff line change
@@ -1,91 +1,23 @@
ROOT_DIR_RELATIVE := .

include $(ROOT_DIR_RELATIVE)/common.mk

BUILD_VERSION ?= $(shell cat BUILD_VERSION)
BUILD_SHA ?= $(shell git rev-parse --short HEAD)
BUILD_DATE ?= $(shell date -u +"%Y-%m-%d")

GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
GOHOSTOS ?= $(shell go env GOHOSTOS)
GOHOSTARCH ?= $(shell go env GOHOSTARCH)

LD_FLAGS = -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Date=$(BUILD_DATE)'
LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.SHA=$(BUILD_SHA)'
LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Version=$(BUILD_VERSION)'
include $(ROOT_DIR_RELATIVE)/plugin-tooling.mk

TOOLS_DIR := tools
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint
GOLANGCI_LINT_VERSION := 1.49.0

GO_SRCS := $(call rwildcard,.,*.go)

ARTIFACTS_DIR ?= ./artifacts
TANZU_PLUGIN_PUBLISH_PATH ?= $(ARTIFACTS_DIR)/published

# Name of Tanzu CLI binary
TZBIN ?= tz

# Add list of plugins separated by space
PLUGINS ?= ""

# Add supported OS-ARCHITECTURE combinations here
ENVS ?= linux-amd64 windows-amd64 darwin-amd64

BUILD_JOBS := $(addprefix build-,${ENVS})
PUBLISH_JOBS := $(addprefix publish-,${ENVS})

go.mod go.sum: $(GO_SRCS)
go mod download
go mod tidy

.PHONY: build-local
build-local: ## Build the plugin
$(TZBIN) builder cli compile --version $(BUILD_VERSION) --ldflags "$(LD_FLAGS)" --path ./cmd/plugin --target local --artifacts artifacts/${GOHOSTOS}/${GOHOSTARCH}/cli

.PHONY: build
build: $(BUILD_JOBS) $(PUBLISH_JOBS) ## Build the plugin

.PHONY: build-%
build-%:
$(eval ARCH = $(word 2,$(subst -, ,$*)))
$(eval OS = $(word 1,$(subst -, ,$*)))
$(TZBIN) builder cli compile --version $(BUILD_VERSION) --ldflags "$(LD_FLAGS)" --path ./cmd/plugin --artifacts artifacts/${OS}/${ARCH}/cli --target ${OS}_${ARCH}

publish-local: ## Publish the plugin to generate discovery and distribution directory for local OS_ARCH
$(TZBIN) builder publish --type local --plugins "$(PLUGINS)" --version $(BUILD_VERSION) --os-arch "${GOHOSTOS}-${GOHOSTARCH}" --local-output-discovery-dir "$(TANZU_PLUGIN_PUBLISH_PATH)/${GOHOSTOS}-${GOHOSTARCH}/discovery/standalone" --local-output-distribution-dir "$(TANZU_PLUGIN_PUBLISH_PATH)/${GOHOSTOS}-${GOHOSTARCH}/distribution" --input-artifact-dir $(ARTIFACTS_DIR)

.PHONY: publish
publish: $(PUBLISH_JOBS) ## Publish the plugin to generate discovery and distribution directory

.PHONY: publish-%
publish-%:
$(eval ARCH = $(word 2,$(subst -, ,$*)))
$(eval OS = $(word 1,$(subst -, ,$*)))
$(TZBIN) builder publish --type local --plugins "$(PLUGINS)" --version $(BUILD_VERSION) --os-arch "${OS}-${ARCH}" --local-output-discovery-dir "$(TANZU_PLUGIN_PUBLISH_PATH)/${OS}-${ARCH}/discovery/standalone" --local-output-distribution-dir "$(TANZU_PLUGIN_PUBLISH_PATH)/${OS}-${ARCH}/distribution" --input-artifact-dir $(ARTIFACTS_DIR)

.PHONY: install-local
install-local: ## Install the locally built plugins
$(TZBIN) plugin install all --local $(TANZU_PLUGIN_PUBLISH_PATH)/${GOHOSTOS}-${GOHOSTARCH}

.PHONY: build-install-local
build-install-local: build-local publish-local ## Build and Install plugin for local OS-ARCH
$(TZBIN) plugin install all --local $(TANZU_PLUGIN_PUBLISH_PATH)/${GOHOSTOS}-${GOHOSTARCH}

.PHONY: release
release: $(BUILD_JOBS) $(PUBLISH_JOBS) # Generates release directory structure for all plugins under `./artifacts/published` (default)

.PHONY: lint
lint: $(GOLANGCI_LINT) ## Lint the plugin
$(GOLANGCI_LINT) run -v

.PHONY: init
init:go.mod go.sum ## Initialise the plugin
.PHONY: gomod
gomod: ## Update go module dependencies
go mod tidy

.PHONY: test
test: $(GO_SRCS) go.sum
test:
go test ./...

$(TOOLS_BIN_DIR):
Expand Down
2 changes: 2 additions & 0 deletions cmd/plugin/builder/template/plugintemplates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"os"

"github.com/vmware-tanzu/tanzu-plugin-runtime/config/types"
"github.com/vmware-tanzu/tanzu-plugin-runtime/log"
"github.com/vmware-tanzu/tanzu-plugin-runtime/plugin"
"github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo"
Expand All @@ -11,6 +12,7 @@ import (
var descriptor = plugin.PluginDescriptor{
Name: "{{ .PluginName | ToLower }}",
Description: "{{ .Description | ToLower }}",
Target: types.TargetUnknown,
anujc25 marked this conversation as resolved.
Show resolved Hide resolved
Version: buildinfo.Version,
BuildSHA: buildinfo.SHA,
Group: plugin.ManageCmdGroup, // set group
Expand Down
190 changes: 190 additions & 0 deletions cmd/plugin/builder/template/plugintemplates/plugin-tooling.mk.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# Copyright 2023 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# Ensure Make is run with bash shell as some syntax below is bash-specific
SHELL := /usr/bin/env bash

GOHOSTOS ?= $(shell go env GOHOSTOS)
GOHOSTARCH ?= $(shell go env GOHOSTARCH)

NUL = /dev/null
ifeq ($(GOHOSTOS),windows)
NUL = NUL
endif

# Build and version information

PLUGIN_BUILD_SHA ?= $(shell git describe --match=$(git rev-parse --short HEAD) --always --dirty)
PLUGIN_BUILD_DATE ?= $(shell date -u +"%Y-%m-%d")
PLUGIN_BUILD_VERSION ?= $(shell git describe --tags 2>$(NUL))

ifeq ($(strip $(PLUGIN_BUILD_VERSION)),)
PLUGIN_BUILD_VERSION = v0.0.0
endif
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Date=$(PLUGIN_BUILD_DATE)'
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.SHA=$(PLUGIN_BUILD_SHA)'
PLUGIN_LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-plugin-runtime/plugin/buildinfo.Version=$(PLUGIN_BUILD_VERSION)'

# Add supported OS-ARCHITECTURE combinations here
PLUGIN_BUILD_OS_ARCH ?= linux-amd64 windows-amd64 darwin-amd64

# Paths and Directory information
ROOT_DIR := $(shell git rev-parse --show-toplevel)

PLUGIN_DIR := ./cmd/plugin
PLUGIN_BINARY_ARTIFACTS_DIR := $(ROOT_DIR)/artifacts/plugins
PLUGIN_PACKAGE_ARTIFACTS_DIR := $(ROOT_DIR)/artifacts/packages
PLUGIN_MANIFEST_FILE := $(PLUGIN_PACKAGE_ARTIFACTS_DIR)/plugin_manifest.yaml
PLUGIN_GROUP_MANIFEST_FILE := $(PLUGIN_BINARY_ARTIFACTS_DIR)/plugin_group_manifest.yaml

REGISTRY_PORT ?= 5001
REGISTRY_ENDPOINT ?= localhost:$(REGISTRY_PORT)
PLUGIN_NAME ?= *

# Repository specific configuration
TZBIN ?= tanzu
BUILDER_PLUGIN ?= $(TZBIN) builder
PUBLISHER ?= tzcli
VENDOR ?= vmware
PLUGIN_PUBLISH_REPOSITORY ?= localhost:$(REGISTRY_PORT)/test/v1/tanzu-cli/plugins
PLUGIN_INVENTORY_IMAGE_TAG ?= latest

PLUGIN_SCOPE_ASSOCIATION_FILE ?= ""
PLUGIN_GROUP_NAME ?=

# Process configuration and setup additional variables
TANZU_BUILDER_OVERRIDE ?=
OVERRIDE_FLAG =
ifneq ($(strip $(TANZU_BUILDER_OVERRIDE)),)
OVERRIDE_FLAG = --override
endif

## --------------------------------------
## Plugin Build and Publish Tooling
## --------------------------------------

PLUGIN_BUILD_TARGETS := $(addprefix plugin-build-,${PLUGIN_BUILD_OS_ARCH})

.PHONY: plugin-build-install-local ## Build and Install all plugins using local plugin artifacts directory
plugin-build-install-local: plugin-build-local plugin-install-local

.PHONY: plugin-install-local ## Install all plugins from local plugin artifacts directory
plugin-install-local:
tanzu plugin install all --local $(PLUGIN_BINARY_ARTIFACTS_DIR)/$(GOHOSTOS)/$(GOHOSTARCH)

.PHONY: plugin-build
plugin-build: $(PLUGIN_BUILD_TARGETS) generate-plugin-bundle ## Build all plugin binaries for all supported os-arch

plugin-build-local: plugin-build-$(GOHOSTOS)-$(GOHOSTARCH) ## Build all plugin binaries for local platform

plugin-build-%:
$(eval ARCH = $(word 2,$(subst -, ,$*)))
$(eval OS = $(word 1,$(subst -, ,$*)))
$(BUILDER_PLUGIN) plugin build \
--path $(PLUGIN_DIR) \
--binary-artifacts $(PLUGIN_BINARY_ARTIFACTS_DIR) \
--version $(PLUGIN_BUILD_VERSION) \
--ldflags "$(PLUGIN_LD_FLAGS)" \
--os-arch $(OS)_$(ARCH) \
--match "$(PLUGIN_NAME)" \
--plugin-scope-association-file $(PLUGIN_SCOPE_ASSOCIATION_FILE)

.PHONY: plugin-build-packages
plugin-build-packages: plugin-local-registry ## Build plugin packages
$(BUILDER_PLUGIN) plugin build-package \
--binary-artifacts $(PLUGIN_BINARY_ARTIFACTS_DIR) \
--package-artifacts $(PLUGIN_PACKAGE_ARTIFACTS_DIR) \
--oci-registry $(REGISTRY_ENDPOINT)

.PHONY: plugin-publish-packages
plugin-publish-packages: plugin-build-packages ## Publish plugin packages
$(BUILDER_PLUGIN) plugin publish-package \
--package-artifacts $(PLUGIN_PACKAGE_ARTIFACTS_DIR) \
--publisher $(PUBLISHER) \
--vendor $(VENDOR) \
--repository $(PLUGIN_PUBLISH_REPOSITORY)

.PHONY: plugin-build-and-publish-packages
plugin-build-and-publish-packages: plugin-build plugin-publish-packages ## Build and Publish plugin packages

.PHONY: inventory-init
inventory-init: ## Initialize empty plugin inventory
$(BUILDER_PLUGIN) inventory init \
--repository $(PLUGIN_PUBLISH_REPOSITORY) \
--plugin-inventory-image-tag $(PLUGIN_INVENTORY_IMAGE_TAG) \
$(OVERRIDE_FLAG)

.PHONY: inventory-plugin-add
inventory-plugin-add: ## Add plugins to the inventory database
$(BUILDER_PLUGIN) inventory plugin add \
--repository $(PLUGIN_PUBLISH_REPOSITORY) \
--plugin-inventory-image-tag $(PLUGIN_INVENTORY_IMAGE_TAG) \
--publisher $(PUBLISHER) \
--vendor $(VENDOR) \
--manifest $(PLUGIN_MANIFEST_FILE)

.PHONY: inventory-plugin-activate
inventory-plugin-activate: ## Activate plugins in the inventory database
$(BUILDER_PLUGIN) inventory plugin activate \
--repository $(PLUGIN_PUBLISH_REPOSITORY) \
--plugin-inventory-image-tag $(PLUGIN_INVENTORY_IMAGE_TAG) \
--publisher $(PUBLISHER) \
--vendor $(VENDOR) \
--manifest $(PLUGIN_MANIFEST_FILE)

.PHONY: inventory-plugin-deactivate
inventory-plugin-deactivate: ## Deactivate plugins in the inventory database
$(BUILDER_PLUGIN) inventory plugin deactivate \
--repository $(PLUGIN_PUBLISH_REPOSITORY) \
--plugin-inventory-image-tag $(PLUGIN_INVENTORY_IMAGE_TAG) \
--publisher $(PUBLISHER) \
--vendor $(VENDOR) \
--manifest $(PLUGIN_MANIFEST_FILE)

.PHONY: inventory-plugin-group-add
inventory-plugin-group-add: ## Add plugin-group to the inventory database. Requires PLUGIN_GROUP_NAME
$(BUILDER_PLUGIN) inventory plugin-group add \
--repository $(PLUGIN_PUBLISH_REPOSITORY) \
--plugin-inventory-image-tag $(PLUGIN_INVENTORY_IMAGE_TAG) \
--publisher $(PUBLISHER) \
--vendor $(VENDOR) \
--manifest $(PLUGIN_GROUP_MANIFEST_FILE) \
--name $(PLUGIN_GROUP_NAME) \
$(OVERRIDE_FLAG)

.PHONY: inventory-plugin-group-activate
inventory-plugin-group-activate: ## Activate plugin-group in the inventory database. Requires PLUGIN_GROUP_NAME
$(BUILDER_PLUGIN) inventory plugin-group activate \
--repository $(PLUGIN_PUBLISH_REPOSITORY) \
--plugin-inventory-image-tag $(PLUGIN_INVENTORY_IMAGE_TAG) \
--publisher $(PUBLISHER) \
--vendor $(VENDOR) \
--name $(PLUGIN_GROUP_NAME)

.PHONY: inventory-plugin-group-deactivate
inventory-plugin-group-deactivate: ## Deactivate plugin-group in the inventory database. Requires PLUGIN_GROUP_NAME
$(BUILDER_PLUGIN) inventory plugin-group deactivate \
--repository $(PLUGIN_PUBLISH_REPOSITORY) \
--plugin-inventory-image-tag $(PLUGIN_INVENTORY_IMAGE_TAG) \
--publisher $(PUBLISHER) \
--vendor $(VENDOR) \
--name $(PLUGIN_GROUP_NAME)

## --------------------------------------
## docker
## --------------------------------------

.PHONY: plugin-local-registry
plugin-local-registry: plugin-clean-registry ## Starts up a local docker registry for generating packages
docker run -d -p $(REGISTRY_PORT):5000 --name temp-package-registry mirror.gcr.io/library/registry:2

.PHONY: plugin-clean-registry
plugin-clean-registry: ## Stops and removes local docker registry
docker stop temp-package-registry && docker rm -v temp-package-registry || true

## --------------------------------------
## Helpers
## --------------------------------------

generate-plugin-bundle:
cd $(PLUGIN_BINARY_ARTIFACTS_DIR) && tar -czvf ../plugin_bundle.tar.gz .
5 changes: 5 additions & 0 deletions cmd/plugin/builder/template/plugintemplates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var Makefile string
//go:embed common.mk.tmpl
var CommonMK string

// PluginToolingMK contains the plugin tooling
//
//go:embed plugin-tooling.mk.tmpl
var PluginToolingMK string

// MainGo contains the plugin main.go template
//
//go:embed main.go.tmpl
Expand Down
6 changes: 6 additions & 0 deletions cmd/plugin/builder/template/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ var Makefile = Target{
Template: plugintemplates.Makefile,
}

// PluginToolingMK target
var PluginToolingMK = Target{
Filepath: "plugin-tooling.mk",
Template: plugintemplates.PluginToolingMK,
}

anujc25 marked this conversation as resolved.
Show resolved Hide resolved
// Codeowners target
// TODO (pbarker): replace with the CLI reviewers group
var Codeowners = Target{
Expand Down
1 change: 1 addition & 0 deletions cmd/plugin/builder/template/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var DefaultInitTargets = []Target{
GolangCIConfig,
Tools,
CommonMK,
PluginToolingMK,
}

// DefaultPluginTargets are the default plugin targets.
Expand Down
Loading