reconcile

package module
v0.0.0-...-c1b1401 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2022 License: MIT Imports: 1 Imported by: 0

README

go-reconcile

Super tiny go package which does reconcile planning: taking desired list and current observed list and returning the action to take to reach desired states (the items adding to or deleting from the current list).

This can be used, for example, when you want to enforce only desired members to have some cloud IAM role. You can ask your cloud provider API about the current members which has the target roles for now. Then you can prepare the desired list of members to have the role and, by reconcile planning, you can get which member to add to or delete. Based on this, you can call cloud provider API and reach to the desired state.

I wrote this package to play with Go1.18 generics functinality and you should not use this in your code. I recommend you to write your own if you need similar functionality.

NOTE: To run this code, you need to use Go1.18 or later

Documentation is available on GoDoc: https://pkg.golang.ir/github.com/tcnksm/go-reconcile

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action[T comparable] struct {
	// The list of items to add to the current list.
	Adds []T
	// The list of items to delete from the current list.
	Deletes []T
}

Action is results returned by Plan function. It contains the items to add to or delete from the current observed list to reach to the desired state.

func Plan

func Plan[V comparable](desireds []V, currents []V) *Action[V]

Plan takes desired lists and current observed lists and decideds what action to take (which item to add to or delete from the current list) to reach to the given desired list. The results are returns as Action.

Example (Int)
// The list of the item you desire to have.
desireds := []int{1, 2, 3}

// The list of the item currently you observes.
currents := []int{1, 2, 4, 5}

actions := Plan(desireds, currents)
fmt.Printf("Add: %v, Delete: %v", actions.Adds, actions.Deletes)
Output:

Add: [3], Delete: [4 5]
Example (String)
// The list of the item you desire to have.
desireds := []string{"alice", "bob", "charlie"}

// The list of the item currently you observes.
currents := []string{"alice", "bob", "dan", "frank"}

actions := Plan(desireds, currents)
fmt.Printf("Add: %v, Delete: %v", actions.Adds, actions.Deletes)
Output:

Add: [charlie], Delete: [dan frank]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL