Skip to content

Commit 429b968

Browse files
authored
Use proper plural names for XRDs (#27)
Fixes #25
1 parent 209a8c0 commit 429b968

File tree

6 files changed

+77
-16
lines changed

6 files changed

+77
-16
lines changed

Contributing.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
## Finding What to Contribute
44

5-
[Roadmap](Roadmap.md) document contains some notes and ideas on what can be done to improve the project. Those may be not very clear, that's how OSS ideas start. Raise the question in Slack about particular item that attracked your attention to get more explanation.
5+
[Roadmap](Roadmap.md) document contains some notes and ideas on what can be done to improve the project. Those may be not very clear, that's how OSS ideas start.
6+
7+
Another good source of small tasks is to search through code for `TODO` and `FIXME` comments.
8+
9+
Raise the question in Slack about particular item that attracked your attention to get more explanation.
610

711
## Setting Up For Local Development
812

@@ -12,4 +16,6 @@ First you need to build frontend (the API backend won't compile without that). T
1216

1317
To build API backend, run `go build -o bin/komoplane .` in the project root.
1418

15-
For frontend development, start API backend with command `bin/komoplane`, then run `npm run dev` inside `pkg/frontend`. Open the suggested URL in your browser.
19+
For frontend development, start API backend with command `bin/komoplane`, then run `npm run dev` inside `pkg/frontend`. Open the suggested URL in your browser.
20+
21+
You can then restart/debug backend without restarting frontend.

examples/issue25.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
apiVersion: apiextensions.crossplane.io/v1
3+
kind: CompositeResourceDefinition
4+
metadata:
5+
name: aks.foo.com
6+
spec:
7+
group: foo.com
8+
names:
9+
kind: Aks
10+
listKind: AksList
11+
plural: aks
12+
singular: aks
13+
versions:
14+
- name: v1alpha1
15+
served: true
16+
referenceable: true
17+
schema:
18+
openAPIV3Schema:
19+
type: object
20+
---
21+
apiVersion: foo.com/v1alpha1
22+
kind: Aks # Direct XR-style
23+
metadata:
24+
name: reproduce-issue-25
25+
spec: {}

pkg/backend/controller.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,12 @@ func (c *Controller) GetComposite(ec echo.Context) error {
509509

510510
// composition ref
511511
compRef := xr.GetCompositionReference()
512-
compRef.SetGroupVersionKind(v13.CompositionGroupVersionKind)
513-
514-
comp := uxres.New()
515-
_ = c.getDynamicResource(compRef, comp)
516-
xr.Object["composition"] = comp
512+
if compRef != nil {
513+
compRef.SetGroupVersionKind(v13.CompositionGroupVersionKind)
514+
comp := uxres.New()
515+
_ = c.getDynamicResource(compRef, comp)
516+
xr.Object["composition"] = comp
517+
}
517518

518519
// MR refs
519520
MRs := []*ManagedUnstructured{}
@@ -558,7 +559,7 @@ func NewController(ctx context.Context, cfg *rest.Config, ns string, version str
558559
ExtV1: ext,
559560
Events: evt,
560561
apiExt: apiExt,
561-
CRDs: crossplane.NewCRDsClient(cfg),
562+
CRDs: crossplane.NewCRDsClient(cfg, ext),
562563
StatusInfo: StatusInfo{
563564
CurVer: version,
564565
},

pkg/backend/crossplane/crds.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package crossplane
33
import (
44
"context"
55
"github.com/crossplane/crossplane-runtime/pkg/resource"
6+
"github.com/komodorio/komoplane/pkg/backend/utils"
7+
log "github.com/sirupsen/logrus"
68
v1 "k8s.io/api/core/v1"
79
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
810
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -16,7 +18,8 @@ type CRDInterface interface {
1618
}
1719

1820
type crdClient struct {
19-
cfg *rest.Config
21+
cfg *rest.Config
22+
ExtV1 *ExtensionsV1Client
2023
}
2124

2225
func (c *crdClient) Get(ctx context.Context, result resource.Object, ref *v1.ObjectReference) error {
@@ -27,6 +30,11 @@ func (c *crdClient) Get(ctx context.Context, result resource.Object, ref *v1.Obj
2730
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
2831
config.UserAgent = rest.DefaultKubernetesUserAgent()
2932

33+
plural, err := c.getPluralKind(ctx, ref)
34+
if err != nil {
35+
return err
36+
}
37+
3038
client, err := rest.RESTClientFor(&config)
3139
if err != nil {
3240
return err
@@ -35,7 +43,7 @@ func (c *crdClient) Get(ctx context.Context, result resource.Object, ref *v1.Obj
3543
err = client.
3644
Get().
3745
NamespaceIfScoped(ref.Namespace, ref.Namespace != "").Name(ref.Name).
38-
Resource(ref.Kind + "s"). // TODO: better way to pluralize?
46+
Resource(plural).
3947
Do(ctx).
4048
Into(result)
4149

@@ -64,8 +72,25 @@ func (c *crdClient) List(ctx context.Context, gvk schema.GroupVersionKind) (*uns
6472
return &result, err
6573
}
6674

67-
func NewCRDsClient(cfg *rest.Config) CRDInterface {
75+
func (c *crdClient) getPluralKind(ctx context.Context, ref *v1.ObjectReference) (string, error) {
76+
xrds, err := c.ExtV1.XRDs().List(ctx)
77+
if err != nil {
78+
return "", err
79+
}
80+
81+
for _, xrd := range xrds.Items {
82+
if xrd.Spec.Names.Kind == ref.Kind && xrd.Spec.Group == ref.GroupVersionKind().Group {
83+
return xrd.Spec.Names.Plural, nil
84+
}
85+
}
86+
87+
log.Debugf("Could not find plural for kind '%s', defaulted to -s suffix", ref.Kind)
88+
return utils.Plural(ref.Kind), nil // poor man's fallback
89+
}
90+
91+
func NewCRDsClient(cfg *rest.Config, ext *ExtensionsV1Client) CRDInterface {
6892
return &crdClient{
69-
cfg: cfg,
93+
cfg: cfg,
94+
ExtV1: ext,
7095
}
7196
}

pkg/frontend/src/components/CompositeResourcesList.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ type ItemProps = {
1818

1919
function ListItem({item, onItemClick}: ItemProps) {
2020
return (
21-
<Grid item xs={12} md={12} key={item.metadata.name} onClick={() => {
21+
<Grid item xs={12} md={12} key={item.apiVersion + item.kind + item.metadata.name} onClick={() => {
2222
onItemClick(item)
2323
}}>
2424
<Card variant="outlined" className="cursor-pointer">
2525
<CardContent>
2626
<Typography variant="h6">{item.metadata.name}</Typography>
2727
<Typography variant="body1">Kind: {item.kind}</Typography>
2828
<Typography variant="body1">Group: {item.apiVersion}</Typography>
29-
<Typography variant="body1">Composition: {item.spec.compositionRef.name}</Typography>
30-
<Typography variant="body1">Composed resources: {item.spec.resourceRefs.length}</Typography>
29+
<Typography variant="body1">Composition: {item.spec.compositionRef?.name}</Typography>
30+
<Typography variant="body1">Composed resources: {item.spec.resourceRefs?.length}</Typography>
3131
<ReadySynced status={item.status}></ReadySynced>
3232
</CardContent>
3333
</Card>

pkg/frontend/src/components/graph/data.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class GraphData {
3030
id: (++this.id).toString(),
3131
type: ntype,
3232
data: {
33-
label: res.metadata.name,
33+
label: res?.metadata.name,
3434
status: status[0],
3535
statusMsg: status[1],
3636
main: isMain,
@@ -79,6 +79,10 @@ export class GraphData {
7979

8080
private getStatus(res: K8sResource): [NodeStatus, string] {
8181
logger.log("get status from", res)
82+
if (!res) {
83+
return [NodeStatus.NotFound, "Not Specified"]
84+
}
85+
8286
const problems: { [key: string]: string } = {}
8387

8488
res.status?.conditions?.forEach((element) => {

0 commit comments

Comments
 (0)