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

Add scripts to synchronize old schemes #53

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Next Next commit
Add scripts to synchronize schemes that have been removed
Signed-off-by: Shiming Zhang <[email protected]>
wzshiming committed Aug 14, 2024
commit fe0e5028f554cfdfd3a174b5ef5ca800a4f9f2ab
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
auger
build
vendor
_tmp

# IDEs
*.iml
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -73,6 +73,14 @@ pkg/scheme/scheme.go: ./hack/gen_scheme.sh go.mod
-rm ./pkg/scheme/scheme.go
./hack/gen_scheme.sh > ./pkg/scheme/scheme.go

pkg/old/scheme/scheme.go: ./hack/gen_old_scheme.sh pkg/old/apis
-rm ./pkg/old/scheme/scheme.go
./hack/gen_old_scheme.sh > ./pkg/old/scheme/scheme.go

pkg/old/apis: ./hack/clone_old_apis.sh go.mod
-rm -rf ./pkg/old/apis/*
./hack/clone_old_apis.sh $(shell cat go.mod | grep 'k8s.io/api v0.' | awk '{print $$2}' | awk -F. '{print $$2}')

.PHONY: generate
generate: pkg/scheme/scheme.go

26 changes: 26 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2024 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.
*/

package cmd

import (
oldscheme "github.com/etcd-io/auger/pkg/old/scheme"
"github.com/etcd-io/auger/pkg/scheme"
)

func init() {
oldscheme.AddToScheme(scheme.Scheme)
}
88 changes: 88 additions & 0 deletions hack/clone_old_apis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bash
# Copyright 2024 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.

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

DIR="$(dirname "${BASH_SOURCE[0]}")"
ROOT_DIR="$(realpath "${DIR}/..")"
REPO=https://github.com/kubernetes/api

function clone_or_checkout() {
local version=$1
local dest=$2

if [[ -d "${dest}" ]]; then
echo "Checking out ${REPO}#${version} to ${dest}"
pushd "${dest}"
git fetch origin
git checkout "${version}"
popd
else
echo "Cloning ${REPO}#${version} to ${dest}"
git clone --branch "${version}" --depth 1 "${REPO}" "${dest}"
fi
}

function find_package() {
local dir=$1
find "${dir}" | grep register.go | sed "s#/register.go##g" | sed "s#${dir}/##g" || :
}

apiset=()

function append_api() {
local api=$1
if [[ ! " ${apiset[@]} " =~ " ${api} " ]]; then
apiset+=("${api}")
return 0
fi
return 1
}

function clone_api() {
local release=$1
local skip=$2
version="release-1.${release}"
version_dir="${ROOT_DIR}/_tmp/k8s-api/${version}"
clone_or_checkout "${version}" "${version_dir}"
for api in $(find_package "${version_dir}"); do
if append_api "${api}" ; then
if [[ "${release}" -eq "${skip}" ]]; then
continue
fi
dir_api="${ROOT_DIR}/pkg/old/apis/${api}"
echo "Copying ${version_dir}/${api}/*.go to ${dir_api}"
mkdir -p "${dir_api}"
cp "${version_dir}/${api}"/*.go "${dir_api}"
echo "# k8s.io/api/${api}

Copying from ${REPO}/tree/${version}/${api}

Generated by ./hack/clone_old_apis.sh" > "${dir_api}/README.md"
fi
done
}

last_release=${1}

# 1.8 incompatible
# 1.9 ~ 1.17 no removed APIs
first_release=18

for release in $(seq "${last_release}" -1 "${first_release}"); do
clone_api "${release}" "${last_release}"
done
73 changes: 73 additions & 0 deletions hack/gen_old_scheme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash
# Copyright 2024 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.

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

DIR="$(dirname "${BASH_SOURCE[0]}")"
ROOT_DIR="$(realpath "${DIR}/..")"

function find_package() {
local dir=$1
find "${dir}" | grep register.go | sed "s#/register.go##g" | sed "s#${dir}/##g" | sort || :
}

function gen() {
cat <<EOF
/*
Copyright 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.
*/

package scheme

// Don't edit this file directly. It is generated by gen_old_scheme.sh.
import (
EOF

for pkg in $(find_package "${ROOT_DIR}/pkg/old/apis"); do
echo "${pkg}" | awk -F '/' '{print " "$1$2, "\"github.com/etcd-io/auger/pkg/old/apis/"$1"\/"$2"\""}'
done

cat <<EOF
"k8s.io/apimachinery/pkg/runtime"
)

// AddToScheme adds all types of this clientset into the given scheme.
func AddToScheme(scheme *runtime.Scheme) {
EOF

for pkg in $(find_package "${ROOT_DIR}/pkg/old/apis"); do
echo "${pkg}" | awk -F '/' '{print " _ = " $1$2"\.AddToScheme(scheme)"}'
done

cat <<EOF
}
EOF
}

gen