Skip to content

Commit

Permalink
Updated pod-simulator to echo from file
Browse files Browse the repository at this point in the history
  • Loading branch information
isala404 committed Jun 27, 2021
1 parent 1a9cd51 commit adb03f2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 28 deletions.
31 changes: 29 additions & 2 deletions operator/controllers/flowtest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
70 changes: 44 additions & 26 deletions pod-simulator/main.go
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)
}
}
}

0 comments on commit adb03f2

Please sign in to comment.