-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
175 lines (161 loc) · 5.36 KB
/
Copy pathmain.go
File metadata and controls
175 lines (161 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/mux"
scenarioconfig "github.com/randsw/cascadescenariocontroller/cascadescenario"
"github.com/randsw/cascadescenariocontroller/handlers"
k8sClient "github.com/randsw/cascadescenariocontroller/k8sclient"
"github.com/randsw/cascadescenariocontroller/logger"
"github.com/randsw/cascadescenariocontroller/process"
prom "github.com/randsw/cascadescenariocontroller/prometheus-exporter"
"go.uber.org/zap"
)
type Payload struct {
PId int `json:"pId"`
OName string `json:"oName"`
SName string `json:"sName"`
ITId int `json:"itId"`
ITUid string `json:"iitUid"`
IncomingCount int `json:"incomingCount"`
IDate string `json:"idate"`
UserId int `json:"userId"`
Path string `json:"path"`
Crc32 string `json:"crc32"`
}
type PayloadOut struct {
UsId int `json:"usId"`
ITUid string `json:"iitUid"` // This used as uid
FCount int `json:"fCount"`
IFCount int `json:"iFCount"`
OPath string `json:"oPath"`
OTId int `json:"oTId"`
IAOut bool `json:"iAOut"`
OName string `json:"oName"`
IDate string `json:"iDate"`
OTUid string `json:"oTUid"`
Crc32 string `json:"crc32"`
ICE bool `json:"iCE"`
PId int `json:"pId"`
ITId int `json:"iTId"`
ODateTime string `json:"oDateTime"`
IPath string `json:"iPath"`
SName string `json:"sName"`
IDateTime string `json:"iDateTime"`
}
var GlobalChannel chan map[string]string = make(chan map[string]string)
func main() {
//Loger Initialization
logger.InitLogger()
defer logger.CloseLogger()
//Get Config from file mounted in tmp folder
configFilename := "/tmp/configuration"
CascadeScenarioConfig := scenarioconfig.ReadConfigJSON(configFilename)
//Get pod namespace
jobNamespace := "cascade-operator"
if envvar := os.Getenv("POD_NAMESPACE"); len(envvar) > 0 {
jobNamespace = envvar
}
// //Get scenario name
scenarioName := "cascadeautooperator-ip"
if envvar := os.Getenv("SCENARIO_NAME"); len(envvar) > 0 {
scenarioName = envvar
}
// Get sID
sID := "UnderTest"
if envvar := os.Getenv("SID"); len(envvar) > 0 {
sID = envvar
}
outMinioAddress := "http://example.com/test-out/"
if envvar := os.Getenv("OUT_MINIO_ADDRESS"); len(envvar) > 0 {
outMinioAddress = envvar
}
//Create channel for signal
cancelChan := make(chan os.Signal, 1)
// catch SIGETRM or SIGINTERRUPT
signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT)
done := make(chan bool, 1)
go func() {
sig := <-cancelChan
logger.Info("Caught signal", zap.String("Signal", sig.String()))
logger.Info("Wait for 1 second to finish processing")
time.Sleep(1 * time.Second)
logger.Info("Exiting.....")
// shutdown other goroutines gracefully
// close other resources
done <- true
os.Exit(0)
}()
k8sAPIClientset := k8sClient.ConnectToK8s()
k8sAPIClientDyn := k8sClient.ConnectTOK8sDinamic()
// watch deletetion timestamp appear in CRD metadate
go func() {
for {
var err error
handlers.IsShutDown, err = k8sClient.WatchDeletetionTimeStampCRD(k8sAPIClientDyn, jobNamespace, scenarioName)
if err != nil {
logger.Error("Fail to read Deletion Timestamp", zap.Error(err))
}
if !handlers.IsShutDown {
time.Sleep(100 * time.Millisecond)
} else {
currval := process.CurrProcess
logger.Info("Waiting for processes to finish...", zap.Int64("Running Processes", process.CurrProcess))
//Delete finalizer
for process.CurrProcess > 0 {
if process.CurrProcess != currval {
logger.Info("Waiting for processes to finish...", zap.Int64("Running Processes", process.CurrProcess))
currval = process.CurrProcess
}
time.Sleep(100 * time.Millisecond)
}
err = k8sClient.DeleteFinalizerCRD(k8sAPIClientDyn, jobNamespace, scenarioName, "shutdown.cascade.cascade.net/finalizer")
if err != nil {
logger.Error("Fail to delete finalizer", zap.Error(err))
}
break
}
}
}()
config := &handlers.RunConfig{
CascadeScenarioConfig: CascadeScenarioConfig,
JobNamespace: jobNamespace,
ScenarioName: scenarioName,
SID: sID,
OutMinioAddress: outMinioAddress,
K8sClient: k8sAPIClientset,
K8sDynClient: k8sAPIClientDyn,
GlobalChannel: GlobalChannel,
}
h := &handlers.ChHandler{Ch: GlobalChannel}
r := &handlers.RunHandler{Config: *config}
mux := mux.NewRouter()
mux.HandleFunc("/", h.GetStatusFromModules)
mux.HandleFunc("/run", r.StartRun)
mux.HandleFunc("/healthz", handlers.GetHealth)
mux.HandleFunc("/metrics", handlers.Metrics)
mux.HandleFunc("/ready", handlers.GetReadiness)
mux.Use(prom.PrometheusMiddleware)
//Start healthz http server
servingAddress := ":8080"
handlers.HTTPRequest = make(map[string][]map[string]string)
srv := &http.Server{
Addr: servingAddress,
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: mux, // Pass our instance of gorilla/mux in.
}
logger.Info("Start serving http request...", zap.String("address", servingAddress))
err := srv.ListenAndServe()
if err != nil {
logger.Error("Fail to start http server", zap.String("err", err.Error()))
}
<-done
// shutdown other goroutines gracefully
// close other resources
}