mirror of
https://github.com/kubeshark/kubeshark.git
synced 2026-05-08 10:17:49 +00:00
* Rename `mizu` to `kubeshark` * Rename `up9inc` to `kubeshark` * Change the logo, title, motto and the main color * Replace the favicon * Update the docs link * Change the copyright text in C files * Remove a comment * Rewrite the `README.md` and update the logo and screenshots used in it * Add a `TODO` * Fix the grammar * Fix the bottom text in the filtering guide * Change the Docker Hub username of cross-compilation intermediate images * Add an install script * Fix `docker/login-action` in the CI * Delete `build-custom-branch.yml` GitHub workflow * Update `README.md` * Remove `install.sh` * Change the motto back to "Traffic viewer for Kubernetes"
94 lines
2.5 KiB
Go
94 lines
2.5 KiB
Go
package apiserver
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/kubeshark/kubeshark/cli/utils"
|
|
|
|
"github.com/kubeshark/kubeshark/cli/config"
|
|
"github.com/kubeshark/kubeshark/logger"
|
|
"github.com/kubeshark/kubeshark/shared"
|
|
core "k8s.io/api/core/v1"
|
|
)
|
|
|
|
type Provider struct {
|
|
url string
|
|
retries int
|
|
client *http.Client
|
|
}
|
|
|
|
const DefaultRetries = 3
|
|
const DefaultTimeout = 2 * time.Second
|
|
|
|
func NewProvider(url string, retries int, timeout time.Duration) *Provider {
|
|
return &Provider{
|
|
url: url,
|
|
retries: config.GetIntEnvConfig(config.ApiServerRetries, retries),
|
|
client: &http.Client{
|
|
Timeout: timeout,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (provider *Provider) TestConnection() error {
|
|
retriesLeft := provider.retries
|
|
for retriesLeft > 0 {
|
|
if isReachable, err := provider.isReachable(); err != nil || !isReachable {
|
|
logger.Log.Debugf("api server not ready yet %v", err)
|
|
} else {
|
|
logger.Log.Debugf("connection test to api server passed successfully")
|
|
break
|
|
}
|
|
retriesLeft -= 1
|
|
time.Sleep(time.Second)
|
|
}
|
|
|
|
if retriesLeft == 0 {
|
|
return fmt.Errorf("couldn't reach the api server after %v retries", provider.retries)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (provider *Provider) isReachable() (bool, error) {
|
|
echoUrl := fmt.Sprintf("%s/echo", provider.url)
|
|
if _, err := utils.Get(echoUrl, provider.client); err != nil {
|
|
return false, err
|
|
} else {
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
func (provider *Provider) ReportTapperStatus(tapperStatus shared.TapperStatus) error {
|
|
tapperStatusUrl := fmt.Sprintf("%s/status/tapperStatus", provider.url)
|
|
|
|
if jsonValue, err := json.Marshal(tapperStatus); err != nil {
|
|
return fmt.Errorf("failed Marshal the tapper status %w", err)
|
|
} else {
|
|
if _, err := utils.Post(tapperStatusUrl, "application/json", bytes.NewBuffer(jsonValue), provider.client); err != nil {
|
|
return fmt.Errorf("failed sending to API server the tapped pods %w", err)
|
|
} else {
|
|
logger.Log.Debugf("Reported to server API about tapper status: %v", tapperStatus)
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func (provider *Provider) ReportTappedPods(pods []core.Pod) error {
|
|
tappedPodsUrl := fmt.Sprintf("%s/status/tappedPods", provider.url)
|
|
|
|
if jsonValue, err := json.Marshal(pods); err != nil {
|
|
return fmt.Errorf("failed Marshal the tapped pods %w", err)
|
|
} else {
|
|
if _, err := utils.Post(tappedPodsUrl, "application/json", bytes.NewBuffer(jsonValue), provider.client); err != nil {
|
|
return fmt.Errorf("failed sending to API server the tapped pods %w", err)
|
|
} else {
|
|
logger.Log.Debugf("Reported to server API about %d taped pods successfully", len(pods))
|
|
return nil
|
|
}
|
|
}
|
|
}
|