Skip to content

Commit

Permalink
Merge pull request #35 from FabianKramm/hooks
Browse files Browse the repository at this point in the history
fix: restart plugin if syncer is restarted
  • Loading branch information
FabianKramm authored Sep 5, 2022
2 parents 0b2ce49 + 96678bc commit 4913a64
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
42 changes: 19 additions & 23 deletions examples/hooks/devspace.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
version: v1beta11
version: v2beta1
name: hooks

vars:
- name: PLUGIN_IMAGE
value: ghcr.io/loft-sh/vcluster-hooks-plugin
- name: K3S_IMAGE
value: rancher/k3s:v1.23.5-k3s1
- name: SERVICE_CIDR
command: vcluster get service-cidr
PLUGIN_IMAGE: ghcr.io/loft-sh/vcluster-hooks-plugin
K3S_IMAGE: rancher/k3s:v1.23.5-k3s1
SERVICE_CIDR: $( vcluster get service-cidr )
DEVSPACE_FLAGS: "-n vcluster"

images:
vcluster:
image: ${PLUGIN_IMAGE}
dockerfile: ./Dockerfile.dev
rebuildStrategy: ignoreContextChanges

deployments:
- name: vcluster
vcluster:
helm:
chart:
name: vcluster
repo: https://charts.loft.sh
version: 0.11.1
version: 0.11.2
valuesFiles:
- plugin.yaml
values:
Expand All @@ -35,19 +37,13 @@ deployments:
create: true
role:
extended: true
syncer:
readinessProbe:
enabled: false
livenessProbe:
enabled: false

dev:
terminal:
vcluster:
imageSelector: ${PLUGIN_IMAGE}
command: ["./devspace_start.sh"]
ports:
- imageSelector: ${PLUGIN_IMAGE}
forward:
- port: 2346
remotePort: 2345
sync:
- imageSelector: ${PLUGIN_IMAGE}
terminal:
command: ./devspace_start.sh
ports:
- port: 2346:2345
sync:
- path: ./:./
4 changes: 2 additions & 2 deletions examples/hooks/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbt
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/FabianKramm/vcluster-sdk v0.0.0-20220905100907-ba4ccfef41a4 h1:zvYmjCLo4S+9fPgDfqXz5lwPcRSGI2QuZCs7HYyuwNc=
github.com/FabianKramm/vcluster-sdk v0.0.0-20220905100907-ba4ccfef41a4/go.mod h1:HdQ75vqJuqiHiCtX+bmRAN9ZrCFimeYfQ5vKX1hEx2o=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd h1:sjQovDkwrZp8u+gxLtPgKGjk5hCxuy2hrRejBTA9xFU=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
Expand Down Expand Up @@ -372,8 +374,6 @@ github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0/go.mod h1:YBCo4D
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/loft-sh/vcluster-sdk v0.3.0 h1:/934jIw+kfpXS7ujX6pt4pDi06MiWyKY7S+Q7kI37Z4=
github.com/loft-sh/vcluster-sdk v0.3.0/go.mod h1:HdQ75vqJuqiHiCtX+bmRAN9ZrCFimeYfQ5vKX1hEx2o=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down
46 changes: 46 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"google.golang.org/grpc"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"net"
"os"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -458,6 +459,9 @@ func (m *manager) start() error {
return errors.Wrap(err, "start hook server")
}

// runID is used to track if the syncer was restarted while we are listening
runID := ""

log.Infof("Waiting for vcluster to become leader...")
conn, err := grpc.Dial(m.address, grpc.WithInsecure())
if err != nil {
Expand All @@ -479,12 +483,54 @@ func (m *manager) start() error {
return false, nil
}

if runID == "" {
runID = isLeader.RunID
} else if runID != isLeader.RunID {
return false, fmt.Errorf("syncer runID has changed while starting up, restarting plugin")
}

return isLeader.Leader, nil
})
if err != nil {
return err
}

// keep watching for run id
go func() {
conn, err := grpc.Dial(m.address, grpc.WithInsecure())
if err != nil {
klog.Fatalf("error dialing vcluster: %v", err)
}
defer conn.Close()

err = wait.PollInfinite(time.Second*5, func() (done bool, err error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

isLeader, err := remote.NewVClusterClient(conn).IsLeader(ctx, &remote.Empty{})
if err != nil {
log.Errorf("error trying to connect to vcluster: %v", err)
conn.Close()
conn, err = grpc.Dial(m.address, grpc.WithInsecure())
if err != nil {
return false, err
}
return false, nil
}

if runID != isLeader.RunID {
return false, fmt.Errorf("syncer has restarted, restarting plugin")
} else if !isLeader.Leader {
return false, fmt.Errorf("syncer lost leader election, restarting plugin")
}

return false, nil
})
if err != nil {
klog.Fatalf("error watching syncer: %v", err)
}
}()

m.started = true
log.Infof("Starting syncers...")
for _, s := range m.syncers {
Expand Down

0 comments on commit 4913a64

Please sign in to comment.