Skip to content

Commit

Permalink
Second working commit - having trouble with concurrency
Browse files Browse the repository at this point in the history
Signed-off-by: nicklesimba <[email protected]>
  • Loading branch information
nicklesimba committed May 13, 2022
1 parent a3a8599 commit a0f4d90
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
30 changes: 17 additions & 13 deletions cmd/controlloop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ const (
)

func main() {
logging.Verbosef("This is a test... :)")

logLevel := flag.String("log-level", defaultLogLevel, "Specify the pod controller application logging level")
if logLevel != nil && logging.GetLoggingLevel().String() != *logLevel {
logging.SetLogLevel(*logLevel)
Expand All @@ -65,9 +63,7 @@ func main() {
os.Exit(couldNotCreateController)
}

networkController.Start(stopChan) // code seems to spin here... further code not executed

logging.Verbosef("2nd test print, doubt this is gonna be reached...") // only gets reached if I ctrl+C during the code's runtime.
networkController.Start(stopChan)

totalReconcilerSuccess := prometheus.NewCounter(prometheus.CounterOpts{
Name: "reconciler_success_total",
Expand All @@ -77,7 +73,10 @@ func main() {
prometheus.MustRegister(totalReconcilerSuccess)

http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))

go func() {
log.Fatal(http.ListenAndServe(":1337", nil)) // ListenAndServe is a blocking call.
}()

// here's where my for { select {} } loop should go - and use tickers
// https://gobyexample.com/tickers
Expand All @@ -87,24 +86,29 @@ func main() {
// c. default: continue to spin
ticker := time.NewTicker(10 * time.Second) // temp value, will eventually be days/weeks duration

for i := 0; i < 5; i++ { // iterating 5 times just for the sake of terminating code and getting logfile output
logging.Verbosef("iteration #", i)
i := 0
for /*i := 0; i < 5; i++*/ {
i++
logging.Verbosef("iteration #%d", i)
select {
case <-stopChan:
return
case t := <-ticker.C:
// fmt.Println("Running ip-reconciler, tick at ", t)
logging.Verbosef("time to run reconciler (dummy), tick at ", t)
go reconciler.InvokeIPReconciler(returnErr) // need to implement a timeout for this
case err := <-returnErr:
fmt.Println("Running ip-reconciler, tick at ", t)
go reconciler.InvokeIPReconciler(returnErr)
case err := <-returnErr: // why is this case only reached one time?
logging.Verbosef("reached counter decision")
if err == nil {
totalReconcilerSuccess.Inc()
logging.Verbosef("ip reconciler success!")
} else {
logging.Verbosef("ip reconciler failure: ", err)
logging.Verbosef("ip reconciler failure: %s", err)
}
}
}

logging.Verbosef("loop ended...you have one minute to curl for counter results.")
time.Sleep(1 * time.Minute)
return
}

Expand Down
26 changes: 15 additions & 11 deletions cmd/reconciler/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,47 @@ package reconciler
import (
"context"
"errors"
"flag"
"time"

"github.com/k8snetworkplumbingwg/whereabouts/pkg/logging"
"github.com/k8snetworkplumbingwg/whereabouts/pkg/reconciler"
)

const defaultReconcilerTimeout = 30
const (
defaultReconcilerTimeout = 30
// reconcilerTimeout = flag.Int("timeout", defaultReconcilerTimeout, "the value for a request timeout in seconds.") // what about this is not constant? >:(
)

func InvokeIPReconciler(returnErr chan error) {
go func() {
doInvokeIPReconciler(returnErr)
}()
select {
case <-time.After(5 * time.Second):
returnErr <- errors.New("timed out (this is the 5 sec test timeout, nbd)")
case <-time.After(5 * time.Minute):
returnErr <- errors.New("ip reconciler timed out")
return
case <-returnErr:
logging.Verbosef("finishing ip reconciler run")
return
}
}

// TODO: get ip_test.go working with this - currently idk if it does...
func doInvokeIPReconciler(returnErr chan error) {
reconcilerTimeout := flag.Int("timeout", defaultReconcilerTimeout, "the value for a request timeout in seconds.")
ipReconcileLoop, err := reconciler.NewReconcileLooper(context.Background(), *reconcilerTimeout)

ipReconcileLoop, err := reconciler.NewReconcileLooper(context.Background(), defaultReconcilerTimeout)
if err != nil {
_ = logging.Errorf("failed to create the reconcile looper: %v", err)
returnErr <- err
return //returns might not even be necessary thanks to timeout code in InvokeIPReconciler
return
}

cleanedUpIps, err := ipReconcileLoop.ReconcileIPPools(context.Background())
if err != nil {
_ = logging.Errorf("failed to clean up IP for allocations: %v", err)
returnErr <- err
return //returns might not even be necessary thanks to timeout code in InvokeIPReconciler
return
}

if len(cleanedUpIps) > 0 {
logging.Debugf("successfully cleanup IPs: %+v", cleanedUpIps)
} else {
Expand All @@ -50,11 +53,12 @@ func doInvokeIPReconciler(returnErr chan error) {

if err := ipReconcileLoop.ReconcileOverlappingIPAddresses(context.Background()); err != nil {
returnErr <- err
return //returns might not even be necessary thanks to timeout code in InvokeIPReconciler
return
}

// increment success metric here - no error occurred.
logging.Verbosef("no errors with ip reconciler...returning in a sec")
returnErr <- err
return
}

// i think this function will get deleted
Expand Down
6 changes: 0 additions & 6 deletions pkg/reconciler/controlloop/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ func (pc *PodController) Start(stopChan <-chan struct{}) {
}

go wait.Until(pc.worker, syncPeriod, stopChan)
// is the code waiting for this goroutine to finish before returning?
// i WANT to return back to main, and eventually send a stopChan when code needs to exit for whatever reason.

// <-stopChan
// logging.Verbosef("shutting down network controller")
// ^^ these two lines need to be removed so i can return execution to main()
}

func (pc *PodController) worker() {
Expand Down

0 comments on commit a0f4d90

Please sign in to comment.