k8s

package
v0.65.3 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: BSD-3-Clause, AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package k8s provides a lightweight Kubernetes API client for coordination Leases. It uses raw HTTP calls against the mounted service account credentials, avoiding a dependency on client-go.

Index

Constants

This section is empty.

Variables

View Source
var ErrConflict = errors.New("conflict: resource version mismatch")

ErrConflict is returned when a Lease update fails due to a resourceVersion mismatch (another writer updated the object first).

Functions

func InCluster

func InCluster() bool

InCluster reports whether the process is running inside a Kubernetes pod by checking for the KUBERNETES_SERVICE_HOST environment variable.

func LeaseNameForDomain

func LeaseNameForDomain(domain string) string

LeaseNameForDomain returns a deterministic, DNS-label-safe Lease name for the given domain. The domain is hashed to avoid dots and length issues.

Types

type Lease

type Lease struct {
	APIVersion string        `json:"apiVersion"`
	Kind       string        `json:"kind"`
	Metadata   LeaseMetadata `json:"metadata"`
	Spec       LeaseSpec     `json:"spec"`
}

Lease represents a coordination.k8s.io/v1 Lease object with only the fields needed for distributed locking.

type LeaseClient

type LeaseClient struct {
	// contains filtered or unexported fields
}

LeaseClient talks to the Kubernetes coordination API using raw HTTP.

func NewLeaseClient

func NewLeaseClient() (*LeaseClient, error)

NewLeaseClient creates a client that authenticates via the pod's mounted service account. It reads the namespace and CA certificate at construction time (they don't rotate) but reads the bearer token fresh on each request (tokens rotate).

func (*LeaseClient) Create

func (c *LeaseClient) Create(ctx context.Context, lease *Lease) (*Lease, error)

Create creates a new Lease. Returns the created Lease with server-assigned fields like resourceVersion populated.

func (*LeaseClient) Get

func (c *LeaseClient) Get(ctx context.Context, name string) (*Lease, error)

Get retrieves a Lease by name. Returns (nil, nil) if the Lease does not exist.

func (*LeaseClient) Namespace

func (c *LeaseClient) Namespace() string

Namespace returns the namespace this client operates in.

func (*LeaseClient) Update

func (c *LeaseClient) Update(ctx context.Context, lease *Lease) (*Lease, error)

Update replaces a Lease. The lease.Metadata.ResourceVersion must match the current server value (optimistic concurrency). Returns ErrConflict on version mismatch.

type LeaseMetadata

type LeaseMetadata struct {
	Name            string            `json:"name"`
	Namespace       string            `json:"namespace,omitempty"`
	ResourceVersion string            `json:"resourceVersion,omitempty"`
	Annotations     map[string]string `json:"annotations,omitempty"`
}

LeaseMetadata holds the standard k8s object metadata fields used by Leases.

type LeaseSpec

type LeaseSpec struct {
	HolderIdentity       *string    `json:"holderIdentity"`
	LeaseDurationSeconds *int32     `json:"leaseDurationSeconds,omitempty"`
	AcquireTime          *MicroTime `json:"acquireTime"`
	RenewTime            *MicroTime `json:"renewTime"`
}

LeaseSpec holds the Lease specification fields.

type MicroTime

type MicroTime struct {
	time.Time
}

MicroTime wraps time.Time with Kubernetes MicroTime JSON formatting.

func (*MicroTime) MarshalJSON

func (t *MicroTime) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler with k8s MicroTime format.

func (*MicroTime) UnmarshalJSON

func (t *MicroTime) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler with k8s MicroTime format.

Jump to

Keyboard shortcuts

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