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

feat: Support for issuing certificates using cert-manager #44

Closed
wants to merge 18 commits into from
Closed
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
9 changes: 5 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.20
go-version: ^1.22
id: go

- name: Check out code into the Go module directory
Expand All @@ -38,17 +38,18 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.22'
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- uses: golangci/golangci-lint-action@v3
- uses: golangci/golangci-lint-action@v6
with:
version: v1.52.2
version: v1.59
args: --timeout=10m

docker-image:
name: "Push to dockerhub"
Expand Down
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ run:

linters-settings:
govet:
check-shadowing: true
shadow: true
gofmt:
simplify: true
maligned:
Expand All @@ -15,6 +15,5 @@ linters:
- goimports
- gofmt
- misspell
- maligned
disable:
- errcheck
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
BINARY=kubernetes-router
TAG=latest
IMAGE=tsuru/$(BINARY)
LOCAL_REGISTRY=10.200.10.1:5000
NAMESPACE=tsuru
LOCAL_REGISTRY=localhost:5000
NAMESPACE=tsuru-system
LINTER_ARGS = \
-j 4 --enable-gc -s vendor -e '.*/vendor/.*' --vendor --enable=misspell --enable=gofmt --enable=goimports \
--disable=gocyclo --disable=gosec --deadline=60m --tests
RUN_FLAGS=-v 9

# When using Podman, set DOCKER=podman
DOCKER ?= docker

.PHONY: run
run: build
./$(BINARY) $(RUN_FLAGS)
Expand All @@ -18,11 +21,11 @@ build:

.PHONY: build-docker
build-docker:
docker build --rm -t $(IMAGE):$(TAG) .
$(DOCKER) build --rm -t $(IMAGE):$(TAG) .

.PHONY: push
push: build-docker
docker push $(IMAGE):$(TAG)
$(DOCKER) push $(IMAGE):$(TAG) --tls-verify=false

.PHONY: test
test:
Expand All @@ -31,8 +34,6 @@ test:
.PHONY: lint
lint:
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin
go install ./...
go test -i ./...
$$(go env GOPATH)/bin/golangci-lint run -c ./.golangci.yml ./...

.PHONY: minikube
Expand Down
34 changes: 34 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func (a *RouterAPI) registerRoutes(r *mux.Router) {
r.Handle("/backend/{name}/certificate/{certname}", handler(a.getCertificate)).Methods(http.MethodGet)
r.Handle("/backend/{name}/certificate/{certname}", handler(a.removeCertificate)).Methods(http.MethodDelete)

// cert-manager
r.Handle("/backend/{name}/cert-manager/{certname}", handler(a.issueCertManagerCert)).Methods(http.MethodPut)

// Supports
r.Handle("/support/tls", handler(a.supportTLS)).Methods(http.MethodGet)
r.Handle("/support/info", handler(func(w http.ResponseWriter, r *http.Request) error {
Expand Down Expand Up @@ -310,6 +313,37 @@ func (a *RouterAPI) removeCertificate(w http.ResponseWriter, r *http.Request) er
return err
}

// issueCertManagerCert Issues certificate for the app
func (a *RouterAPI) issueCertManagerCert(_ http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
vars := mux.Vars(r)
name := vars["name"]
certName := vars["certname"]

log.Printf("Issuing certificate %s for %s", certName, name)

cert := router.CertManagerIssuerData{}
err := json.NewDecoder(r.Body).Decode(&cert)
if err != nil {
return err
}

svc, err := a.router(ctx, vars["mode"], r.Header)
if err != nil {
return err
}

cmRouter, ok := svc.(router.RouterCertManager)
if !ok {
return httpError{
Status: http.StatusNotFound,
Body: fmt.Sprintf("Router %s doesn't have cert-manager support", vars["mode"]),
}
}

return cmRouter.IssueCertManagerCertificate(ctx, instanceID(r), certName, cert.Issuer)
}

// Check for TLS Support
func (a *RouterAPI) supportTLS(w http.ResponseWriter, r *http.Request) error {
var err error
Expand Down
2 changes: 1 addition & 1 deletion deployments/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spec:
scheme: HTTP
timeoutSeconds: 5
command: ["./kubernetes-router"]
args: ["-v", "3", "--k8s-namespace", "NAMESPACE"]
args: ["-v", "3", "--k8s-namespace", "NAMESPACE", "--controller-modes", "ingress-nginx"]
ports:
- containerPort: 8077
---
Expand Down
99 changes: 51 additions & 48 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,75 +1,78 @@
module github.com/tsuru/kubernetes-router

go 1.20
go 1.22.0

toolchain go1.22.5

require (
github.com/cert-manager/cert-manager v1.15.2
github.com/ghodss/yaml v1.0.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/glog v1.2.0
github.com/gorilla/mux v1.8.0
github.com/opentracing-contrib/go-stdlib v1.0.0
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/stretchr/testify v1.7.0
github.com/prometheus/client_golang v1.18.0
github.com/stretchr/testify v1.9.0
github.com/tsuru/tsuru v0.0.0-20201016203419-9a2686f0f674
github.com/uber/jaeger-client-go v2.25.0+incompatible
github.com/urfave/negroni v0.2.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sync v0.7.0
istio.io/api v0.0.0-20200911191701-0dc35ad5c478
istio.io/client-go v0.0.0-20200807182027-d287a5abb594
istio.io/pkg v0.0.0-20201020203611-6565bf4f242a
k8s.io/api v0.22.9
k8s.io/apiextensions-apiserver v0.22.9
k8s.io/apimachinery v0.22.9
k8s.io/client-go v0.22.9
k8s.io/api v0.30.1
k8s.io/apiextensions-apiserver v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
sigs.k8s.io/controller-runtime v0.18.2
)

require (
cloud.google.com/go v0.65.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/onsi/gomega v1.10.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/cobra v1.1.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.15.0 // indirect
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
istio.io/gogo-genproto v0.0.0-20201015184601-1e80d26d6249 // indirect
k8s.io/klog/v2 v2.9.0 // indirect
k8s.io/kube-openapi v0.0.0-20211109043538-20434351676c // indirect
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240430033511-f0e62f92d13f // indirect
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
sigs.k8s.io/gateway-api v1.1.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading
Loading