-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_ready.go
42 lines (37 loc) · 1.06 KB
/
resource_ready.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
"kmodules.xyz/client-go/discovery"
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
)
var grCRD = schema.GroupResource{
Group: "apiextensions.k8s.io",
Resource: "customresourcedefinitions",
}
func IsResourceExistsAndReady(ctx context.Context, dc dynamic.Interface, mapper discovery.ResourceMapper, gvr schema.GroupVersionResource) (bool, error) {
exists, err := mapper.ExistsGVR(gvr)
if gvr.GroupResource() != grCRD {
return exists, err
}
crdGVR, err := mapper.Preferred(gvr)
if err != nil {
return false, err
}
obj, err := dc.Resource(crdGVR).Get(ctx, gvr.GroupResource().String(), metav1.GetOptions{})
if err != nil {
return false, err
}
s, err := status.Compute(obj)
if err != nil {
return false, err
}
if s.Status != status.CurrentStatus {
// How to surface this back to the Flow CR?
klog.Warningf("gvr %v status %+v", gvr, s)
}
return s.Status == status.CurrentStatus, nil
}