Skip to content

Commit

Permalink
Merge pull request #437 from meshery/utils
Browse files Browse the repository at this point in the history
Add generic utility functions for channels and for retrieving GVR.
  • Loading branch information
MUzairS15 authored Dec 24, 2023
2 parents 0878150 + 5b38e86 commit 803e2d5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
54 changes: 54 additions & 0 deletions utils/kubernetes/crd.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
9 changes: 9 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 803e2d5

Please sign in to comment.