service

package
v0.0.0-...-a212bfd Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2019 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package service provides functions for manipulating Service object in Kubernetes cluster.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(c Conf) (reconcile.Result, error)

Create generates the Service as per the `Conf` struct passed and creates it in the cluster

Example
package main

import (
	"log"

	"github.com/ankitrgadiya/operatorlib/pkg/interfaces"
	"github.com/ankitrgadiya/operatorlib/pkg/service"

	corev1 "k8s.io/api/core/v1"
)

var ownerObject interfaces.Object
var ownerReconcile interfaces.Reconcile

func main() {
	result, err := service.Create(service.Conf{
		// Instance is the pointer to owner object under which
		// Service is being created.
		Instance: ownerObject,
		// OwnerReference can be used to tell if owner reference is
		// required to set on the Service object.
		OwnerReference: true,
		// Reconcile is the reconcile struct of the owner object which
		// implements the interfaces.Reconcile struct. For more
		// details check Reconcile interface documentation.
		Reconcile: ownerReconcile,
		// Name is the name of generated Service. There are several
		// options defines in service.Conf which can be used to
		// manipulate ObjectMeta of the generated object.
		Name: "cm-test",
		Type: "ClusterIP",
		// GenServicePortsFunc is the function that generates list of
		// service ports for the Service. This can be anonymous
		// function like this or can be defined somewhere else and
		// just pass the name of the function here. Check service.Conf
		// struct for other such funtions.
		GenServicePortsFunc: func(interfaces.Object) ([]corev1.ServicePort, error) {
			return []corev1.ServicePort{{Port: int32(80)}}, nil
		},
	})
	if err != nil {
		log.Fatal(result, err)
	}
}
Output:

func CreateOrUpdate

func CreateOrUpdate(c Conf) (reconcile.Result, error)

CreateOrUpdate is a combination of `Create` and `Update` functions. It creates the Service object if it is not already in the cluster and updates the Service if one exists.

func Delete

func Delete(c Conf) (reconcile.Result, error)

Delete generates the ObjectMeta for Service as per the `Conf` struct passed and deletes it from the cluster

func GenerateService

func GenerateService(c Conf) (s *corev1.Service, err error)

GenerateService generates Service object as per the `Conf` struct passed. The function only supports a subset of all the Service features but should be good enough for most usecases. Support for ExternalService or LoadBalancer service is not there.

func MaybeUpdate

func MaybeUpdate(original interfaces.Object, new interfaces.Object) (bool, error)

MaybeUpdate is the implementation of operation.MaybeUpdateFunc for Service object. It compares two service objects and update the first one if required. Note however that this does not compare both services exhaustively since some fields can also be filled by the API Server. If those are also compared here that everytime this function is called it will always update/remove those fields. Also, service type is immutable and cannot be updated so it returns error if that is detected.

func Update

func Update(c Conf) (reconcile.Result, error)

Update generates the Service as per the `Conf` struct passed and compares it with the in-cluster version. If required, it updates the in-cluster Service with the changes.

Types

type Conf

type Conf struct {
	// Instance is the Owner object which manages the Service
	Instance interfaces.Object
	// Reconcile is the pointer to reconcile struct of owner object
	interfaces.Reconcile
	// Name of the Service
	Name string
	// Namespace of the Service
	Namespace string
	// GenLabelsFunc is used to generate labels for ObjectMeta
	meta.GenLabelsFunc
	// GenAnnotationsFunc is used to generate annotations for
	// ObjectMeta
	meta.GenAnnotationsFunc
	// GenFinalizers is used to generate finalizers for ObjectMeta
	meta.GenFinalizersFunc
	// AppendLabels is used to determine if labels from Owner object
	// are to be inherited
	AppendLabels bool
	// OwnerReference is used to determine if owner reference needs to
	// be set on Service before creating it in cluster
	OwnerReference bool
	// MaybeUpdateFunc defines an update function with custom logic
	// for Service update
	operation.MaybeUpdateFunc
	// AfterCreateFunc hook is called after creating the Service
	operation.AfterCreateFunc
	// AfterUpdateFunc hook is called after updating the Service
	operation.AfterUpdateFunc
	// AfterDeleteFunc hook is called after deleting the Service
	operation.AfterDeleteFunc
	// GenServiceFunc defines a function to generate the Service
	// object. The package comes with default service generator
	// function which is used by operation functions. By specifying
	// this field, user can override the default function with a
	// custom one.
	GenServiceFunc
	// GenServicePortsFunc defines a function to generate ports for
	// the Service
	GenServicePortsFunc
	// GenSelectorFunc defines a function to generate selector for the
	// Service
	GenSelectorFunc
	// Type defines the type of Service object to be created
	Type string
}

Conf is used to pass parameters to functions in this package to perform operations on Service objects.

type GenSelectorFunc

type GenSelectorFunc func(interfaces.Object) (map[string]string, error)

GenSelectorFunc defines a function which generates selectors for the Service

type GenServiceFunc

type GenServiceFunc func(Conf) (*corev1.Service, error)

GenServiceFunc defines a function which generates Service

type GenServicePortsFunc

type GenServicePortsFunc func(interfaces.Object) ([]corev1.ServicePort, error)

GenServicePortsFunc defines a function which generates slice of ServicePort for Service

Jump to

Keyboard shortcuts

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