-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding CRD health customization and tests for getCustomResourceDefini…
…tionHealth Signed-off-by: Almo Elda <[email protected]>
- Loading branch information
Showing
8 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package health | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/argoproj/gitops-engine/pkg/utils/kube" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func getCustomResourceDefinitionHealth(obj *unstructured.Unstructured) (*HealthStatus, error) { | ||
gvk := obj.GroupVersionKind() | ||
switch gvk { | ||
case apiextensionsv1.SchemeGroupVersion.WithKind(kube.CustomResourceDefinitionKind): | ||
var crd apiextensionsv1.CustomResourceDefinition | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &crd) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert unstructured CustomResourceDefinition to typed: %v", err) | ||
} | ||
return getApiExtenstionsV1CustomResourceDefinitionHealth(&crd) | ||
default: | ||
return nil, fmt.Errorf("unsupported CustomResourceDefinition GVK: %s", gvk) | ||
} | ||
} | ||
|
||
func getApiExtenstionsV1CustomResourceDefinitionHealth(crd *apiextensionsv1.CustomResourceDefinition) (*HealthStatus, error) { | ||
|
||
if crd.Status.Conditions == nil || crd.Status.Conditions != nil && len(crd.Status.Conditions) == 0 { | ||
return &HealthStatus{ | ||
Status: HealthStatusProgressing, | ||
Message: "Status conditions not found", | ||
}, nil | ||
} | ||
|
||
var ( | ||
isEstablished bool | ||
isTerminating bool | ||
namesNotAccepted bool | ||
hasViolations bool | ||
conditionMsg string | ||
) | ||
|
||
// Check conditions | ||
for _, condition := range crd.Status.Conditions { | ||
switch condition.Type { | ||
case apiextensionsv1.Terminating: | ||
if condition.Status == apiextensionsv1.ConditionTrue { | ||
isTerminating = true | ||
conditionMsg = condition.Message | ||
} | ||
case apiextensionsv1.NamesAccepted: | ||
if condition.Status == apiextensionsv1.ConditionFalse { | ||
namesNotAccepted = true | ||
conditionMsg = condition.Message | ||
} | ||
case apiextensionsv1.Established: | ||
if condition.Status == apiextensionsv1.ConditionTrue { | ||
isEstablished = true | ||
} else { | ||
conditionMsg = condition.Message | ||
} | ||
case apiextensionsv1.NonStructuralSchema: | ||
if condition.Status == apiextensionsv1.ConditionTrue { | ||
hasViolations = true | ||
conditionMsg = condition.Message | ||
} | ||
} | ||
} | ||
|
||
// Return appropriate health status | ||
switch { | ||
case isTerminating: | ||
return &HealthStatus{ | ||
Status: HealthStatusProgressing, | ||
Message: fmt.Sprintf("CRD is being terminated: %s", conditionMsg), | ||
}, nil | ||
case namesNotAccepted: | ||
return &HealthStatus{ | ||
Status: HealthStatusDegraded, | ||
Message: fmt.Sprintf("CRD names have not been accepted: %s", conditionMsg), | ||
}, nil | ||
case !isEstablished: | ||
return &HealthStatus{ | ||
Status: HealthStatusDegraded, | ||
Message: fmt.Sprintf("CRD is not established: %s", conditionMsg), | ||
}, nil | ||
case hasViolations: | ||
return &HealthStatus{ | ||
Status: HealthStatusDegraded, | ||
Message: fmt.Sprintf("Schema violations found: %s", conditionMsg), | ||
}, nil | ||
default: | ||
return &HealthStatus{ | ||
Status: HealthStatusHealthy, | ||
Message: "CRD is healthy", | ||
}, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: 'False' | ||
type: NamesAccepted | ||
- lastTransitionTime: '2024-05-19T23:35:28Z' | ||
message: the initial names have been accepted | ||
reason: InitialNamesAccepted | ||
status: 'False' | ||
type: Established | ||
storedVersions: | ||
- v1alpha1 |
Oops, something went wrong.