Skip to content

Commit 97fd724

Browse files
authored
exit gracefully on fatal errors related to core symlinking functionality (#19)
1 parent b10d200 commit 97fd724

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

cmd/hostpaths/hostpaths.go

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ func Start(ctx context.Context, options *VirtualClusterOptions, init bool) error
224224
return err
225225
}
226226
}
227-
mapHostPaths(ctx, localManager, virtualClusterManager)
228-
return nil
227+
228+
return mapHostPaths(ctx, localManager, virtualClusterManager)
229229
}
230230

231231
func getSyncerPodSpec(ctx context.Context, kubeClient kubernetes.Interface, vclusterName, vclusterNamespace string) (*corev1.PodSpec, error) {
@@ -378,14 +378,14 @@ podLoop:
378378
return nil
379379
}
380380

381-
func mapHostPaths(ctx context.Context, pManager, vManager manager.Manager) {
381+
func mapHostPaths(ctx context.Context, pManager, vManager manager.Manager) error {
382382
options := ctx.Value(optionsKey).(*VirtualClusterOptions)
383383

384-
wait.Forever(func() {
384+
mapFunc := func() error {
385385
podMappings, err := getPhysicalPodMap(ctx, options, pManager)
386386
if err != nil {
387387
klog.Errorf("unable to get physical pod mapping: %v", err)
388-
return
388+
return nil
389389
}
390390

391391
vPodList := &corev1.PodList{}
@@ -396,7 +396,7 @@ func mapHostPaths(ctx context.Context, pManager, vManager manager.Manager) {
396396
})
397397
if err != nil {
398398
klog.Errorf("unable to list pods: %v", err)
399-
return
399+
return nil
400400
}
401401

402402
existingVPodsWithNamespace := make(map[string]bool)
@@ -416,21 +416,27 @@ func mapHostPaths(ctx context.Context, pManager, vManager manager.Manager) {
416416

417417
_, err := createPodLogSymlinkToPhysical(source, target)
418418
if err != nil {
419-
klog.Errorf("unable to create symlink for %s: %v", podDetail.Target, err)
419+
return fmt.Errorf("unable to create symlink for %s: %w", podDetail.Target, err)
420420
}
421421

422422
// create kubelet pod symlink
423423
kubeletPodSymlinkSource := filepath.Join(options.VirtualKubeletPodPath, string(vPod.GetUID()))
424424
kubeletPodSymlinkTarget := filepath.Join(podtranslate.PhysicalKubeletVolumeMountPath, string(podDetail.PhysicalPod.GetUID()))
425425
existingKubeletPodsPath[kubeletPodSymlinkSource] = true
426-
createKubeletVirtualToPhysicalPodLinks(kubeletPodSymlinkSource, kubeletPodSymlinkTarget)
426+
err = createKubeletVirtualToPhysicalPodLinks(kubeletPodSymlinkSource, kubeletPodSymlinkTarget)
427+
if err != nil {
428+
return err
429+
}
427430

428431
// podDetail.SymLinkName = symlinkName
429432

430433
// create container to vPod symlinks
431434
containerSymlinkTargetDir := filepath.Join(PodLogsMountPath,
432435
fmt.Sprintf("%s_%s_%s", vPod.Namespace, vPod.Name, string(vPod.UID)))
433-
createContainerToPodSymlink(ctx, vPod, podDetail, containerSymlinkTargetDir)
436+
err = createContainerToPodSymlink(ctx, vPod, podDetail, containerSymlinkTargetDir)
437+
if err != nil {
438+
return err
439+
}
434440
}
435441
}
436442

@@ -451,7 +457,17 @@ func mapHostPaths(ctx context.Context, pManager, vManager manager.Manager) {
451457
}
452458

453459
klog.Infof("successfully reconciled mapper")
454-
}, time.Second*5)
460+
return nil
461+
}
462+
463+
for {
464+
err := mapFunc()
465+
if err != nil {
466+
return err
467+
}
468+
469+
time.Sleep(5 * time.Second)
470+
}
455471
}
456472

457473
func getPhysicalPodMap(ctx context.Context, options *VirtualClusterOptions, pManager manager.Manager) (PhysicalPodMap, error) {
@@ -557,20 +573,18 @@ func cleanupOldContainerPaths(ctx context.Context, existingVPodsWithNS map[strin
557573
return nil
558574
}
559575

560-
func createKubeletVirtualToPhysicalPodLinks(vPodDirName, pPodDirName string) {
576+
func createKubeletVirtualToPhysicalPodLinks(vPodDirName, pPodDirName string) error {
561577
err := os.MkdirAll(vPodDirName, os.ModeDir)
562578
if err != nil {
563-
klog.Errorf("error creating vPod kubelet directory for %s: %v", vPodDirName, err)
564-
return
579+
return fmt.Errorf("error creating vPod kubelet directory for %s: %w", vPodDirName, err)
565580
}
566581

567582
// scan all contents in the physical pod dir
568583
// and create equivalent symlinks from virtual
569584
// path to physical
570585
contents, err := os.ReadDir(pPodDirName)
571586
if err != nil {
572-
klog.Errorf("error reading physical kubelet pod dir %s: %v", pPodDirName, err)
573-
return
587+
return fmt.Errorf("error reading physical kubelet pod dir %s: %w", pPodDirName, err)
574588
}
575589

576590
for _, content := range contents {
@@ -582,12 +596,14 @@ func createKubeletVirtualToPhysicalPodLinks(vPodDirName, pPodDirName string) {
582596
fullKubeletVirtualPodPath)
583597
if err != nil {
584598
if !os.IsExist(err) {
585-
klog.Errorf("error creating symlink for %s -> %s: %v", fullKubeletVirtualPodPath, fullKubeletPhysicalPodPath, err)
599+
return fmt.Errorf("error creating symlink for %s -> %s: %w", fullKubeletVirtualPodPath, fullKubeletPhysicalPodPath, err)
586600
}
587601
} else {
588602
klog.Infof("created kubelet pod symlink %s -> %s", fullKubeletVirtualPodPath, fullKubeletPhysicalPodPath)
589603
}
590604
}
605+
606+
return nil
591607
}
592608

593609
func cleanupOldPodPath(ctx context.Context, cleanupDirPath string, existingPodPathsFromAPIServer map[string]bool) error {
@@ -641,7 +657,7 @@ func cleanupOldPodPath(ctx context.Context, cleanupDirPath string, existingPodPa
641657
return nil
642658
}
643659

644-
func createContainerToPodSymlink(ctx context.Context, vPod corev1.Pod, pPodDetail *PodDetail, targetDir string) {
660+
func createContainerToPodSymlink(ctx context.Context, vPod corev1.Pod, pPodDetail *PodDetail, targetDir string) error {
645661
options := ctx.Value(optionsKey).(*VirtualClusterOptions)
646662

647663
for _, containerStatus := range vPod.Status.ContainerStatuses {
@@ -673,14 +689,16 @@ func createContainerToPodSymlink(ctx context.Context, vPod corev1.Pod, pPodDetai
673689
err = os.Symlink(target, source)
674690
if err != nil {
675691
if !os.IsExist(err) {
676-
klog.Errorf("error creating container:%s to pod:%s symlink: %v", source, target, err)
692+
return fmt.Errorf("error creating container:%s to pod:%s symlink: %w", source, target, err)
677693
}
678694

679695
continue
680696
}
681697

682698
klog.Infof("created container:%s -> pod:%s symlink", source, target)
683699
}
700+
701+
return nil
684702
}
685703

686704
// we need to get the info that which log file in the physical pod dir

0 commit comments

Comments
 (0)