kubeeye/pkg/validator/audit.go

178 lines
4.8 KiB
Go
Raw Normal View History

package validator
import (
"context"
"fmt"
2020-11-20 18:57:02 +08:00
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
conf "kubeye/pkg/config"
"kubeye/pkg/kube"
2020-11-20 18:57:02 +08:00
"strings"
"text/tabwriter"
"os"
)
2020-11-20 18:57:02 +08:00
func Cluster(ctx context.Context) error {
k, err := kube.CreateResourceProvider(ctx)
if err != nil {
2020-11-20 18:57:02 +08:00
return errors.Wrap(err, "Failed to get cluster information")
}
2020-11-20 18:57:02 +08:00
basicComponentStatus, err := ComponentStatusResult(k.ComponentStatus)
if err != nil {
2020-11-20 18:57:02 +08:00
return errors.Wrap(err, "Failed to get BasicComponentStatus information")
}
2020-11-10 10:49:25 +08:00
clusterCheckResults, err := ProblemDetectorResult(k.ProblemDetector)
if err != nil {
2020-11-20 18:57:02 +08:00
return errors.Wrap(err, "Failed to get clusterCheckResults information")
}
nodeStatus, err := NodeStatusResult(k.Nodes)
if err != nil {
2020-11-20 18:57:02 +08:00
return errors.Wrap(err, "Failed to get nodeStatus information")
}
var config conf.Configuration
config, err = conf.ParseFile()
goodPractice, err := ValidatePods(ctx, &config, k)
if err != nil {
fmt.Println("1")
}
2020-11-20 18:57:02 +08:00
w := tabwriter.NewWriter(os.Stdout, 10, 4, 3, ' ', 0)
if len(nodeStatus) != 0 {
fmt.Fprintln(w, "HEARTBEATTIME\tSEVERITY\tNODENAME\tREASON\tMESSAGE")
for _, nodestatus := range nodeStatus {
s := fmt.Sprintf("%s\t%s\t%s\t%s\t%-8v",
nodestatus.HeartbeatTime,
nodestatus.Severity,
nodestatus.Name,
nodestatus.Reason,
nodestatus.Message,
)
fmt.Fprintln(w, s)
continue
}
}
2020-11-23 10:57:32 +08:00
2020-11-20 18:57:02 +08:00
if len(basicComponentStatus) != 0 {
2020-11-23 10:57:32 +08:00
fmt.Fprintln(w, "\nNAME\tSEVERITY\tMESSAGE")
2020-11-20 18:57:02 +08:00
for _, basiccomponentStatus := range basicComponentStatus {
s := fmt.Sprintf("%s\t%s\t%-8v",
basiccomponentStatus.Name,
basiccomponentStatus.Severity,
basiccomponentStatus.Message,
)
fmt.Fprintln(w, s)
continue
}
}
2020-11-23 10:57:32 +08:00
2020-11-20 18:57:02 +08:00
if len(clusterCheckResults) != 0 {
2020-11-23 10:57:32 +08:00
fmt.Fprintln(w, "\nEVENTTIME\tNODENAME\tNAMESPACE\tREASON\tMESSAGE")
2020-11-20 18:57:02 +08:00
for _, clusterCheckResult := range clusterCheckResults {
s := fmt.Sprintf("%s\t%s\t%s\t%s\t%-8v",
clusterCheckResult.EventTime,
clusterCheckResult.Name,
clusterCheckResult.Namespace,
clusterCheckResult.Reason,
clusterCheckResult.Message,
)
fmt.Fprintln(w, s)
continue
}
}
2020-11-23 10:57:32 +08:00
2020-11-20 18:57:02 +08:00
if len(goodPractice) != 0 {
2020-11-23 10:57:32 +08:00
fmt.Fprintln(w, "\nTIME\tNAME\tNAMESPACE\tKIND\tMESSAGE")
2020-11-20 18:57:02 +08:00
for _, goodpractice := range goodPractice {
s := fmt.Sprintf("%s\t%s\t%s\t%s\t%-8v",
goodpractice.CreatedTime,
goodpractice.Name,
goodpractice.Namespace,
goodpractice.Kind,
goodpractice.ContainerResults,
)
fmt.Fprintln(w, s)
continue
}
}
2020-11-20 18:57:02 +08:00
w.Flush()
//auditData := AuditData{
// AuditTime: k.CreationTime.Format(time.RFC3339),
// AuditAddress: k.AuditAddress,
//BasicComponentStatus: basicComponentStatus,
//BasicClusterInformation: BasicClusterInformation{
// K8sVersion: k.ServerVersion,
// PodNum: len(k.Pods),
// NodeNum: len(k.Nodes),
// NamespaceNum: len(k.Namespaces),
//},
2020-11-20 18:57:02 +08:00
//ClusterConfigurationResults: goodPractice,
//AllNodeStatusResults: nodeStatus,
//ClusterCheckResults: clusterCheckResults,
//}
//jsonBytes, err := json.Marshal(auditData)
//outputBytes, err := yaml.JSONToYAML(jsonBytes)
//os.Stdout.Write(outputBytes)
return nil
}
2020-11-20 18:57:02 +08:00
func ComponentStatusResult(cs []v1.ComponentStatus) ([]BasicComponentStatus, error) {
var crs []BasicComponentStatus
for i := 0; i < len(cs); i++ {
2020-11-20 18:57:02 +08:00
if strings.Contains(cs[i].Conditions[0].Message, "ok") == true || strings.Contains(cs[i].Conditions[0].Message, "true") == true {
continue
}
cr := BasicComponentStatus{
Name: cs[i].ObjectMeta.Name,
Message: cs[i].Conditions[0].Message,
Severity: "danger",
}
crs = append(crs, cr)
}
2020-11-20 18:57:02 +08:00
return crs, nil
}
2020-11-10 10:49:25 +08:00
func ProblemDetectorResult(event []v1.Event) ([]ClusterCheckResults, error) {
var pdrs []ClusterCheckResults
for j := 0; j < len(event); j++ {
if event[j].Type == "Warning" {
pdr := ClusterCheckResults{
Namespace: event[j].ObjectMeta.Namespace,
2020-11-10 10:49:25 +08:00
Name: event[j].ObjectMeta.Name,
EventTime: event[j].LastTimestamp.Time,
2020-11-10 10:49:25 +08:00
Reason: event[j].Reason,
Message: event[j].Message,
}
pdrs = append(pdrs, pdr)
}
}
return pdrs, nil
}
2020-11-10 10:49:25 +08:00
func NodeStatusResult(nodes []v1.Node) ([]AllNodeStatusResults, error) {
var nodestatus []AllNodeStatusResults
for k := 0; k < len(nodes); k++ {
2020-11-20 18:57:02 +08:00
if nodes[k].Status.Conditions[len(nodes[k].Status.Conditions)-1].Status == "True" {
continue
}
2020-11-10 10:49:25 +08:00
nodestate := AllNodeStatusResults{
Name: nodes[k].ObjectMeta.Name,
HeartbeatTime: nodes[k].Status.Conditions[len(nodes[k].Status.Conditions)-1].LastHeartbeatTime.Time,
2020-11-10 10:49:25 +08:00
Status: nodes[k].Status.Conditions[len(nodes[k].Status.Conditions)-1].Status,
Reason: nodes[k].Status.Conditions[len(nodes[k].Status.Conditions)-1].Reason,
Message: nodes[k].Status.Conditions[len(nodes[k].Status.Conditions)-1].Message,
2020-11-20 18:57:02 +08:00
Severity: "danger",
}
2020-11-20 18:57:02 +08:00
nodestatus = append(nodestatus, nodestate)
}
return nodestatus, nil
2020-11-10 10:49:25 +08:00
}