Skip to content

Commit

Permalink
Fetch resource metadata from unstructured
Browse files Browse the repository at this point in the history
In sink.createResource, we need access to resource metadata such as
name, namespace, apiVersion, and kind from the raw json template. We
were using gjson.GetBytes to do this. However, later in the function
we were also unmarshalling the raw template to
`unstructured.Unstructured` which already has methods for accessing
all of the required metadata.

This commit refactors the createResource function to first umarshall
the raw template to unstructured and then get all of the metadata we
need from that object.  This was also the only place where we were
using gjson so, as a side effect we can now remove our dependency on
the GJson library.

Signed-off-by: Dibyo Mukherjee <dibyo@google.com>
  • Loading branch information
dibyom authored and tekton-robot committed Dec 11, 2019
1 parent 3c645a9 commit 5347b05
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 273 deletions.
1 change: 0 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 21 additions & 19 deletions pkg/sink/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/tektoncd/triggers/pkg/interceptors"

"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -163,43 +162,46 @@ func (r Sink) createResources(resources []json.RawMessage, triggerName, eventID
// createResource uses the kubeClient to create the resource defined in the
// TriggerResourceTemplate and returns any errors with this process
func (r Sink) createResource(rt json.RawMessage, triggerName string, eventID string) error {
// Add common labels
rt, err := addLabels(rt, map[string]string{
triggersv1.EventListenerLabelKey: r.EventListenerName,
triggersv1.EventIDLabelKey: eventID,
triggersv1.TriggerLabelKey: triggerName,
})
if err != nil {
return err
}

// Assume the TriggerResourceTemplate is valid (it has an apiVersion and Kind)
apiVersion := gjson.GetBytes(rt, "apiVersion").String()
kind := gjson.GetBytes(rt, "kind").String()
namespace := gjson.GetBytes(rt, "metadata.namespace").String()
data := new(unstructured.Unstructured)
if err := data.UnmarshalJSON(rt); err != nil {
return err
}

namespace := data.GetNamespace()
// Default the resource creation to the EventListenerNamespace if not found in the resource template
if namespace == "" {
namespace = r.EventListenerNamespace
}

// Resolve resource kind to the underlying API Resource type.
apiResource, err := r.findAPIResource(apiVersion, kind)
apiResource, err := r.findAPIResource(data.GetAPIVersion(), data.GetKind())
if err != nil {
return err
}

rt, err = addLabels(rt, map[string]string{
triggersv1.EventListenerLabelKey: r.EventListenerName,
triggersv1.EventIDLabelKey: eventID,
triggersv1.TriggerLabelKey: triggerName,
})
if err != nil {
return err
name := data.GetName()
if name == "" {
name = data.GetGenerateName()
}

resourcename := gjson.GetBytes(rt, "metadata.name")
r.Logger.Infof("Generating resource: kind: %+v, name: %s", apiResource, resourcename)
r.Logger.Infof("Generating resource: kind: %+v, name: %s", apiResource, name)

gvr := schema.GroupVersionResource{
Group: apiResource.Group,
Version: apiResource.Version,
Resource: apiResource.Name,
}

data := new(unstructured.Unstructured)
if err := data.UnmarshalJSON(rt); err != nil {
return err
}
_, err = r.DynamicClient.Resource(gvr).Namespace(namespace).Create(data, metav1.CreateOptions{})
return err
}
Expand Down
Loading

0 comments on commit 5347b05

Please sign in to comment.