diff --git a/go.mod b/go.mod index 952966b2..6a0c8720 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ replace ( github.com/googleapis/gnostic => github.com/googleapis/gnostic v0.5.5 github.com/jaguilar/vt100 => github.com/tonistiigi/vt100 v0.0.0-20190402012908-ad4c4a574305 github.com/kubernetes/kompose => github.com/meshery/kompose v1.0.1 + oras.land/oras-go v1.2.4 => oras.land/oras-go v1.2.3 ) require ( diff --git a/go.sum b/go.sum index d17e78cd..b90b9bf2 100644 --- a/go.sum +++ b/go.sum @@ -1408,8 +1408,8 @@ k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/ k8s.io/utils v0.0.0-20211116205334-6203023598ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk= k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= -oras.land/oras-go v1.2.4 h1:djpBY2/2Cs1PV87GSJlxv4voajVOMZxqqtq9AB8YNvY= -oras.land/oras-go v1.2.4/go.mod h1:DYcGfb3YF1nKjcezfX2SNlDAeQFKSXmf+qrFmrh4324= +oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY= +oras.land/oras-go v1.2.3/go.mod h1:M/uaPdYklze0Vf3AakfarnpoEckvw0ESbRdN8Z1vdJg= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/utils/kubernetes/crd.go b/utils/kubernetes/crd.go new file mode 100644 index 00000000..05b6f6ee --- /dev/null +++ b/utils/kubernetes/crd.go @@ -0,0 +1,54 @@ +package kubernetes + +import ( + "context" + + "github.com/layer5io/meshkit/utils" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/rest" +) + +type CRD struct { + Items []CRDItem `json:"items"` +} + +type CRDItem struct { + Spec Spec `json:"spec"` +} + +type Spec struct { + Names names `json:"names"` + Group string `json:"group"` + Versions []struct{ + Name string `json:"name"` + } `json:"versions"` +} + +type names struct { + ResourceName string `json:"plural"` +} + +func GetAllCustomResourcesInCluster(ctx context.Context, client rest.Interface) ([]*schema.GroupVersionResource, error) { + crdresult, err := client.Get().RequestURI("/apis/apiextensions.k8s.io/v1/customresourcedefinitions").Do(context.Background()).Raw() + if err != nil { + return nil, err + } + var xcrd CRD + gvks := []*schema.GroupVersionResource{} + err = utils.Unmarshal(string(crdresult), &xcrd) + if err != nil { + return nil, err + } + for _, c := range xcrd.Items { + gvks = append(gvks, GetGVRForCustomResources(&c)) + } + return gvks, nil +} + +func GetGVRForCustomResources(crd *CRDItem) *schema.GroupVersionResource { + return &schema.GroupVersionResource{ + Group: crd.Spec.Group, + Version: crd.Spec.Versions[0].Name, + Resource: crd.Spec.Names.ResourceName, + } +} \ No newline at end of file diff --git a/utils/utils.go b/utils/utils.go index 0ae44d41..6767a1ca 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -267,3 +267,12 @@ func MarshalAndUnmarshal[fromType any, toType any](val fromType) (unmarshalledva } return } + +func IsClosed[K any](ch chan K) bool { + select { + case <- ch: + return true + default: + return false + } +} \ No newline at end of file