Skip to content

Commit

Permalink
Move constants to unversioned package
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyom committed May 20, 2021
1 parent 3872993 commit 814452f
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 49 deletions.
5 changes: 3 additions & 2 deletions cmd/triggerrun/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"os"

"github.com/spf13/cobra"
triggers2 "github.com/tektoncd/triggers/pkg/apis/triggers"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
triggersclientset "github.com/tektoncd/triggers/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -105,7 +106,7 @@ func trigger(triggerFile, httpPath, action, kubeconfig string, writer io.Writer)
sugerLogger := logger.Sugar()
eventID := template.UUID()
r := newSink(config, sugerLogger)
eventLog := sugerLogger.With(zap.String(triggersv1.EventIDLabelKey, eventID))
eventLog := sugerLogger.With(zap.String(triggers2.EventIDLabelKey, eventID))
for _, tri := range triggers {
resources, err := processTriggerSpec(kubeClient, triggerClient, tri,
request, body, eventID, eventLog, r)
Expand Down Expand Up @@ -193,7 +194,7 @@ func processTriggerSpec(kubeClient kubernetes.Interface, client triggersclientse
return nil, errors.New("trigger is not defined")
}

log := eventLog.With(zap.String(triggersv1.TriggerLabelKey, r.EventListenerName))
log := eventLog.With(zap.String(triggers2.TriggerLabelKey, r.EventListenerName))

finalPayload, header, iresp, err := r.ExecuteInterceptors(*tri, request, body, log, eventID)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/apis/triggers/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package triggers

const (
// GroupName is the Kubernetes resource group name for Tekton types.
GroupName = "triggers.tekton.dev"

// EventListenerLabelKey is used as the label identifier for an EventListener.
EventListenerLabelKey = "/eventlistener"

// EventIDLabelKey is used as the label identifier for an EventListener event.
EventIDLabelKey = "/triggers-eventid"

// TriggerLabelKey is used as the label identifier for a Trigger
TriggerLabelKey = "/trigger"
)
17 changes: 2 additions & 15 deletions pkg/apis/triggers/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,14 @@ limitations under the License.
package v1alpha1

import (
"github.com/tektoncd/triggers/pkg/apis/triggers"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
// GroupName is the Kubernetes resource group name for Tekton types.
GroupName = "triggers.tekton.dev"

// EventListenerLabelKey is used as the label identifier for an EventListener.
EventListenerLabelKey = "/eventlistener"

// EventIDLabelKey is used as the label identifier for an EventListener event.
EventIDLabelKey = "/triggers-eventid"

// TriggerLabelKey is used as the label identifier for a Trigger
TriggerLabelKey = "/trigger"
)

// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha1"}
var SchemeGroupVersion = schema.GroupVersion{Group: triggers.GroupName, Version: "v1alpha1"}

// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
Expand Down
17 changes: 2 additions & 15 deletions pkg/apis/triggers/v1beta1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,14 @@ limitations under the License.
package v1beta1

import (
"github.com/tektoncd/triggers/pkg/apis/triggers"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
// GroupName is the Kubernetes resource group name for Tekton types.
GroupName = "triggers.tekton.dev"

// EventListenerLabelKey is used as the label identifier for an EventListener.
EventListenerLabelKey = "/eventlistener"

// EventIDLabelKey is used as the label identifier for an EventListener event.
EventIDLabelKey = "/triggers-eventid"

// TriggerLabelKey is used as the label identifier for a Trigger
TriggerLabelKey = "/trigger"
)

// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1beta1"}
var SchemeGroupVersion = schema.GroupVersion{Group: triggers.GroupName, Version: "v1beta1"}

// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
Expand Down
10 changes: 5 additions & 5 deletions pkg/resources/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
"fmt"
"strings"

"github.com/tektoncd/triggers/pkg/apis/triggers"
kerrors "k8s.io/apimachinery/pkg/api/errors"

"k8s.io/client-go/dynamic"

"go.uber.org/zap"

triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -71,9 +71,9 @@ func Create(logger *zap.SugaredLogger, rt json.RawMessage, triggerName, eventID,
}

data, err := addLabels(data, map[string]string{
triggersv1.EventListenerLabelKey: elName,
triggersv1.EventIDLabelKey: eventID,
triggersv1.TriggerLabelKey: triggerName,
triggers.EventListenerLabelKey: elName,
triggers.EventIDLabelKey: eventID,
triggers.TriggerLabelKey: triggerName,
})
if err != nil {
return err
Expand Down Expand Up @@ -125,7 +125,7 @@ func addLabels(us *unstructured.Unstructured, labelsToAdd map[string]string) (*u
labels = make(map[string]string)
}
for k, v := range labelsToAdd {
l := fmt.Sprintf("%s/%s", triggersv1.GroupName, strings.TrimLeft(k, "/"))
l := fmt.Sprintf("%s/%s", triggers.GroupName, strings.TrimLeft(k, "/"))
labels[l] = v
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/resources/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"fmt"
"testing"

"github.com/tektoncd/triggers/pkg/apis/triggers"
"go.uber.org/zap/zaptest"

"github.com/google/go-cmp/cmp"
resourcev1 "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
dynamicclientset "github.com/tektoncd/triggers/pkg/client/dynamic/clientset"
"github.com/tektoncd/triggers/pkg/client/dynamic/clientset/tekton"
"github.com/tektoncd/triggers/test"
Expand All @@ -39,9 +39,9 @@ import (
)

const (
resourceLabel = triggersv1.GroupName + triggersv1.EventListenerLabelKey
triggerLabel = triggersv1.GroupName + triggersv1.TriggerLabelKey
eventIDLabel = triggersv1.GroupName + triggersv1.EventIDLabelKey
resourceLabel = triggers.GroupName + triggers.EventListenerLabelKey
triggerLabel = triggers.GroupName + triggers.TriggerLabelKey
eventIDLabel = triggers.GroupName + triggers.EventIDLabelKey

triggerName = "trigger"
eventID = "12345"
Expand Down
5 changes: 3 additions & 2 deletions pkg/sink/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io/ioutil"
"net/http"

triggers2 "github.com/tektoncd/triggers/pkg/apis/triggers"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
triggersclientset "github.com/tektoncd/triggers/pkg/client/clientset/versioned"
listers "github.com/tektoncd/triggers/pkg/client/listers/triggers/v1alpha1"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (r Sink) HandleEvent(response http.ResponseWriter, request *http.Request) {
}

eventID := template.UUID()
log = log.With(zap.String(triggersv1.EventIDLabelKey, eventID))
log = log.With(zap.String(triggers2.EventIDLabelKey, eventID))
log.Debugf("handling event with path %s, payload: %s and header: %v", request.URL.Path, string(event), request.Header)
var trItems []*triggersv1.Trigger
labelSelector := labels.Everything()
Expand Down Expand Up @@ -238,7 +239,7 @@ func (r Sink) merge(et []triggersv1.EventListenerTrigger, trItems []*triggersv1.
}

func (r Sink) processTrigger(t triggersv1.Trigger, request *http.Request, event []byte, eventID string, eventLog *zap.SugaredLogger) error {
log := eventLog.With(zap.String(triggersv1.TriggerLabelKey, t.Name))
log := eventLog.With(zap.String(triggers2.TriggerLabelKey, t.Name))

finalPayload, header, iresp, err := r.ExecuteInterceptors(t, request, event, log, eventID)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions test/eventlistener_scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"testing"

"github.com/tektoncd/triggers/pkg/apis/triggers"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -117,7 +118,7 @@ func createServiceAccount(t *testing.T, c *clients, namespace, name string) {
&rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{Name: "sa-role"},
Rules: []rbacv1.PolicyRule{{
APIGroups: []string{triggersv1.GroupName},
APIGroups: []string{triggers.GroupName},
Resources: []string{"eventlisteners", "triggerbindings", "triggertemplates", "triggers"},
Verbs: []string{"get", "list", "watch"},
}, {
Expand Down Expand Up @@ -153,7 +154,7 @@ func createServiceAccount(t *testing.T, c *clients, namespace, name string) {
&rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{Name: "sa-clusterrole"},
Rules: []rbacv1.PolicyRule{{
APIGroups: []string{triggersv1.GroupName},
APIGroups: []string{triggers.GroupName},
Resources: []string{"clustertriggerbindings", "clusterinterceptors"},
Verbs: []string{"get", "list", "watch"},
}},
Expand Down
9 changes: 5 additions & 4 deletions test/eventlistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"
"testing"

"github.com/tektoncd/triggers/pkg/apis/triggers"
"knative.dev/pkg/ptr"

"k8s.io/client-go/kubernetes"
Expand All @@ -57,9 +58,9 @@ import (
)

const (
resourceLabel = triggersv1.GroupName + triggersv1.EventListenerLabelKey
triggerLabel = triggersv1.GroupName + triggersv1.TriggerLabelKey
eventIDLabel = triggersv1.GroupName + triggersv1.EventIDLabelKey
resourceLabel = triggers.GroupName + triggers.EventListenerLabelKey
triggerLabel = triggers.GroupName + triggers.TriggerLabelKey
eventIDLabel = triggers.GroupName + triggers.EventIDLabelKey

examplePRJsonFilename = "pr.json"
)
Expand Down Expand Up @@ -241,7 +242,7 @@ func TestEventListenerCreate(t *testing.T) {
&rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{Name: "my-role"},
Rules: []rbacv1.PolicyRule{{
APIGroups: []string{triggersv1.GroupName},
APIGroups: []string{triggers.GroupName},
Resources: []string{"clustertriggerbindings", "eventlisteners", "clusterinterceptors", "triggerbindings", "triggertemplates", "triggers"},
Verbs: []string{"get", "list", "watch"},
}, {
Expand Down

0 comments on commit 814452f

Please sign in to comment.