Skip to content

LeaderElection NewFromKubeconfig support "Functional Options" pattern #1403

Open
@ivankatliarchuk

Description

Currently if we would like to have an automated cleanup of leases, the only way is to

The code is here https://github.com/kubernetes/client-go/blob/ce24f3c462e8e98af9d79453327a77ffdc4a8b0e/tools/leaderelection/resourcelock/interface.go#L104C2-L111C3

The proposed functionality is to add something like below, this should not break current clients, or add one more

pseudo code

func NewFromKubeconfig(...., opts ...LeaseLockOptions) {
}
// or
func NewFromKubeconfigOpts(...., opts ...LeaseLockOptions) {
}

type LeaseLockOption func(d *LeaseLock) error

func ObjectMeta(m ObjectMeta) LeaseLockOption {
	return func(l *LeaseLock) error {
		l.LeaseMeta = m
		return nil
	}
} 

func Labels(m Labels) LeaseLockOption {
	return func(l *LeaseLock) error {
		l.Labels = m
		return nil
	}
}

// If ALL objects in the list have been deleted, this object will be garbage collected
func OwnerReferences(m OwnerReferences) LeaseLockOption {
	return func(l *LeaseLock) error {
		l.OwnerReferences = m
		return nil
	}
}
..

I'm happy to submit a pull request if it make sense.
A bit more explanation https://golang.cafe/blog/golang-functional-options-pattern.html

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions