Skip to content

Commit

Permalink
Clean syncthing database on pod restarts if PV is not enabled (#961)
Browse files Browse the repository at this point in the history
* Fail dev pod if it is a restart and PV is not enabled

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>

* Recrate pod if needed as part of the "okteto up" sequence

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>
  • Loading branch information
pchico83 committed Jun 30, 2020
1 parent c5ed31f commit 635106f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -46,6 +46,10 @@ integration:
build:
$(BUILDCOMMAND) -o ${BINDIR}/okteto

.PHONY: build-bin-image
build-bin-image:
okteto build -t okteto/bin:1.1.21 -f images/bin/Dockerfile images/bin

.PHONY: dep
dep:
go mod tidy
21 changes: 16 additions & 5 deletions cmd/up/up.go
Expand Up @@ -314,7 +314,7 @@ func (up *upContext) activate(autoDeploy, build, resetSyncthing bool) {

if err := up.sync(resetSyncthing && !isRetry); err != nil {

if !pods.Exists(up.Pod, up.Dev.Namespace, up.Client) {
if err == errors.ErrLostSyncthing || !pods.Exists(up.Pod, up.Dev.Namespace, up.Client) {
log.Yellow("\nConnection lost to your development container, reconnecting...\n")
up.shutdown()
continue
Expand Down Expand Up @@ -350,11 +350,15 @@ func (up *upContext) activate(autoDeploy, build, resetSyncthing bool) {

prevError := up.waitUntilExitOrInterrupt()

if prevError != nil {
if up.shouldRetry(prevError) {
up.shutdown()
continue
if up.shouldRetry(prevError) {
if !up.Dev.PersistentVolumeEnabled() {
if err := pods.Destroy(up.Pod, up.Dev.Namespace, up.Client); err != nil {
up.Exit <- err
return
}
}
up.shutdown()
continue
}

up.Exit <- prevError
Expand All @@ -364,6 +368,8 @@ func (up *upContext) activate(autoDeploy, build, resetSyncthing bool) {

func (up *upContext) shouldRetry(err error) bool {
switch err {
case nil:
return false
case errors.ErrLostSyncthing:
return true
case errors.ErrCommandFailed:
Expand Down Expand Up @@ -722,6 +728,11 @@ func (up *upContext) startSyncthing(resetSyncthing bool) error {
}
}
} else {
if pods.OktetoDevPodMustBeRecreated(up.Context, up.Dev, up.Client) {
if err := pods.Destroy(up.Pod, up.Dev.Namespace, up.Client); err == nil {
return errors.ErrLostSyncthing
}
}
if pods.OktetoFolderINotWritable(up.Context, up.Dev, up.Client) {
return errors.UserError{
E: fmt.Errorf("User %d doesn't have write permissions for the %s directory", userID, up.Dev.MountPath),
Expand Down
13 changes: 11 additions & 2 deletions images/bin/start.sh
Expand Up @@ -24,8 +24,12 @@ mkdir -p "${oktetoFolder}"
touch $OKTETO_MARKER_PATH

remote=0
while getopts ":s:r" opt; do
ephemeral=0
while getopts ":s:re" opt; do
case $opt in
e)
ephemeral=1
;;
r)
remote=1
;;
Expand All @@ -51,12 +55,17 @@ while getopts ":s:r" opt; do
done

syncthingHome=/var/syncthing
if [ $ephemeral -eq 1 ] && [ -f ${syncthingHome}/executed ]; then
echo "failing: syncthing restarted and persistent volumes are not enabled in the okteto manifest. Run 'okteto down' and try again"
exit 1
fi
touch ${syncthingHome}/executed
echo "Copying configuration files to $syncthingHome ..."
cp /var/syncthing/secret/* $syncthingHome

params=""
if [ $remote -eq 1 ]; then
params="--remote"
params="--remote"
fi

echo "Executing supervisor..."
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/deployments/translate.go
Expand Up @@ -37,7 +37,7 @@ const (
oktetoBinName = "okteto-bin"

//syncthing
oktetoBinImageTag = "okteto/bin:1.1.20"
oktetoBinImageTag = "okteto/bin:1.1.21"
oktetoSyncSecretVolume = "okteto-sync-secret" // skipcq GSC-G101 not a secret
oktetoDevSecretVolume = "okteto-dev-secret" // skipcq GSC-G101 not a secret
oktetoSecretTemplate = "okteto-%s"
Expand Down
1 change: 1 addition & 0 deletions pkg/k8s/deployments/translate_test.go
Expand Up @@ -521,6 +521,7 @@ image: web:latest`)
Image: "web:latest",
ImagePullPolicy: apiv1.PullAlways,
Command: []string{"/var/okteto/bin/start.sh"},
Args: []string{"-e"},
WorkingDir: "/okteto",
Env: []apiv1.EnvVar{
{
Expand Down
24 changes: 24 additions & 0 deletions pkg/k8s/pods/pod.go
Expand Up @@ -285,6 +285,20 @@ func Exists(podName, namespace string, c kubernetes.Interface) bool {
return pod.GetObjectMeta().GetDeletionTimestamp() == nil
}

//Destroy destroys a pod by name
func Destroy(podName, namespace string, c kubernetes.Interface) error {
err := c.CoreV1().Pods(namespace).Delete(
podName,
&metav1.DeleteOptions{
GracePeriodSeconds: &devTerminationGracePeriodSeconds,
},
)
if err != nil && !errors.IsNotFound(err) {
return err
}
return nil
}

//GetDevPodUserID returns the user id running the dev pod
func GetDevPodUserID(ctx context.Context, dev *model.Dev, c *kubernetes.Clientset) int64 {
devPodLogs, err := GetDevPodLogs(ctx, dev, false, c)
Expand All @@ -295,6 +309,16 @@ func GetDevPodUserID(ctx context.Context, dev *model.Dev, c *kubernetes.Clientse
return parseUserID(devPodLogs)
}

//OktetoDevPodMustBeRecreated returns true if the dev pod must be recreated
func OktetoDevPodMustBeRecreated(ctx context.Context, dev *model.Dev, c *kubernetes.Clientset) bool {
devPodLogs, err := GetDevPodLogs(ctx, dev, false, c)
if err != nil {
log.Errorf("failed to access development container logs: %s", err)
return false
}
return strings.Contains(devPodLogs, "failing: syncthing restarted and persistent volumes are not enabled")
}

//OktetoFolderINotWritable returns tru if there is an error due to writable permissions
func OktetoFolderINotWritable(ctx context.Context, dev *model.Dev, c *kubernetes.Clientset) bool {
devPodLogs, err := GetDevPodLogs(ctx, dev, false, c)
Expand Down
3 changes: 3 additions & 0 deletions pkg/model/dev.go
Expand Up @@ -682,6 +682,9 @@ func (dev *Dev) ToTranslationRule(main *Dev) *TranslationRule {
for _, s := range rule.Secrets {
rule.Args = append(rule.Args, "-s", fmt.Sprintf("%s:%s", s.GetFileName(), s.RemotePath))
}
if !main.PersistentVolumeEnabled() {
rule.Args = append(rule.Args, "-e")
}
} else if len(dev.Command.Values) > 0 {
rule.Command = dev.Command.Values
rule.Args = []string{}
Expand Down

0 comments on commit 635106f

Please sign in to comment.