feat: resource customization for CustomResourceDefinition (#21416)

Signed-off-by: Almo Elda <almogldbh@gmail.com>
This commit is contained in:
almoelda 2025-01-17 18:02:13 +02:00 committed by GitHub
parent d765aabc1f
commit 3e09f946f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 499 additions and 0 deletions

View file

@ -0,0 +1,82 @@
local hs = {}
-- Check if CRD is terminating
if obj.metadata.deletionTimestamp ~= nil then
hs.status = "Progressing"
hs.message = "CRD is terminating"
return hs
end
if obj.status.conditions == nil then
hs.status = "Progressing"
hs.message = "Status conditions not found"
return hs
end
if #obj.status.conditions == 0 then
hs.status = "Progressing"
hs.message = "Status conditions not found"
return hs
end
local isEstablished
local isTerminating
local namesNotAccepted
local hasViolations
local conditionMsg = ""
for _, condition in pairs(obj.status.conditions) do
-- Check if CRD is terminating
if condition.type == "Terminating" and condition.status == "True" then
isTerminating = true
conditionMsg = condition.message
end
-- Check if K8s has accepted names for this CRD
if condition.type == "NamesAccepted" and condition.status == "False" then
namesNotAccepted = true
conditionMsg = condition.message
end
-- Checking if CRD is established
if condition.type == "Established" and condition.status == "True" then
isEstablished = true
conditionMsg = condition.message
end
-- Checking if CRD has violations
if condition.type == "NonStructuralSchema" and condition.status == "True" then
hasViolations = true
conditionMsg = condition.message
end
end
if isTerminating then
hs.status = "Progressing"
hs.message = "CRD is terminating: " .. conditionMsg
return hs
end
if namesNotAccepted then
hs.status = "Degraded"
hs.message = "CRD names have not been accepted: " .. conditionMsg
return hs
end
if not isEstablished then
hs.status = "Degraded"
hs.message = "CRD is not established"
return hs
end
if hasViolations then
hs.status = "Degraded"
hs.message = "Schema violations found: " .. conditionMsg
return hs
end
hs.status = "Healthy"
hs.message = "CRD is healthy"
return hs

View file

@ -0,0 +1,29 @@
tests:
- healthStatus:
status: Healthy
message: "CRD is healthy"
inputPath: testdata/crd-v1-healthy.yaml
- healthStatus:
status: Degraded
message: "CRD names have not been accepted: the initial names have not been accepted"
inputPath: testdata/crd-v1-names-not-accepted-degraded.yaml
- healthStatus:
status: Degraded
message: "Schema violations found: spec.preserveUnknownFields: Invalid value: true: must be false"
inputPath: testdata/crd-v1-non-structual-degraded.yaml
- healthStatus:
status: Degraded
message: "CRD is not established"
inputPath: testdata/crd-v1-not-established-degraded.yaml
- healthStatus:
status: Progressing
message: "CRD is terminating: user has deleted the CRD"
inputPath: testdata/crd-v1-terminating-condition-progressing.yaml
- healthStatus:
status: Progressing
message: "CRD is terminating"
inputPath: testdata/crd-v1-terminating-timestamp-progressing.yaml
- healthStatus:
status: Progressing
message: "Status conditions not found"
inputPath: testdata/crd-v1-no-conditions-progressing.yaml

View file

@ -0,0 +1,56 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.example.io
spec:
conversion:
strategy: None
group: example.io
names:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
preserveUnknownFields: true
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: >-
CreationTimestamp is a timestamp representing the server time when
this object was created. It is not guaranteed to be set in
happens-before order across separate operations. Clients may not set
this value. It is represented in RFC3339 form and is in UTC.
Populated by the system. Read-only. Null for lists. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
conditions:
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: no conflicts found
reason: NoConflicts
status: 'True'
type: NamesAccepted
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: the initial names have been accepted
reason: InitialNamesAccepted
status: 'True'
type: Established
storedVersions:
- v1alpha1

View file

@ -0,0 +1,56 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.example.io
spec:
conversion:
strategy: None
group: example.io
names:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
preserveUnknownFields: true
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: >-
CreationTimestamp is a timestamp representing the server time when
this object was created. It is not guaranteed to be set in
happens-before order across separate operations. Clients may not set
this value. It is represented in RFC3339 form and is in UTC.
Populated by the system. Read-only. Null for lists. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
conditions:
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: the initial names have not been accepted
reason: NoConflicts
status: 'False'
type: NamesAccepted
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: the initial names have been accepted
reason: InitialNamesAccepted
status: 'False'
type: Established
storedVersions:
- v1alpha1

View file

@ -0,0 +1,46 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.example.io
spec:
conversion:
strategy: None
group: example.io
names:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
preserveUnknownFields: true
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: >-
CreationTimestamp is a timestamp representing the server time when
this object was created. It is not guaranteed to be set in
happens-before order across separate operations. Clients may not set
this value. It is represented in RFC3339 form and is in UTC.
Populated by the system. Read-only. Null for lists. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
conditions: []
storedVersions:
- v1alpha1

View file

@ -0,0 +1,61 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.example.io
spec:
conversion:
strategy: None
group: example.io
names:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
preserveUnknownFields: true
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: >-
CreationTimestamp is a timestamp representing the server time when
this object was created. It is not guaranteed to be set in
happens-before order across separate operations. Clients may not set
this value. It is represented in RFC3339 form and is in UTC.
Populated by the system. Read-only. Null for lists. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
conditions:
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: no conflicts found
reason: NoConflicts
status: 'True'
type: NamesAccepted
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: the initial names have been accepted
reason: InitialNamesAccepted
status: 'True'
type: Established
- lastTransitionTime: '2024-10-26T19:44:57Z'
message: 'spec.preserveUnknownFields: Invalid value: true: must be false'
reason: Violations
status: 'True'
type: NonStructuralSchema
storedVersions:
- v1alpha1

View file

@ -0,0 +1,56 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.example.io
spec:
conversion:
strategy: None
group: example.io
names:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
preserveUnknownFields: true
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: >-
CreationTimestamp is a timestamp representing the server time when
this object was created. It is not guaranteed to be set in
happens-before order across separate operations. Clients may not set
this value. It is represented in RFC3339 form and is in UTC.
Populated by the system. Read-only. Null for lists. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
conditions:
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: no conflicts found
reason: NoConflicts
status: 'True'
type: NamesAccepted
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: the initial names have not been accepted
reason: InitialNamesAccepted
status: 'False'
type: Established
storedVersions:
- v1alpha1

View file

@ -0,0 +1,56 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.example.io
spec:
conversion:
strategy: None
group: example.io
names:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
preserveUnknownFields: true
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: >-
CreationTimestamp is a timestamp representing the server time when
this object was created. It is not guaranteed to be set in
happens-before order across separate operations. Clients may not set
this value. It is represented in RFC3339 form and is in UTC.
Populated by the system. Read-only. Null for lists. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
conditions:
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: no conflicts found
reason: NoConflicts
status: 'True'
type: NamesAccepted
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: user has deleted the CRD
reason: terminating
status: 'True'
type: Terminating
storedVersions:
- v1alpha1

View file

@ -0,0 +1,57 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.example.io
deletionTimestamp: '2024-12-28T13:51:19Z'
spec:
conversion:
strategy: None
group: example.io
names:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
preserveUnknownFields: true
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: >-
CreationTimestamp is a timestamp representing the server time when
this object was created. It is not guaranteed to be set in
happens-before order across separate operations. Clients may not set
this value. It is represented in RFC3339 form and is in UTC.
Populated by the system. Read-only. Null for lists. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: Example
listKind: ExampleList
plural: examples
shortNames:
- ex
singular: example
conditions:
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: no conflicts found
reason: NoConflicts
status: 'True'
type: NamesAccepted
- lastTransitionTime: '2024-05-19T23:35:28Z'
message: the initial names have been accepted
reason: InitialNamesAccepted
status: 'True'
type: Established
storedVersions:
- v1alpha1