diff --git a/Dockerfile b/Dockerfile index 4ba18b6..2465f77 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,7 @@ FROM golang:1.22 AS builder ARG TARGETOS ARG TARGETARCH +ARG IMAGE_VERSION_TAG WORKDIR /workspace # Copy the Go Modules manifests @@ -21,7 +22,7 @@ COPY internal/ internal/ # was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO # the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, # by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. -RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags "-X shared.BACKUP_JOB_IMAGE=ghcr.io/lukaszraczylo/kubernetes-images-sync-worker:v${IMAGE_VERSION_TAG}" -a -o manager cmd/main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details diff --git a/Makefile b/Makefile index 1737457..ea39c71 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ # Image URL to use all building/pushing image targets IMG ?= ghcr.io/lukaszraczylo/kubernetes-images-sync-operator +IMG_WORKER ?= ghcr.io/lukaszraczylo/kubernetes-images-sync-worker +CHART_NAME = kube-images-sync-operator # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. ENVTEST_K8S_VERSION = 1.31.0 @@ -115,10 +117,22 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross - $(CONTAINER_TOOL) buildx create --name kubernetes-images-sync-operator-builder $(CONTAINER_TOOL) buildx use kubernetes-images-sync-operator-builder - - $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG}:${CURRENT_VERSION} -f Dockerfile.cross . + - $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --build-arg IMAGE_VERSION_TAG=${CURRENT_VERSION} --tag ${IMG}:${CURRENT_VERSION} -f Dockerfile.cross . - $(CONTAINER_TOOL) buildx rm kubernetes-images-sync-operator-builder rm Dockerfile.cross + +.PHONY: docker-buildx-job-container +docker-buildx-job-container: ## Build and push docker image for the manager for cross-platform support + @cd docker-image-worker && ( \ + sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross && \ + $(CONTAINER_TOOL) buildx create --name kubernetes-images-sync-operator-builder || true && \ + $(CONTAINER_TOOL) buildx use kubernetes-images-sync-operator-builder && \ + $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --build-arg IMAGE_VERSION_TAG=${CURRENT_VERSION} --tag ${IMG_WORKER}:${CURRENT_VERSION} -f Dockerfile.cross . && \ + $(CONTAINER_TOOL) buildx rm kubernetes-images-sync-operator-builder || true && \ + rm Dockerfile.cross \ + ) && cd .. + .PHONY: build-installer build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment. mkdir -p dist @@ -204,6 +218,22 @@ helm: manifests kustomize helmify cp chart-defaults/Chart.yaml chart/Chart.yaml && \ ./update-version.sh $(CURRENT_VERSION) $(IMG) +.PHONY: release # Generates helm chart, builds docker images and pushes them to the registry +release: helm docker-buildx docker-buildx-job-container release-chart + +.PHONY: release-chart +release-chart: + @test -d ../helm-charts || exit 1 + rm -fr ../helm-charts/charts/${CHART_NAME} || true + mkdir -p ../helm-charts/charts/${CHART_NAME} + cp -R chart/* ../helm-charts/charts/${CHART_NAME} + cd ../helm-charts/charts/${CHART_NAME}; \ + cr package --config ../../chart-releaser.yaml; + cd ../helm-charts/; git add -A charts/packages; git fix; git push; + cd ../helm-charts/charts/${CHART_NAME}; cr upload --config ../../chart-releaser.yaml --skip-existing; + cd ../helm-charts/charts/${CHART_NAME}; rm -fr .cr-index; mkdir .cr-index; cr index --config ../../chart-releaser.yaml; cp .cr-index/index.yaml ../../index.yaml; || true + git fix; git push + # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary # $2 - package url which can be installed diff --git a/README.md b/README.md index 9ebcbfc..3031a0a 100644 --- a/README.md +++ b/README.md @@ -1,102 +1,58 @@ # kubernetes-images-sync-operator -// TODO(user): Add simple overview of use/purpose + +Kubernetes operator backing up images into the local / S3 compatible storage ## Description -// TODO(user): An in-depth paragraph about your project and overview of use + +Operator was created to simplify the impex between open-to-world and air-gapped environment. +As transfer of the deployment manifests is relatively easy, images are a completely different story. +Air-gapped environments usually have issues with missing images and amount of data required to be transferred between them. +This operator takes care of it and ensures that no images were missed out ( including initImages and ephemeralImages ) and +impex itself is as small as possible due to the cross comparison with previouslly executed backups. ## Getting Started -### Prerequisites -- go version v1.22.0+ -- docker version 17.03+. -- kubectl version v1.11.3+. -- Access to a Kubernetes v1.11.3+ cluster. +Operator installation -### To Deploy on the cluster -**Build and push your image to the location specified by `IMG`:** -```sh -make docker-build docker-push IMG=/kubernetes-images-sync-operator:tag +``` +helm repo add raczylo https://lukaszraczylo.github.io/helm-charts/ +helm install raczylo/kube-images-sync ``` -**NOTE:** This image ought to be published in the personal registry you specified. -And it is required to have access to pull the image from the working environment. -Make sure you have the proper permission to the registry if the above commands don’t work. +## Starting the backup -**Install the CRDs into the cluster:** +Please remember that backups are triggered whenever the new object appears -```sh -make install +``` +apiVersion: raczylo.com/v1 +kind: ClusterImageExport +metadata: + name: backup-20240901 +spec: + # Excludes will remove all images with listed wording from the backup list + # excludes: + # - nginx + # Includes will add ONLY images with listed wording to the backup list + includes: + - busybox + basePath: /images # base path in the target directory + storage: + target: S3 # file backup is not ready yet + s3: + bucket: my-backup-in-s3 + region: us-west-2 + accessKey: yyy + secretKey: zzz + # Endpoint allows you to direct the backup to your own S3 compatible endpoint like minio + # endpoint: http://127.0.0.1:8010 + # secretName: my-secret-in-cluster # Not ready yet + # useRole: true # Current role to be used instead of access / secret keys + # roleARN: my-awesome-role # Instead of picking the default role, use the specified one + maxConcurrentJobs: 1 ``` -**Deploy the Manager to the cluster with the image specified by `IMG`:** - -```sh -make deploy IMG=/kubernetes-images-sync-operator:tag -``` - -> **NOTE**: If you encounter RBAC errors, you may need to grant yourself cluster-admin -privileges or be logged in as admin. - -**Create instances of your solution** -You can apply the samples (examples) from the config/sample: - -```sh -kubectl apply -k config/samples/ -``` - ->**NOTE**: Ensure that the samples has default values to test it out. - -### To Uninstall -**Delete the instances (CRs) from the cluster:** - -```sh -kubectl delete -k config/samples/ -``` - -**Delete the APIs(CRDs) from the cluster:** - -```sh -make uninstall -``` - -**UnDeploy the controller from the cluster:** - -```sh -make undeploy -``` - -## Project Distribution - -Following are the steps to build the installer and distribute this project to users. - -1. Build the installer for the image built and published in the registry: - -```sh -make build-installer IMG=/kubernetes-images-sync-operator:tag -``` - -NOTE: The makefile target mentioned above generates an 'install.yaml' -file in the dist directory. This file contains all the resources built -with Kustomize, which are necessary to install this project without -its dependencies. - -2. Using the installer - -Users can just run kubectl apply -f to install the project, i.e.: - -```sh -kubectl apply -f https://raw.githubusercontent.com//kubernetes-images-sync-operator//dist/install.yaml -``` - -## Contributing -// TODO(user): Add detailed information on how you would like others to contribute to this project - -**NOTE:** Run `make help` for more information on all potential `make` targets - -More information can be found via the [Kubebuilder Documentation](https://book.kubebuilder.io/introduction.html) - -## License +#### Random fluff Copyright 2024. diff --git a/chart/Chart.yaml b/chart/Chart.yaml index b5e855a..3568d5c 100644 --- a/chart/Chart.yaml +++ b/chart/Chart.yaml @@ -10,9 +10,9 @@ description: | type: application -version: 0.0.26 +version: 0.1.5 -appVersion: "0.0.26" +appVersion: "0.1.5" home: https://github.com/lukaszraczylo/kubernetes-images-sync-operator diff --git a/chart/values.yaml b/chart/values.yaml index 2560416..6f99b67 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -10,8 +10,8 @@ cmRaczyloCom: drop: - ALL image: - repository: controller - tag: latest + repository: ghcr.io/lukaszraczylo/kubernetes-images-sync-operator + tag: 0.1.5 resources: limits: cpu: 500m diff --git a/docker-image-management/Dockerfile b/docker-image-worker/Dockerfile similarity index 100% rename from docker-image-management/Dockerfile rename to docker-image-worker/Dockerfile diff --git a/docker-image-management/cleanup.py b/docker-image-worker/cleanup.py similarity index 100% rename from docker-image-management/cleanup.py rename to docker-image-worker/cleanup.py diff --git a/docker-image-management/containers.conf b/docker-image-worker/containers.conf similarity index 100% rename from docker-image-management/containers.conf rename to docker-image-worker/containers.conf diff --git a/docker-image-management/export.py b/docker-image-worker/export.py similarity index 100% rename from docker-image-management/export.py rename to docker-image-worker/export.py diff --git a/docker-image-management/registries.conf b/docker-image-worker/registries.conf similarity index 100% rename from docker-image-management/registries.conf rename to docker-image-worker/registries.conf diff --git a/docker-image-management/requirements.txt b/docker-image-worker/requirements.txt similarity index 100% rename from docker-image-management/requirements.txt rename to docker-image-worker/requirements.txt diff --git a/docker-image-management/storage.conf b/docker-image-worker/storage.conf similarity index 100% rename from docker-image-management/storage.conf rename to docker-image-worker/storage.conf diff --git a/internal/shared/definitions.go b/internal/shared/definitions.go index e4a8f6c..c424881 100644 --- a/internal/shared/definitions.go +++ b/internal/shared/definitions.go @@ -7,7 +7,7 @@ import ( const ( // JOB IMAGES - BACKUP_JOB_IMAGE = "ghcr.io/lukaszraczylo/docker-image-management:v0.0.6" + BACKUP_JOB_IMAGE = "ghcr.io/lukaszraczylo/kubernetes-images-sync-worker:1.0.2" // AVAILABLE STATUSES STATUS_PENDING = "PENDING" diff --git a/update-version.sh b/update-version.sh index 6c88942..376c263 100755 --- a/update-version.sh +++ b/update-version.sh @@ -1,8 +1,10 @@ #!/bin/bash if [[ "$OSTYPE" == "darwin"* ]]; then find chart/ -type f -exec sed -i '' "s/0.0.0/$1/g" {} + - find chart/values.yaml -type f -exec sed -i '' "s/repository: controller/$2/g" {} + + find chart/values.yaml -type f -exec sed -i '' "s|controller|$2|g" {} + + find chart/values.yaml -type f -exec sed -i '' "s|latest|$1|g" {} + else find chart/ -type f -exec sed -i "s/0.0.0/$1/g" {} + - find chart/values.yaml -type f -exec sed -i "s/repository: controller/$2/g" {} + + find chart/values.yaml -type f -exec sed "s|controller|$2|g" {} + + find chart/values.yaml -type f -exec sed -i "s|latest|$1|g" {} + fi \ No newline at end of file