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
Show file tree
Hide file tree
Changes from all commits
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
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
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ 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}')
./scripts/fix.sh

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

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,14 @@ auger checksum -f <member-3-boltdb-file> -r 7
> revision: 7
# Oh noes! The checksum should have been the same!
```

### Scheme

When updating `k8s.io/api` in go.mod, run:

```sh
make pkg/scheme/scheme.go pkg/old/scheme/scheme.go
```

`pkg/scheme/scheme.go` includes the current release of `k8s.io/api`
`pkg/old/scheme/scheme.go` includes the previous release removed of `k8s.io/api`
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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
toolchain go1.22.6

require (
github.com/gogo/protobuf v1.3.2
github.com/google/safetext v0.0.0-20220914124124-e18e3fe012bf
github.com/spf13/cobra v1.8.1
go.etcd.io/bbolt v1.3.10
Expand All @@ -21,7 +22,6 @@ require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
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
5 changes: 5 additions & 0 deletions pkg/old/apis/auditregistration/v1alpha1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# k8s.io/api/auditregistration/v1alpha1

Copying from https://github.com/kubernetes/api/tree/release-1.18/auditregistration/v1alpha1

Generated by ./hack/clone_old_apis.sh
23 changes: 23 additions & 0 deletions pkg/old/apis/auditregistration/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2018 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.
*/

// +k8s:deepcopy-gen=package
// +k8s:protobuf-gen=package
// +k8s:openapi-gen=true

// +groupName=auditregistration.k8s.io

package v1alpha1 // import "k8s.io/api/auditregistration/v1alpha1"
Loading
Loading