GithubHelp home page GithubHelp logo

isabella232 / operator-boilerplate-legacy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openshift/operator-boilerplate-legacy

0.0 0.0 0.0 37 KB

Fork of monis.app/go "Kitchen sink for Go libraries".

Home Page: https://monis.app/go

License: Apache License 2.0

Go 100.00%

operator-boilerplate-legacy's Introduction

Operator Boilerplate (Legacy)

Operator Boilerplate is a tiny library containing a few helpers to make writing an operator or custom controller easier by abstracting some of the repetitive mechanisms common to most controllers.

Install

go get github.com/openshift/operator-boilerplate

Use

The pattern for use is something like this:

// starter.go

runner := example.NewExampleController(
   // pass in informers, clients, etc needed to create your controller
)

go runner.Run(ctx.Done())


// example_controller.go
import "github.com/openshift/operator-boilerplate/openshift/operator"

type ExampleController struct {
    // clients,
    // informers,
    // etc
}

func NewExampleController(/* client,etc */) operator.Runner {
   c := ExampleController{
      // client, etc
   }

   optionalFilter :=operator.FilterByNames("example")

   return operator.New("example-controller", c,
      operator.WithInformer(someInformer.Resource(), optionalFilter),
      // other filters
   )
}

func(c *ExampleController) Key(metav1.Object, error) {
  // the resource most important to your controller can act as the key
  return c.exampleClient.Get("example", metav1.GetOptions{})
}

func(c *ExampleController) Sync(obj metav1.Object) error {
    startTime := time.Now()
	klog.V(4).Infof("started syncing operator %q (%v)", obj.GetName(), startTime)
	defer klog.V(4).Infof("finished syncing operator %q (%v)", obj.GetName(), time.Since(startTime))

    // cast the key to the appropriate resource type
    exampleConfig := obj.(*examplev1.Example)

    if err := c.handleSync(exampleConfig); err != nil {
		return err
	}

	return nil
}


func(c *ExampleController) handleSync(config *examplev1.Example) {
    // if the controller responds to management state:
	switch config.Spec.ManagementState {
	case operatorsv1.Managed:
		klog.V(4).Infoln("example is in a managed state: syncing resources")
	case operatorsv1.Unmanaged:
		klog.V(4).Infoln("example is in an unmanaged state: skipping sync")
		return nil
	case operatorsv1.Removed:
		klog.V(4).Infoln("example is in a removed state: deleting resources")
		return c.deleteExampleResources()
	default:
		return fmt.Errorf("example is in an unknown state: %v", updatedOperatorConfig.Spec.ManagementState)
	}

    // if passed management state checks, handle sync loop here
    err := c.SyncExampleResources(config)
    // update your aggregated status
    HandleDegraded(c.exampleConfigClient, "ExampleType", "ExampleReason", true||false)

    // bubble up errors
    if err != nil {
       return err
    }
}

Existing

There are several operators currently using this library:

operator-boilerplate-legacy's People

Contributors

benjaminapetersen avatar enj avatar marun avatar sttts 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.