Skip to content

Commit 9bc8e5c

Browse files
committed
feat: impl krb based recycle and restore abilities.
1 parent 4a25aba commit 9bc8e5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3561
-1
lines changed

.github/workflows/release.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# .github/workflows/release.yml
2+
name: release
3+
4+
env:
5+
ALIYUN_REGISTRY: registry.cn-hangzhou.aliyuncs.com/ketches
6+
7+
on:
8+
pull_request:
9+
push:
10+
tags:
11+
- v[0-9]+.*
12+
13+
permissions:
14+
contents: write
15+
16+
jobs:
17+
build:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Set ENV
24+
run: |
25+
echo "KRB_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
26+
27+
- name: Login to Aliyun Container Registry
28+
uses: docker/login-action@v3
29+
with:
30+
registry: ${{ vars.ALIYUNACR_REGISTRY }}
31+
username: ${{ vars.ALIYUNACR_USERNAME }}
32+
password: ${{ secrets.ALIYUNACR_TOKEN }}
33+
34+
- name: Login to Docker Hub
35+
uses: docker/login-action@v3
36+
with:
37+
username: ${{ vars.DOCKERHUB_USERNAME }}
38+
password: ${{ secrets.DOCKERHUB_TOKEN }}
39+
40+
- name: Set up Docker Buildx
41+
uses: docker/setup-buildx-action@v3
42+
43+
- name: Build and push krb-controller
44+
uses: docker/build-push-action@v6
45+
with:
46+
platforms: linux/amd64,linux/arm64
47+
push: true
48+
build-args: |
49+
KRB_APPNAME=krb-controller
50+
tags: ${{ vars.ALIYUNACR_REGISTRY }}/${{ vars.ALIYUNACR_NAMESPACE }}/krb-controller:${{ env.KRB_VERSION }},${{ vars.ALIYUNACR_REGISTRY }}/${{ vars.ALIYUNACR_NAMESPACE }}/krb-controller:latest
51+
52+
- name: Build and push krb-webhook
53+
uses: docker/build-push-action@v6
54+
with:
55+
platforms: |
56+
- linux/amd64
57+
- linux/arm64
58+
push: true
59+
build-args: |
60+
KRB_APPNAME=krb-webhook
61+
tags: |
62+
- ${{ vars.ALIYUNACR_REGISTRY }}/${{ vars.ALIYUNACR_NAMESPACE }}/krb-webhook:${{ env.KRB_VERSION }}
63+
- ${{ vars.ALIYUNACR_REGISTRY }}/${{ vars.ALIYUNACR_NAMESPACE }}/krb-controller:latest
64+
65+
goreleaser:
66+
runs-on: ubuntu-latest
67+
steps:
68+
- name: Checkout
69+
uses: actions/checkout@v4
70+
with:
71+
fetch-depth: 0
72+
- name: Set up Go
73+
uses: actions/setup-go@v5
74+
- name: Run GoReleaser
75+
uses: goreleaser/goreleaser-action@v6
76+
with:
77+
# either 'goreleaser' (default) or 'goreleaser-pro'
78+
distribution: goreleaser
79+
# 'latest', 'nightly', or a semver
80+
version: "~> v2"
81+
args: release --clean
82+
env:
83+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
84+
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution
85+
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ go.work.sum
2323

2424
# env file
2525
.env
26+
27+
bin/

.goreleaser.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
4+
# The lines below are called `modelines`. See `:help modeline`
5+
# Feel free to remove those if you don't want/need to use them.
6+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
7+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
8+
9+
version: 2
10+
11+
before:
12+
hooks:
13+
# You may remove this if you don't use go modules.
14+
- go mod tidy
15+
# you may remove this if you don't need go generate
16+
# - go generate ./...
17+
18+
builds:
19+
- env:
20+
- CGO_ENABLED=0
21+
ldflags: -s -X github.com/ketches/kube-recycle-bin/cmd/krb-cli/cmd.Version={{.Version}}
22+
main: ./cmd/krb-cli
23+
goos:
24+
- linux
25+
- darwin
26+
- windows
27+
ignore:
28+
- goos: windows
29+
goarch: arm64
30+
31+
archives:
32+
- formats: ["binary"]
33+
# this name template makes the OS and Arch compatible with the results of `uname`.
34+
name_template: >-
35+
{{ .ProjectName }}_
36+
{{- .Version }}_
37+
{{- .Os }}_
38+
{{- .Arch }}
39+
40+
changelog:
41+
sort: asc
42+
filters:
43+
exclude:
44+
- "^docs:"

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM golang AS builder
2+
ARG KRB_APPNAME
3+
4+
WORKDIR /app
5+
6+
COPY go.mod go.sum ./
7+
RUN go mod download
8+
9+
COPY *.go ./
10+
COPY cmd/${KRB_APPNAME} ./cmd/${KRB_APPNAME}
11+
COPY pkg/ ./pkg/
12+
COPY internal/ ./internal/
13+
14+
RUN CGO_ENABLED=0 GOOS=linux go build -o ./bin/${KRB_APPNAME} ./cmd/${KRB_APPNAME}
15+
16+
FROM alpine:latest
17+
ARG KRB_APPNAME
18+
19+
WORKDIR /run
20+
21+
COPY --from=builder /app/bin/${KRB_APPNAME} app
22+
23+
ENTRYPOINT ["./app"]

Dockerfile.local

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM alpine:latest
2+
ARG KRB_APPNAME
3+
ARG TARGETARCH
4+
5+
WORKDIR /run
6+
7+
COPY ./bin/${TARGETARCH}/${KRB_APPNAME} app
8+
9+
ENTRYPOINT ["./app"]

Makefile

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
KRB_VERSION := v0.1.0
2+
3+
ALIYUN_REGISTRY := registry.cn-hangzhou.aliyuncs.com/ketches
4+
5+
.PHONY: install
6+
install:
7+
@echo "» installing krb-cli..."
8+
go install -ldflags="-X github.com/ketches/kube-recycle-bin/cmd/krb-cli/cmd.Version=${KRB_VERSION}" ./cmd/krb-cli
9+
10+
.PHONY: build
11+
build: build-binary build-binary build-docker
12+
13+
build-binary: build-controller-binary build-webhook-binary
14+
15+
build-controller-binary:
16+
@echo "» building krb binary..."
17+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/amd64/krb-controller cmd/krb-controller/main.go
18+
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o bin/arm64/krb-controller cmd/krb-controller/main.go
19+
20+
build-webhook-binary:
21+
@echo "» building krb binary..."
22+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/amd64/krb-webhook cmd/krb-webhook/main.go
23+
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o bin/arm64/krb-webhook cmd/krb-webhook/main.go
24+
25+
docker-buildx-init:
26+
@echo "» initializing docker buildx..."
27+
docker buildx create --use --name gobuilder 2>/dev/null || docker buildx use gobuilder
28+
29+
build-docker: docker-buildx-init build-docker-controller build-docker-webhook
30+
31+
build-docker-controller:
32+
@echo "» building krb-controller docker image..."
33+
docker buildx build --platform linux/amd64,linux/arm64 \
34+
--build-arg KRB_APPNAME=krb-controller \
35+
-t ketches/krb-controller:${KRB_VERSION} \
36+
-t ketches/krb-controller:latest \
37+
-t ${ALIYUN_REGISTRY}/krb-controller:${KRB_VERSION} \
38+
-t ${ALIYUN_REGISTRY}/krb-controller:latest \
39+
--push . -f Dockerfile.local
40+
41+
build-docker-webhook:
42+
@echo "» building krb-webhook docker image..."
43+
docker buildx build --platform linux/amd64,linux/arm64 \
44+
--build-arg KRB_APPNAME=krb-webhook \
45+
-t ketches/krb-webhook:${KRB_VERSION} \
46+
-t ketches/krb-webhook:latest \
47+
-t ${ALIYUN_REGISTRY}/krb-webhook:${KRB_VERSION} \
48+
-t ${ALIYUN_REGISTRY}/krb-webhook:latest \
49+
--push . -f Dockerfile.local
50+
51+
.PHONY: deploy
52+
deploy: deploy-crds
53+
@echo "» deploying krb controller and webhook..."
54+
kubectl apply -f manifests/deploy.yaml
55+
56+
deploy-crds:
57+
@echo "» deploying krb crds..."
58+
kubectl apply -f manifests/crds.yaml
59+
60+
.PHONY: undeploy
61+
undeploy: undeploy-crds
62+
@echo "» undeploying krb controller and webhook..."
63+
kubectl delete -f manifests/deploy.yaml
64+
65+
undeploy-crds:
66+
@echo "» undeploying krb crds..."
67+
kubectl delete -f manifests/crds.yaml
68+
69+
.PHONY: release
70+
release:
71+
@if [ -z "${KRB_VERSION}" ]; then \
72+
echo "KRB_VERSION is not set"; \
73+
exit 1; \
74+
fi
75+
@if git rev-parse "refs/tags/${KRB_VERSION}" >/dev/null 2>&1; then \
76+
echo "Git tag ${KRB_VERSION} already exists, please use a new version."; \
77+
exit 1; \
78+
fi
79+
@sed -E -i '' 's/(var Version = ")[^"]+(")/\1${KRB_VERSION}\2/' cmd/krb-cli/cmd/version.go
80+
@git add cmd/krb-cli/cmd/version.go
81+
@git commit -m "Release ${KRB_VERSION}"
82+
@git push
83+
git tag -a "${KRB_VERSION}" -m "release ${KRB_VERSION}"
84+
git push origin "${KRB_VERSION}"

README.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,102 @@
11
# kube-recycle-bin
2-
A recycle bin for deleted Kubernetes resources.
2+
3+
English | [简体中文](README_zh-CN.md)
4+
5+
kube-recycle-bin(krb) is a Kubernetes resource recycling bin that can automatically recycle and quickly restore deleted resources.
6+
7+
> In Kubernetes, resource deletion is an irreversible operation. While there are methods like Velero or etcd backup/restore that can help us recover deleted resources, have you ever felt that in practical scenarios, "using a sledgehammer to crack a nut" is excessive?
8+
>
9+
> Then try kube-recycle-bin!
10+
11+
## Features
12+
13+
Since it is a recycling bin, its main functions are:
14+
15+
1. Recycle: Supports recycling all Kubernetes resource types and allows specifying namespaces.
16+
2. Restore: 100% restoration of recycled resources.
17+
18+
## Principle
19+
20+
![principle.png](docs/images/principle.png)
21+
22+
1. Use the `krb-cli recycle` command to create a `RecyclePolicy` resource, specifying the resource types and namespaces to be recycled.
23+
2. The `krb-controller` watching for the creation, update, and deletion of `RecyclePolicy` resources, automatically synchronizing the creation, update, and deletion of corresponding `ValidatingWebhookConfiguration` resources.
24+
3. The `kube-apiserver` receives the deletion request for the specified resource and forwards the request to `krb-webhook` through `ValidatingWebhookConfiguration`.
25+
4. The `krb-webhook` parses the request and stores the deleted resource (in JSON format) into a new `RecycleItem` resource object, completing the resource recycling.
26+
5. Use the `krb-cli restore` command to restore the recycled resource. After the resource is restored, the `RecycleItem` resource object is automatically deleted.
27+
28+
## Deploy
29+
30+
1. Install CRDs
31+
32+
```bash
33+
kubectl apply -f https://raw.githubusercontent.com/ketches/kube-recycle-bin/master/manifests/crds.yaml
34+
```
35+
36+
2. Deploy `krb-controller` and `krb-webhook`
37+
38+
```bash
39+
40+
kubectl apply -f https://raw.githubusercontent.com/ketches/kube-recycle-bin/master/manifests/deploy.yaml
41+
```
42+
43+
## Install CLI
44+
45+
Multiple installation methods are available:
46+
47+
1. Install using `go install` command:
48+
49+
```bash
50+
go install github.com/ketches/kube-recycle-bin/cmd/krb-cli@latest
51+
```
52+
53+
2. Use the script (suitable for Linux and MacOS):
54+
55+
```bash
56+
curl -sSL https://github.com/ketches/kube-recycle-bin/raw/master/install_cli.sh | sh
57+
```
58+
59+
3. Download the corresponding binary file for your operating system from the [Release](https://github.com/ketches/kube-recycle-bin/releases) page, extract it, and move `krb-cli` to a directory in your `$PATH`.
60+
61+
4. Install from source code:
62+
63+
```bash
64+
git clone https://github.com/ketches/kube-recycle-bin.git
65+
cd kube-recycle-bin
66+
make install
67+
```
68+
69+
## Guide
70+
71+
Note: The prerequisite is that `krb-controller` and `krb-webhook` have been successfully deployed.
72+
73+
场景:自动回收 `dev`, `prod` 命名空间下删除的 `Deployment`, `StatefulSet``Service` 资源。
74+
Scenario: Automatically recycle deleted `Deployment`, `StatefulSet`, and `Service` resources in the `dev` and `prod` namespaces.
75+
76+
1. Create a recycling policy
77+
78+
```bash
79+
krb-cli recycle deployments statefulsets services -n dev,prod
80+
```
81+
82+
1. Restore the recycled resource
83+
84+
```bash
85+
# First, create test resources
86+
kubectl create deployment krb-test-nginx-deploy --image=nginx --replicas=0 -n dev
87+
kubectl expose deployment krb-test-nginx-deploy --name krb-test-nginx-svc --port=80 --target-port=80 -n dev
88+
89+
# Delete test resources
90+
kubectl delete deploy krb-test-nginx-deploy -n dev
91+
kubectl delete svc krb-test-nginx-svc -n dev
92+
93+
# Check the recycle bin. Execute the following command to get the deleted resources, indicating that the recycling policy has taken effect.
94+
krb-cli get ri
95+
96+
# Restore the resource using the resource name obtained from the above command
97+
krb-cli restore krb-test-nginx-deploy-skk5c89b krb-test-nginx-svc-txv4vj6v
98+
99+
# Check the restored resources
100+
kubectl get deploy krb-test-nginx-deploy -n dev
101+
kubectl get svc krb-test-nginx-svc -n dev
102+
```

0 commit comments

Comments
 (0)