From 2a99c662af6923c5d00a020c13ae572845fe0e1b Mon Sep 17 00:00:00 2001 From: X Date: Thu, 14 May 2020 18:29:28 +0300 Subject: [PATCH 1/3] Prepare for kubernetes deploy --- .gitignore | 2 +- deploy/kubernetes/july/credentials-secret.sh | 3 +++ deploy/kubernetes/july/july.yml | 27 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100755 deploy/kubernetes/july/credentials-secret.sh create mode 100644 deploy/kubernetes/july/july.yml diff --git a/.gitignore b/.gitignore index c3101cb..1321419 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -cmd/july/july vendor gdrive_test credentials.* token.json +cmd/july/july diff --git a/deploy/kubernetes/july/credentials-secret.sh b/deploy/kubernetes/july/credentials-secret.sh new file mode 100755 index 0000000..b9a8182 --- /dev/null +++ b/deploy/kubernetes/july/credentials-secret.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +../kubectl create secret generic july-credentials --from-file=credentials.json=./credentials.json --from-file=token.json=./token.json \ No newline at end of file diff --git a/deploy/kubernetes/july/july.yml b/deploy/kubernetes/july/july.yml new file mode 100644 index 0000000..4f6bd9c --- /dev/null +++ b/deploy/kubernetes/july/july.yml @@ -0,0 +1,27 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: july + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: july-deployment + labels: + app: july +spec: + replicas: 1 + selector: + matchLabels: + app: july + template: + metadata: + labels: + app: july + spec: + containers: + - name: july + image: docker.pkg.github.com/vasyahuyasa/july/july:v.0.0.3 + ports: + - containerPort: 80 From abe5d7a81fbcdf6fa511bb1cdca53d2fbe687e29 Mon Sep 17 00:00:00 2001 From: X Date: Thu, 14 May 2020 19:40:25 +0300 Subject: [PATCH 2/3] deply to kubernetes --- deploy/kubernetes/july/july.yml | 65 +++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/deploy/kubernetes/july/july.yml b/deploy/kubernetes/july/july.yml index 4f6bd9c..e8c0362 100644 --- a/deploy/kubernetes/july/july.yml +++ b/deploy/kubernetes/july/july.yml @@ -1,13 +1,7 @@ -apiVersion: v1 -kind: Namespace -metadata: - name: july - ---- apiVersion: apps/v1 kind: Deployment metadata: - name: july-deployment + name: july labels: app: july spec: @@ -22,6 +16,61 @@ spec: spec: containers: - name: july - image: docker.pkg.github.com/vasyahuyasa/july/july:v.0.0.3 + image: vasyahuyasa/july:0.0.3 ports: - containerPort: 80 + volumeMounts: + - name: credentials + mountPath: "/app/credentials.json" + - name: token + mountPath: "/app/token.json" + env: + - name: STORAGE_DRIVER + value: "gdrive" + - name: CATALOG_ROOT + value: "root" + volumes: + - name: credentials + projected: + sources: + - secret: + name: july-credentials + items: + - key: credentials.json + path: credentials.json + - name: token + projected: + sources: + - secret: + name: july-credentials + items: + - key: token.json + path: token.json + +--- +apiVersion: v1 +kind: Service +metadata: + name: july + labels: + run: july +spec: + ports: + - port: 80 + protocol: TCP + selector: + app: july + +--- +apiVersion: networking.k8s.io/v1beta1 +kind: Ingress +metadata: + name: july-ingress +spec: + rules: + - http: + paths: + - path: / + backend: + serviceName: july + servicePort: 80 \ No newline at end of file From 0971d83d1833ee88f56beda1c623c94e6f533bee Mon Sep 17 00:00:00 2001 From: vasyahuyasa Date: Thu, 14 May 2020 23:25:14 +0300 Subject: [PATCH 3/3] Pass path to google auth files --- Dockerfile | 8 +++++++- cmd/july/main.go | 10 +++++----- opds/storage/gdrive/gdrive.go | 12 +++++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index b517172..f1bb0d3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,10 @@ COPY --from=builder /build/july /app/july EXPOSE 80 ENV STORAGE_DRIVER=gdrive ENV CATALOG_ROOT=root -CMD /app/july -drv $STORAGE_DRIVER -d $CATALOG_ROOT +ENV GOOGLE_CREDENTIALS_PATH=credentials.json +env GOOGLE_TOKEN_PATH=token.json +CMD /app/july \ + -s $STORAGE_DRIVER \ + -d $CATALOG_ROOT \ + -googlecred $GOOGLE_CREDENTIALS_PATH \ + -googletoken $GOOGLE_TOKEN_PATH diff --git a/cmd/july/main.go b/cmd/july/main.go index e186f87..58ebb1f 100644 --- a/cmd/july/main.go +++ b/cmd/july/main.go @@ -15,10 +15,10 @@ func main() { root := flag.String("d", "./", "Root storage directory") port := flag.Int("p", 80, "Service port") host := flag.String("i", "0.0.0.0", "Service network interface") - driver := flag.String("drv", "local", "Storage driver (can be local, gdrive, yadisk)") + driver := flag.String("s", "local", "Storage driver (can be local, gdrive, yadisk)") - gdriveCredFile := flag.String("googlecred", "credentials.json", "Path to file with secret for google drive driver") - //gdriveTokenFile := flag.String("gtoken", "token.json", "Path to file with token for google drive driver") + gdriveCredFile := flag.String("googlecred", "credentials.json", "Path to file with secret for google drive") + gdriveTokenFile := flag.String("googletoken", "token.json", "Path to file with token for google drive") flag.Parse() @@ -30,10 +30,10 @@ func main() { case "gdrive": o, err := gdrive.OAuth2FromFile(*gdriveCredFile) if err != nil { - log.Fatalf("Can not load google credentials: %v", err) + log.Fatalf("Can not load google credentials from file: %v", err) } - svc, err := gdrive.NewServiceFromOauth2(o) + svc, err := gdrive.NewServiceFromOauth2(o, *gdriveTokenFile) if err != nil { log.Fatalf("Can not init google client: %v", err) } diff --git a/opds/storage/gdrive/gdrive.go b/opds/storage/gdrive/gdrive.go index a266c77..a037c09 100644 --- a/opds/storage/gdrive/gdrive.go +++ b/opds/storage/gdrive/gdrive.go @@ -19,8 +19,6 @@ import ( const ( MIMETypeFolder = "application/vnd.google-apps.folder" - - tokFile = "token.json" ) var _ storage.Storage = &GdriveStorage{} @@ -137,8 +135,8 @@ func OAuth2FromFile(filename string) (*oauth2.Config, error) { return config, nil } -func NewServiceFromOauth2(config *oauth2.Config) (*drive.Service, error) { - client := getClient(config) +func NewServiceFromOauth2(config *oauth2.Config, tokenPath string) (*drive.Service, error) { + client := getClient(config, tokenPath) srv, err := drive.New(client) if err != nil { @@ -148,14 +146,14 @@ func NewServiceFromOauth2(config *oauth2.Config) (*drive.Service, error) { return srv, err } -func getClient(config *oauth2.Config) *http.Client { +func getClient(config *oauth2.Config, tokenPath string) *http.Client { // The file token.json stores the user's access and refresh tokens, and is // created automatically when the authorization flow completes for the first // time. - tok, err := tokenFromFile(tokFile) + tok, err := tokenFromFile(tokenPath) if err != nil { tok = getTokenFromWeb(config) - saveToken(tokFile, tok) + saveToken(tokenPath, tok) } return config.Client(context.Background(), tok) }