Skip to content

Commit

Permalink
protobuf support
Browse files Browse the repository at this point in the history
  • Loading branch information
phosae committed May 12, 2023
1 parent d0f9af1 commit 07e2494
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
steps:
- name: Checkout copier
- name: Checkout Repository
uses: actions/checkout@master
with:
repository: phosae/kube-code-generator
Expand All @@ -40,4 +40,4 @@ jobs:
zengxu/kube-code-generator:${{github.ref_name}}
platforms: |
linux/amd64,linux/arm64
push: ${{ github.ref == 'refs/heads/main' }}
push: true
42 changes: 27 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
FROM golang:1.20.4-bullseye
ARG TARGETOS
ARG TARGETARCH
ARG CODEGEN_VERSION="1.27.1"
ARG KUBE_VERSION="1.27.1"
ARG CODEGEN_VERSION="0.27.1"
ARG CONTROLLER_GEN_VERSION="0.12.0"

ENV OS=${TARGETOS}
ENV ARCH=${TARGETARCH}

RUN apt-get update && \
apt-get install -y \
git
git \
unzip

# Code generator stuff
RUN wget http:https://github.com/kubernetes/code-generator/archive/kubernetes-${CODEGEN_VERSION}.tar.gz && \
RUN wget http:https://github.com/kubernetes/kubernetes/archive/v${KUBE_VERSION}.tar.gz && \
mkdir -p /go/src/k8s.io/code-generator/ && \
tar zxvf kubernetes-${CODEGEN_VERSION}.tar.gz --strip 1 -C /go/src/k8s.io/code-generator/ && \
rm kubernetes-${CODEGEN_VERSION}.tar.gz && \
\
wget http:https://github.com/kubernetes/apimachinery/archive/kubernetes-${CODEGEN_VERSION}.tar.gz && \
mkdir -p /go/src/k8s.io/apimachinery/ && \
tar zxvf kubernetes-${CODEGEN_VERSION}.tar.gz --strip 1 -C /go/src/k8s.io/apimachinery/ && \
rm kubernetes-${CODEGEN_VERSION}.tar.gz && \
mkdir -p /go/src/github.com/gogo/ && \
mkdir -p /go/src/k8s.io/kubernetes/third_party/protobuf/ && \
tar zxvf v${KUBE_VERSION}.tar.gz --strip 5 -C /go/src/k8s.io/code-generator/ kubernetes-${KUBE_VERSION}/staging/src/k8s.io/code-generator && \
tar zxvf v${KUBE_VERSION}.tar.gz --strip 5 -C /go/src/k8s.io/apimachinery/ kubernetes-${KUBE_VERSION}/staging/src/k8s.io/apimachinery && \
tar zxvf v${KUBE_VERSION}.tar.gz --strip 4 -C /go/src/github.com/gogo/ kubernetes-${KUBE_VERSION}/vendor/github.com/gogo && \
tar zxvf v${KUBE_VERSION}.tar.gz --strip 3 -C /go/src/k8s.io/kubernetes/third_party/protobuf/ kubernetes-${KUBE_VERSION}/third_party/protobuf && \
rm v${KUBE_VERSION}.tar.gz && \
\
wget http:https://github.com/kubernetes/api/archive/kubernetes-${CODEGEN_VERSION}.tar.gz && \
mkdir -p /go/src/k8s.io/api/ && \
tar zxvf kubernetes-${CODEGEN_VERSION}.tar.gz --strip 1 -C /go/src/k8s.io/api/ && \
rm kubernetes-${CODEGEN_VERSION}.tar.gz && \
cd /go/src/k8s.io/code-generator/ && \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o protoc-gen-gogo ./cmd/go-to-protobuf/protoc-gen-gogo && \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o go-to-protobuf ./cmd/go-to-protobuf && \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} GOBIN=/usr/bin go install golang.org/x/tools/cmd/goimports@latest && \
mv ./protoc-gen-gogo /usr/bin/ && \
mv ./go-to-protobuf /usr/bin/ && cd - && \
\
wget https://github.com/kubernetes-sigs/controller-tools/archive/v${CONTROLLER_GEN_VERSION}.tar.gz && \
tar xvf ./v${CONTROLLER_GEN_VERSION}.tar.gz && \
cd ./controller-tools-${CONTROLLER_GEN_VERSION}/ && \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o controller-gen ./cmd/controller-gen/ && \
mv ./controller-gen /usr/bin/ && \
rm -rf ../v${CONTROLLER_GEN_VERSION}.tar.gz && \
rm -rf ../controller-tools-${CONTROLLER_GEN_VERSION}
rm -rf ../controller-tools-${CONTROLLER_GEN_VERSION} && \
rm -rf /go/pkg

COPY hack/install-protoc.sh /go/install-protoc.sh
RUN /go/install-protoc.sh
ENV PATH="${PATH}:/go/protoc"

# Create user
ARG uid=1000
Expand All @@ -42,7 +54,7 @@ RUN addgroup --gid $gid codegen && \

COPY hack /hack
RUN chown codegen:codegen -R /hack && \
mv /hack/* /usr/bin
mv /hack/update* /usr/bin

USER codegen

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Kube code generator

[![Docker Repository on Quay](https://quay.io/repository/slok/kube-code-generator/status "Docker Repository on Quay")](https://quay.io/repository/slok/kube-code-generator)
![Kubernetes release](https://img.shields.io/badge/Kubernetes-v1.26-green?logo=Kubernetes&style=flat&color=326CE5&logoColor=white)
[![Docker Repository on Quay](https://quay.io/repository/slok/kube-code-generator/status "Docker Repository on Quay")](https://hub.docker.com/r/zengxu/kube-code-generator/tags)
![Kubernetes release](https://img.shields.io/badge/Kubernetes-v1.27-green?logo=Kubernetes&style=flat&color=326CE5&logoColor=white)

A kubernetes code generator container that makes easier to create CRD manifests (registration API) and clients.

Expand Down
33 changes: 33 additions & 0 deletions hack/install-protoc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

PROTOC_VERSION=3.19.4
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")

download_folder="protoc-v${PROTOC_VERSION}-${OS}-${ARCH}"
download_file="${download_folder}.zip"

cd "${SCRIPT_ROOT}" || return 1

if [[ $(readlink protoc) != "${download_folder}" ]]; then
if [[ ${OS} == "darwin" ]]; then
url="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-osx-x86_64.zip"
elif [[ ${OS} == "linux" && ${ARCH} == "amd64" ]]; then
url="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.zip"
elif [[ ${OS} == "linux" && ${ARCH} == "arm64" ]]; then
url="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-aarch_64.zip"
else
echo "This install script does not support ${OS}/${ARCH}"
return 1
fi
curl -fsSL --retry 3 --keepalive-time 2 "${url}" -o "${download_file}"
unzip -o "${download_file}" -d "${download_folder}"
ln -fns "${download_folder}" protoc
mv protoc/bin/protoc protoc/protoc
chmod -R +rX protoc/protoc
rm -fr protoc/include
rm "${download_file}"
fi
17 changes: 17 additions & 0 deletions hack/update-generated-protobuf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

# Usage:
# update-generated-protobuf.sh "${APIROOTS[@]}"
# An example APIROOT is: "k8s.io/api/admissionregistration/v1"

set -eufo pipefail

BOILERPLATE_PATH=/tmp/fake-boilerplate.txt
touch "${BOILERPLATE_PATH}"

go-to-protobuf \
--proto-import=/go/src \
--proto-import=/go/src/k8s.io/kubernetes/third_party/protobuf \
--packages "$(IFS=, ; echo "$*")" \
--output-base /go/src/ \
--go-header-file "${BOILERPLATE_PATH}"

0 comments on commit 07e2494

Please sign in to comment.