From 4fb4c59d7b567b0d25f0ffd2625734ce5ae413e2 Mon Sep 17 00:00:00 2001 From: jlandowner Date: Thu, 6 Jun 2024 23:00:53 +0900 Subject: [PATCH] Remove cosmo-traefik-plugin image and bundle it in controller-manager --- .gitignore | 3 ++- Makefile | 24 ++++++++----------- charts/cosmo/values.yaml | 12 ++++++---- cmd/controller-manager/main.go | 13 ++++++++++ dockerfile/controller-manager.Dockerfile | 16 ++++++++++++- dockerfile/traefik-plugins.Dockerfile | 11 --------- go.work | 2 +- hack/local-run-test/Makefile | 16 +++---------- .../cosmo-workspace/cosmoauth/.gitignore | 0 .../cosmo-workspace/cosmoauth/.traefik.yml | 0 .../cosmo-workspace/cosmoauth/Makefile | 0 .../cosmoauth_middleware_test.snap | 0 .../cosmoauth/cosmoauth_middleware.go | 0 .../cosmoauth/cosmoauth_middleware_test.go | 0 .../cosmo-workspace/cosmoauth/go.mod | 2 +- .../cosmo-workspace/cosmoauth/go.sum | 0 .../cosmo-workspace/cosmoauth/html.go | 0 17 files changed, 53 insertions(+), 46 deletions(-) delete mode 100644 dockerfile/traefik-plugins.Dockerfile rename {traefik/plugins => traefik-plugins/src/github.com}/cosmo-workspace/cosmoauth/.gitignore (100%) rename {traefik/plugins => traefik-plugins/src/github.com}/cosmo-workspace/cosmoauth/.traefik.yml (100%) rename {traefik/plugins => traefik-plugins/src/github.com}/cosmo-workspace/cosmoauth/Makefile (100%) rename {traefik/plugins => traefik-plugins/src/github.com}/cosmo-workspace/cosmoauth/__snapshots__/cosmoauth_middleware_test.snap (100%) rename {traefik/plugins => traefik-plugins/src/github.com}/cosmo-workspace/cosmoauth/cosmoauth_middleware.go (100%) rename {traefik/plugins => traefik-plugins/src/github.com}/cosmo-workspace/cosmoauth/cosmoauth_middleware_test.go (100%) rename {traefik/plugins => traefik-plugins/src/github.com}/cosmo-workspace/cosmoauth/go.mod (92%) rename {traefik/plugins => traefik-plugins/src/github.com}/cosmo-workspace/cosmoauth/go.sum (100%) rename {traefik/plugins => traefik-plugins/src/github.com}/cosmo-workspace/cosmoauth/html.go (100%) diff --git a/.gitignore b/.gitignore index 9db248da..ce3cbae3 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,5 @@ bin *.crt *.cert -config/crd/traefik \ No newline at end of file +config/crd/traefik +cmd/controller-manager/traefik-plugins.tar.gz diff --git a/Makefile b/Makefile index 1244a0a5..fddc75cc 100644 --- a/Makefile +++ b/Makefile @@ -7,13 +7,11 @@ QUICK_BUILD ?= no MANAGER_VERSION ?= $(VERSION) DASHBOARD_VERSION ?= $(VERSION) COSMOCTL_VERSION ?= $(VERSION) -TRAEFIK_PLUGINS_VERSION ?= $(VERSION) CHART_VERSION ?= $(VERSION) IMG_MANAGER ?= cosmo-controller-manager:$(MANAGER_VERSION) IMG_DASHBOARD ?= cosmo-dashboard:$(DASHBOARD_VERSION) -IMG_TRAEFIK_PLUGINS ?= cosmo-traefik-plugins:$(TRAEFIK_PLUGINS_VERSION) # Produce CRDs that work back to Kubernetes 1.11 (no version conversion) CRD_OPTIONS ?= "crd:generateEmbeddedObjectMeta=true" @@ -73,7 +71,7 @@ ifeq ($(QUICK_BUILD),no) endif .PHONY: vet -vet: go ## Run go vet against code. +vet: go embed-traefik-plugins ## Run go vet against code. ifeq ($(QUICK_BUILD),no) $(GO) vet ./... endif @@ -204,11 +202,17 @@ run: go generate fmt vet manifests ## Run controller-manager against the configu --metrics-bind-address :8085 \ --cert-dir . +embed-traefik-plugins: cmd/controller-manager/traefik-plugins.tar.gz +cmd/controller-manager/traefik-plugins.tar.gz: + make -C traefik-plugins/src/github.com/cosmo-workspace/cosmoauth vendor + cd traefik-plugins && tar zcvf traefik-plugins.tar.gz src/ + mv traefik-plugins/traefik-plugins.tar.gz cmd/controller-manager/ + ##--------------------------------------------------------------------- ##@ Docker build ##--------------------------------------------------------------------- .PHONY: docker-build -docker-build: docker-build-manager docker-build-dashboard docker-build-traefik-plugins ## Build the docker image. +docker-build: docker-build-manager docker-build-dashboard ## Build the docker image. .PHONY: docker-build-manager docker-build-manager: test ## Build the docker image for controller-manager. @@ -218,12 +222,8 @@ docker-build-manager: test ## Build the docker image for controller-manager. docker-build-dashboard: test ## Build the docker image for dashboard. DOCKER_BUILDKIT=1 docker build . -t ${IMG_DASHBOARD} -f dockerfile/dashboard.Dockerfile -.PHONY: docker-build-traefik-plugins -docker-build-traefik-plugins: test ## Build the docker image for traefik-plugins. - DOCKER_BUILDKIT=1 docker build . -t ${IMG_TRAEFIK_PLUGINS} -f dockerfile/traefik-plugins.Dockerfile - -.PHONY: docker-push docker-push-manager docker-push-dashboard docker-push-traefik-plugins -docker-push: docker-push-manager docker-push-dashboard docker-push-traefik-plugins ## Build the docker image. +.PHONY: docker-push docker-push-manager docker-push-dashboard +docker-push: docker-push-manager docker-push-dashboard ## Build the docker image. REGISTORY ?= ghcr.io/cosmo-workspace @@ -235,10 +235,6 @@ docker-push-dashboard: docker-build-dashboard ## push cosmo dashboard image. docker tag ${IMG_DASHBOARD} ${REGISTORY}/${IMG_DASHBOARD} docker push ${REGISTORY}/${IMG_DASHBOARD} -docker-push-traefik-plugins: docker-build-traefik-plugins ## push cosmo traefik-plugins image. - docker tag ${IMG_TRAEFIK_PLUGINS} ${REGISTORY}/${IMG_TRAEFIK_PLUGINS} - docker push ${REGISTORY}/${IMG_TRAEFIK_PLUGINS} - ##--------------------------------------------------------------------- ##@ Deployment ##--------------------------------------------------------------------- diff --git a/charts/cosmo/values.yaml b/charts/cosmo/values.yaml index 8d99c01f..542089b9 100644 --- a/charts/cosmo/values.yaml +++ b/charts/cosmo/values.yaml @@ -253,20 +253,24 @@ traefik: # Set the same namespace with controller and dashboard namespaceOverride: cosmo-system + # ---------- DO NOT EDIT BELOW EXCEPT UNUSUAL USECASE ---------- # Enable plugins # local plugin is not supported by this options on 28.0.0 # so add configs manually via additionalVolumes, additionalVolumeMounts and additionalArguments # experimental: # plugins: # enabled: true - deployment: - # Installing local plugins by init containers initContainers: - name: copy-plugins - image: ghcr.io/cosmo-workspace/cosmo-traefik-plugins:latest + image: alpine imagePullPolicy: IfNotPresent - command: ["sh", "-c", "cp -r /plugins-local/* /local-plugins/"] + command: ["sh", "-c"] + args: + - | + cd /tmp && \ + wget --no-check-certificate https://cosmo-webhook-service/traefik-plugins.tar.gz && \ + tar -xvC /local-plugins -f traefik-plugins.tar.gz volumeMounts: - name: local-plugins mountPath: /local-plugins diff --git a/cmd/controller-manager/main.go b/cmd/controller-manager/main.go index 5e06a124..a3106fd0 100644 --- a/cmd/controller-manager/main.go +++ b/cmd/controller-manager/main.go @@ -1,8 +1,10 @@ package main import ( + _ "embed" "flag" "fmt" + "net/http" "os" "reflect" @@ -48,6 +50,9 @@ var ( scheme = runtime.NewScheme() setupLog = ctrl.Log.WithName("setup") o = &option{} + + //go:embed traefik-plugins.tar.gz + traefikPluginFileContent []byte ) type option struct { @@ -210,6 +215,14 @@ MIT 2023 cosmo-workspace/cosmo Decoder: admission.NewDecoder(mgr.GetScheme()), }).SetupWebhookWithManager(mgr) + // Serving traefik plugin + mgr.GetWebhookServer().Register( + "/traefik-plugins.tar.gz", + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write(traefikPluginFileContent) + }), + ) + ctx := ctrl.SetupSignalHandler() if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/dockerfile/controller-manager.Dockerfile b/dockerfile/controller-manager.Dockerfile index 58802e2f..650655df 100644 --- a/dockerfile/controller-manager.Dockerfile +++ b/dockerfile/controller-manager.Dockerfile @@ -1,4 +1,15 @@ -# Build the manager binary +# ----- traefik-plugin-builder ------ +# https://traefik.io/blog/using-private-plugins-in-traefik-proxy-2-5/ +FROM golang:1.22 as traefik-plugin-builder + +WORKDIR /cosmo +COPY . . + +RUN make -C traefik-plugins/src/github.com/cosmo-workspace/cosmoauth vendor + +RUN cd traefik-plugins && tar zcvf traefik-plugins.tar.gz src/ + +# ----- builder ------ FROM golang:1.22 as builder ENV GO111MODULE=on @@ -17,6 +28,9 @@ COPY api/ api/ COPY pkg/ pkg/ COPY internal/ internal/ +# Copy traefik-plugins embeded +COPY --from=traefik-plugin-builder /cosmo/traefik-plugins/traefik-plugins.tar.gz cmd/controller-manager/ + # Build RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager ./cmd/controller-manager/main.go diff --git a/dockerfile/traefik-plugins.Dockerfile b/dockerfile/traefik-plugins.Dockerfile deleted file mode 100644 index 0da0a0f7..00000000 --- a/dockerfile/traefik-plugins.Dockerfile +++ /dev/null @@ -1,11 +0,0 @@ -### see https://traefik.io/blog/using-private-plugins-in-traefik-proxy-2-5/ -FROM golang:1.22 - -WORKDIR /cosmo - -COPY . . - -RUN make -C traefik/plugins/cosmo-workspace/cosmoauth vendor - -FROM alpine:3 -COPY --from=0 /cosmo/traefik/plugins/ /plugins-local/src/github.com/ diff --git a/go.work b/go.work index a1d6ba82..3106e586 100644 --- a/go.work +++ b/go.work @@ -4,5 +4,5 @@ toolchain go1.22.3 use ( . - ./traefik/plugins/cosmo-workspace/cosmoauth + ./traefik-plugins/src/github.com/cosmo-workspace/cosmoauth ) diff --git a/hack/local-run-test/Makefile b/hack/local-run-test/Makefile index 4fa1e3e2..936186f5 100644 --- a/hack/local-run-test/Makefile +++ b/hack/local-run-test/Makefile @@ -69,7 +69,7 @@ show-url: ## show-url create-all: create-cluster docker-build-all install-all apply-template add-user add-workspace ## Create all delete-all: delete-cluster ## Delete all -docker-build-all: docker-build-manager docker-build-dashboard docker-build-traefik-plugins ## Docker build all +docker-build-all: docker-build-manager docker-build-dashboard ## Docker build all install-all: install-cosmo ## Install cosmo resources. uninstall-all: uninstall-cosmo ## Uninstall cosmo resources. @@ -195,7 +195,7 @@ MANAGER_IMAGE_TAG ?= $(IMAGE_TAG) DASHBOARD_IMAGE_TAG ?= $(IMAGE_TAG) TRAEFIK_PLUGINS_IMAGE_TAG ?= $(IMAGE_TAG) -.PHONY: docker-build-manager docker-build-dashboard docker-build-traefik-plugins docker-cache-clear +.PHONY: docker-build-manager docker-build-dashboard docker-cache-clear docker-build-manager: ## build & push cosmo contoller-manager image. @echo ====== $@ ====== @@ -215,16 +215,6 @@ docker-build-dashboard: ## build & push cosmo dashboard image. k3d image import localhost:5000/cosmo-dashboard:$(DASHBOARD_IMAGE_TAG) -c $(CLUSTER_NAME) -kubectl rollout restart deploy -n cosmo-system cosmo-dashboard -docker-build-traefik-plugins: ## build & push cosmo traefik-plugins image. - @echo ====== $@ ====== - cd ../.. && make docker-build-traefik-plugins VERSION=$(TRAEFIK_PLUGINS_IMAGE_TAG) QUICK_BUILD=yes - docker tag cosmo-traefik-plugins:$(TRAEFIK_PLUGINS_IMAGE_TAG) localhost:5000/traefik-plugins:$(TRAEFIK_PLUGINS_IMAGE_TAG) - docker push localhost:5000/traefik-plugins:$(TRAEFIK_PLUGINS_IMAGE_TAG) - docker rmi cosmo-traefik-plugins:$(TRAEFIK_PLUGINS_IMAGE_TAG) - k3d image import localhost:5000/traefik-plugins:$(TRAEFIK_PLUGINS_IMAGE_TAG) -c $(CLUSTER_NAME) - -kubectl rollout restart deploy -n cosmo-system traefik - - docker-cache-clear: ## docker cache clear. -docker system df -docker images @@ -240,7 +230,7 @@ docker-cache-clear: ## docker cache clear. LOGLEVEL ?= info -install-cosmo: #helm kubectl docker-build-manager docker-build-dashboard docker-build-traefik-plugins ## Install cosmo resources. +install-cosmo: #helm kubectl docker-build-manager docker-build-dashboard ## Install cosmo resources. @echo ====== $@ ====== helm dependency update ../../charts/cosmo helm upgrade --install cosmo ../../charts/cosmo \ diff --git a/traefik/plugins/cosmo-workspace/cosmoauth/.gitignore b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/.gitignore similarity index 100% rename from traefik/plugins/cosmo-workspace/cosmoauth/.gitignore rename to traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/.gitignore diff --git a/traefik/plugins/cosmo-workspace/cosmoauth/.traefik.yml b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/.traefik.yml similarity index 100% rename from traefik/plugins/cosmo-workspace/cosmoauth/.traefik.yml rename to traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/.traefik.yml diff --git a/traefik/plugins/cosmo-workspace/cosmoauth/Makefile b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/Makefile similarity index 100% rename from traefik/plugins/cosmo-workspace/cosmoauth/Makefile rename to traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/Makefile diff --git a/traefik/plugins/cosmo-workspace/cosmoauth/__snapshots__/cosmoauth_middleware_test.snap b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/__snapshots__/cosmoauth_middleware_test.snap similarity index 100% rename from traefik/plugins/cosmo-workspace/cosmoauth/__snapshots__/cosmoauth_middleware_test.snap rename to traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/__snapshots__/cosmoauth_middleware_test.snap diff --git a/traefik/plugins/cosmo-workspace/cosmoauth/cosmoauth_middleware.go b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/cosmoauth_middleware.go similarity index 100% rename from traefik/plugins/cosmo-workspace/cosmoauth/cosmoauth_middleware.go rename to traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/cosmoauth_middleware.go diff --git a/traefik/plugins/cosmo-workspace/cosmoauth/cosmoauth_middleware_test.go b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/cosmoauth_middleware_test.go similarity index 100% rename from traefik/plugins/cosmo-workspace/cosmoauth/cosmoauth_middleware_test.go rename to traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/cosmoauth_middleware_test.go diff --git a/traefik/plugins/cosmo-workspace/cosmoauth/go.mod b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/go.mod similarity index 92% rename from traefik/plugins/cosmo-workspace/cosmoauth/go.mod rename to traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/go.mod index 7d065220..f520aef1 100644 --- a/traefik/plugins/cosmo-workspace/cosmoauth/go.mod +++ b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/go.mod @@ -25,4 +25,4 @@ require ( github.com/tidwall/sjson v1.2.5 // indirect ) -replace github.com/cosmo-workspace/cosmo => ../../../../ +replace github.com/cosmo-workspace/cosmo => ../../../../../ diff --git a/traefik/plugins/cosmo-workspace/cosmoauth/go.sum b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/go.sum similarity index 100% rename from traefik/plugins/cosmo-workspace/cosmoauth/go.sum rename to traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/go.sum diff --git a/traefik/plugins/cosmo-workspace/cosmoauth/html.go b/traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/html.go similarity index 100% rename from traefik/plugins/cosmo-workspace/cosmoauth/html.go rename to traefik-plugins/src/github.com/cosmo-workspace/cosmoauth/html.go