Skip to content

Commit

Permalink
minor clean up
Browse files Browse the repository at this point in the history
Signed-off-by: Bella Khizgiyaev <[email protected]>
  • Loading branch information
bkhizgiy committed May 20, 2024
1 parent 76fd600 commit e090985
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 41 deletions.
1 change: 0 additions & 1 deletion cmd/virt-v2v-monitor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ go_library(
"//vendor/github.com/prometheus/client_golang/prometheus",
"//vendor/github.com/prometheus/client_golang/prometheus/promhttp",
"//vendor/github.com/prometheus/client_model/go",
"//vendor/k8s.io/klog/v2:klog",
],
)
49 changes: 12 additions & 37 deletions cmd/virt-v2v-monitor/virt-v2v-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"flag"
"fmt"
"net/http"
"os"
Expand All @@ -12,7 +11,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
dto "github.com/prometheus/client_model/go"
"k8s.io/klog/v2"
)

var COPY_DISK_RE = regexp.MustCompile(`^.*Copying disk (\d+)/(\d+)`)
Expand Down Expand Up @@ -52,28 +50,15 @@ func updateProgress(progressCounter *prometheus.CounterVec, disk, progress uint6

change := float64(progress) - previous_progress
if change > 0 {
klog.Infof("Progress changed for disk %d about %v", disk, change)
fmt.Printf("virt-v2v monitoring: Progress changed for disk %d about %v\n", disk, change)
progressCounter.WithLabelValues(label).Add(change)
}
return
}

func NewBufferedScanner(r *bufio.Reader, bufferSize int) *bufio.Scanner {
scanner := bufio.NewScanner(r)
buf := make([]byte, bufferSize)
scanner.Buffer(buf, bufferSize)
scanner.Split(LimitedScanLines)
return scanner
}

func main() {
klog.InitFlags(nil)
defer klog.Flush()
flag.Parse()

// Start prometheus metrics HTTP handler
klog.Info("Setting up prometheus endpoint :2112/metrics")
klog.Info("this is Bella test")
fmt.Println("virt-v2v monitoring: Setting up prometheus endpoint :2112/metrics")
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":2112", nil)

Expand All @@ -88,62 +73,52 @@ func main() {
if err := prometheus.Register(progressCounter); err != nil {
// Exit gracefully if we fail here. We don't need monitoring
// failures to hinder guest conversion.
klog.Error("Prometheus progress counter not registered:", err)
fmt.Println("virt-v2v monitoring: Prometheus progress counter not registered:", err)
return
}
klog.Info("Prometheus progress counter registered.")
fmt.Println("virt-v2v monitoring: Prometheus progress counter registered.")

var diskNumber uint64 = 0
var disks uint64 = 0
var progress uint64 = 0

reader := bufio.NewReader(os.Stdin)
scanner := NewBufferedScanner(reader, 1*1024*1024)
fmt.Println("Arik")
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(LimitedScanLines)
for scanner.Scan() {
line := scanner.Bytes()
os.Stdout.Write(line)
os.Stdout.Write([]byte("\n"))
err := scanner.Err()
if err != nil {
klog.Fatal("Output monitoring failed! ", err)
fmt.Println("virt-v2v monitoring: Output monitoring failed! ", err)
}

//fmt.Println("this is the line we scanning now ", string(line))

if match := COPY_DISK_RE.FindSubmatch(line); match != nil {
diskNumber, _ = strconv.ParseUint(string(match[1]), 10, 0)
disks, _ = strconv.ParseUint(string(match[2]), 10, 0)
//klog.Infof("Copying disk %d out of %d", diskNumber, disks)
fmt.Printf("Copying disk %d out of %d", diskNumber, disks)
fmt.Printf("virt-v2v monitoring: Copying disk %d out of %d\n", diskNumber, disks)
progress = 0
err = updateProgress(progressCounter, diskNumber, progress)
} else if match := DISK_PROGRESS_RE.FindSubmatch(line); match != nil {
//klog.Info("we are here at progress ", line)
fmt.Printf("we are here at progress line=%s match-0=%s match-1=%s\n", string(line), string(match[0]), string(match[1]))
progress, _ = strconv.ParseUint(string(match[1]), 10, 0)
//klog.Infof("Progress update, completed %d %%", progress)
fmt.Printf("Progress update, completed %d %%\n", progress)
fmt.Printf("virt-v2v monitoring: Progress update, completed %d %%\n", progress)
err = updateProgress(progressCounter, diskNumber, progress)
} else if match := FINISHED_RE.Find(line); match != nil {
// Make sure we flag conversion as finished. This is
// just in case we miss the last progress update for some reason.
//klog.Infof("Finished")
fmt.Printf("Finished\n")
fmt.Println("virt-v2v monitoring: Finished")
for disk := uint64(0); disk < disks; disk++ {
err = updateProgress(progressCounter, disk, 100)
}
} else {
klog.Infof("Ignoring line: ", string(line))
}
if err != nil {
// Don't make processing errors fatal.
klog.Error("Error updating progress: ", err)
fmt.Println("virt-v2v monitoring: Error updating progress: ", err)
err = nil
}
}
err := scanner.Err()
if err != nil {
klog.Fatal("Output monitoring failed! ", err)
fmt.Println("virt-v2v monitoring: Output monitoring failed! ", err)
}
}
5 changes: 2 additions & 3 deletions virt-v2v/cold/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,14 @@ func executeVirtV2v(source string, args []string) (err error) {
virtV2vCmd.Stdout = w
virtV2vCmd.Stderr = w

fmt.Println("exec ", virtV2vCmd)
if err = virtV2vCmd.Start(); err != nil {
fmt.Printf("Error executing command: %v\n", err)
return
}

virtV2vMonitorCmd := exec.Command("/usr/local/bin/virt-v2v-monitor")
virtV2vMonitorCmd.Stdin = r
//k, _ := virtV2vMonitorCmd.StderrPipe()
//r2 := io.TeeReader(k, os.Stderr)
virtV2vMonitorCmd.Stdout = os.Stdout

if err = virtV2vMonitorCmd.Start(); err != nil {
Expand All @@ -215,7 +214,7 @@ func executeVirtV2v(source string, args []string) (err error) {
}

if source == OVA {
scanner := bufio.NewScanner(nil)
scanner := bufio.NewScanner(r)
const maxCapacity = 1024 * 1024
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, maxCapacity)
Expand Down

0 comments on commit e090985

Please sign in to comment.