From e3067da03bc530131f2b39d3bceb4cdb30c3910b Mon Sep 17 00:00:00 2001 From: hunjixin <1084400399@qq.com> Date: Wed, 17 Jan 2024 20:06:38 +0800 Subject: [PATCH] feat: support make docker images --- 1 | 109 ++++++++++++++++++++++++++ 2 | 126 +++++++++++++++++++++++++++++++ Dockerfile | 11 +++ README.md | 4 + charts/Chart.yaml | 13 ++++ charts/templates/deployment.yaml | 24 ++++++ charts/templates/ingress.yaml | 28 +++++++ charts/templates/service.yaml | 14 ++++ charts/values.yaml | 10 +++ cmd/daemon.go | 14 ++-- cmd/root.go | 5 +- makefile | 5 ++ models/migrations/main.go | 6 -- script/postgress/configmap.yaml | 11 +++ script/postgress/deployment.yaml | 34 +++++++++ script/postgress/pv.yaml | 15 ++++ script/postgress/pvc.yaml | 13 ++++ script/postgress/svc.yaml | 14 ++++ script/start.sh | 17 +++++ 19 files changed, 459 insertions(+), 14 deletions(-) create mode 100644 1 create mode 100644 2 create mode 100644 Dockerfile create mode 100644 charts/Chart.yaml create mode 100644 charts/templates/deployment.yaml create mode 100644 charts/templates/ingress.yaml create mode 100644 charts/templates/service.yaml create mode 100644 charts/values.yaml create mode 100644 script/postgress/configmap.yaml create mode 100644 script/postgress/deployment.yaml create mode 100644 script/postgress/pv.yaml create mode 100644 script/postgress/pvc.yaml create mode 100644 script/postgress/svc.yaml create mode 100755 script/start.sh diff --git a/1 b/1 new file mode 100644 index 0000000..fa506d9 --- /dev/null +++ b/1 @@ -0,0 +1,109 @@ +apiVersion: v1 +kind: Pod +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: | + {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"app":"jiaozifs-backend-api","apptype":"jiaozifs-api"},"name":"jiaozifstest","namespace":"jiaozifs"},"spec":{"containers":[{"args":["--db postgres://admin:psltest@jiaozifs-api-db-service.jiaozifs.svc.cluster.local:5432/postgresdb?sslmode=disable --log-level debug --bs_path /app/data --listen http://0.0.0.0:34913 --config /app/config.toml"],"image":"gitdatateam/jzfs:latest","imagePullPolicy":"Always","name":"jiaozifs-backend","ports":[{"containerPort":34913,"protocol":"TCP"}]}]}} + creationTimestamp: "2024-01-19T13:01:17Z" + labels: + app: jiaozifs-backend-api + apptype: jiaozifs-api + name: jiaozifstest + namespace: jiaozifs + resourceVersion: "1046785" + uid: 65b9d4c1-b0fc-4af8-a999-4f0c707543b0 +spec: + containers: + - args: + - --db postgres://admin:psltest@jiaozifs-api-db-service.jiaozifs.svc.cluster.local:5432/postgresdb?sslmode=disable + --log-level debug --bs_path /app/data --listen http://0.0.0.0:34913 --config + /app/config.toml + image: gitdatateam/jzfs:latest + imagePullPolicy: Always + name: jiaozifs-backend + ports: + - containerPort: 34913 + protocol: TCP + resources: {} + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + volumeMounts: + - mountPath: /var/run/secrets/kubernetes.io/serviceaccount + name: kube-api-access-t5s4h + readOnly: true + dnsPolicy: ClusterFirst + enableServiceLinks: true + nodeName: k3 + preemptionPolicy: PreemptLowerPriority + priority: 0 + restartPolicy: Always + schedulerName: default-scheduler + securityContext: {} + serviceAccount: default + serviceAccountName: default + terminationGracePeriodSeconds: 30 + tolerations: + - effect: NoExecute + key: node.kubernetes.io/not-ready + operator: Exists + tolerationSeconds: 300 + - effect: NoExecute + key: node.kubernetes.io/unreachable + operator: Exists + tolerationSeconds: 300 + volumes: + - name: kube-api-access-t5s4h + projected: + defaultMode: 420 + sources: + - serviceAccountToken: + expirationSeconds: 3607 + path: token + - configMap: + items: + - key: ca.crt + path: ca.crt + name: kube-root-ca.crt + - downwardAPI: + items: + - fieldRef: + apiVersion: v1 + fieldPath: metadata.namespace + path: namespace +status: + conditions: + - lastProbeTime: null + lastTransitionTime: "2024-01-19T13:01:17Z" + status: "True" + type: Initialized + - lastProbeTime: null + lastTransitionTime: "2024-01-19T13:01:21Z" + status: "True" + type: Ready + - lastProbeTime: null + lastTransitionTime: "2024-01-19T13:01:21Z" + status: "True" + type: ContainersReady + - lastProbeTime: null + lastTransitionTime: "2024-01-19T13:01:17Z" + status: "True" + type: PodScheduled + containerStatuses: + - containerID: docker://44953ac2b21fe99ea6eb72870acdbf59d6612a4d01194b7259ee907d81212cc1 + image: gitdatateam/jzfs:latest + imageID: docker-pullable://gitdatateam/jzfs@sha256:2b3b3bde619609ca845684874d67ccbd3f0fcb4206427901bc8be485d36a2daa + lastState: {} + name: jiaozifs-backend + ready: true + restartCount: 0 + started: true + state: + running: + startedAt: "2024-01-19T13:01:21Z" + hostIP: 172.16.0.81 + phase: Running + podIP: 10.42.1.19 + podIPs: + - ip: 10.42.1.19 + qosClass: BestEffort + startTime: "2024-01-19T13:01:17Z" diff --git a/2 b/2 new file mode 100644 index 0000000..365332d --- /dev/null +++ b/2 @@ -0,0 +1,126 @@ +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: "2024-01-19T13:27:10Z" + generateName: jiaozifs-backend-api-deployment-864f944b45- + labels: + app: jiaozifs-backend-api + apptype: jiaozifs-api + pod-template-hash: 864f944b45 + name: jiaozifs-backend-api-deployment-864f944b45-ghjrl + namespace: jiaozifs + ownerReferences: + - apiVersion: apps/v1 + blockOwnerDeletion: true + controller: true + kind: ReplicaSet + name: jiaozifs-backend-api-deployment-864f944b45 + uid: 486bd632-61ab-4245-8ac0-c8e905d2d431 + resourceVersion: "1057080" + uid: b7b61713-df10-4c77-ba6f-e458ab6b7674 +spec: + containers: + - args: + - --db postgres://admin:psltest@jiaozifs-api-db-service.jiaozifs.svc.cluster.local:5432/postgresdb + --log-level debug --bs_path /app/data --listen http://0.0.0.0:34913 --config + /app/config.toml + image: gitdatateam/jzfs:latest + imagePullPolicy: Always + name: jiaozifs-backend + ports: + - containerPort: 34913 + protocol: TCP + resources: {} + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + volumeMounts: + - mountPath: /var/run/secrets/kubernetes.io/serviceaccount + name: kube-api-access-225t9 + readOnly: true + dnsPolicy: ClusterFirst + enableServiceLinks: true + nodeName: k3 + preemptionPolicy: PreemptLowerPriority + priority: 0 + restartPolicy: Always + schedulerName: default-scheduler + securityContext: {} + serviceAccount: default + serviceAccountName: default + terminationGracePeriodSeconds: 30 + tolerations: + - effect: NoExecute + key: node.kubernetes.io/not-ready + operator: Exists + tolerationSeconds: 300 + - effect: NoExecute + key: node.kubernetes.io/unreachable + operator: Exists + tolerationSeconds: 300 + volumes: + - name: kube-api-access-225t9 + projected: + defaultMode: 420 + sources: + - serviceAccountToken: + expirationSeconds: 3607 + path: token + - configMap: + items: + - key: ca.crt + path: ca.crt + name: kube-root-ca.crt + - downwardAPI: + items: + - fieldRef: + apiVersion: v1 + fieldPath: metadata.namespace + path: namespace +status: + conditions: + - lastProbeTime: null + lastTransitionTime: "2024-01-19T13:27:10Z" + status: "True" + type: Initialized + - lastProbeTime: null + lastTransitionTime: "2024-01-19T13:27:10Z" + message: 'containers with unready status: [jiaozifs-backend]' + reason: ContainersNotReady + status: "False" + type: Ready + - lastProbeTime: null + lastTransitionTime: "2024-01-19T13:27:10Z" + message: 'containers with unready status: [jiaozifs-backend]' + reason: ContainersNotReady + status: "False" + type: ContainersReady + - lastProbeTime: null + lastTransitionTime: "2024-01-19T13:27:10Z" + status: "True" + type: PodScheduled + containerStatuses: + - containerID: docker://ef199f6d332d957859472e55424301153f7cb660eb936b950f3b0a891b63cfea + image: gitdatateam/jzfs:latest + imageID: docker-pullable://gitdatateam/jzfs@sha256:2b3b3bde619609ca845684874d67ccbd3f0fcb4206427901bc8be485d36a2daa + lastState: + terminated: + containerID: docker://ef199f6d332d957859472e55424301153f7cb660eb936b950f3b0a891b63cfea + exitCode: 1 + finishedAt: "2024-01-19T13:27:56Z" + reason: Error + startedAt: "2024-01-19T13:27:56Z" + name: jiaozifs-backend + ready: false + restartCount: 3 + started: false + state: + waiting: + message: back-off 40s restarting failed container=jiaozifs-backend pod=jiaozifs-backend-api-deployment-864f944b45-ghjrl_jiaozifs(b7b61713-df10-4c77-ba6f-e458ab6b7674) + reason: CrashLoopBackOff + hostIP: 172.16.0.81 + phase: Running + podIP: 10.42.1.20 + podIPs: + - ip: 10.42.1.20 + qosClass: BestEffort + startTime: "2024-01-19T13:27:10Z" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6be840a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM ubuntu:22.04 + + +WORKDIR /app + +COPY jzfs /jzfs +COPY script/start.sh /start.sh + +RUN chmod +x /start.sh + +ENTRYPOINT ["/start.sh"] diff --git a/README.md b/README.md index 818a85e..91250a1 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,10 @@ After following the above steps, you should be able to see an executable file na ./jzfs daemon ``` +#### run with docker +```bash +docker run -v :/app -p 34913:34913 gitdatateam/jzfs:latest --db "postgres://:@192.168.1.16:5432/jiaozifs?sslmode=disable" --bs_path /app/data --listen http://0.0.0.0:34913 --config /app/config.toml +``` ## License Dual-licensed under [MIT](https://github.com/jiaozifs/jiaozifs/blob/main/LICENSE-MIT) + [Apache 2.0](https://github.com/jiaozifs/jiaozifs/blob/main/LICENSE-APACHE) diff --git a/charts/Chart.yaml b/charts/Chart.yaml new file mode 100644 index 0000000..30f7a4e --- /dev/null +++ b/charts/Chart.yaml @@ -0,0 +1,13 @@ +apiVersion: v2 +name: jiaozifs-api +description: Install jiaozifs api to provider backend function of jiaozifs. +version: 1.0.0 +kubeVersion: < 1.28.0-0 +home: https://jiaozifs.com/ +keywords: + - jiaozifs +sources: + - https://github.com/jiaozifs/jiaozifs + - https://github.com/jiaozifs/jiaozifs-ui +maintainers: + - name: jiaozifs team diff --git a/charts/templates/deployment.yaml b/charts/templates/deployment.yaml new file mode 100644 index 0000000..3018839 --- /dev/null +++ b/charts/templates/deployment.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: jiaozifs-backend-api-deployment + labels: + apptype: jiaozifs-api +spec: + replicas: 1 + selector: + matchLabels: + app: jiaozifs-backend-api + template: + metadata: + labels: + app: jiaozifs-backend-api + apptype: jiaozifs-api + spec: + containers: + - name: jiaozifs-backend + image: gitdatateam/jzfs:latest + imagePullPolicy: Always + args: ["--db {{ .Values.db }} --log-level {{ .Values.log_level }} --bs_path {{ .Values.bs_path }} --listen http://0.0.0.0:{{ .Values.port }} --config {{ .Values.config }}"] + ports: + - containerPort: {{ .Values.port }} diff --git a/charts/templates/ingress.yaml b/charts/templates/ingress.yaml new file mode 100644 index 0000000..b8b4c91 --- /dev/null +++ b/charts/templates/ingress.yaml @@ -0,0 +1,28 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: jiaozifs-api + annotations: + meta.helm.sh/release-name: jiaozifs-api + nginx.ingress.kubernetes.io/rewrite-target: / + nginx.ingress.kubernetes.io/proxy-connect-timeout: "30" + nginx.ingress.kubernetes.io/proxy-read-timeout: "1800" + nginx.ingress.kubernetes.io/proxy-send-timeout: "1800" + labels: + apptype: jiaozifs-api + app.kubernetes.io/managed-by: Helm + heritage: Helm + release: jiaozifs-api +spec: + ingressClassName: {{.Values.ingress_name}} + rules: + - host: api.jiaozifs.com + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: jiaozifs-backend-api-service + port: + number: {{ .Values.port }} diff --git a/charts/templates/service.yaml b/charts/templates/service.yaml new file mode 100644 index 0000000..7abcd84 --- /dev/null +++ b/charts/templates/service.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: jiaozifs-backend-api-service + labels: + apptype: jiaozifs-api +spec: + type: ClusterIP + selector: + app: jiaozifs-backend-api + ports: + - protocol: TCP + port: {{ .Values.port }} + targetPort: {{ .Values.port }} diff --git a/charts/values.yaml b/charts/values.yaml new file mode 100644 index 0000000..59b3eb9 --- /dev/null +++ b/charts/values.yaml @@ -0,0 +1,10 @@ +# Additional Trusted CAs. +# Enable this flag and add your CA certs as a secret named tls-ca-additional in the namespace. +# See README.md for details. +replicas: 1 +db: "" +bs_path: "/app/data" +config: "/app/config.toml" +port: 34913 +ingress_name: nginx +log_level: info diff --git a/cmd/daemon.go b/cmd/daemon.go index 93e5e62..5523cb0 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -3,6 +3,8 @@ package cmd import ( "context" + "github.com/pelletier/go-toml/v2" + "github.com/gorilla/sessions" logging "github.com/ipfs/go-log/v2" apiImpl "github.com/jiaozifs/jiaozifs/api/api_impl" @@ -16,7 +18,6 @@ import ( "github.com/jiaozifs/jiaozifs/utils" "github.com/jiaozifs/jiaozifs/version" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/uptrace/bun" ) @@ -38,6 +39,12 @@ var daemonCmd = &cobra.Command{ return err } + cfgData, err := toml.Marshal(cfg) + if err != nil { + return err + } + log.Debug(string(cfgData)) + shutdown := make(utils.Shutdown) stop, err := fx_opt.New(cmd.Context(), fx_opt.Override(new(context.Context), cmd.Context()), @@ -76,9 +83,4 @@ var daemonCmd = &cobra.Command{ func init() { rootCmd.AddCommand(daemonCmd) - daemonCmd.Flags().String("db", "", "pg connection string eg. postgres://user:pass@localhost:5432/jiaozifs?sslmode=disable") - daemonCmd.Flags().String("log-level", "INFO", "set log level eg. DEBUG INFO ERROR") - - _ = viper.BindPFlag("database.connection", daemonCmd.Flags().Lookup("db")) - _ = viper.BindPFlag("log.level", daemonCmd.Flags().Lookup("log-level")) } diff --git a/cmd/root.go b/cmd/root.go index fb79624..1919986 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,7 +3,6 @@ package cmd import ( "os" - "github.com/jiaozifs/jiaozifs/config" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -31,7 +30,9 @@ func RootCmd() *cobra.Command { } func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "~/.jiaozifs/config.toml", "config file (default is $HOME/.jiaozifs/config.toml)") - rootCmd.PersistentFlags().String("listen", config.DefaultLocalBSPath, "config blockstore path") + rootCmd.PersistentFlags().String("listen", "http://127.0.0.1:34913", "config list url") + rootCmd.PersistentFlags().String("log-level", "INFO", "set log level eg. DEBUG INFO ERROR") _ = viper.BindPFlag("api.listen", rootCmd.PersistentFlags().Lookup("listen")) _ = viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")) + _ = viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log-level")) } diff --git a/makefile b/makefile index 346c6f6..a43e12a 100644 --- a/makefile +++ b/makefile @@ -27,3 +27,8 @@ test: gen-api go test -timeout=30m -parallel=4 -v ./... build:gen-api go build $(GOFLAGS) -o jzfs + +TAG:=test +docker:build + docker build -t gitdatateam/jzfs:$(TAG) . + docker push gitdatateam/jzfs:$(TAG) diff --git a/models/migrations/main.go b/models/migrations/main.go index dce0dfd..15012e7 100644 --- a/models/migrations/main.go +++ b/models/migrations/main.go @@ -9,12 +9,6 @@ import ( var Migrations = migrate.NewMigrations() -func init() { - if err := Migrations.DiscoverCaller(); err != nil { - panic(err) - } -} - func MigrateDatabase(ctx context.Context, sqlDB *bun.DB) error { migrator := migrate.NewMigrator(sqlDB, Migrations) err := migrator.Init(ctx) diff --git a/script/postgress/configmap.yaml b/script/postgress/configmap.yaml new file mode 100644 index 0000000..e1ba1c8 --- /dev/null +++ b/script/postgress/configmap.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: jiaozifs-api-postgres-config + labels: + app: jiaozifs-api-db-cfg + apptype: jiaozifs-pg-db +data: + POSTGRES_DB: "postgresdb" + POSTGRES_USER: "admin" + POSTGRES_PASSWORD: "psltest" diff --git a/script/postgress/deployment.yaml b/script/postgress/deployment.yaml new file mode 100644 index 0000000..1b22f99 --- /dev/null +++ b/script/postgress/deployment.yaml @@ -0,0 +1,34 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: jiaozifs-api-pg-db-deployment # Sets Deployment name + labels: + apptype: jiaozifs-pg-db +spec: + replicas: 1 + selector: + matchLabels: + app: jiaozifs-api-pg-db + template: + metadata: + labels: + app: jiaozifs-api-pg-db + apptype: jiaozifs-pg-db + spec: + nodeName: k2 + containers: + - name: postgres + image: postgres:13.13 + imagePullPolicy: "IfNotPresent" + ports: + - containerPort: 5432 + envFrom: + - configMapRef: + name: jiaozifs-api-postgres-config + volumeMounts: + - mountPath: /var/lib/postgresql/data + name: postgredb + volumes: + - name: postgredb + persistentVolumeClaim: + claimName: jiaozifs-postgres-pv-claim diff --git a/script/postgress/pv.yaml b/script/postgress/pv.yaml new file mode 100644 index 0000000..84da925 --- /dev/null +++ b/script/postgress/pv.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: PersistentVolume +metadata: + name: jiaozifs-postgres-pv-volume + labels: + type: local + apptype: jiaozifs-pg-db +spec: + storageClassName: manual + capacity: + storage: 5Gi + accessModes: + - ReadWriteOnce + hostPath: + path: "/data/jiaozifs-pg-data" diff --git a/script/postgress/pvc.yaml b/script/postgress/pvc.yaml new file mode 100644 index 0000000..808d4c6 --- /dev/null +++ b/script/postgress/pvc.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: jiaozifs-postgres-pv-claim + labels: + apptype: jiaozifs-pg-db +spec: + storageClassName: manual + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 5Gi diff --git a/script/postgress/svc.yaml b/script/postgress/svc.yaml new file mode 100644 index 0000000..f6a8fc2 --- /dev/null +++ b/script/postgress/svc.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: jiaozifs-api-db-service + labels: + apptype: jiaozifs-pg-db +spec: + type: ClusterIP + selector: + app: jiaozifs-api-pg-db + ports: + - protocol: TCP + port: 5432 + targetPort: 5432 diff --git a/script/start.sh b/script/start.sh new file mode 100755 index 0000000..e7ab43a --- /dev/null +++ b/script/start.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +regex="--config[[:space:]]+([^[:space:]]+)" +if [[ $@ =~ $regex ]]; then + config_value="--config ${BASH_REMATCH[1]}" + echo "Config value: $config_value" +fi + +regex="--log-level[[:space:]]+([^[:space:]]+)" +if [[ $@ =~ $regex ]]; then + loglevel="--log-level ${BASH_REMATCH[1]}" + echo "Log level: $loglevel" +fi + +/jzfs init $@ + +/jzfs daemon $config_value $loglevel