Skip to content

Commit

Permalink
feat: enabling workflow identity for GKE (#593)
Browse files Browse the repository at this point in the history
* enabling workflow identity for GKE

* Updated gcp-pubsub-workflow-identity.yaml to remove unneccesary double quotes

* updating gcp-pubsub-workflow-identity.yaml

* removed extra pubsub client

* updated gcp pubsub validate

* update gcp pubsub validate

* condense gcp pubsub examples
  • Loading branch information
chaseterry committed Apr 23, 2020
1 parent 50f1103 commit be49e97
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 15 deletions.
13 changes: 13 additions & 0 deletions api/event-source.html
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,19 @@ <h3 id="argoproj.io/v1alpha1.PubSubEventSource">PubSubEventSource
</tr>
<tr>
<td>
<code>enableWorkflowIdentity</code></br>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>EnableWorkflowIdentity determines if your project authenticates to GCP with WorkflowIdentity or CredentialsFile.
If true, authentication is done with WorkflowIdentity. If false or omited, authentication is done with CredentialsFile.</p>
</td>
</tr>
<tr>
<td>
<code>deleteSubscriptionOnFinish</code></br>
<em>
bool
Expand Down
25 changes: 25 additions & 0 deletions api/event-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -3462,6 +3462,31 @@ for GCP

<td>

<code>enableWorkflowIdentity</code></br> <em> bool </em>

</td>

<td>

<em>(Optional)</em>

<p>

EnableWorkflowIdentity determines if your project authenticates to GCP
with WorkflowIdentity or CredentialsFile. If true, authentication is
done with WorkflowIdentity. If false or omited, authentication is done
with CredentialsFile.

</p>

</td>

</tr>

<tr>

<td>

<code>deleteSubscriptionOnFinish</code></br> <em> bool </em>

</td>
Expand Down
15 changes: 15 additions & 0 deletions examples/event-sources/gcp-pubsub.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ spec:
topic: test
# Refers to path of the credential file that is mounted in the gateway pod.
credentialsFile: /creds/key.json

# example-workload-identity:
# # jsonBody specifies that all event body payload coming from this
# # source will be JSON
# jsonBody: true
# # id of your project
# projectID: argo-events-XXXXX
# # (optional) id of project for topic, same as projectID by default
# # topicProjectID: "project-id"
# # topic name
# topic: test
# # Empty credentials file when using Workflow Identity
# credentialsFile: ""
# # If enableWorkflowIdentity is true, the projects uses Workflow Identity for authentication
# enableWorkflowIdentity: true
28 changes: 16 additions & 12 deletions gateways/server/gcp-pubsub/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,35 @@ func (listener *EventListener) listenEvents(eventSource *gateways.EventSource, c
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Create a new topic with the given name if none exists
logger.Infoln("setting up a client to connect to PubSub...")
client, err := pubsub.NewClient(ctx, pubsubEventSource.ProjectID, option.WithCredentialsFile(pubsubEventSource.CredentialsFile))
if err != nil {
return errors.Wrapf(err, "failed to set up client for %s", eventSource.Name)

var opt []option.ClientOption
projectId := pubsubEventSource.ProjectID

if !pubsubEventSource.EnableWorkflowIdentity {
opt = append(opt, option.WithCredentialsFile(pubsubEventSource.CredentialsFile))
}

// use same client for topic and subscription by default
topicClient := client
// Use default ProjectID unless TopicProjectID exists
if pubsubEventSource.TopicProjectID != "" && pubsubEventSource.TopicProjectID != pubsubEventSource.ProjectID {
topicClient, err = pubsub.NewClient(ctx, pubsubEventSource.TopicProjectID, option.WithCredentialsFile(pubsubEventSource.CredentialsFile))
if err != nil {
return errors.Wrapf(err, "failed to set up client for %s", eventSource.Name)
}
projectId = pubsubEventSource.TopicProjectID
}

// Create a new topic with the given name if none exists
client, err := pubsub.NewClient(ctx, projectId, opt...)
if err != nil {
return errors.Wrapf(err, "failed to set up client for %s", eventSource.Name)
}

logger.Infoln("getting topic information from PubSub...")
topic := topicClient.Topic(pubsubEventSource.Topic)
topic := client.Topic(pubsubEventSource.Topic)
exists, err := topic.Exists(ctx)
if err != nil {
return errors.Wrapf(err, "failed to get status of the topic %s for %s", pubsubEventSource.Topic, eventSource.Name)
}
if !exists {
logger.Infoln("topic doesn't exist, creating the PubSub topic...")
if _, err := topicClient.CreateTopic(ctx, pubsubEventSource.Topic); err != nil {
if _, err := client.CreateTopic(ctx, pubsubEventSource.Topic); err != nil {
return errors.Wrapf(err, "failed to create the topic %s for %s", pubsubEventSource.Topic, eventSource.Name)
}
}
Expand Down
5 changes: 3 additions & 2 deletions gateways/server/gcp-pubsub/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package pubsub
import (
"context"
"fmt"

"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/gateways"
apicommon "github.com/argoproj/argo-events/pkg/apis/common"
Expand Down Expand Up @@ -65,8 +66,8 @@ func validate(eventSource *v1alpha1.PubSubEventSource) error {
if eventSource.Topic == "" {
return fmt.Errorf("must specify topic")
}
if eventSource.CredentialsFile == "" {
return fmt.Errorf("must specify credentials file path")
if !eventSource.EnableWorkflowIdentity && eventSource.CredentialsFile == "" {
return fmt.Errorf("must specify credentials file path if not using Workflow Identity")
}
return nil
}
6 changes: 5 additions & 1 deletion pkg/apis/eventsources/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,13 @@ type PubSubEventSource struct {
Topic string `json:"topic" protobuf:"bytes,3,name=topic"`
// CredentialsFile is the file that contains credentials to authenticate for GCP
CredentialsFile string `json:"credentialsFile" protobuf:"bytes,4,name=credentialsFile"`
// EnableWorkflowIdentity determines if your project authenticates to GCP with WorkflowIdentity or CredentialsFile.
// If true, authentication is done with WorkflowIdentity. If false or omited, authentication is done with CredentialsFile.
// +optional
EnableWorkflowIdentity bool `json:"enableWorkflowIdentity,omitempty" protobuf:"bytes,5,opt,name=enableWorkflowIdentity"`
// DeleteSubscriptionOnFinish determines whether to delete the GCP PubSub subscription once the event source is stopped.
// +optional
DeleteSubscriptionOnFinish bool `json:"deleteSubscriptionOnFinish,omitempty" protobuf:"bytes,1,opt,name=deleteSubscriptionOnFinish"`
DeleteSubscriptionOnFinish bool `json:"deleteSubscriptionOnFinish,omitempty" protobuf:"bytes,6,opt,name=deleteSubscriptionOnFinish"`
// JSONBody specifies that all event body payload coming from this
// source will be JSON
// +optional
Expand Down

0 comments on commit be49e97

Please sign in to comment.