Skip to main content

Kubernetes Pattern Detection API Documentation

API version v1.1.0

Overview

The Kubernetes Pattern Detection API allows you to write rules to identify Patterns within the Kubernetes Cluster that might be suitable to improve performance, compliance, modularity, observability, security, and reliability.

API Specification

Pattern Schema Version

  • Version: kubepattern.it/v1
  • Format: YAML

Pattern Definition Structure

Root Level Properties

PropertyTypeRequiredDescription
versionstringYesVersion of the pattern schema (kubepattern.it/v1)
kindenumYesMust be Pattern
metadataobjectYesPattern metadata information
specobjectYesPattern specification

metadata

PropertyTypeRequiredDescription
namestringYesName of the pattern (e.g., my-pattern)
displayNamestringYesDisplay name of the pattern (e.g., My Pattern)
patternTypeenumYesType: STRUCTURAL, BEHAVIORAL, FUNCTIONAL, ...
severityenumYesSeverity level: CRITICAL, WARNING, INFO
categoryenumYesCategory: BestPractice, Security, Reliability, Performance, CostOptimization
docUrlstringNoURL to pattern documentation
gitUrlstringNoURL to Git repository containing pattern rules
descriptionstringNoBrief description of the pattern

spec

PropertyTypeRequiredDescription
messagestringYesMessage displayed when pattern is matched
topologyenumYesTopology: LEADER_FOLLOWER, SINGLE
resourcesarrayYesArray of resource definitions (minimum 1 required)
relationshipsarrayNoArray of relationship definitions between resources
minRelationshipPointsintegerNoNew: Minimum score required for required: false relationships to validate the pattern. Default: 0.

spec.resources

Each resource in the resources array must contain:

PropertyTypeRequiredDescription
resourcestringYesKubernetes resource kind (e.g., "Pod", "Service", "Deployment")
idstringYesUnique identifier for the resource within the pattern (e.g., main-container)
leaderboolean(Req Single)Specify if the resource is the leader (entry point of analysis).
filtersobjectNoFiltering conditions for this resource

spec.resources.filters

PropertyTypeRequiredDescription
matchAllarrayNoAll conditions must be met
matchAnyarrayNoAt least one condition must be met
matchNonearrayNoNone of these conditions must be met

spec.resources.filters.matchAll/Any/None[*]

Each condition within filter arrays contains:

PropertyTypeRequiredDescription
keystringYesJSONPath or key to match against
operatorenumYesComparison operator (e.g., EQUALS, CONTAINS, EXISTS)
valuesarrayConditionalValues to match against (not required for Exists/NotExists)

spec.actors

This is an array of resourceIds that cooperate in the pattern.

info

Actors must contain at least one resource for SINGLE Topology and at least two for LEADER_FOLLOWER Topology.

spec.relationships & spec.commonRelationships

Each relationship definition contains:

PropertyTypeRequiredDescription
typeenumYesRelationship type (see Supported Relationship Types)
resourceIdsarrayYesArray of resource IDs participating in this relationship
descriptionstringNoHuman-readable description
weightfloatNoPoints awarded if the relationship matches logically (used only if required: false).
requiredbooleanYesControl Flag: Determines if this is a Veto/Mandatory condition (true) or a Scoring condition (false).
sharedbooleanYesExpected State: true means the relationship MUST exist; false means it MUST NOT exist.

Pattern Matching Logic (Veto vs. Scoring)

The analysis engine uses a combination of strict boolean logic ("Veto") and point-based logic ("Scoring"), determined by the combination of the required and shared fields.

1. The Logic Matrix

The combination of required and shared defines four distinct behaviors:

requiredsharedBehaviorLogical Description
TRUETRUEMANDATORY (Must Have)The relationship MUST exist. If it is missing, the resource is immediately discarded.
(Example: A Deployment must own a ReplicaSet)
TRUEFALSEVETO (Must NOT Have)The relationship MUST NOT exist. If found, the resource is immediately discarded.
(Example: An orphan Table must NOT be connected to any Row)
FALSETRUESCORE (Positive)If the relationship exists, add weight to the total score. If missing, nothing happens (non-blocking).
(Example: "Referenced By Any": points if connected to Row OR Panel)
FALSEFALSESCORE (Negative)If the relationship does NOT exist, add weight to the total score.
(Example: Preference for loose coupling/isolation)

2. Execution Flow

For each candidate "Leader" resource, the engine performs the following steps:

  1. Veto Phase (Required Checks):

    • All relationships with required: true are verified.
    • If even a single condition fails (e.g., finding a relationship when shared: false, or missing one when shared: true), the resource is immediately filtered out.
  2. Scoring Phase (Scoring Checks):

    • If the resource passes the Veto Phase, relationships with required: false are evaluated.
    • Points (weight) are added only if the logical condition is met (Positive or Negative match as per the matrix above).
  3. Threshold Verification:

    • The accumulated total score is compared against spec.minRelationshipPoints.
    • If Total Score < minRelationshipPoints, the resource is discarded.
    • Otherwise, it is a MATCH.

Supported Relationship Types

TypeDescription / Example
OWNSDeployment -> ReplicaSet
MANAGESReplicaSet -> Pod
MOUNTSPod -> Volume
EXPOSESService -> Pod
USES_CONFIGReference to ConfigMap/Secret
SAME_NETWORKNetwork policies
IS_NAMESPACE_OFNamespace -> Generic Resource
USES_SAPod -> ServiceAccount
HAS_AFFINITY_TOPod -> Pod
REFERENCES_KRATEOTable -> Widget (Custom)

Supported Operators

OperatorDescription
EQUALSExact match
NOT_EQUALSNot equal to
GREATER_THANGreater than
GREATER_OR_EQUALGreater than or equal to
LESS_THANLess than
LESS_OR_EQUALLess than or equal to
EXISTSKey exists
NOT_EXISTSKey does not exist
IS_EMPTYIs empty

JSONPath Key Syntax

The key property supports JSONPath-like syntax for accessing Kubernetes resource properties:

Examples:

  • metadata.namespace - Access resource namespace
  • metadata.labels['app'] - Access specific label
  • metadata.ownerReferences[*].uid - Access owner reference UIDs
  • spec.containers[*].name - Access all container names
  • spec.template.spec.containers[*].image - Access container images in pod template
  • spec.volumes[*].name - Access volume names
  • spec.nodeName - Access assigned node
  • spec.hostNetwork - Access host network setting

Example Usage

Simple Sidecar Pattern

{
"version": "kubepattern.it/v1",
"kind": "Pattern",
"metadata": {
"name": "sidecar",
"displayName": "Sidecar",
"patternType": "STRUCTURAL",
"severity": "INFO",
"category": "architecture",
"description": "Identifies pods that should implement the sidecar pattern but are incorrectly separated."
},
"spec": {
"message": "Pod '{{main-app.name}}' appears to be separated from its sidecar pod '{{sidecar.name}}'.",
"topology": "LEADER_FOLLOWER",
"resources": [
{
"resource": "Pod",
"id": "main-app",
"leader": true,
"filters": {
"matchAny": [
{ "key": ".metadata.labels.app", "operator": "EQUALS", "values": ["frontend", "backend"] }
]
}
},
{
"resource": "Pod",
"id": "sidecar",
"filters": {
"matchAny": [
{ "key": ".metadata.labels.app", "operator": "EQUALS", "values": ["logging", "monitoring"] }
]
}
}
],
"actors": [ "main-app", "sidecar" ],
"commonRelationships": [
{
"id": "shared-volume-mount",
"type": "MOUNTS",
"description": "Both pods mount volumes with the same name.",
"weight": 0.6,
"required": true,
"shared": true,
"resourceIds": [ "main-app", "sidecar" ]
}
],
"minRelationshipPoints": 0
}
}