Skip to content

Commit

Permalink
fix: introduce prestop
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <[email protected]>
  • Loading branch information
hlts2 committed Dec 24, 2024
1 parent 24e9a38 commit c3bdd85
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
4 changes: 4 additions & 0 deletions deploy/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ spec:
mountPath: /var/lib/kubelet/plugins/csi.civo.com
- name: civo-csi-plugin
image: gcr.io/consummate-yew-302509/csi:latest
lifecycle:
preStop:
exec:
command: ["/app/civo-csi", "pre-stop"]
env:
- name: CSI_ENDPOINT
value: unix:///var/lib/kubelet/plugins/csi.civo.com/csi.sock
Expand Down
73 changes: 48 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"

"github.com/civo/civo-csi/pkg/driver"
"github.com/civo/civo-csi/pkg/driver/hook"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand All @@ -18,30 +20,17 @@ var (
versionInfo = flag.Bool("version", false, "Print the driver version")
)

func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

flag.Parse()
if *versionInfo {
log.Info().Str("version", driver.Version).Msg("CSI driver")
return
}

apiURL := strings.TrimSpace(os.Getenv("CIVO_API_URL"))
apiKey := strings.TrimSpace(os.Getenv("CIVO_API_KEY"))
region := strings.TrimSpace(os.Getenv("CIVO_REGION"))
ns := strings.TrimSpace(os.Getenv("CIVO_NAMESPACE"))
clusterID := strings.TrimSpace(os.Getenv("CIVO_CLUSTER_ID"))

d, err := driver.NewDriver(apiURL, apiKey, region, ns, clusterID)
if err != nil {
log.Fatal().Err(err)
}

log.Info().Interface("d", d).Msg("Created a new driver")
var (
apiURL = strings.TrimSpace(os.Getenv("CIVO_API_URL"))
apiKey = strings.TrimSpace(os.Getenv("CIVO_API_KEY"))
region = strings.TrimSpace(os.Getenv("CIVO_REGION"))
ns = strings.TrimSpace(os.Getenv("CIVO_NAMESPACE"))
clusterID = strings.TrimSpace(os.Getenv("CIVO_CLUSTER_ID"))
node = strings.TrimSpace(os.Getenv("KUBE_NODE_NAME"))
)

ctx, cancel := context.WithCancel(context.Background())
func run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

c := make(chan os.Signal, 1)
Expand All @@ -53,9 +42,43 @@ func main() {
cancel()
}()

flag.Parse()
if *versionInfo {
log.Info().Str("version", driver.Version).Msg("CSI driver")
return nil
}

if len(os.Args) > 1 {
hook, err := hook.NewHook(
hook.WithNodeName(node),
)
if err != nil {
return err
}
switch cmd := os.Args[1]; cmd {
case "pre-stop":
log.Info().Msg("Running the pre-stop hook for driver")
return hook.PreStop(ctx)
default:
return fmt.Errorf("not supported command: %q", cmd)
}
}

d, err := driver.NewDriver(apiURL, apiKey, region, ns, clusterID)
if err != nil {
return err
}
log.Info().Interface("d", d).Msg("Created a new driver")

log.Info().Msg("Running the driver")
return d.Run(ctx)
}

func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

if err := d.Run(ctx); err != nil {
log.Fatal().Err(err)
if err := run(context.Background()); err != nil {
log.Fatal().Err(err).Msg("Application error occured")
}
}

0 comments on commit c3bdd85

Please sign in to comment.