refresh

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

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

Go to latest
Published: Feb 25, 2022 License: Apache-2.0 Imports: 11 Imported by: 1

README

resource-refresher

resource-refresher makes it easy to handle resources with parent-child relationships.

How to use

Let's consider the process of creating a child resource from a parent resource.

    // some process to create child resource 
    
	if _, err := controllerutil.CreateOrUpdate(context.TODO(), r.Client, copiedDeploy, func() error {
		copiedDeploy.Labels = child.labels
		copiedDeploy.Annotations = child.annotations
		copiedDeploy.Spec = child.spec

		// In order to support Update, set controller reference here
		return controllerutil.SetControllerReference(instance, copiedDeploy, r.Scheme)
	}); err != nil {
		return ctrl.Result{}, errors.WithStack(err)
	}

In this case, it is tedious to calculate the difference between the existing resources in Reconciler.

The resource-refresher will fill in the cluster and differences if you pass it the ideal resource state.

	ref := refresh.New(r.client, r.scheme)
    if err := ref.Refresh(ctx, &parent, &child); err != nil {
        return errors.WithStack(err)
    }
}

Read more about the architecture using resource-refresher: Go design theory learned from Kubernetes (in Japanese)

Installation

Run go get github.com/wantedly.com/resource-refresher and import like

import (
    refresh "github.com/wantedly/resource-refresher"
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder interface {
	Build(ctx context.Context) (Lister, error)
}

Builder is responsible for creating on Application

Builder - collect information - should not contain any logic regarding generating objects

type Lister

type Lister interface {
	GenerateLists() []ObjectList
}

Lister is responsible for generating ObjectList for each resource

Lister - generate objects - should not contain any logic accessing API servers

type ObjectList

type ObjectList struct {
	Items            []client.Object
	GroupVersionKind schema.GroupVersionKind
	Identity         func(client.Object) (string, error)
}

type Refresher

type Refresher interface {
	// Refresh syncs objects
	//
	// each object will be processed as described below
	// - when it doesn't exist, it will be created
	// - when the object exists, it will be updated
	//
	// Also an object that matches all the conditions below will be deleted
	// - owned by parent
	// - is not present in list
	//
	// when return value from Identify of two objects are the same they are considered to be a same object
	Refresh(ctx context.Context, parent client.Object, list ObjectList) error
}

Refresher reconciles objects by creating, updating, and deleting

func New

func New(client client.Client, scheme *runtime.Scheme) Refresher

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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