Skip to content

Commit

Permalink
Add scripts to synchronize schemes that have been removed
Browse files Browse the repository at this point in the history
Signed-off-by: Shiming Zhang <[email protected]>
  • Loading branch information
wzshiming committed May 11, 2024
1 parent e0dda1c commit cfa032d
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
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
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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 (
"github.com/etcd-io/auger/pkg/scheme"
oldscheme "github.com/etcd-io/auger/pkg/old/scheme"
)

func init() {
oldscheme.AddToScheme(scheme.Scheme)
}
85 changes: 85 additions & 0 deletions hack/clone_old_apis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/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 clone_or_checkout() {
local repo=https://github.com/kubernetes/api
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 release 1.${release}
Generated by ./hack/clone_old_apis.sh
" > "${dir_api}/README.md"
fi
done
}

last_release=${1}

for release in $(seq ${last_release} -1 9); 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" || :
}

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 "github.com/etcd-io/auger/pkg/old/apis/${pkg}" | awk -F '/' '{print " "$7$8, "\""$1"\/"$2"\/"$3"\/"$4"\/"$5"\/"$6"\/"$7"\/"$8"\""}'
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

0 comments on commit cfa032d

Please sign in to comment.