Skip to content

Commit

Permalink
Remove cosmo-traefik-plugin image and bundle it in controller-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed Jun 6, 2024
1 parent 976bc72 commit 4fb4c59
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ bin
*.crt
*.cert

config/crd/traefik
config/crd/traefik
cmd/controller-manager/traefik-plugins.tar.gz
24 changes: 10 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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
##---------------------------------------------------------------------
Expand Down
12 changes: 8 additions & 4 deletions charts/cosmo/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions cmd/controller-manager/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
_ "embed"
"flag"
"fmt"
"net/http"
"os"
"reflect"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion dockerfile/controller-manager.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
11 changes: 0 additions & 11 deletions dockerfile/traefik-plugins.Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ toolchain go1.22.3

use (
.
./traefik/plugins/cosmo-workspace/cosmoauth
./traefik-plugins/src/github.com/cosmo-workspace/cosmoauth
)
16 changes: 3 additions & 13 deletions hack/local-run-test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ====== $@ ======
Expand All @@ -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
Expand All @@ -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 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ../../../../../

0 comments on commit 4fb4c59

Please sign in to comment.