-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from vijeyash1/develop
Develop
- Loading branch information
Showing
8 changed files
with
947 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/kube-tarian/kubviz/agent/rakkess" | ||
"github.com/kube-tarian/kubviz/model" | ||
"github.com/nats-io/nats.go" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
const eventSubject_rakees = "METRICS.rakees" | ||
|
||
func accessToOutcome(access rakkess.Access) (rakkess.Outcome, error) { | ||
switch access { | ||
case 0: | ||
return rakkess.None, nil | ||
case 1: | ||
return rakkess.Up, nil | ||
case 2: | ||
return rakkess.Down, nil | ||
case 3: | ||
return rakkess.Err, nil | ||
default: | ||
return rakkess.None, fmt.Errorf("unknown access code: %d", access) | ||
} | ||
} | ||
|
||
func RakeesOutput(js nats.JetStreamContext, wg *sync.WaitGroup, errCh chan error) { | ||
// var kubeConfig string | ||
// if home := homedir.HomeDir(); home != "" { | ||
// kubeConfig = filepath.Join(home, ".kube", "dev-config") | ||
// } | ||
// Build Kubernetes configuration from flags | ||
// config, err := clientcmd.BuildConfigFromFlags("", kubeConfig) | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Create a new Kubernetes client | ||
client, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
|
||
// Retrieve all available resource types | ||
resourceList, err := client.Discovery().ServerPreferredResources() | ||
if err != nil { | ||
errCh <- err | ||
} | ||
var opts = rakkess.NewRakkessOptions() | ||
opts.Verbs = []string{"list", "create", "update", "delete"} | ||
opts.OutputFormat = "icon-table" | ||
opts.ResourceList = resourceList | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
catchCtrlC(cancel) | ||
|
||
res, err := rakkess.Resource(ctx, opts) | ||
if err != nil { | ||
fmt.Println("Error") | ||
errCh <- err | ||
} | ||
fmt.Println("Result..") | ||
for resourceType, access := range res { | ||
createOutcome, err := accessToOutcome(access["create"]) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
deleteOutcome, err := accessToOutcome(access["delete"]) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
listOutcome, err := accessToOutcome(access["list"]) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
updateOutcome, err := accessToOutcome(access["update"]) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
metrics := model.RakeesMetrics{ | ||
ClusterName: ClusterName, | ||
Name: resourceType, | ||
Create: rakkess.HumanreadableAccessCode(createOutcome), | ||
Delete: rakkess.HumanreadableAccessCode(deleteOutcome), | ||
List: rakkess.HumanreadableAccessCode(listOutcome), | ||
Update: rakkess.HumanreadableAccessCode(updateOutcome), | ||
} | ||
metricsJson, _ := json.Marshal(metrics) | ||
_, err = js.Publish(eventSubject_rakees, metricsJson) | ||
if err != nil { | ||
errCh <- err | ||
} | ||
log.Printf("Metrics with resource %s has been published", resourceType) | ||
} | ||
// t := res.Table(opts.Verbs) | ||
// t.Render(opts.Streams.Out, opts.OutputFormat) | ||
|
||
} | ||
|
||
func catchCtrlC(cancel context.CancelFunc) { | ||
catchSigs(cancel, syscall.SIGINT, syscall.SIGPIPE, syscall.SIGTERM) | ||
} | ||
|
||
func catchSigs(cancel context.CancelFunc, sigs ...os.Signal) { | ||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, sigs...) | ||
|
||
go func() { | ||
<-sigChan | ||
cancel() | ||
}() | ||
} |
Oops, something went wrong.