@@ -24,6 +24,7 @@ type mqttAgentTransport struct {
2424 agentID string
2525}
2626
27+ // Deprecated: use v2.mqtt.NewAgentOptions instead
2728func NewAgentOptions (mqttOptions * MQTTOptions , clusterName , agentID string ) * options.CloudEventsAgentOptions {
2829 mqttAgentOptions := & mqttAgentTransport {
2930 MQTTOptions : * mqttOptions ,
@@ -40,8 +41,6 @@ func NewAgentOptions(mqttOptions *MQTTOptions, clusterName, agentID string) *opt
4041}
4142
4243func (o * mqttAgentTransport ) WithContext (ctx context.Context , evtCtx cloudevents.EventContext ) (context.Context , error ) {
43- logger := klog .FromContext (ctx )
44-
4544 topic , err := getAgentPubTopic (ctx )
4645 if err != nil {
4746 return nil , err
@@ -51,69 +50,23 @@ func (o *mqttAgentTransport) WithContext(ctx context.Context, evtCtx cloudevents
5150 return cloudeventscontext .WithTopic (ctx , string (* topic )), nil
5251 }
5352
54- eventType , err := types .ParseCloudEventsType (evtCtx .GetType ())
55- if err != nil {
56- return nil , fmt .Errorf ("unsupported event type %s, %v" , eventType , err )
57- }
58-
59- originalSource , err := evtCtx .GetExtension (types .ExtensionOriginalSource )
60- if err != nil {
61- return nil , err
62- }
63-
64- // agent request to sync resource spec from all sources
65- if eventType .Action == types .ResyncRequestAction && originalSource == types .SourceAll {
66- if len (o .Topics .AgentBroadcast ) == 0 {
67- logger .Info ("the agent broadcast topic not set, fall back to the agent events topic" )
68-
69- // TODO after supporting multiple sources, we should list each source
70- eventsTopic := replaceLast (o .Topics .AgentEvents , "+" , o .clusterName )
71- return cloudeventscontext .WithTopic (ctx , eventsTopic ), nil
72- }
73-
74- resyncTopic := strings .Replace (o .Topics .AgentBroadcast , "+" , o .clusterName , 1 )
75- return cloudeventscontext .WithTopic (ctx , resyncTopic ), nil
76- }
77-
78- topicSource , err := getSourceFromEventsTopic (o .Topics .AgentEvents )
53+ pubTopic , err := AgentPubTopic (ctx , & o .MQTTOptions , o .clusterName , evtCtx )
7954 if err != nil {
8055 return nil , err
8156 }
8257
83- // agent publishes status events or spec resync events
84- eventsTopic := replaceLast (o .Topics .AgentEvents , "+" , o .clusterName )
85- eventsTopic = replaceLast (eventsTopic , "+" , topicSource )
86- return cloudeventscontext .WithTopic (ctx , eventsTopic ), nil
58+ return cloudeventscontext .WithTopic (ctx , pubTopic ), nil
8759}
8860
8961func (o * mqttAgentTransport ) Connect (ctx context.Context ) error {
90- subscribe := & paho.Subscribe {
91- Subscriptions : []paho.SubscribeOptions {
92- {
93- // TODO support multiple sources, currently the client require the source events topic has a sourceID, in
94- // the future, client may need a source list, it will subscribe to each source
95- // receiving the sources events
96- Topic : replaceLast (o .Topics .SourceEvents , "+" , o .clusterName ), QoS : byte (o .SubQoS ),
97- },
98- },
99- }
100-
101- // receiving status resync events from all sources
102- if len (o .Topics .SourceBroadcast ) != 0 {
103- subscribe .Subscriptions = append (subscribe .Subscriptions , paho.SubscribeOptions {
104- Topic : o .Topics .SourceBroadcast ,
105- QoS : byte (o .SubQoS ),
106- })
107- }
108-
10962 protocol , err := o .GetCloudEventsProtocol (
11063 ctx ,
11164 fmt .Sprintf ("%s-client" , o .agentID ),
11265 func (err error ) {
11366 o .errorChan <- err
11467 },
11568 cloudeventsmqtt .WithPublish (& paho.Publish {QoS : byte (o .PubQoS )}),
116- cloudeventsmqtt .WithSubscribe (subscribe ),
69+ cloudeventsmqtt .WithSubscribe (AgentSubscribe ( & o . MQTTOptions , o . clusterName ) ),
11770 )
11871 if err != nil {
11972 return err
@@ -157,3 +110,62 @@ func (o *mqttAgentTransport) Close(ctx context.Context) error {
157110func (o * mqttAgentTransport ) ErrorChan () <- chan error {
158111 return o .errorChan
159112}
113+
114+ func AgentPubTopic (ctx context.Context , o * MQTTOptions , clusterName string , evtCtx cloudevents.EventContext ) (string , error ) {
115+ logger := klog .FromContext (ctx )
116+
117+ ceType := evtCtx .GetType ()
118+ eventType , err := types .ParseCloudEventsType (ceType )
119+ if err != nil {
120+ return "" , fmt .Errorf ("unsupported event type %q, %v" , ceType , err )
121+ }
122+
123+ originalSource , err := evtCtx .GetExtension (types .ExtensionOriginalSource )
124+ if err != nil {
125+ return "" , err
126+ }
127+
128+ // agent request to sync resource spec from all sources
129+ if eventType .Action == types .ResyncRequestAction && originalSource == types .SourceAll {
130+ if len (o .Topics .AgentBroadcast ) == 0 {
131+ logger .Info ("the agent broadcast topic not set, fall back to the agent events topic" )
132+
133+ // TODO after supporting multiple sources, we should list each source
134+ return replaceLast (o .Topics .AgentEvents , "+" , clusterName ), nil
135+ }
136+
137+ return strings .Replace (o .Topics .AgentBroadcast , "+" , clusterName , 1 ), nil
138+ }
139+
140+ topicSource , err := getSourceFromEventsTopic (o .Topics .AgentEvents )
141+ if err != nil {
142+ return "" , err
143+ }
144+
145+ // agent publishes status events or spec resync events
146+ eventsTopic := replaceLast (o .Topics .AgentEvents , "+" , clusterName )
147+ return replaceLast (eventsTopic , "+" , topicSource ), nil
148+ }
149+
150+ func AgentSubscribe (o * MQTTOptions , clusterName string ) * paho.Subscribe {
151+ subscribe := & paho.Subscribe {
152+ Subscriptions : []paho.SubscribeOptions {
153+ {
154+ // TODO support multiple sources, currently the client require the source events topic has a sourceID, in
155+ // the future, client may need a source list, it will subscribe to each source
156+ // receiving the sources events
157+ Topic : replaceLast (o .Topics .SourceEvents , "+" , clusterName ), QoS : byte (o .SubQoS ),
158+ },
159+ },
160+ }
161+
162+ // receiving status resync events from all sources
163+ if len (o .Topics .SourceBroadcast ) != 0 {
164+ subscribe .Subscriptions = append (subscribe .Subscriptions , paho.SubscribeOptions {
165+ Topic : o .Topics .SourceBroadcast ,
166+ QoS : byte (o .SubQoS ),
167+ })
168+ }
169+
170+ return subscribe
171+ }
0 commit comments