github.com/argoproj/argo-events@v1.9.1/sensors/context.go (about)

     1  package sensors
     2  
     3  /*
     4  Copyright 2020 BlackRock, Inc.
     5  
     6  Licensed under the Apache License, Version 2.0 (the "License");
     7  you may not use this file except in compliance with the License.
     8  You may obtain a copy of the License at
     9  
    10  	http://www.apache.org/licenses/LICENSE-2.0
    11  
    12  Unless required by applicable law or agreed to in writing, software
    13  distributed under the License is distributed on an "AS IS" BASIS,
    14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  See the License for the specific language governing permissions and
    16  limitations under the License.
    17  */
    18  
    19  import (
    20  	"net/http"
    21  	"time"
    22  
    23  	eventhubs "github.com/Azure/azure-event-hubs-go/v3"
    24  	servicebus "github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
    25  	"github.com/IBM/sarama"
    26  	"github.com/apache/openwhisk-client-go/whisk"
    27  	"github.com/apache/pulsar-client-go/pulsar"
    28  	"github.com/aws/aws-sdk-go/service/lambda"
    29  	natslib "github.com/nats-io/nats.go"
    30  	"google.golang.org/grpc"
    31  	"k8s.io/client-go/dynamic"
    32  	"k8s.io/client-go/kubernetes"
    33  
    34  	"github.com/argoproj/argo-events/common"
    35  	sensormetrics "github.com/argoproj/argo-events/metrics"
    36  	eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1"
    37  	"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
    38  )
    39  
    40  // SensorContext contains execution context for Sensor
    41  type SensorContext struct {
    42  	// kubeClient is the kubernetes client
    43  	kubeClient kubernetes.Interface
    44  	// dynamic clients.
    45  	dynamicClient dynamic.Interface
    46  	// Sensor object
    47  	sensor *v1alpha1.Sensor
    48  	// EventBus config
    49  	eventBusConfig *eventbusv1alpha1.BusConfig
    50  	// EventBus subject
    51  	eventBusSubject string
    52  	hostname        string
    53  
    54  	// httpClients holds the reference to HTTP clients for HTTP triggers.
    55  	httpClients common.StringKeyedMap[*http.Client]
    56  	// customTriggerClients holds the references to the gRPC clients for the custom trigger servers
    57  	customTriggerClients common.StringKeyedMap[*grpc.ClientConn]
    58  	// http client to send slack messages.
    59  	slackHTTPClient *http.Client
    60  	// kafkaProducers holds references to the active kafka producers
    61  	kafkaProducers common.StringKeyedMap[sarama.AsyncProducer]
    62  	// pulsarProducers holds references to the active pulsar producers
    63  	pulsarProducers common.StringKeyedMap[pulsar.Producer]
    64  	// natsConnections holds the references to the active nats connections.
    65  	natsConnections common.StringKeyedMap[*natslib.Conn]
    66  	// awsLambdaClients holds the references to active AWS Lambda clients.
    67  	awsLambdaClients common.StringKeyedMap[*lambda.Lambda]
    68  	// openwhiskClients holds the references to active OpenWhisk clients.
    69  	openwhiskClients common.StringKeyedMap[*whisk.Client]
    70  	// azureEventHubsClients holds the references to active Azure Event Hub clients.
    71  	azureEventHubsClients common.StringKeyedMap[*eventhubs.Hub]
    72  	// azureServiceBusClients holds the references to active Azure Service Bus clients.
    73  	azureServiceBusClients common.StringKeyedMap[*servicebus.Sender]
    74  	metrics                *sensormetrics.Metrics
    75  }
    76  
    77  // NewSensorContext returns a new sensor execution context.
    78  func NewSensorContext(kubeClient kubernetes.Interface, dynamicClient dynamic.Interface, sensor *v1alpha1.Sensor, eventBusConfig *eventbusv1alpha1.BusConfig, eventBusSubject, hostname string, metrics *sensormetrics.Metrics) *SensorContext {
    79  	return &SensorContext{
    80  		kubeClient:           kubeClient,
    81  		dynamicClient:        dynamicClient,
    82  		sensor:               sensor,
    83  		eventBusConfig:       eventBusConfig,
    84  		eventBusSubject:      eventBusSubject,
    85  		hostname:             hostname,
    86  		httpClients:          common.NewStringKeyedMap[*http.Client](),
    87  		customTriggerClients: common.NewStringKeyedMap[*grpc.ClientConn](),
    88  		slackHTTPClient: &http.Client{
    89  			Timeout: time.Minute * 5,
    90  		},
    91  		kafkaProducers:         common.NewStringKeyedMap[sarama.AsyncProducer](),
    92  		pulsarProducers:        common.NewStringKeyedMap[pulsar.Producer](),
    93  		natsConnections:        common.NewStringKeyedMap[*natslib.Conn](),
    94  		awsLambdaClients:       common.NewStringKeyedMap[*lambda.Lambda](),
    95  		openwhiskClients:       common.NewStringKeyedMap[*whisk.Client](),
    96  		azureEventHubsClients:  common.NewStringKeyedMap[*eventhubs.Hub](),
    97  		azureServiceBusClients: common.NewStringKeyedMap[*servicebus.Sender](),
    98  		metrics:                metrics,
    99  	}
   100  }