-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pod distribution budget information logger.
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |