Skip to content

Commit

Permalink
Partial working code
Browse files Browse the repository at this point in the history
  • Loading branch information
vs4vijay committed Apr 21, 2020
1 parent b469cfd commit 64d2fdc
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 50 deletions.
55 changes: 51 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

## References
- https://pkg.go.dev/k8s.io/client-go/kubernetes?tab=doc
-
- https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/


## ToDo
- [x] Auth
Expand Down Expand Up @@ -88,6 +89,46 @@ time.Sleep(5 * time.Second)
fmt.Println(p.Status.Phase)
}


watchlist := cache.NewListWatchFromClient(clientset.Core().RESTClient(), "pods", v1.NamespaceDefault,
fields.Everything())
_, controller := cache.NewInformer(
watchlist,
&v1.Pod{},
time.Second * 0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
fmt.Printf("add: %s \n", obj)
},
DeleteFunc: func(obj interface{}) {
fmt.Printf("delete: %s \n", obj)
},
UpdateFunc:func(oldObj, newObj interface{}) {
fmt.Printf("old: %s, new: %s \n", oldObj, newObj)
},
},
)
stop := make(chan struct{})
go controller.Run(stop)


informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options meta_v1.ListOptions) (runtime.Object, error) {
return kubeClient.CoreV1().Pods(conf.Namespace).List(options)
},
WatchFunc: func(options meta_v1.ListOptions) (watch.Interface, error) {
return kubeClient.CoreV1().Pods(conf.Namespace).Watch(options)
},
},
&api_v1.Pod{},
0, //Skip resync
cache.Indexers{},
)




deploymentsClient := clientset.ExtensionsV1beta1().Deployments("namespace-ffledgling")

// List existing deployments in namespace
Expand Down Expand Up @@ -140,15 +181,21 @@ func Loader() string {

https://stackoverflow.com/questions/40975307/how-to-watch-events-on-a-kubernetes-service-using-its-go-client

https://github.com/NetApp/trident/blob/master/k8s_client/k8s_client.go

cache.NewInformer
NewSharedIndexInformer




CPU
MEM
View Logs
Execute Shell
? Events
Events


https://raw.githubusercontent.com/kubernetes/kubernetes/master/hack/testdata/recursive/pod/pod/busybox.yaml
https://raw.githubusercontent.com/istio/istio/master/samples/sleep/sleep.yaml


```
114 changes: 92 additions & 22 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"errors"
"fmt"
"io"
"time"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -85,6 +86,7 @@ func (kubeapi *KubeAPI) GetInfo() (map[string]string, error) {
context := rawConfig.Contexts[rawConfig.CurrentContext]
info["cluster"] = rawConfig.Clusters[context.Cluster].Server
info["user"] = rawConfig.AuthInfos[context.AuthInfo].Username
// info["server_version"] = kubeapi.Clientset.Discovery().ServerVersion()

return info, nil
}
Expand All @@ -105,6 +107,14 @@ func (kubeapi *KubeAPI) DeleteNamespaces(namespaceName string) error {
return nil
}

func (kubeapi *KubeAPI) WatchNamespaces() (watch.Interface, error) {
namespaceWatch, err := kubeapi.Clientset.CoreV1().Namespaces().Watch(metav1.ListOptions{})
if err != nil {
return nil, err
}
return namespaceWatch, nil
}

func (kubeapi *KubeAPI) GetNodes() ([]v1.Node, error) {
nodeList, err := kubeapi.Clientset.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
Expand Down Expand Up @@ -171,9 +181,24 @@ func (kubeapi *KubeAPI) GetContainerLogs(namespace string, podName string, conta
return err
}

func (kubeapi *KubeAPI) GetDeployments(namespace v1.Namespace) ([]v1beta1.Deployment, error) {
deploymentList, err := kubeapi.Clientset.ExtensionsV1beta1().Deployments(namespace.GetName()).List(metav1.ListOptions{})
// AppsV1().Deployments(namespace).List(metav1.ListOptions{})
func (kubeapi *KubeAPI) WatchPodLogs(namespace string, podName string) (watch.Interface, error) {
// tailLines := int64(100)
podLogOptions := v1.PodLogOptions{
// Container: containerName,
// TailLines: &tailLines,
}

// fmt.Println("Logs: ")
watchLogs, err := kubeapi.Clientset.CoreV1().Pods(namespace).GetLogs(podName, &podLogOptions).Watch()
if err != nil {
return nil, err
}
return watchLogs, nil
}

func (kubeapi *KubeAPI) GetDeployments(namespace v1.Namespace) ([]appsv1.Deployment, error) {
deploymentList, err := kubeapi.Clientset.AppsV1().Deployments(namespace.GetName()).List(metav1.ListOptions{})
// ExtensionsV1beta1().Deployments(namespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
Expand All @@ -193,13 +218,31 @@ func (kubeapi *KubeAPI) GetEvents() ([]v1.Event, error) {
}

func (kubeapi *KubeAPI) WatchEvents() (watch.Interface, error) {
eventWatch, err := kubeapi.Clientset.CoreV1().Events("").Watch(metav1.ListOptions{})
eventWatch, err := kubeapi.Clientset.CoreV1().Events("default").Watch(metav1.ListOptions{})
if err != nil {
return nil, err
}
return eventWatch, nil
}

func (kubeapi *KubeAPI) GetSecrets() ([]v1.Secret, error) {
secretList, err := kubeapi.Clientset.CoreV1().Secrets("").List(metav1.ListOptions{})
if err != nil {
return nil, err
}
return secretList.Items, nil
}

func (kubeapi *KubeAPI) CreateSecret(secret *v1.Secret) (*v1.Secret, error) {
// TODO: Pass namespace
return kubeapi.Clientset.CoreV1().Secrets("").Create(secret)
}

func (kubeapi *KubeAPI) DeleteSecret(secretName string) error {
// TODO: Pass namespace
return kubeapi.Clientset.CoreV1().Secrets("").Delete(secretName, &metav1.DeleteOptions{})
}

func (kubeapi *KubeAPI) DryRun() {
fmt.Println("Dry Run")

Expand All @@ -213,27 +256,54 @@ func (kubeapi *KubeAPI) DryRun() {
fmt.Println("\t", namespace.GetName())
}

// Pods
for _, namespace := range namespaces {
fmt.Println("\t", namespace.GetName())
// Secrets
secrets, _ := kubeapi.GetSecrets()
fmt.Println("Secrets: ")
for _, secret := range secrets {
fmt.Println("\t", secret.GetName(), secret.StringData)
}

pods, _ := kubeapi.GetPods(namespace.GetName())
fmt.Println("Pods: ")
for _, pod := range pods {
fmt.Println("\t", pod.GetName())
eventWatch, _ := kubeapi.Clientset.CoreV1().Namespaces().Watch(metav1.ListOptions{})
// eventWatch, _ := kubeapi.WatchEvents()
defer eventWatch.Stop()
go func() {
for event := range eventWatch.ResultChan() {
fmt.Printf("Event Type : %+v\n", event.Type)

switch event.Type {
case watch.Error:
fmt.Errorf("received error while watching pod: %s", event.Object.GetObjectKind().GroupVersionKind().String())
}

e, _ := event.Object.(*v1.Namespace)
fmt.Printf("%v : %v \n", e.GetName())
// renderData(v.Name(), e.Message)
}
}
}()

// Services
for _, namespace := range namespaces {
fmt.Println("\t", namespace.GetName())
time.Sleep(20 * time.Second)

services, _ := kubeapi.GetServices(namespace.GetName())
fmt.Println("Services: ")
for _, service := range services {
fmt.Println("\t", service.GetName())
}
}
// Pods
// for _, namespace := range namespaces {
// fmt.Println("\t", namespace.GetName())
//
// pods, _ := kubeapi.GetPods(namespace.GetName())
// fmt.Println("Pods: ")
// for _, pod := range pods {
// fmt.Println("\t", pod.GetName())
// }
// }

// Services
// for _, namespace := range namespaces {
// fmt.Println("\t", namespace.GetName())
//
// services, _ := kubeapi.GetServices(namespace.GetName())
// fmt.Println("Services: ")
// for _, service := range services {
// fmt.Println("\t", service.GetName())
// }
// }

// k8s.GetContainers(clientset, "kube-system", "kube-apiserver-kind-control-plane")
// k8s.GetContainers(clientset, "kube-system", "kube-controller-manager-kind-control-plane")
Expand Down
Loading

0 comments on commit 64d2fdc

Please sign in to comment.