Coder Social home page Coder Social logo

keptn-go-utils's Introduction

Keptn go-utils

GitHub release (latest by date) tests Go Report Card

This repo serves as a util package for common functionalities such as logging of the Keptn Project.

Please post any issues with this package to the keptn/keptn repository and label them with area:go-utils.

Installation

Get the latest version using

go get github.com/keptn/go-utils

Also consider browsing our Releases Page to find out about all releases.

Contributing

If you want to contribute, just create a PR on the master branch.

Please also see CONTRIBUTING.md instructions on how to contribute.

Usage

Below we have listed some basic examples. You can find more information about the usage within the docs/ folder.

Utils

If you need to access several utility functions:

import {
  "github.com/keptn/go-utils/pkg/lib"
}

This module provides you with a convenient Keptn helper struct that allows you to access several resources that are relevant within the context of a Keptn event. The helper struct can be initialized by passing a CloudEvent to the NewKeptn function. Example:

func HandleEvent(event cloudevents.Event) error {
	keptnHandler, err := keptn.NewKeptn(&event, keptn.KeptnOpts{})
	if err != nil {
		return nil, err
	}
	
    // get the shipyard file of the project
    shipyard, _ := keptnHandler.GetShipyard()
    
    // get a resource within the current context (i.e., project, stage, service) of the event
    resourceContent, _ := keptnHandler.GetKeptnResource("resource.yaml")

    // send a cloud event
    _ = keptnHandler.SendCloudEvent(event)
    // ...
}

By default, the SendCloudEvent function of the Keptn struct will send events to the distributor sidecar that is running within the same pod of the Keptn service (see the Keptn doc for more details). This behavior can be overridden by passing an implementation of the EventSender interface via the KeptnOpts object that is passed to the NewKeptn function, e.g.:

// custom EventSender

type MyCustomEventSender struct {
}

func (es *MyCustomEventSender) SendEvent(event cloudevents.Event) error {
    // custom implementation
    return nil
}
//...
keptnHandler, err := keptn.NewKeptn(&event, keptn.KeptnOpts{
    EventSender: &MyCustomEventSender{}
})
if err != nil {
    return nil, err
}

For unit testing purposes, we offer a mock implementation of the EventSender interface in the keptn/v0_2_0/fake package. This mock implementation can be used to check if your service sends the expected events. E.g.:

func MyTest(t *testing.T) {
    fakeSender := &keptnfake.EventSender{}

    // optionally, you can add custom behavior for certain event types (e.g. returning an error for a certain event type):
    fakeSender.AddReactor("sh.keptn.event.deployment.finished", func(event cloudevents.Event) error {
        return errors.New("i throw an error if i should send a 'sh.keptn.event.deployment.finished' event")
    })

    keptnHandler, err := keptn.NewKeptn(&event, keptn.KeptnOpts{
        EventSender: fakeSender
    })

    myService := &MyKeptnService{
        KeptnHandler: keptnHandler
    }
    
    myService.handleEvent(myReceivedCloudEvent)
    
    // check if your service sends out events of the type "sh.keptn.event.deployment.started" and "sh.keptn.event.deployment.finished"
    if err := fakeSender.AssertSentEventTypes([]string{"sh.keptn.event.deployment.started", "sh.keptn.event.deployment.finished"}); err != nil {
        t.Errorf("%s", err.Error())
    }

    // to inspect the sent events in more detail, you can access them via fakeSender.SentEvents
    for _, event := range fakeSender.SentEvents {
        // do some validation here
    }

    if err != nil {
        return nil, err
    }
}

CloudEvent Data

If you need to access data within CloudEvents:

import {
	"github.com/keptn/go-utils/pkg/lib"
)

Example:

func parseCloudEvent(event cloudevents.Event) (keptnevents.TestFinishedEventData, error) {
	eventData := &keptn.TestsFinishedEventData{}
	err := event.DataAs(eventData)
    
    return eventData, err
}

Models

If you need to access Models for YAML files:

import {
	"github.com/keptn/go-utils/pkg/lib"
)

Querying Events from event store

// Create an event Handler
eventHandler := apiutils.NewAuthenticatedEventHandler("1.2.3.4/api", token, "x-token", nil, "http")

// Create a filter
filter := &apiutils.EventFilter{KeptnContext:  "03b2b951-9835-4e87-b0b0-0ad0bc288214"}

// Query for event(s)
events, err := eventHandler.GetEvents(filter)

Watching for events in event store

// Create a watcher
watcher := api.NewEventWatcher(eventhandler),
	api.WithEventFilter(api.EventFilter{  // use custom filter
           Project: "sockshop",
           KeptnContext: "..."}),
	api.WithInterval(time.NewTicker(5*time.Second)), // fetch every 5 seconds
	api.WithStartTime(time.Now()),                  // start fetching events newer than this timestamp
	api.WithTimeout(time.Second * 15),             // stop fetching events after 15 secs
)

    // start watcher and consume events
	allEvents, _ := watcher.Watch(context.Background())
	for events := range allEvents {
		for _, e := range events {
			fmt.Println(*e.Type)
		}
	}

Updating model definitions

After updating the model definitions in the swagger.yaml file, execute the command

swagger generate model --spec=swagger.yaml -t=./pkg/api/

to update the models located in ./pkg/api/models

Automation

A Github Action is used that creates a Pull Request to github.com/keptn/keptn to update go.mod files with an updated version of this package (based on the commit hash).

keptn-go-utils's People

Contributors

agrimmer avatar bacherfl avatar checkelmann avatar christian-kreuzberger-dtx avatar dependabot[bot] avatar grabnerandi avatar johannes-b avatar tannergabriel avatar thschue avatar warber avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.