Skip to content

Commit

Permalink
API: generate code-gen style helpers
Browse files Browse the repository at this point in the history
As the FRR-K8s api is meant to be consumed by other controllers, we
export the helpers so a pre-controller runtime controller can consume
it.

Note: the "core" symlink is to get around
kubernetes/code-generator#167

Signed-off-by: Federico Paolinelli <fpaoline@redhat.com>
  • Loading branch information
fedepaol committed Jul 8, 2024
1 parent 7e667a6 commit 9e34c15
Show file tree
Hide file tree
Showing 27 changed files with 1,396 additions and 15 deletions.
2 changes: 2 additions & 0 deletions api/v1beta1/frrconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ type FRRConfigurationStatus struct {

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//nolint
//+genclient

// FRRConfiguration is a piece of FRR configuration.
type FRRConfiguration struct {
Expand Down
6 changes: 6 additions & 0 deletions api/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ var (
// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)

var SchemeGroupVersion = schema.GroupVersion{Group: "frrk8s.metallb.io", Version: "v1beta1"}

func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}
1 change: 1 addition & 0 deletions core
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
Expand Down
16 changes: 1 addition & 15 deletions hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
/*
Copyright 2023.

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.
*/
// SPDX-License-Identifier:Apache-2.0
108 changes: 108 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env bash

# Copyright 2020 The Kubernetes Authors.
#
# 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.

# Strongly "inspired" by https://github.com/kubernetes-sigs/gateway-api/blob/1b14708b143e837fd94be97698ecbe0e6a4e058d/hack/update-codegen.sh

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

readonly SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE}")"/.. && pwd)"

# Keep outer module cache so we don't need to redownload them each time.
# The build cache already is persisted.
readonly GOMODCACHE="$(go env GOMODCACHE)"
readonly GO111MODULE="on"
readonly GOFLAGS="-mod=readonly"
readonly GOPATH="$(mktemp -d)"
readonly MIN_REQUIRED_GO_VER="$(go list -m -f '{{.GoVersion}}')"

function go_version_matches {
go version | perl -ne "exit 1 unless m{go version go([0-9]+.[0-9]+)}; exit 1 if (\$1 < ${MIN_REQUIRED_GO_VER})"
return $?
}

if ! go_version_matches; then
echo "Go v${MIN_REQUIRED_GO_VER} or later is required to run code generation"
exit 1
fi

export GOMODCACHE GO111MODULE GOFLAGS GOPATH

# Even when modules are enabled, the code-generator tools always write to
# a traditional GOPATH directory, so fake on up to point to the current
# workspace.
mkdir -p "$GOPATH/src/github.com/metallb"
ln -s "${SCRIPT_ROOT}" "$GOPATH/src/github.com/metallb/frr-k8s"

echo $GOPATH
readonly OUTPUT_PKG=github.com/metallb/frr-k8s/pkg/client
readonly APIS_PKG=github.com/metallb/frr-k8s
readonly CLIENTSET_NAME=versioned
readonly CLIENTSET_PKG_NAME=clientset
readonly VERSIONS=(v1beta1)

INPUT_DIRS_SPACE=""
INPUT_DIRS_COMMA=""
for VERSION in "${VERSIONS[@]}"
do
INPUT_DIRS_SPACE+="${APIS_PKG}/api/${VERSION} "
INPUT_DIRS_COMMA+="${APIS_PKG}/api/${VERSION},"
done
INPUT_DIRS_SPACE="${INPUT_DIRS_SPACE%,}" # drop trailing space
INPUT_DIRS_COMMA="${INPUT_DIRS_COMMA%,}" # drop trailing comma


if [[ "${VERIFY_CODEGEN:-}" == "true" ]]; then
echo "Running in verification mode"
readonly VERIFY_FLAG="--verify-only"
fi

readonly COMMON_FLAGS="${VERIFY_FLAG:-} --go-header-file ${SCRIPT_ROOT}/hack/boilerplate.go.txt"

# throw away
new_report="$(mktemp -t "$(basename "$0").api_violations.XXXXXX")"


# TEMPORARY FIX (sure!) until https://github.com/kubernetes/kubernetes/pull/125162
# lands on a tagged release. See also https://github.com/kubernetes/code-generator/issues/167
rm core || true
ln -s api core

echo "Generating clientset at ${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}"
go run k8s.io/code-generator/cmd/client-gen@v0.30.2 \
--clientset-name "${CLIENTSET_NAME}" \
--input-base "${APIS_PKG}" \
--input "${INPUT_DIRS_COMMA//${APIS_PKG}/}" \
--output-dir "pkg/client/${CLIENTSET_PKG_NAME}" \
--output-pkg "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}" \
${COMMON_FLAGS}

echo "Generating listers at ${OUTPUT_PKG}/listers"
go run k8s.io/code-generator/cmd/lister-gen@v0.30.2 \
--output-dir "pkg/client/listers" \
--output-pkg "${OUTPUT_PKG}/listers" \
${COMMON_FLAGS} \
${INPUT_DIRS_SPACE}

echo "Generating informers at ${OUTPUT_PKG}/informers"
go run k8s.io/code-generator/cmd/informer-gen@v0.30.2 \
--versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}/${CLIENTSET_NAME}" \
--listers-package "${OUTPUT_PKG}/listers" \
--output-dir "pkg/client/informers" \
--output-pkg "${OUTPUT_PKG}/informers" \
${COMMON_FLAGS} \
${INPUT_DIRS_SPACE}
106 changes: 106 additions & 0 deletions pkg/client/clientset/versioned/clientset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions pkg/client/clientset/versioned/fake/clientset_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/client/clientset/versioned/fake/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions pkg/client/clientset/versioned/fake/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/client/clientset/versioned/scheme/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9e34c15

Please sign in to comment.