From 5d78055dfb0b3eb94ca523e70ea231e6c3185733 Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 17 Sep 2020 13:33:11 +0100 Subject: [PATCH] Adding the work to move us to Github Actions --- .github/workflows/ci.yml | 23 +++++++++++++ .github/workflows/release.yml | 24 +++++++++++++ .gitignore | 3 +- .goreleaser.yml | 35 +++++++++++++++++++ Makefile | 21 ----------- README.md | 65 +++++++++++++++++++++++++---------- main.go | 6 +++- 7 files changed, 136 insertions(+), 41 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/release.yml create mode 100644 .goreleaser.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..721906b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,23 @@ +name: ci +on: + pull_request: + branches: + - master + push: + branches: + - master +jobs: + ci: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Install Go 1.14 + uses: actions/setup-go@v2 + with: + go-version: '1.14.x' + - name: Run Go Build + run: go build main.go + - name: Run tests + run: go test -v . + working-directory: tests diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..718fdff --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,24 @@ +name: release +on: + push: + tags: [ "v*.[0-99]" ] # only a valid semver tag + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Unshallow clone + run: git fetch --prune --unshallow --tags + - name: Install Go 1.14 + uses: actions/setup-go@v2 + with: + go-version: '1.14.x' + - name: Goreleaser publish + uses: goreleaser/goreleaser-action@v1 + with: + version: v0.134.0 + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.PULUMI_BOT_TOKEN }} diff --git a/.gitignore b/.gitignore index eed44cf..d35a10e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_STORE .idea/ -releases/ \ No newline at end of file +releases/ +main diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..479b880 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,35 @@ +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + ldflags: + - -X github.com/pulumi/crd2pulumi/gen.Version={{.Tag}} + goarch: + - amd64 + binary: crd2pulumi + main: ./main.go +archives: + - name_template: "{{ .Binary }}-{{ .Tag }}-{{ .Os }}-{{ .Arch }}" + format_overrides: + - goos: windows + format: zip +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-next" +changelog: + skip: true +brews: + - + name: crd2pulumi + github: + owner: pulumi + name: homebrew-tap + commit_author: + name: pulumi-bot + email: bot@pulumi.com + homepage: "https://pulumi.com" + description: "Generate typed CustomResources in Pulumi from Kubernetes CRDs" diff --git a/Makefile b/Makefile index 5780ca1..2642b3e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,4 @@ PROJECT := github.com/pulumi/crd2pulumi -VERSION := 1.0.5 -LDFLAGS := "-X 'github.com/pulumi/crd2pulumi/gen.Version=$(VERSION)'" GO ?= go GOMODULE = GO111MODULE=on @@ -10,22 +8,3 @@ ensure:: build:: $(GOMODULE) $(GO) build $(PROJECT) - -release: rel-darwin rel-linux rel-windows - -rel-darwin:: - GOOS=darwin GOARCH=amd64 $(GO) build -ldflags=$(LDFLAGS) -o releases/crd2pulumi-darwin-amd64/crd2pulumi $(PROJECT) - tar -zcvf releases/crd2pulumi-darwin-amd64.tar.gz -C releases/crd2pulumi-darwin-amd64 . - -rel-linux:: - GOOS=linux GOARCH=386 $(GO) build -ldflags=$(LDFLAGS) -o releases/crd2pulumi-linux-386/crd2pulumi $(PROJECT) - tar -zcvf releases/crd2pulumi-linux-386.tar.gz -C releases/crd2pulumi-linux-386 . - GOOS=linux GOARCH=amd64 $(GO) build -ldflags=$(LDFLAGS) -o releases/crd2pulumi-linux-amd64/crd2pulumi $(PROJECT) - tar -zcvf releases/crd2pulumi-linux-amd64.tar.gz -C releases/crd2pulumi-linux-amd64 . - -rel-windows:: - GOOS=windows GOARCH=386 $(GO) build -ldflags=$(LDFLAGS) -o releases/crd2pulumi-windows-386/crd2pulumi.exe $(PROJECT) - zip -j releases/crd2pulumi-windows-386.zip releases/crd2pulumi-windows-386/crd2pulumi.exe - GOOS=windows GOARCH=amd64 $(GO) build -ldflags=$(LDFLAGS) -o releases/crd2pulumi-windows-amd64/crd2pulumi.exe $(PROJECT) - zip -j releases/crd2pulumi-windows-amd64.zip releases/crd2pulumi-windows-amd64/crd2pulumi.exe - diff --git a/README.md b/README.md index 8df5b07..86cc94d 100644 --- a/README.md +++ b/README.md @@ -3,48 +3,75 @@ Generate typed CustomResources based on Kubernetes CustomResourceDefinitions. ## Goals -`crd2pulumi` is a CLI tool that generates typed CustomResources based on Kubernetes CustomResourceDefinition (CRDs). CRDs allow you to extend the Kubernetes API by defining your own schemas for custom objects. While Pulumi lets you create [CustomResources](https://www.pulumi.com/docs/reference/pkg/kubernetes/apiextensions/customresource/), there was previously no strong-typing for these objects since every schema was, well, custom. This can be a massive headache for popular CRDs such as [cert-manager](https://github.com/jetstack/cert-manager/tree/master/deploy/crds) or [istio](https://github.com/istio/istio/tree/0321da58ca86fc786fb03a68afd29d082477e4f2/manifests/charts/base/crds), which contain thousands of lines of complex YAML schemas. By generating typed versions of CustomResources, `crd2pulumi` makes filling out their arguments more convenient by allowing you to leverage existing IDE type checking and autocomplete features. +`crd2pulumi` is a CLI tool that generates typed CustomResources based on Kubernetes CustomResourceDefinition (CRDs). +CRDs allow you to extend the Kubernetes API by defining your own schemas for custom objects. While Pulumi lets you create + [CustomResources](https://www.pulumi.com/docs/reference/pkg/kubernetes/apiextensions/customresource/), there was previously + no strong-typing for these objects since every schema was, well, custom. This can be a massive headache for popular CRDs + such as [cert-manager](https://github.com/jetstack/cert-manager/tree/master/deploy/crds) or + [istio](https://github.com/istio/istio/tree/0321da58ca86fc786fb03a68afd29d082477e4f2/manifests/charts/base/crds), which + contain thousands of lines of complex YAML schemas. By generating typed versions of CustomResources, `crd2pulumi` makes + filling out their arguments more convenient by allowing you to leverage existing IDE type checking and autocomplete features. ## Building and Installation If you wish to use `crd2pulumi` without developing the tool itself, you can use one of the [binary releases](https://github.com/pulumi/crd2pulumi/releases) hosted on this repository. -`crd2pulumi` uses Go modules to manage dependencies. If you want to develop `crd2pulumi` itself, you'll need to have Go installed in order to build. Once you install this prerequisite, run the following to build the `crd2pulumi` binary and install it into `$GOPATH/bin`: +### Homebrew +`crd2pulumi` can be installed on Mac from the Pulumi Homebrew tap. +```console +brew install pulumi/tap/crd2pulumi +``` + +`crd2pulumi` uses Go modules to manage dependencies. If you want to develop `crd2pulumi` itself, you'll need to have +Go installed in order to build. Once you install this prerequisite, run the following to build the `crd2pulumi` binary +and install it into `$GOPATH/bin`: ```bash -$ go build -ldflags="-X 'github.com/pulumi/crd2pulumi/gen.Version=1.0.0'" -o $GOPATH/bin/crd2pulumi main.go +$ go build -ldflags="-X github.com/pulumi/crd2pulumi/gen.Version=dev" -o $GOPATH/bin/crd2pulumi main.go ``` -The `ldflags` argument is necessary to dynamically set the `crd2pulumi` version at build time. However, the version -itself can be anything, so you don't have to set it to `1.0.0`. +The `ldflags` argument is necessary to dynamically set the `crd2pulumi` version at build time. However, the version +itself can be anything, so you don't have to set it to `dev`. -Go should then automatically handle pulling the dependencies for you. If `$GOPATH/bin` is not on your path, you may want to move the `crd2pulumi` binary from `$GOPATH/bin` into a directory that is on your path. +Go should then automatically handle pulling the dependencies for you. If `$GOPATH/bin` is not on your path, you may +want to move the `crd2pulumi` binary from `$GOPATH/bin` into a directory that is on your path. ## Usage ```bash -crd2pulumi [-dgnp] [--nodejsPath path] [--pythonPath path] [--dotnetPath path] [--goPath path] [crd2.yaml ...] [--force] +crd2pulumi is a CLI tool that generates typed Kubernetes +CustomResources to use in Pulumi programs, based on a +CustomResourceDefinition YAML schema. + +Usage: + crd2pulumi [-dgnp] [--nodejsPath path] [--pythonPath path] [--dotnetPath path] [--goPath path] [crd2.yaml ...] [flags] + +Examples: +crd2pulumi --nodejs crontabs.yaml +crd2pulumi -dgnp crd-certificates.yaml crd-issuers.yaml crd-challenges.yaml +crd2pulumi --pythonPath=crds/python/istio --nodejsPath=crds/nodejs/istio crd-all.gen.yaml crd-mixer.yaml crd-operator.yaml + +Notice that by just setting a language-specific output path (--pythonPath, --nodejsPath, etc) the code will +still get generated, so setting -p, -n, etc becomes unnecessary. -Example usage: -$ crd2pulumi --nodejs crontabs.yaml -$ crd2pulumi -dgnp crd-certificates.yaml crd-issuers.yaml crd-challenges.yaml -$ crd2pulumi --pythonPath=crds/python/istio --nodejsPath=crds/nodejs/istio crd-all.gen.yaml crd-mixer.yaml crd-operator.yaml Flags: -d, --dotnet generate .NET --dotnetPath string optional .NET output dir + -f, --force overwrite existing files -g, --go generate Go --goPath string optional Go output dir + -h, --help help for crd2pulumi -n, --nodejs generate NodeJS --nodejsPath string optional NodeJS output dir -p, --python generate Python --pythonPath string optional Python output dir - - -f, --force overwrite existing files - -v, --version version for crd2pulumi - -h, --help help for crd2pulumi ``` -Setting only a language-specific flag will output the generated code in the default directory; so `-d` will output to `crds/dotnet`, `-g` will output to `crds/go`, `-n` will output to `crds/nodejs`, and `-p` will output to `crds/python`. You can also specify a language-specific path (`--pythonPath`, `--nodejsPath`, etc) to control where the code will be outputted, in which case setting `-p`, `-n`, etc becomes unnecessary. +Setting only a language-specific flag will output the generated code in the default directory; so `-d` will output to +`crds/dotnet`, `-g` will output to `crds/go`, `-n` will output to `crds/nodejs`, and `-p` will output to `crds/python`. +You can also specify a language-specific path (`--pythonPath`, `--nodejsPath`, etc) to control where the code will be +outputted, in which case setting `-p`, `-n`, etc becomes unnecessary. ## Examples -Let's use the example CronTab CRD specified in `resourcedefinition.yaml` from the [Kubernetes Documentation](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/). +Let's use the example CronTab CRD specified in `resourcedefinition.yaml` from the +[Kubernetes Documentation](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/). ### TypeScript To generate a strongly-typed CronTab CustomResource in TypeScript, we can run this command: @@ -105,7 +132,9 @@ crontab_instance = crontabs.stable.v1.CronTab( ```bash $ crd2pulumi --goPath ./crontabs resourcedefinition.yaml ``` -Now we can access the `NewCronTab()` constructor. Create a `main.go` file with the following code. In this example, the Pulumi project's module is named `crds-go-final`, so the import path is `crds-go-final/crontabs/stable/v1`. Make sure to swap this out with your own module's name. +Now we can access the `NewCronTab()` constructor. Create a `main.go` file with the following code. In this example, +the Pulumi project's module is named `crds-go-final`, so the import path is `crds-go-final/crontabs/stable/v1`. Make +sure to swap this out with your own module's name. ```go package main diff --git a/main.go b/main.go index f984f17..1a814f8 100644 --- a/main.go +++ b/main.go @@ -15,12 +15,16 @@ package main import ( + "fmt" + "os" + "github.com/pulumi/crd2pulumi/cmd" ) func main() { err := cmd.Execute() if err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "error: %v\n", err) + os.Exit(-1) } }