-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature for syncing container logs
- Loading branch information
Showing
6 changed files
with
453 additions
and
38 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,19 @@ | ||
package schema | ||
|
||
// | ||
//import ( | ||
// "github.com/icinga/icinga-kubernetes/pkg/contracts" | ||
// kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
//) | ||
// | ||
//type Container struct { | ||
// kmetaWithNamespace | ||
//} | ||
// | ||
//func NewContainer() contracts.Resource { | ||
// return &Container{} | ||
//} | ||
// | ||
//func (c *Container) Obtain(kobject kmetav1.Object) { | ||
// c.kmetaWithNamespace.Obtain(kobject) | ||
//} |
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,10 @@ | ||
package schema | ||
|
||
type Log struct { | ||
kmetaWithoutNamespace | ||
Id []byte | ||
ReferenceId []byte | ||
ContainerName string | ||
Time string | ||
Log string | ||
} |
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,235 @@ | ||
package sync | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"crypto/sha1" | ||
"fmt" | ||
"github.com/icinga/icinga-go-library/database" | ||
"github.com/icinga/icinga-go-library/logging" | ||
"github.com/icinga/icinga-kubernetes/pkg/contracts" | ||
"github.com/icinga/icinga-kubernetes/pkg/schema" | ||
"github.com/pkg/errors" | ||
"golang.org/x/sync/errgroup" | ||
"io" | ||
kcorev1 "k8s.io/api/core/v1" | ||
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"slices" | ||
"strings" | ||
msync "sync" | ||
"time" | ||
) | ||
|
||
type LogSync struct { | ||
list []*kcorev1.Pod | ||
lastChecked map[[20]byte]*kmetav1.Time | ||
mutex *msync.RWMutex | ||
clientset *kubernetes.Clientset | ||
db *database.DB | ||
logger *logging.Logger | ||
} | ||
|
||
func NewLogSync(clientset *kubernetes.Clientset, db *database.DB, logger *logging.Logger) *LogSync { | ||
return &LogSync{ | ||
list: []*kcorev1.Pod{}, | ||
lastChecked: make(map[[20]byte]*kmetav1.Time), | ||
mutex: &msync.RWMutex{}, | ||
clientset: clientset, | ||
db: db, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (ls *LogSync) splitTimestampsFromMessages(log []byte, curContainerId [20]byte) (times []string, messages []string, err error) { | ||
|
||
stringReader := strings.NewReader(string(log)) | ||
reader := bufio.NewReader(stringReader) | ||
|
||
for { | ||
line, err := reader.ReadString('\n') | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return nil, nil, errors.Wrap(err, "error reading log message") | ||
} | ||
|
||
messageTime, err := time.Parse("2006-01-02T15:04:05.999999999Z", strings.Split(line, " ")[0]) | ||
if err != nil { | ||
logging.Fatal(errors.Wrap(err, "error parsing log timestamp")) | ||
continue | ||
} | ||
|
||
if ls.lastChecked[curContainerId] != nil && messageTime.UnixNano() <= ls.lastChecked[curContainerId].UnixNano() { | ||
continue | ||
} | ||
|
||
times = append(times, strings.Split(line, " ")[0]) | ||
messages = append(messages, strings.Join(strings.Split(line, " ")[1:], " ")) | ||
} | ||
|
||
return times, messages, nil | ||
} | ||
|
||
func (ls *LogSync) removeFromList(id database.ID) { | ||
out := make([]*kcorev1.Pod, 0) | ||
|
||
for _, element := range ls.list { | ||
|
||
elementId := sha1.Sum([]byte(element.Namespace + "/" + element.Name)) | ||
|
||
if fmt.Sprintf("%x", elementId) != id.String() { | ||
out = append(out, element) | ||
} | ||
} | ||
|
||
ls.list = out | ||
} | ||
|
||
func (ls *LogSync) MaintainList(ctx context.Context, addChannel <-chan contracts.KUpsert, deleteChannel <-chan contracts.KDelete) error { | ||
|
||
ls.logger.Info("Starting maintain list") | ||
|
||
g, ctx := errgroup.WithContext(ctx) | ||
|
||
deletes := make(chan any) | ||
defer close(deletes) | ||
|
||
g.Go(func() error { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return errors.Wrap(ctx.Err(), "context canceled maintain log sync list") | ||
|
||
case podFromChannel, more := <-addChannel: | ||
if !more { | ||
return nil | ||
} | ||
|
||
pod := podFromChannel.KObject().(*kcorev1.Pod) | ||
|
||
podIsInList := false | ||
|
||
for _, listPod := range ls.list { | ||
if listPod.UID == pod.UID { | ||
podIsInList = true | ||
} | ||
} | ||
|
||
if podIsInList { | ||
continue | ||
} | ||
|
||
ls.mutex.RLock() | ||
ls.list = append(ls.list, pod) | ||
ls.mutex.RUnlock() | ||
|
||
case podIdFromChannel, more := <-deleteChannel: | ||
if !more { | ||
return nil | ||
} | ||
|
||
idOfPod := podIdFromChannel.ID() | ||
|
||
ls.mutex.RLock() | ||
ls.removeFromList(idOfPod) | ||
ls.mutex.RUnlock() | ||
|
||
deletes <- idOfPod | ||
} | ||
|
||
} | ||
}) | ||
|
||
g.Go(func() error { | ||
return ls.db.DeleteStreamedByField(ctx, &schema.Log{}, "reference_id", deletes) | ||
}) | ||
|
||
return g.Wait() | ||
} | ||
|
||
func (ls *LogSync) Run(ctx context.Context) error { | ||
|
||
ls.logger.Info("Starting sync") | ||
|
||
g, ctx := errgroup.WithContext(ctx) | ||
|
||
upsertStmt := ls.upsertStmt() | ||
|
||
upserts := make(chan database.Entity) | ||
defer close(upserts) | ||
|
||
g.Go(func() error { | ||
for { | ||
for _, pod := range ls.list { | ||
|
||
curPodId := sha1.Sum([]byte(pod.Namespace + "/" + pod.Name)) | ||
|
||
for _, container := range pod.Spec.Containers { | ||
|
||
curContainerId := sha1.Sum([]byte(pod.Namespace + "/" + pod.Name + "/" + container.Name)) | ||
|
||
podLogOpts := kcorev1.PodLogOptions{Container: container.Name, Timestamps: true} | ||
|
||
if ls.lastChecked[curContainerId] != nil { | ||
podLogOpts.SinceTime = ls.lastChecked[curContainerId] | ||
} | ||
|
||
log, err := ls.clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts).Do(ctx).Raw() | ||
if err != nil { | ||
fmt.Println(errors.Wrap(err, "error reading container log")) | ||
continue | ||
} | ||
|
||
times, messages, err := ls.splitTimestampsFromMessages(log, curContainerId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(messages) == 0 { | ||
continue | ||
} | ||
|
||
newLog := &schema.Log{ | ||
Id: curContainerId[:], | ||
ReferenceId: curPodId[:], | ||
ContainerName: container.Name, | ||
Time: strings.Join(times, "\n"), | ||
Log: strings.Join(messages, "\n"), | ||
} | ||
|
||
upserts <- newLog | ||
|
||
lastTime, err := time.Parse("2006-01-02T15:04:05.999999999Z", times[len(times)-1]) | ||
if err != nil { | ||
return errors.Wrap(err, "error parsing log time") | ||
} | ||
|
||
if !slices.Contains(ls.list, pod) { | ||
continue | ||
} | ||
|
||
lastV1Time := kmetav1.Time{Time: lastTime} | ||
ls.lastChecked[curContainerId] = &lastV1Time | ||
} | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-time.After(time.Second * 5): | ||
} | ||
} | ||
}) | ||
|
||
g.Go(func() error { | ||
return ls.db.UpsertStreamedWithStatement(ctx, upserts, upsertStmt, 5) | ||
}) | ||
|
||
return g.Wait() | ||
} | ||
|
||
func (ls *LogSync) upsertStmt() string { | ||
return "INSERT INTO log (id, reference_id, container_name, time, log) VALUES (:id, :reference_id, :container_name, :time, :log) ON DUPLICATE KEY UPDATE time=CONCAT(time, '\n', :time), log=CONCAT(log, '\n', :log)" | ||
} |
Oops, something went wrong.