Skip to content

Commit

Permalink
Add Kubernetes code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed May 17, 2022
1 parent 3f6eaf6 commit df906ac
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,13 @@ jobs:
workdir: ./go
version: latest
args: release --snapshot --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
- name: Run GoReleaser - Kubernetes
uses: goreleaser/goreleaser-action@v2
with:
workdir: ./kubernetes
version: latest
args: release --snapshot --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
45 changes: 45 additions & 0 deletions .github/workflows/release-kubernetes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: release-kubernetes

on:
push:
tags:
- 'kubernetes/v*'
pull_request:

jobs:
build:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
- name: Cache Go modules
uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Tests
run: |
go mod tidy
go test -v ./...
- name: Docker Login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
if: success() && startsWith(github.ref, 'refs/tags/')
with:
workdir: ./kubernetes
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GOLANG_CROSS_VERSION := v1.18.1

.PHONY: build docs client driver go

all: build build-client build-deps build-docs build-driver build-example build-go
all: build build-client build-deps build-docs build-driver build-example build-go build-kubernetes

build:
goreleaser release --snapshot --rm-dist
Expand All @@ -29,6 +29,9 @@ build-example:
build-go:
$(MAKE) -C go build

build-kubernetes:
$(MAKE) -C kubernetes build

reuse-tool: # @HELP install reuse if not present
command -v reuse || python3 -m pip install reuse

Expand Down
4 changes: 2 additions & 2 deletions deps/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func run(cmd *cobra.Command, args []string) error {
}
defer os.RemoveAll(tmpDir)

tgtModURL := fmt.Sprintf("https://raw.githubusercontent.com/atomix/sdk/%s/go.mod", version)
fmt.Fprintf(cmd.OutOrStdout(), "Downloading go.mod for atomix/sdk %s from %s\n", version, tgtModURL)
tgtModURL := fmt.Sprintf("https://raw.githubusercontent.com/atomix/runtime/%s/go.mod", version)
fmt.Fprintf(cmd.OutOrStdout(), "Downloading go.mod for atomix/runtime %s from %s\n", version, tgtModURL)
resp, err := http.Get(tgtModURL)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/atomix/codegen
go 1.18

require (
github.com/atomix/runtime v0.0.0-20220517155953-04a4c764b1e6
github.com/atomix/runtime v0.0.0-20220517161348-de968c41c7c5
github.com/bmatcuk/doublestar/v4 v4.0.2
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/atomix/runtime v0.0.0-20220517155953-04a4c764b1e6 h1:HtJMAnFnzvyN/Hqza44CsVY5D8Z8pE+Y1L3z0Ph7RBI=
github.com/atomix/runtime v0.0.0-20220517155953-04a4c764b1e6/go.mod h1:zO5bDrNq2MDrw+hfiPx/NTT2SbtiTK9k4p9xlV5NB9k=
github.com/atomix/runtime v0.0.0-20220517161348-de968c41c7c5 h1:snysczYxfkBIKEOuGJpYlon9BQhf2sshbVpERH5BtYc=
github.com/atomix/runtime v0.0.0-20220517161348-de968c41c7c5/go.mod h1:zO5bDrNq2MDrw+hfiPx/NTT2SbtiTK9k4p9xlV5NB9k=
github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA=
github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
49 changes: 49 additions & 0 deletions kubernetes/.goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
project_name: atomix-gen-kubernetes

before:
hooks:
- go mod tidy

builds:
- id: atomix-gen-kubernetes
main: .
binary: atomix-gen-kubernetes
goos:
- linux
goarch:
- amd64
env:
- CC=gcc
- CXX=g++
flags:
- -mod=readonly
- -trimpath
gcflags:
- all=-N -l
ldflags:
- -s
- -X github.com/atomix/codegen/pkg/version.shortCommit={{ .ShortCommit }}
- -X github.com/atomix/codegen/pkg/version.commit={{ .FullCommit }}
- -X github.com/atomix/codegen/pkg/version.version=v{{ .Version }}
- -X github.com/atomix/codegen/pkg/version.buildType={{ if .IsSnapshot }}snapshot{{ else }}release{{ end }}

dockers:
- id: codegen-kubernetes
ids:
- atomix-gen-kubernetes
image_templates:
- "atomix/codegen:kubernetes-latest"
- "{{ if (not .IsSnapshot) }}atomix/codegen:kubernetes-{{ .Tag }}{{ end }}"
- "{{ if (not .IsSnapshot) }}atomix/codegen:kubernetes-v{{ .Major }}.{{ .Minor }}{{ end }}"

checksum:
name_template: 'checksums.txt'

snapshot:
name_template: "{{ incpatch .Version }}-{{.ShortCommit}}"

changelog:
sort: asc
filters:
exclude:
- '^docs:'
11 changes: 11 additions & 0 deletions kubernetes/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2022-present Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0

ARG VERSION=latest

FROM atomix/codegen:$VERSION

COPY atomix-gen-kubernetes /usr/local/bin/atomix-gen-kubernetes

ENTRYPOINT ["atomix-gen-kubernetes"]
14 changes: 14 additions & 0 deletions kubernetes/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-FileCopyrightText: 2022-present Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0

GOLANG_CROSS_VERSION := v1.18.1

build:
goreleaser release --snapshot --rm-dist

reuse-tool: # @HELP install reuse if not present
command -v reuse || python3 -m pip install reuse

license: reuse-tool # @HELP run license checks
reuse lint
27 changes: 27 additions & 0 deletions kubernetes/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: 2022-present Open Networking Foundation <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/spf13/cobra"
)

func GetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "atomix-gen-kubernetes",
Args: cobra.NoArgs,
RunE: run,
}
cmd.Flags().StringP("input-path", "p", ".", "the relative path to the API root")
cmd.Flags().StringP("output-path", "o", "", "the relative path to the output directory")
cmd.Flags().StringP("group-version", "g", "", "the group:version tuple")
cmd.Flags().Bool("deepcopy", false, "generate deepcopy files")
cmd.Flags().Bool("client", false, "generate API clients")
cmd.Flags().String("boilerplate", "", "the path to a boilerplate file")
_ = cmd.MarkFlagRequired("input-path")
_ = cmd.MarkFlagRequired("input-path")
_ = cmd.MarkFlagRequired("group-version")
return cmd
}
80 changes: 80 additions & 0 deletions kubernetes/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-FileCopyrightText: 2022-present Open Networking Foundation <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/atomix/codegen/internal/exec"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"path/filepath"
"strings"
)

func run(cmd *cobra.Command, _ []string) error {
inputPath, err := cmd.Flags().GetString("input-path")
if err != nil {
return err
}
outputPath, err := cmd.Flags().GetString("output-path")
if err != nil {
return err
}
if outputPath == "" {
outputPath = inputPath
}
groupVersion, err := cmd.Flags().GetString("group-version")
if err != nil {
return err
}
deepcopy, err := cmd.Flags().GetBool("deepcopy")
if err != nil {
return err
}
client, err := cmd.Flags().GetBool("client")
if err != nil {
return err
}
boilerplate, err := cmd.Flags().GetString("boilerplate")
if err != nil {
return err
}

tmpDir, err := ioutil.TempDir("", "code-generator")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

err = exec.Run("git", "clone", "https://github.com/kubernetes/code-generator.git", tmpDir)
if err != nil {
return err
}
err = exec.RunIn(tmpDir, "git", "checkout", "release-1.24")
if err != nil {
return err
}

var generators []string
if deepcopy {
generators = append(generators, "deepcopy")
}
if client {
generators = append(generators, "client")
}

var args []string
args = append(args, filepath.Join(tmpDir, "generate-groups.sh"))
args = append(args, strings.Join(generators, ","))
args = append(args, inputPath, outputPath, groupVersion)
if boilerplate != "" {
args = append(args, "--go-header-file", boilerplate)
}
err = exec.Run("bash", args...)
if err != nil {
return err
}
return nil
}
13 changes: 13 additions & 0 deletions kubernetes/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/atomix/codegen/kubernetes

go 1.18

require github.com/spf13/cobra v1.4.0

require (
github.com/atomix/codegen v0.0.0-20220508094714-cc2cae885ff9
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)

replace github.com/atomix/codegen => ../
10 changes: 10 additions & 0 deletions kubernetes/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
19 changes: 19 additions & 0 deletions kubernetes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2022-present Open Networking Foundation <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0

package main

import (
"fmt"
"github.com/atomix/codegen/kubernetes/cmd"
"os"
)

func main() {
cmd := cmd.GetCommand()
if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

0 comments on commit df906ac

Please sign in to comment.