Created
May 22, 2023 13:35
-
-
Save huseyinbabal/d77da408436b72341a6cb1e02a1e6c7e to your computer and use it in GitHub Desktop.
Keptn Consume Events
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
// source.go | |
package keptn | |
const pollPeriodInSeconds = 5 | |
... | |
func (p *Source) consumeEvents(ctx context.Context, cfg Config, ch chan<- source.Event) { | |
keptn, err := NewClient(cfg.URL, cfg.Token) | |
log := loggerx.New(cfg.Log) | |
exitOnError(err, log) | |
for { | |
req := GetEventsRequest{ | |
Project: cfg.Project, | |
FromTime: time.Now().Add(-time.Second * pollPeriodInSeconds), | |
} | |
res, err := keptn.Events(ctx, &req) | |
if err != nil { | |
log.Errorf("failed to get events. %v", err) | |
} | |
for _, event := range res { | |
textFields := []api.TextField{ | |
{Key: "Source", Value: PluginName}, | |
{Key: "Type", Value: event.Type}, | |
} | |
if event.Data.Status != "" { | |
textFields = append(textFields, api.TextField{Key: "State", Value: event.Data.Status}) | |
} | |
var bulletLists []api.BulletList | |
if event.Data.Message != "" { | |
bulletLists = []api.BulletList{ | |
{ | |
Title: "Description", | |
Items: []string{ | |
event.Data.Message, | |
}, | |
}, | |
} | |
} | |
msg := api.Message{ | |
Type: api.NonInteractiveSingleSection, | |
Timestamp: time.Now(), | |
Sections: []api.Section{ | |
{ | |
TextFields: textFields, | |
BulletLists: bulletLists, | |
}, | |
}, | |
} | |
ch <- source.Event{ | |
Message: msg, | |
RawObject: event, | |
} | |
} | |
// Fetch events periodically with given frequency | |
time.Sleep(time.Second * pollPeriodInSeconds) | |
} | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment