Skip to content

Commit

Permalink
Fix issue with object getting failure (#15)
Browse files Browse the repository at this point in the history
* Fix issue with object getting failure

* Cosmetics

* Build image in PR
  • Loading branch information
undera authored Jul 4, 2023
1 parent 1871841 commit 802228a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:

- name: Build and push
uses: docker/build-push-action@v4
if: github.event_name != 'pull_request'
# if: github.event_name != 'pull_request'
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
Expand Down
43 changes: 32 additions & 11 deletions pkg/backend/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,24 @@ type ConditionedObject interface {
resource.Conditioned
}

type ManagedUnstructured struct { // no dedicated type for it in base CP
type ManagedUnstructured struct { // no dedicated type for it in base CP, just resource.Managed interface
uxres.Unstructured
}

func NewManagedUnstructured() *ManagedUnstructured {
res := ManagedUnstructured{
Unstructured: *uxres.New(),
}

res.Object["spec"] = map[string]interface{}{
"providerConfigRef": map[string]interface{}{
"name": "",
},
}

return &res
}

func (m *ManagedUnstructured) GetProviderConfigReference() *xpv1.Reference {
// TODO: find a way to organize code better
// TODO: check field existence
Expand Down Expand Up @@ -234,8 +248,8 @@ func (c *Controller) GetClaim(ec echo.Context) error {
claimRef := v12.ObjectReference{Namespace: ec.Param("namespace"), Name: ec.Param("name")}
claimRef.SetGroupVersionKind(gvk)

claim := uclaim.Unstructured{}
err := c.getDynamicResource(&claimRef, &claim)
claim := uclaim.New()
err := c.getDynamicResource(&claimRef, claim)
if err != nil {
return err
}
Expand All @@ -249,10 +263,10 @@ func (c *Controller) GetClaim(ec echo.Context) error {

MRs := []*ManagedUnstructured{}
for _, mrRef := range xr.GetResourceReferences() {
mr := ManagedUnstructured{}
_ = c.getDynamicResource(&mrRef, &mr)
mr := NewManagedUnstructured()
_ = c.getDynamicResource(&mrRef, mr)

MRs = append(MRs, &mr)
MRs = append(MRs, mr)
}
claim.Object["managedResources"] = MRs

Expand Down Expand Up @@ -296,6 +310,13 @@ func (c *Controller) getDynamicResource(ref *v12.ObjectReference, res Conditione
res.SetNamespace(ref.Namespace)
res.SetName(ref.Name)

// we need to guarantee for frontend that "metadata" is in structure
if r, ok := res.(*uxres.Unstructured); ok {
if _, ok := r.Object["metadata"]; !ok {
r.Object["metadata"] = map[string]interface{}{}
}
}

return err
}

Expand Down Expand Up @@ -346,8 +367,8 @@ func (c *Controller) GetManaged(ec echo.Context) error {
ref := v12.ObjectReference{Name: ec.Param("name")}
ref.SetGroupVersionKind(gvk)

xr := ManagedUnstructured{}
err := c.getDynamicResource(&ref, &xr)
xr := NewManagedUnstructured()
err := c.getDynamicResource(&ref, xr)
if err != nil {
return err
}
Expand Down Expand Up @@ -493,10 +514,10 @@ func (c *Controller) GetComposite(ec echo.Context) error {
// MR refs
MRs := []*ManagedUnstructured{}
for _, mrRef := range xr.GetResourceReferences() {
mr := ManagedUnstructured{}
_ = c.getDynamicResource(&mrRef, &mr)
mr := NewManagedUnstructured()
_ = c.getDynamicResource(&mrRef, mr)

MRs = append(MRs, &mr)
MRs = append(MRs, mr)
}
xr.Object["managedResources"] = MRs
}
Expand Down

0 comments on commit 802228a

Please sign in to comment.