-
Notifications
You must be signed in to change notification settings - Fork 42
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
61 changed files
with
3,589 additions
and
96 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
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,21 @@ | ||
FROM golang:1.19-alpine AS builder | ||
|
||
RUN apk add --no-cache gcc musl-dev libaio-dev | ||
RUN apk add --no-cache sanlock-dev --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/ | ||
|
||
WORKDIR /workspace | ||
|
||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
RUN go mod download | ||
|
||
COPY cmd/ cmd/ | ||
COPY pkg/ pkg/ | ||
RUN --mount=type=cache,target=/root/.cache/go-build go build -a cmd/lockspace-attacher/main.go | ||
|
||
FROM alpine | ||
|
||
RUN apk add --no-cache sanlock --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/ | ||
|
||
COPY --from=builder /workspace/main /usr/bin/lockspace-attacher | ||
ENTRYPOINT ["lockspace-attacher"] |
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,21 @@ | ||
FROM golang:1.19-alpine AS builder | ||
|
||
RUN apk add --no-cache gcc musl-dev libaio-dev | ||
RUN apk add --no-cache sanlock-dev --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/ | ||
|
||
WORKDIR /workspace | ||
|
||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
RUN go mod download | ||
|
||
COPY cmd/ cmd/ | ||
COPY pkg/ pkg/ | ||
RUN --mount=type=cache,target=/root/.cache/go-build go build -a cmd/lockspace-detector/main.go | ||
|
||
FROM alpine | ||
|
||
RUN apk add --no-cache sanlock --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/ | ||
|
||
COPY --from=builder /workspace/main /usr/bin/lockspace-detector | ||
ENTRYPOINT ["lockspace-detector"] |
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,21 @@ | ||
FROM golang:1.19-alpine AS builder | ||
|
||
RUN apk add --no-cache gcc musl-dev libaio-dev | ||
RUN apk add --no-cache sanlock-dev --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/ | ||
|
||
WORKDIR /workspace | ||
|
||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
RUN go mod download | ||
|
||
COPY cmd/ cmd/ | ||
COPY pkg/ pkg/ | ||
RUN --mount=type=cache,target=/root/.cache/go-build go build -a cmd/lockspace-initializer/main.go | ||
|
||
FROM alpine | ||
|
||
RUN apk add --no-cache sanlock --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/ | ||
|
||
COPY --from=builder /workspace/main /usr/bin/lockspace-initializer | ||
ENTRYPOINT ["lockspace-initializer"] |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,100 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"path/filepath" | ||
"strconv" | ||
"syscall" | ||
|
||
"github.com/namsral/flag" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
"github.com/smartxworks/virtink/pkg/sanlock" | ||
) | ||
|
||
func main() { | ||
var lockspaceName string | ||
var nodeName string | ||
flag.StringVar(&lockspaceName, "lockspace-name", "", "") | ||
flag.StringVar(&nodeName, "node-name", "", "") | ||
flag.Parse() | ||
|
||
cfg, err := clientcmd.BuildConfigFromFlags("", "") | ||
if err != nil { | ||
log.Fatalf("failed to build kubeconfig: %s", err) | ||
} | ||
kubeClient, err := kubernetes.NewForConfig(cfg) | ||
if err != nil { | ||
log.Fatalf("failed to create Kubernetes client: %s", err) | ||
} | ||
|
||
hostID, err := getHostID(kubeClient, nodeName) | ||
if err != nil { | ||
log.Fatalf("failed to get host ID: %s", err) | ||
|
||
} | ||
|
||
leaseFilePath := filepath.Join("/var/lib/sanlock", lockspaceName, "leases") | ||
if err := sanlock.AcquireDeltaLease(lockspaceName, leaseFilePath, hostID); err != nil { | ||
log.Fatalf("failed to acquire delta lease: %s", err) | ||
} | ||
log.Println("succeeded to acquire delta lease") | ||
|
||
stop := make(chan struct{}) | ||
c := make(chan os.Signal, 2) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
go func() { | ||
<-c | ||
close(stop) | ||
<-c | ||
os.Exit(1) | ||
}() | ||
|
||
<-stop | ||
if err := sanlock.ReleaseDeltaLease(lockspaceName, leaseFilePath, hostID); err != nil { | ||
if err != sanlock.ENOENT { | ||
log.Fatalf("failed to release delta lease: %s", err) | ||
} | ||
} | ||
if err := umountLeaseVolume(lockspaceName); err != nil { | ||
log.Fatalf("failed to umont lease volume: %s", err) | ||
} | ||
} | ||
|
||
//+kubebuilder:rbac:groups="",resources=nodes,verbs=get | ||
|
||
func getHostID(client *kubernetes.Clientset, nodeName string) (uint64, error) { | ||
node, err := client.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{}) | ||
if err != nil { | ||
return 0, fmt.Errorf("get node: %s", err) | ||
} | ||
|
||
if id, exist := node.Annotations["virtink.smartx.com/sanlock-host-id"]; exist { | ||
if hostID, err := strconv.ParseUint(id, 10, 64); err != nil { | ||
return 0, fmt.Errorf("parse uint: %s", err) | ||
} else { | ||
return hostID, nil | ||
} | ||
} | ||
return 0, fmt.Errorf("sanlock host %s ID not found", nodeName) | ||
} | ||
|
||
func umountLeaseVolume(lockspace string) error { | ||
leaseFileDir := filepath.Join("/var/lib/sanlock", lockspace) | ||
output, err := exec.Command("sh", "-c", fmt.Sprintf("mount | grep '%s'", leaseFileDir)).CombinedOutput() | ||
if err != nil && string(output) == "" { | ||
return nil | ||
} | ||
|
||
if _, err := exec.Command("umount", leaseFileDir).CombinedOutput(); err != nil { | ||
return fmt.Errorf("unmount volume: %s", err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.