This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check(osm): Check for bad logs in osm controller pods
Signed-off-by: Johnson Shi <[email protected]>
- Loading branch information
1 parent
fae1393
commit 592627a
Showing
12 changed files
with
147 additions
and
61 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
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,52 @@ | ||
package podhelper | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"regexp" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// HasNoBadLogs checks whether the logs of the pod container contain bad (fatal/error/warning/fail) logs | ||
func HasNoBadLogs(client kubernetes.Interface, pod *v1.Pod, containerName string) error { | ||
logsTailLines := int64(10) | ||
podLogsOpt := v1.PodLogOptions{ | ||
Container: containerName, | ||
Follow: false, | ||
Previous: false, | ||
TailLines: &logsTailLines, | ||
} | ||
|
||
request := client.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogsOpt) | ||
podLogsReader, err := request.Stream(context.TODO()) | ||
if err != nil { | ||
// If there are issues obtaining current container logs, return previously terminated container logs. | ||
podLogsOpt.Previous = true | ||
request := client.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogsOpt) | ||
podLogsReader, err = request.Stream(context.TODO()) | ||
if err != nil { | ||
return fmt.Errorf("could not obtain %s container logs of pod %s: %#v", containerName, pod.Name, err) | ||
} | ||
} | ||
defer podLogsReader.Close() //nolint: errcheck,gosec | ||
|
||
re := regexp.MustCompile("(?i)(fatal)|(error)|(warn)|(fail)") | ||
scanner := bufio.NewScanner(podLogsReader) | ||
var badLogLines string | ||
for scanner.Scan() { | ||
logLine := scanner.Text() | ||
if re.MatchString(logLine) { | ||
badLogLines += logLine + "\n" | ||
} | ||
} | ||
|
||
if len(badLogLines) != 0 { | ||
log.Error().Msgf("%s container of pod %s contains bad logs", containerName, pod.Name) | ||
log.Error().Msg(badLogLines) | ||
} | ||
|
||
return nil | ||
} |
2 changes: 1 addition & 1 deletion
2
pkg/kubernetes/pod/containers.go → pkg/kubernetes/podhelper/containers.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pod | ||
package podhelper | ||
|
||
import ( | ||
"fmt" | ||
|
2 changes: 1 addition & 1 deletion
2
pkg/kubernetes/pod/containers_test.go → pkg/kubernetes/podhelper/containers_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pod | ||
package podhelper | ||
|
||
import ( | ||
"testing" | ||
|
2 changes: 1 addition & 1 deletion
2
pkg/kubernetes/pod/errors.go → pkg/kubernetes/podhelper/errors.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pod | ||
package podhelper | ||
|
||
import ( | ||
"errors" | ||
|
2 changes: 1 addition & 1 deletion
2
pkg/kubernetes/pod/events.go → pkg/kubernetes/podhelper/events.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pod | ||
package podhelper | ||
|
||
import ( | ||
"context" | ||
|
2 changes: 1 addition & 1 deletion
2
pkg/kubernetes/pod/labels.go → pkg/kubernetes/podhelper/labels.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pod | ||
package podhelper | ||
|
||
import ( | ||
"fmt" | ||
|
2 changes: 1 addition & 1 deletion
2
pkg/kubernetes/pod/labels_test.go → pkg/kubernetes/podhelper/labels_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pod | ||
package podhelper | ||
|
||
import ( | ||
"testing" | ||
|
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,7 @@ | ||
package podhelper | ||
|
||
import ( | ||
"github.com/openservicemesh/osm-health/pkg/logger" | ||
) | ||
|
||
var log = logger.New("osm-health/kubernetes/pod") |
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,67 @@ | ||
package osm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
|
||
"github.com/openservicemesh/osm-health/pkg/common" | ||
"github.com/openservicemesh/osm-health/pkg/kubernetes/podhelper" | ||
"github.com/openservicemesh/osm/pkg/constants" | ||
) | ||
|
||
// Verify interface compliance | ||
var _ common.Runnable = (*NoBadOsmControllerLogsCheck)(nil) | ||
|
||
// NoBadOsmControllerLogsCheck implements common.Runnable | ||
type NoBadOsmControllerLogsCheck struct { | ||
client kubernetes.Interface | ||
osmControlPlaneNamespace string | ||
} | ||
|
||
// HasNoBadOsmControllerLogsCheck checks whether the osm controller pods in the controller namespace have bad (fatal/error/warning/fail) log messages | ||
func HasNoBadOsmControllerLogsCheck(client kubernetes.Interface, osmControlPlaneNamespace string) NoBadOsmControllerLogsCheck { | ||
return NoBadOsmControllerLogsCheck{ | ||
client: client, | ||
osmControlPlaneNamespace: osmControlPlaneNamespace, | ||
} | ||
} | ||
|
||
// Info implements common.Runnable | ||
func (check NoBadOsmControllerLogsCheck) Info() string { | ||
return fmt.Sprintf("Checking whether namespace %s has bad (fatal/error/warning/fail) logs in osm controller pods", check.osmControlPlaneNamespace) | ||
} | ||
|
||
// Run implements common.Runnable | ||
func (check NoBadOsmControllerLogsCheck) Run() error { | ||
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"app": constants.OSMControllerName}} | ||
listOptions := metav1.ListOptions{ | ||
LabelSelector: labels.Set(labelSelector.MatchLabels).String(), | ||
} | ||
pods, err := check.client.CoreV1().Pods(check.osmControlPlaneNamespace).List(context.TODO(), listOptions) | ||
if err != nil { | ||
return fmt.Errorf("unable to list osm controller pods in namespace %s", check.osmControlPlaneNamespace) | ||
} | ||
|
||
for i := range pods.Items { | ||
if err := podhelper.HasNoBadLogs(check.client, &pods.Items[i], "osm-controller"); err != nil { | ||
return err // TODO since we can have multiple osm-controller pods, should we return err on the first controller with bad logs? | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Suggestion implements common.Runnable. | ||
func (check NoBadOsmControllerLogsCheck) Suggestion() string { | ||
panic("implement me") | ||
} | ||
|
||
// FixIt implements common.Runnable. | ||
func (check NoBadOsmControllerLogsCheck) FixIt() error { | ||
panic("implement me") | ||
} |
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