Skip to content

Commit

Permalink
Add cutsomResourceDefinition trackability
Browse files Browse the repository at this point in the history
  • Loading branch information
britaniar committed Dec 2, 2024
1 parent 0d2abd5 commit 5c9ae37
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/controllers/work/apply_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.uber.org/atomic"
appv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -448,6 +449,9 @@ func trackResourceAvailability(gvr schema.GroupVersionResource, curObj *unstruct
case utils.ServiceGVR:
return trackServiceAvailability(curObj)

case utils.CustomResourceDefinitionGVR:
return trackCRDAvailability(curObj)

default:
if isDataResource(gvr) {
klog.V(2).InfoS("Data resources are available immediately", "gvr", gvr, "resource", klog.KObj(curObj))
Expand All @@ -458,6 +462,29 @@ func trackResourceAvailability(gvr schema.GroupVersionResource, curObj *unstruct
}
}

func trackCRDAvailability(curObj *unstructured.Unstructured) (ApplyAction, error) {
var crd apiextensionsv1.CustomResourceDefinition
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(curObj.Object, &crd); err != nil {
return errorApplyAction, controller.NewUnexpectedBehaviorError(err)
}
// Check if there is a condition of type "Established"
var establishedCondition *apiextensionsv1.CustomResourceDefinitionCondition
for _, condition := range crd.Status.Conditions {
if condition.Type == apiextensionsv1.Established {
establishedCondition = &condition

Check failure on line 474 in pkg/controllers/work/apply_controller.go

View workflow job for this annotation

GitHub Actions / Lint

G601: Implicit memory aliasing in for loop. (gosec)
break
}
}

// If the condition is found, and it is True, the CRD is available
if establishedCondition != nil && establishedCondition.Status == apiextensionsv1.ConditionTrue {
klog.V(2).InfoS("CustomResourceDefinition is available", "customResourceDefinition", klog.KObj(curObj))
return manifestAvailableAction, nil
}
klog.V(2).InfoS("Still need to wait for CustomResourceDefinition to be available", "customResourceDefinition", klog.KObj(curObj))
return manifestNotAvailableYetAction, nil
}

func trackDeploymentAvailability(curObj *unstructured.Unstructured) (ApplyAction, error) {
var deployment appv1.Deployment
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(curObj.Object, &deployment); err != nil {
Expand Down
128 changes: 128 additions & 0 deletions pkg/controllers/work/apply_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,134 @@ func TestTrackResourceAvailability(t *testing.T) {
expected: manifestNotTrackableAction,
err: nil,
},
"Test CustomResourceDefinition available": {
gvr: utils.CustomResourceDefinitionGVR,
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "CustomResourceDefinition",
"metadata": map[string]interface{}{
"name": "testresources.example.com",
},
"spec": map[string]interface{}{
"group": "example.com",
"names": map[string]interface{}{
"plural": "testresources",
"singular": "testresource",
"kind": "TestResource",
"shortNames": []string{"tr"},
},
"scope": "Namespaced",
"versions": []interface{}{
map[string]interface{}{
"name": "v1",
"served": true,
"storage": true,
"schema": map[string]interface{}{
"openAPIV3Schema": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"spec": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"field": map[string]interface{}{
"type": "string",
},
},
},
},
},
},
},
},
},
"status": map[string]interface{}{
"conditions": []interface{}{
map[string]interface{}{
"type": "Established",
"status": "True",
"lastTransitionTime": metav1.Now(),
"reason": "CRDEstablished",
"message": "The CustomResourceDefinition has been established.",
},
map[string]interface{}{
"type": "NamesAccepted",
"status": "True",
"lastTransitionTime": metav1.Now(),
"reason": "NoConflicts",
"message": "The CustomResourceDefinition name was accepted.",
},
},
},
},
},
expected: manifestAvailableAction,
err: nil,
},
"Test CustomResourceDefinition unavailable": {
gvr: utils.CustomResourceDefinitionGVR,
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "CustomResourceDefinition",
"metadata": map[string]interface{}{
"name": "testresources.example.com",
},
"spec": map[string]interface{}{
"group": "example.com",
"names": map[string]interface{}{
"plural": "testresources",
"singular": "testresource",
"kind": "TestResource",
"shortNames": []string{"tr"},
},
"scope": "Namespaced",
"versions": []interface{}{
map[string]interface{}{
"name": "v1",
"served": true,
"storage": true,
"schema": map[string]interface{}{
"openAPIV3Schema": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"spec": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"field": map[string]interface{}{
"type": "string",
},
},
},
},
},
},
},
},
},
"status": map[string]interface{}{
"conditions": []interface{}{
map[string]interface{}{
"type": "Established",
"status": "False",
"lastTransitionTime": metav1.Now(),
"reason": "CRDEstablished",
"message": "The CustomResourceDefinition has been established.",
},
map[string]interface{}{
"type": "NamesAccepted",
"status": "True",
"lastTransitionTime": metav1.Now(),
"reason": "NoConflicts",
"message": "The CustomResourceDefinition name was accepted.",
},
},
},
},
},
expected: manifestNotAvailableYetAction,
err: nil,
},
}

for name, tt := range tests {
Expand Down
6 changes: 6 additions & 0 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ var (
Kind: "CustomResourceDefinition",
}

CustomResourceDefinitionGVR = schema.GroupVersionResource{
Group: apiextensionsv1.SchemeGroupVersion.Group,
Version: apiextensionsv1.SchemeGroupVersion.Version,
Resource: "customresourcedefinitions",
}

EndpointSliceExportMetaGVK = metav1.GroupVersionKind{
Group: fleetnetworkingv1alpha1.GroupVersion.Group,
Version: fleetnetworkingv1alpha1.GroupVersion.Version,
Expand Down

0 comments on commit 5c9ae37

Please sign in to comment.