🔨 Move cli folder contents into project root (#1253)

* Remove `logger` module

* Remove `shared` module

* Move `cli` folder contents into project root

* Fix linter

* Change the module name from `github.com/kubeshark/kubeshark/cli` to `github.com/kubeshark/kubeshark`

* Set the default `Makefile` rule to `build`

* Add `lint` rule

* Fix the linter errors
This commit is contained in:
M. Mert Yildiran
2022-11-25 14:17:50 -08:00
committed by GitHub
parent 9aeb1fadea
commit cb60a4cc4c
92 changed files with 512 additions and 2219 deletions

10
utils/consts.go Normal file
View File

@@ -0,0 +1,10 @@
package utils
const (
KubesharkFilteringOptionsEnvVar = "SENSITIVE_DATA_FILTERING_OPTIONS"
KubesharkAgentImageRepo = "docker.io/kubeshark/hub"
LogLevelEnvVar = "LOG_LEVEL"
BaseninePort = "9099"
HostModeEnvVar = "HOST_MODE"
NodeNameEnvVar = "NODE_NAME"
)

46
utils/httpUtils.go Normal file
View File

@@ -0,0 +1,46 @@
package utils
import (
"bytes"
"fmt"
"io"
"net/http"
"strings"
)
// Get - When err is nil, resp always contains a non-nil resp.Body.
// Caller should close resp.Body when done reading from it.
func Get(url string, client *http.Client) (*http.Response, error) {
return checkError(client.Get(url))
}
// Post - When err is nil, resp always contains a non-nil resp.Body.
// Caller should close resp.Body when done reading from it.
func Post(url, contentType string, body io.Reader, client *http.Client) (*http.Response, error) {
return checkError(client.Post(url, contentType, body))
}
// Do - When err is nil, resp always contains a non-nil resp.Body.
// Caller should close resp.Body when done reading from it.
func Do(req *http.Request, client *http.Client) (*http.Response, error) {
return checkError(client.Do(req))
}
func checkError(response *http.Response, errInOperation error) (*http.Response, error) {
if errInOperation != nil {
return response, errInOperation
// Check only if status != 200 (and not status >= 300). Agent APIs return only 200 on success.
} else if response.StatusCode != http.StatusOK {
body, err := io.ReadAll(response.Body)
response.Body.Close()
response.Body = io.NopCloser(bytes.NewBuffer(body)) // rewind
if err != nil {
return response, err
}
errorMsg := strings.ReplaceAll(string(body), "\n", ";")
return response, fmt.Errorf("got response with status code: %d, body: %s", response.StatusCode, errorMsg)
}
return response, nil
}

39
utils/sliceUtils.go Normal file
View File

@@ -0,0 +1,39 @@
package utils
func Contains(slice []string, containsValue string) bool {
for _, sliceValue := range slice {
if sliceValue == containsValue {
return true
}
}
return false
}
func Unique(slice []string) []string {
keys := make(map[string]bool)
var list []string
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func EqualStringSlices(slice1 []string, slice2 []string) bool {
if len(slice1) != len(slice2) {
return false
}
for _, v := range slice1 {
if !Contains(slice2, v) {
return false
}
}
return true
}

25
utils/waitUtils.go Normal file
View File

@@ -0,0 +1,25 @@
package utils
import (
"context"
"log"
"os"
"os/signal"
"syscall"
)
func WaitForFinish(ctx context.Context, cancel context.CancelFunc) {
log.Printf("waiting for finish...")
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
// block until ctx cancel is called or termination signal is received
select {
case <-ctx.Done():
log.Printf("ctx done")
break
case <-sigChan:
log.Printf("Got termination signal, canceling execution...")
cancel()
}
}