Skip to content

Commit

Permalink
feat: Add CustomResourceDefinition trackability (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
britaniar authored Dec 9, 2024
1 parent bfaf5ee commit 08eaa65
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/controllers/work/apply_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"go.uber.org/atomic"
appv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
apiextensionshelpers "k8s.io/apiextensions-apiserver/pkg/apihelpers"
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 +450,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 +463,22 @@ 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)
}

// If both conditions are True, the CRD is available
if apiextensionshelpers.IsCRDConditionTrue(&crd, apiextensionsv1.Established) && apiextensionshelpers.IsCRDConditionTrue(&crd, apiextensionsv1.NamesAccepted) {
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
185 changes: 185 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,191 @@ 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": "InitialNamesAccepted",
"message": "the initial names have been accepted",
},
map[string]interface{}{
"type": "NamesAccepted",
"status": "True",
"lastTransitionTime": metav1.Now(),
"reason": "NoConflicts",
"message": "no conflicts found",
},
},
},
},
},
expected: manifestAvailableAction,
err: nil,
},
"Test CustomResourceDefinition unavailable (not established)": {
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": "Installing",
"message": "the initial names have been accepted",
},
map[string]interface{}{
"type": "NamesAccepted",
"status": "True",
"lastTransitionTime": metav1.Now(),
"reason": "NoConflicts",
"message": "no conflicts found",
},
},
},
},
},
expected: manifestNotAvailableYetAction,
err: nil,
},
"Test CustomResourceDefinition unavailable (name not accepted)": {
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",
},
"status": map[string]interface{}{
"conditions": []interface{}{
map[string]interface{}{
"type": "NamesAccepted",
"status": "False",
"lastTransitionTime": metav1.Now(),
"reason": "NameConflict",
"message": "names conflict",
},
},
},
},
},
expected: manifestNotAvailableYetAction,
err: nil,
},
"Test CustomResourceDefinition unavailable (established but name not accepted)": {
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",
},
"status": map[string]interface{}{
"conditions": []interface{}{
map[string]interface{}{
"type": "Established",
"status": "True",
"lastTransitionTime": metav1.Now(),
"reason": "InitialNamesAccepted",
"message": "the initial names have been accepted",
},
map[string]interface{}{
"type": "NamesAccepted",
"status": "False",
"lastTransitionTime": metav1.Now(),
"reason": "NotAccepted",
"message": "not all names are 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 08eaa65

Please sign in to comment.