Files
kubeshark/agent/pkg/resolver
M. Mert Yıldıran 0eb7883a47 Rename the project to Kubeshark (#1226)
* 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"
2022-11-19 11:13:15 +03:00
..

Usage

Full example

errOut := make(chan error, 100)
k8sResolver, err := resolver.NewFromOutOfCluster("", errOut)
if err != nil {
    logger.Log.Errorf("error creating k8s resolver %s", err)
}

ctx, cancel := context.WithCancel(context.Background())
k8sResolver.Start(ctx)

resolvedName := k8sResolver.Resolve("10.107.251.91") // will always return `nil` in real scenarios as the internal map takes a moment to populate after `Start` is called
if resolvedName != nil {
    logger.Log.Errorf("resolved 10.107.251.91=%s", *resolvedName)
} else {
    logger.Log.Error("Could not find a resolved name for 10.107.251.91")
}

for {
    select {
        case err := <- errOut:
            logger.Log.Errorf("name resolving error %s", err)
    }
}

In cluster authentication

Create resolver using the function NewFromInCluster(errOut chan error)

Out of cluster authentication

Create resolver using the function NewFromOutOfCluster(kubeConfigPath string, errOut chan error)

the kubeConfigPath param is optional, pass an empty string "" for resolver to auto locate the default kubeconfig file

Error handling

Please ensure there is always a thread reading from the errOut channel, not doing so will result in the resolver threads getting blocked and the resolver will fail to update.

Also note that any error you receive through this channel does not necessarily mean that resolver is no longer running. the resolver will infinitely retry watching k8s resources until the provided context is cancelled.