selector

package
v0.5.0 Latest Latest
Warning

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

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

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
}

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

func Parse

func Parse(expr string) (Selector, error)

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

func (Selector) Match

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

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

Jump to

Keyboard shortcuts

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