Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
feat: use tls (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
zalsader authored Aug 25, 2023
1 parent 4da1e6f commit fe6e408
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
# generate Docker tags based on the following events/attributes
tags: |
type=ref,event=branch
type=raw,value=latest,enable={{is_default_branch}}
type=sha,prefix={{branch}}-
type=semver,pattern=v{{version}}
type=semver,pattern=v{{major}}.{{minor}}
type=semver,pattern=v{{major}}
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ bin

# Go workspace file
go.work

# Local certs
certs/*.pem
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Version is derived from tags
VERSION ?= $(shell git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')
VERSION ?= $(shell git describe --dirty --always --tags)

# REGISTRY_HOST defines the registry and organization used to publish the images.
# Use localhost:5000 for the local registry
Expand Down Expand Up @@ -48,6 +48,12 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

.PHONY: local-certs
local-certs: certs/cert.pem ## generate local certs

certs/cert.pem:
mkcert -install -cert-file=certs/cert.pem -key-file=certs/key.pem localhost

##@ Build

.PHONY: build
Expand All @@ -57,6 +63,11 @@ build: generate fmt vet ## Build server binary.
.PHONY: run
run: generate fmt vet ## Run a server from your host.
@echo "Listening on http://localhost:8082"
NO_TLS=true go run ./main.go

.PHONY: run-tls
run-tls: generate fmt vet local-certs ## Run a server from your host.
@echo "Listening on https://localhost:8082"
go run ./main.go

# If you wish to build the version service image targeting other platforms you can use the --platform flag.
Expand All @@ -72,7 +83,7 @@ docker-push: ## Push docker image with the version service.

.PHONY: docker-run-it
docker-run-it: docker-build ## Run docker image
docker run -it --rm -p 8082:8082 ${IMG}
docker run -it --rm -p 8082:8082 -e NO_TLS=true ${IMG}

##@ Build Dependencies

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# KSD Version Service

KSD Version service provides an API to access the supported versions of KSD
and its dependencies.

For each version of the operator, there is a file in the `data` directory that
contains a list of supported components for that version.

## Running locally

```bash
make run
```

To run with tls, make sure that [`mkcert`](https://github.com/FiloSottile/mkcert) is installed, then run

```bash
make run-tls
```
Empty file added certs/.gitkeep
Empty file.
7 changes: 5 additions & 2 deletions deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ spec:
name: koor-version-service
spec:
containers:
- image: koorinc/version-service:master-8ac1b21
- image: koorinc/version-service:main
imagePullPolicy: Always
name: koor-version-service
ports:
- containerPort: 8082
protocol: TCP
env:
- name: NO_TLS
value: "true"
---
apiVersion: v1
kind: Service
Expand All @@ -33,7 +36,7 @@ metadata:
name: koor-version-service
spec:
ports:
- port: 8082
- port: 80
protocol: TCP
targetPort: 8082
selector:
Expand Down
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"net/http"
"os"

"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
Expand All @@ -31,9 +32,25 @@ func main() {
mux := http.NewServeMux()
path, handler := apiv1connect.NewVersionServiceHandler(vs)
mux.Handle(path, handler)
http.ListenAndServe(
":8082",
// Use h2c so we can serve HTTP/2 without TLS.
h2c.NewHandler(mux, &http2.Server{}),
)

connectPort := os.Getenv("CONNECT_PORT")
if connectPort == "" {
connectPort = "8082"
}

useTLS := os.Getenv("NO_TLS") != "true"
if useTLS {
http.ListenAndServeTLS(
":"+connectPort,
"certs/cert.pem",
"certs/key.pem",
mux,
)
} else {
http.ListenAndServe(
":"+connectPort,
// Use h2c so we can serve HTTP/2 without TLS.
h2c.NewHandler(mux, &http2.Server{}),
)
}
}

0 comments on commit fe6e408

Please sign in to comment.