-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated pod-simulator to echo from file
- Loading branch information
Showing
2 changed files
with
73 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"time" | ||
) | ||
|
||
// FlowTestReconciler reconciles a FlowTest object | ||
|
@@ -45,9 +46,35 @@ type FlowTestReconciler struct { | |
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *FlowTestReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
_ = log.FromContext(ctx) | ||
logger := log.FromContext(ctx) | ||
|
||
return ctrl.Result{}, nil | ||
var flowTest loggingplumberv1alpha1.FlowTest | ||
if err := r.Get(ctx, req.NamespacedName, &flowTest); err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
logger.Info("Reconciling") | ||
|
||
//var referencePod v1.Pod | ||
//if err := r.Get(ctx, types.NamespacedName{ | ||
// Namespace: flowTest.Spec.ReferencePod.Namespace, | ||
// Name: flowTest.Spec.ReferencePod.Name, | ||
//}, &referencePod); err != nil { | ||
// //if apierrors.IsNotFound(err) { | ||
// // logger.V() | ||
// //} | ||
// //client.IgnoreNotFound() | ||
// return ctrl.Result{}, err | ||
//} | ||
|
||
//var referenceFlow flowv1beta1.Flow | ||
//if err := r.Get(ctx, types.NamespacedName{ | ||
// Namespace: flowTest.Spec.ReferenceFlow.Namespace, | ||
// Name: flowTest.Spec.ReferenceFlow.Name, | ||
//}, &referenceFlow); err != nil { | ||
// return ctrl.Result{}, err | ||
//} | ||
|
||
return ctrl.Result{RequeueAfter: time.Second * 10}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"bufio" | ||
"fmt" | ||
"github.com/gorilla/mux" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func echo(w http.ResponseWriter, req *http.Request) { | ||
var logs []interface{} | ||
err := json.NewDecoder(req.Body).Decode(&logs) | ||
func main() { | ||
echoDelay := 3 * time.Second | ||
if delay, err := strconv.Atoi(os.Getenv("ECHO_DELAY")); err == nil { | ||
echoDelay = time.Duration(delay) * time.Second | ||
} | ||
|
||
// Source - https://www.geeksforgeeks.org/how-to-read-a-file-line-by-line-to-string-in-golang/ | ||
|
||
// os.Open() opens specific file in | ||
// read-only mode and this return | ||
// a pointer of type os. | ||
file, err := os.Open("./simulation.log") | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
panic("failed to open simulation.log") | ||
|
||
} | ||
|
||
for _, log := range logs{ | ||
jsonString, err := json.Marshal(log) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
fmt.Println(string(jsonString)) | ||
// The bufio.NewScanner() function is called in which the | ||
// object os.File passed as its parameter and this returns a | ||
// object bufio.Scanner which is further used on the | ||
// bufio.Scanner.Split() method. | ||
scanner := bufio.NewScanner(file) | ||
|
||
// The bufio.ScanLines is used as an | ||
// input to the method bufio.Scanner.Split() | ||
// and then the scanning forwards to each | ||
// new line using the bufio.Scanner.Scan() | ||
// method. | ||
scanner.Split(bufio.ScanLines) | ||
var text []string | ||
|
||
for scanner.Scan() { | ||
text = append(text, scanner.Text()) | ||
} | ||
} | ||
|
||
func main() { | ||
var webAddr string | ||
flag.StringVar(&webAddr, "web-addr", ":9090", "The address http server binds to.") | ||
flag.Parse() | ||
// The method os.File.Close() is called | ||
// on the os.File object to close the file | ||
file.Close() | ||
|
||
r := mux.NewRouter() | ||
r.HandleFunc("/", echo).Methods("POST") | ||
// ------------------------------------------------------------------------------------------ | ||
|
||
err := http.ListenAndServe(webAddr, r) | ||
if err != nil { | ||
panic(err) | ||
for ; ; { | ||
for _, line := range text { | ||
fmt.Println(line) | ||
time.Sleep(echoDelay) | ||
} | ||
} | ||
} |