selector

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package selector 实现 k8s label selector 语法的解析与匹配。 所有 label 值按字符串处理,无序关系;操作符:= != in notin Exists(!)DoesNotExist。

与 k8s 的差异(刻意收敛):in/notin 的操作符前后必须各有一个空格, 即只接受 "key in (a,b)" 这种写法,不接受 "key in(a,b)"。

新增操作符时需同步改三处:常量块、parse 的分支、match 的 switch。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchAny

func MatchAny(ss []Selector, labels map[string]string) bool

MatchAny 判定 labels 是否命中任一 selector(OR)。

func ValidateLabelKey

func ValidateLabelKey(k string) error

ValidateLabelKey 校验 label key 的字符集,供生成期(mcpgen)与解析期共用同一套规则。 语法规则的所有权在本包:调用方不要另行实现,否则规则会漂移出「能标注但永远匹配不上」的输入。

func ValidateLabelValue

func ValidateLabelValue(v string) error

ValidateLabelValue 校验 label value 的字符集,与 ValidateLabelKey 同理。

Types

type Op

type Op string

Op 是单个条件的操作符。

const (
	OpEqual    Op = "="
	OpNotEqual Op = "!="
	OpIn       Op = "in"
	OpNotIn    Op = "notin"
	// OpExists 没有对应的字面语法 token,裸 key(如 "risk")即表示存在性判定。
	OpExists   Op = "exists"
	OpNotExist Op = "!"
)

支持的操作符集合。除 OpExists 外,常量值都是表达式里的真实字面 token, 会被直接打进解析错误信息,改动即改动对外错误文案。

type Requirement

type Requirement struct {
	Key    string
	Op     Op
	Values []string
}

Requirement 是一个条件,如 risk in (low,high)。

type Selector

type Selector struct {
	// Raw 是解析前的原始表达式,供基座做错误回显与启动日志打印生效规则,勿删。
	Raw          string
	Requirements []Requirement
	// contains filtered or unexported fields
}

Selector 是若干条件的 AND 组合。 零值非法:必须经 Parse 构造,直接用 Selector{} 会被 Match 判为不匹配。

func Parse

func Parse(expr string) (Selector, error)

Parse 解析 selector 字符串;语法错误返回 error。 空串按 k8s 语义解析为「匹配一切」,不是错误。 出错时返回零值 Selector(不匹配任何 labels),确保调用方跳过坏规则时是 fail-closed。

func (Selector) Match

func (s Selector) Match(labels map[string]string) bool

Match 判定一组 labels 是否满足全部条件(AND)。 沿用 k8s 缺失语义:!= 与 notin 对不存在该 key 的对象也算匹配成功; 空 selector(Parse(""))匹配一切。 无条件且非空 selector 说明它没经 Parse 构造,按非法态 fail-closed 返回 false。

func (Selector) MatchesEverything added in v0.6.0

func (s Selector) MatchesEverything() bool

MatchesEverything 返回该 selector 是否为空 selector(匹配一切),供调用方在启动日志里 声明「当前管辖全部对象」——那与「管辖某一小撮」的运维含义相差很大。

Jump to

Keyboard shortcuts

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