-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
239 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"net" | ||
"os" | ||
) | ||
|
||
var ( | ||
storagePath string | ||
hostIP string | ||
dataServePort string | ||
) | ||
|
||
func init() { | ||
// read storage path from environment variable | ||
storagePath = os.Getenv("STORAGE_PATH") | ||
|
||
// read host ip from environment variable | ||
hostIP = os.Getenv("HOST_IP") | ||
dataServePort = os.Getenv("DATA_SERVE_PORT") | ||
log.Printf("IP:PORT %s:%s\n", hostIP, dataServePort) | ||
|
||
// Write Data Serve IP:PORT to file | ||
f, err := os.Create(storagePath + "/DATA_SERVE_IP") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.WriteString(hostIP + ":" + dataServePort) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func main() { | ||
handleConnections() | ||
} | ||
|
||
func handleConnections() { | ||
listener, err := net.Listen("tcp", ":"+dataServePort) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer listener.Close() | ||
|
||
log.Println("Data Server is running on " + hostIP + ":" + dataServePort) | ||
|
||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
log.Println("[Error] listener.Accept(): ", err) | ||
continue | ||
} | ||
|
||
go handleConnection(conn) | ||
} | ||
} | ||
|
||
func handleConnection(conn net.Conn) { | ||
var req Request | ||
reader := bufio.NewReader(conn) | ||
|
||
// read request from client | ||
err := json.NewDecoder(reader).Decode(&req) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
// Handle the request | ||
switch req.Type { | ||
case "download": | ||
// open file | ||
file, err := os.Open(req.Body) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
// send file | ||
_, err = io.Copy(conn, file) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
log.Printf("File %s sent to %d", req.Body, conn.RemoteAddr()) | ||
} | ||
|
||
// close connection | ||
conn.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package main | ||
|
||
type Request struct { | ||
Type string `json:"type"` | ||
Body string `json:"body"` | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
apiVersion: apps/v1 | ||
kind: DaemonSet | ||
metadata: | ||
name: data-serve-daemonset-disk | ||
labels: | ||
app.kubernetes.io/name: data-serve-disk | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: data-serve | ||
template: | ||
metadata: | ||
labels: | ||
app: data-serve | ||
spec: | ||
tolerations: | ||
# these tolerations are to have the daemonset runnable on control plane nodes | ||
# remove them if your control plane nodes should not run pods | ||
- key: node-role.kubernetes.io/control-plane | ||
operator: Exists | ||
effect: NoSchedule | ||
- key: node-role.kubernetes.io/master | ||
operator: Exists | ||
effect: NoSchedule | ||
containers: | ||
- name: data-serve-container | ||
image: dasbd72/data-serve:latest | ||
imagePullPolicy: Always | ||
ports: | ||
- containerPort: 12348 | ||
hostPort: 12348 | ||
volumeMounts: | ||
- name: shared-volume | ||
mountPath: /shared | ||
env: | ||
- name: STORAGE_PATH | ||
value: /shared | ||
- name: HOST_IP | ||
valueFrom: | ||
fieldRef: | ||
fieldPath: status.hostIP | ||
- name: DATA_SERVE_PORT | ||
value: "12348" | ||
volumes: | ||
- name: shared-volume | ||
hostPath: | ||
path: /storage-disk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
apiVersion: apps/v1 | ||
kind: DaemonSet | ||
metadata: | ||
name: data-serve-daemonset | ||
labels: | ||
app.kubernetes.io/name: data-serve | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: data-serve | ||
template: | ||
metadata: | ||
labels: | ||
app: data-serve | ||
spec: | ||
tolerations: | ||
# these tolerations are to have the daemonset runnable on control plane nodes | ||
# remove them if your control plane nodes should not run pods | ||
- key: node-role.kubernetes.io/control-plane | ||
operator: Exists | ||
effect: NoSchedule | ||
- key: node-role.kubernetes.io/master | ||
operator: Exists | ||
effect: NoSchedule | ||
containers: | ||
- name: data-serve-container | ||
image: dasbd72/data-serve:latest | ||
imagePullPolicy: Always | ||
ports: | ||
- containerPort: 12347 | ||
hostPort: 12347 | ||
volumeMounts: | ||
- name: shared-volume | ||
mountPath: /shared | ||
env: | ||
- name: STORAGE_PATH | ||
value: /shared | ||
- name: HOST_IP | ||
valueFrom: | ||
fieldRef: | ||
fieldPath: status.hostIP | ||
- name: DATA_SERVE_PORT | ||
value: "12347" | ||
volumes: | ||
- name: shared-volume | ||
hostPath: | ||
path: "/dev/shm" |