Skip to content

Commit

Permalink
add pod distribution budget information logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatsinnit committed Nov 17, 2021
1 parent f55868b commit 7f87b40
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/aks-periscope/aks-periscope.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func main() {
osmCollector := collector.NewOsmCollector()
smiCollector := collector.NewSmiCollector()
podsCollector := collector.NewPodsContainerLogs(config)
pdbCollector := collector.NewPDBCollector(config)

collectors := []interfaces.Collector{
dnsCollector,
Expand All @@ -76,6 +77,7 @@ func main() {
collectors = append(collectors, nodeLogsCollector)
collectors = append(collectors, kubeletCmdCollector)
collectors = append(collectors, systemPerfCollector)
collectors = append(collectors, pdbCollector)
}

// OSM and SMI flags are mutually exclusive
Expand Down
79 changes: 79 additions & 0 deletions pkg/collector/pdb_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package collector

import (
"context"
"encoding/json"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
)

type PDBInfo struct {
Name string `json:"name"`
MinAvailable string `json:"minavailable"`
MaxUnavailable string `json:"maxunavailable"`
DisruptionsAllowed int32 `json:"disruptionsallowed"`
}

// PDBCollector defines a Pod disruption Budget Collector struct
type PDBCollector struct {
kubeconfig *restclient.Config
data map[string]string
}

// NewPDBCollector is a constructor
func NewPDBCollector(config *restclient.Config) *PDBCollector {
return &PDBCollector{
data: make(map[string]string),
kubeconfig: config,
}
}

func (collector *PDBCollector) GetName() string {
return "poddisruptionbudget"
}

// Collect implements the interface method
func (collector *PDBCollector) Collect() error {
// Creates the clientset
clientset, err := kubernetes.NewForConfig(collector.kubeconfig)
if err != nil {
return fmt.Errorf("getting access to K8S failed: %w", err)
}

namespacesList, err := clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
return fmt.Errorf("unable to list namespaces in the cluster: %w", err)
}

for _, namespace := range namespacesList.Items {
podDistInterface, err := clientset.PolicyV1beta1().PodDisruptionBudgets(namespace.Name).List(context.Background(), metav1.ListOptions{}) //.Get(context.Background(), namespace.Name, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("PDB error cluster: %w", err)
}

for _, i := range podDistInterface.Items {
pdbinfo := PDBInfo{
Name: i.Name,
MinAvailable: i.Spec.MinAvailable.String(),
MaxUnavailable: i.Spec.MaxUnavailable.String(),
DisruptionsAllowed: i.Status.DisruptionsAllowed,
}

data, err := json.Marshal(pdbinfo)

if err != nil {
return fmt.Errorf("marshall PDB to json: %w", err)
}
collector.data["pdb-"+namespace.Name] = string(data)
}
}

return nil
}

func (collector *PDBCollector) GetData() map[string]string {
return collector.data
}
59 changes: 59 additions & 0 deletions pkg/collector/pdb_collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package collector

import (
"os"
"path"
"testing"

"k8s.io/client-go/tools/clientcmd"
)

func TestPDBCollector(t *testing.T) {
tests := []struct {
name string
want int
wantErr bool
collectorName string
}{
{
name: "get pdb information for logs",
want: 1,
wantErr: false,
collectorName: "poddisruptionbudget",
},
}

dirname, err := os.UserHomeDir()
if err != nil {
t.Fatalf("Cannot get user home dir: %v", err)
}

master := ""
kubeconfig := path.Join(dirname, ".kube/config")
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
t.Fatalf("Cannot load kube config: %v", err)
}

c := NewPDBCollector(config)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := c.Collect()

if (err != nil) != tt.wantErr {
t.Errorf("Collect() error = %v, wantErr %v", err, tt.wantErr)
}
raw := c.GetData()

if len(raw) < tt.want {
t.Errorf("len(GetData()) = %v, want %v", len(raw), tt.want)
}

name := c.GetName()
if name != tt.collectorName {
t.Errorf("GetName()) = %v, want %v", name, tt.collectorName)
}
})
}
}

0 comments on commit 7f87b40

Please sign in to comment.