git

package module
v0.0.0-...-706d37a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

README

Git backend for leader election

Go Reference

Usage

package main

import (
  "context"
  "os"
  "time"

  gssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
  "github.com/sirupsen/logrus"
  "k8s.io/apimachinery/pkg/util/rand"

  le "go.linka.cloud/leaderelection"
  "go.linka.cloud/leaderelection/git"
)

const leaseName = "git-lock"

var (
  user    = os.Getenv("GIT_USER")
  keyPath = os.Getenv("GIT_KEY_PATH")
  repo    = os.Getenv("GIT_REPO")
)

func main() {
  ctx, cancel := context.WithCancel(le.SetupSignalHandler())
  defer cancel()

  auth, err := gssh.NewPublicKeysFromFile("git", keyPath, "")
  if err != nil {
    logrus.Fatal(err)
  }

  l, err := git.New(ctx, leaseName, repo, auth, rand.String(8))
  if err != nil {
    logrus.Fatal(err)
  }
  config := le.Config{
    Lock:            l,
    LeaseDuration:   15 * time.Second,
    RenewDeadline:   10 * time.Second,
    RetryPeriod:     5 * time.Second,
    ReleaseOnCancel: true,
    Name:            leaseName,
    Callbacks: le.Callbacks{
      OnStartedLeading: func(ctx context.Context) {
        logrus.Info("started leading")
      },
      OnStoppedLeading: func() {
        logrus.Info("stopped leading")
      },
      OnNewLeader: func(identity string) {
        logrus.Infof("new leader: %s", identity)
      },
    },
  }
  e, err := le.New(config)
  if err != nil {
    logrus.Fatal(err)
  }
  e.Run(ctx)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, name, url string, auth transport.AuthMethod, id string) (le.Lock, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL