Skip to main content

KubePattern Pattern as Code

What is Pattern-as-Code?

Pattern-as-Code (PaC) is a Kubernetes Custom Resource Definition (CRD) that allows you to define metrics and patterns to recognize Kubernetes resources that deviate from architectural principles and best practices. They also are comprehensive of a remediation reverence documentation and a severity level to prioritize the most critical issues.

API Documentation

The API allows you to write rules to identify Smells and their remediation strategies within the Kubernetes Cluster.

API version

Current Pattern as Code api version: v1

Pattern Definition Structure

Root Level Properties

PropertyTypeRequiredDescription
apiVersionstringYesAPI version of the Pattern CRD (e.g., kubepattern.dev/v1)
kindstringYesAlways Pattern
metadataobjectYesStandard Kubernetes metadata (name, namespace, labels, annotations)
specobjectYesCore specification of the pattern, containing detection logic and relationships

metadata

PropertyTypeRequiredDescription
namestringYesUnique name of the pattern regex: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$ (e.g., my-pattern)

spec

PropertyTypeRequiredDescription
displayNamestringYesHuman-readable name for the pattern (e.g., My Pattern)
severityenumYesSeverity level: LOW, MEDIUM, HIGH, CRITICAL
categorystringNoCustom category for organizational purposes (e.g., architecture, security, cost)
messagestringNoCustom message template for detected Smells (supports placeholders)
reference`stringNoURL to documentation or remediation guide for the detected Smell
targetobjectYesDefinition of the primary resource to analyze
dependenciesarrayNoList of additional resources that interact with the target
relationshipsobjectNoDefinition of relationships between the target and dependent resources

target

PropertyTypeRequiredDescription
kindstringYesKubernetes resource kind (e.g., Pod, Deployment)
apiVersionstringYesAPI version of the resource (e.g., apps/v1)
pluralstringYesPlural name of the resource (e.g., pods, deployments)
filtersobjectNoCriteria to narrow down the target resources (e.g., namespace, labels)

dependencies

PropertyTypeRequiredDescription
idstringYesUnique identifier for the dependency (e.g., database)
kindstringYesKubernetes resource kind of the dependency (e.g., Service)
apiVersionstringYesAPI version of the dependency resource (e.g., v1)
pluralstringYesPlural name of the dependency resource (e.g., services)
filtersobjectNoCriteria to narrow down the dependency resources (e.g., namespace, labels)

target/dependencies.filters

PropertyTypeRequiredDescription
matchAllarrayNoList of filter conditions that must all be satisfied
matchAnyarrayNoList of filter conditions where at least one must be satisfied
matchNonearrayNoList of filter conditions that must not be satisfied

target/dependencies.filters.matchAll/matchAny/matchNone

PropertyTypeRequiredDescription
pathstringYesJSONPath to the field to evaluate (e.g., metadata.namespace)
operatorenumYesComparison operator (e.g., EQUALS, CONTAINS)
valuesarrayYesList of values to compare against (e.g., ["default", "production"])

relationships

PropertyTypeRequiredDescription
matchAllarrayNoList of relationship criteria that must all be satisfied
matchAnyarrayNoList of relationship criteria where at least one must be satisfied
matchNonearrayNoList of relationship criteria that must not be satisfied

relationships.matchAll/matchAny/matchNone

PropertyTypeRequiredDescription
withstringYesIdentifier of the dependency to evaluate the relationship against (e.g., database)
typeenumYesType of relationship to evaluate: custom, owns, ownedBy
criteriaarrayNoList of criteria to evaluate the relationship (custom logic based on resource fields)

relationships.matchAll/matchAny/matchNone.criteria (if type is custom)

PropertyTypeRequiredDescription
targetPathstringYesJSONPath to the field in the target resource to evaluate (e.g., metadata.name)
dependencyPathstringYesJSONPath to the field in the dependency resource to evaluate (e.g., metadata.name)
operatorenumYesComparison operator (e.g., EQUALS, CONTAINS)

Operators Reference

Filter Operators

OperatorDescription
EQUALSChecks if the field value is equal to any of the specified values
IS_EMPTYChecks if the field value is empty or not defined
EXISTSChecks if the field exists in the resource
GREATER_THANChecks if the field value is greater than the specified value (numeric comparison)
GREATER_OR_EQUALChecks if the field value is greater than or equal to the specified value (numeric comparison)
LESS_THANChecks if the field value is less than the specified value (numeric comparison)
LESS_OR_EQUALChecks if the field value is less than or equal to the specified value (numeric comparison)
ARRAY_SIZE_EQUALSChecks if the size of the array field is equal to the specified value
ARRAY_SIZE_GREATER_THANChecks if the size of the array field is greater than the specified value
ARRAY_SIZE_GREATER_OR_EQUALChecks if the size of the array field is greater than or equal to the specified value
ARRAY_SIZE_LESS_THANChecks if the size of the array field is less than the specified value
ARRAY_SIZE_LESS_OR_EQUALChecks if the size of the array field is less than or equal to the specified value

Criteria Operators (for custom relationships)

OperatorDescription
EQUALSChecks if the value at targetPath is equal to the value at dependencyPath

Example Pattern-as-Code Definition

apiVersion: kubepattern.dev/v1
kind: Pattern
metadata:
name: page-not-referenced
spec:
message: "Page {{target.metadata.name}} in namespace {{target.metadata.namespace}} is not managed by any NavMenuItem."
displayName: Page Not Referenced
category: Architecture
severity: HIGH
target:
kind: Page
apiVersion: widgets.templates.krateo.io/v1beta1
plural: pages
filters:
matchNone:
- path: "metadata.namespace"
operator: EQUALS
values:
- krateo-system

dependencies:
- id: navmenuitem
kind: NavMenuItem
apiVersion: widgets.templates.krateo.io/v1beta1
plural: navmenuitems

relationships:
matchNone:
- with: navmenuitem
type: custom
criteria:
- targetPath: "metadata.name"
dependencyPath: "spec.resourcesRefs.items[*].name"
operator: EQUALS
- targetPath: "metadata.namespace"
dependencyPath: "spec.resourcesRefs.items[*].namespace"
operator: EQUALS

---
apiVersion: kubepattern.dev/v1
kind: Pattern
metadata:
name: paragraph-not-referenced
spec:
message: "Paragraph {{target.metadata.name}} in namespace {{target.metadata.namespace}} is not managed by any widgets."
displayName: Paragraph Not Referenced
category: Architecture
severity: HIGH
target:
kind: Paragraph
apiVersion: widgets.templates.krateo.io/v1beta1
plural: paragraphs
filters:
matchNone:
- path: "metadata.namespace"
operator: EQUALS
values:
- krateo-system

dependencies:
- id: panel
kind: Panel
apiVersion: widgets.templates.krateo.io/v1beta1
plural: panels
- id: column
kind: Column
apiVersion: widgets.templates.krateo.io/v1beta1
plural: columns
- id: row
kind: Row
apiVersion: widgets.templates.krateo.io/v1beta1
plural: rows

relationships:
matchNone:
- with: panel
type: custom
criteria:
- targetPath: "metadata.name"
dependencyPath: "spec.resourcesRefs.items[*].name"
operator: EQUALS
- targetPath: "metadata.namespace"
dependencyPath: "spec.resourcesRefs.items[*].namespace"
operator: EQUALS
- with: column
type: custom
criteria:
- targetPath: "metadata.name"
dependencyPath: "spec.resourcesRefs.items[*].name"
operator: EQUALS
- targetPath: "metadata.namespace"
dependencyPath: "spec.resourcesRefs.items[*].namespace"
operator: EQUALS
- with: row
type: custom
criteria:
- targetPath: "metadata.name"
dependencyPath: "spec.resourcesRefs.items[*].name"
operator: EQUALS
- targetPath: "metadata.namespace"
dependencyPath: "spec.resourcesRefs.items[*].namespace"
operator: EQUALS