Skip to content

Commit 470faaf

Browse files
committed
acrolinx updates and yaml update
Signed-off-by: Ryan Winter <[email protected]>
1 parent eb5aae7 commit 470faaf

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

articles/iot-operations/create-edge-apps/howto-develop-mqttnet-apps.md

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,28 @@ The [sample code](https://github.com/Azure-Samples/explore-iot-operations/tree/m
3333
##3. The mounted token is used as the password with well-known username `K8S-SAT`:
3434

3535
```csharp
36-
static string token_path = "/var/run/secrets/tokens/mqtt-client-token";
36+
static string sat_auth_file = "/var/run/secrets/tokens/mqtt-client-token";
3737
...
38-
39-
// Read SAT Token
40-
var satToken = File.ReadAllText(token_path);
38+
var satToken = File.ReadAllBytes(sat_auth_file);
4139
```
4240

4341
1. The MQTT client options are configured using the `MqttClientOptions` class. Using the `MqttClientOptionsBuilder` as advised in the [client](https://github.com/dotnet/MQTTnet/wiki/Client) documentation is the advised way of setting the options:
4442
4543
```csharp
46-
// Create TCP based options using the builder amd connect to broker
4744
var mqttClientOptions = new MqttClientOptionsBuilder()
48-
.WithTcpServer(broker, 1883)
49-
.WithProtocolVersion(MqttProtocolVersion.V311)
45+
.WithTcpServer(hostname, tcp_port)
46+
.WithProtocolVersion(MqttProtocolVersion.V500)
5047
.WithClientId("mqtt-client-dotnet")
51-
.WithCredentials("K8S-SAT", satToken);
52-
.Build();
48+
.WithAuthentication("K8S-SAT", satToken);
5349
```
5450

55-
5. After setting up the MQTT client options, a connection can be established. The following code shows how to connect with a server. You can replace the *CancellationToken.None* with a valid *CancellationToken*, if needed.
51+
5. After setting up the MQTT client options, a connection can be established. The following code shows how to connect with a server. You can replace the `CancellationToken.None` with a valid CancellationToken if needed.
5652

5753
```csharp
58-
var response = await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
54+
var response = await mqttClient.ConnectAsync(mqttClientOptions.Build(), CancellationToken.None);
5955
```
6056

61-
6. MQTT messages can be created using the properties directly or via using `MqttApplicationMessageBuilder`. This class has some useful overloads that allow dealing with different payload formats. The API of the builder is a fluent API. The following code shows how to compose an application message and publish them to an article called *sampletopic*:
57+
6. MQTT messages can be created using the properties directly or using `MqttApplicationMessageBuilder`. This class has overloads that allow dealing with different payload formats. The API of the builder is a fluent API. The following code shows how to compose an application message and publish them to an article called *sampletopic*:
6258

6359
```csharp
6460
var applicationMessage = new MqttApplicationMessageBuilder()
@@ -67,58 +63,65 @@ The [sample code](https://github.com/Azure-Samples/explore-iot-operations/tree/m
6763
.Build();
6864

6965
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
70-
Console.WriteLine("The MQTT client published a message.");
7166
```
7267

7368
## Pod specification
7469

75-
The `serviceAccountName` field in the pod configuration must match the service account associated with the token being used. Also, note the `serviceAccountToken.expirationSeconds` is set to **86400 seconds**, and once it expires, you need to reload the token from disk. This logic isn't currently implemented in the sample.
70+
The `serviceAccountName` field in the pod configuration must match the service account associated with the token being used. Also, note the `serviceAccountToken.expirationSeconds` is set to **86400 seconds**, and once it expires, you need to reload the token from disk. This logic isn't implemented in this sample.
7671

7772
```yaml
7873
apiVersion: v1
74+
kind: ServiceAccount
75+
metadata:
76+
name: mqtt-client
77+
namespace: azure-iot-operations
78+
79+
---
80+
apiVersion: v1
7981
kind: Pod
8082
metadata:
8183
name: mqtt-client-dotnet
82-
labels:
83-
app: publisher
84+
namespace: azure-iot-operations
8485
spec:
8586
serviceAccountName: mqtt-client
8687

87-
volumes:
88-
# SAT token used to authenticate between the application and the MQTT broker
89-
- name: mqtt-client-token
90-
projected:
91-
sources:
92-
- serviceAccountToken:
93-
path: mqtt-client-token
94-
audience: aio-mq-dmqtt
95-
expirationSeconds: 86400
96-
97-
# Certificate chain for the application to validate the MQTT broker
98-
- name: aio-mq-ca-cert-chain
99-
configMap:
100-
name: aio-mq-ca-cert-chain
88+
volumes:
89+
90+
# SAT token used to authenticate between the application and the MQTT broker
91+
- name: mqtt-client-token
92+
projected:
93+
sources:
94+
- serviceAccountToken:
95+
path: mqtt-client-token
96+
audience: aio-mq
97+
expirationSeconds: 86400
98+
99+
# Certificate chain for the application to validate the MQTT broker
100+
- name: aio-ca-trust-bundle
101+
configMap:
102+
name: aio-ca-trust-bundle-test-only
101103

102104
containers:
103-
- name: mqtt-client-dotnet
104-
image: ghcr.io/azure-samples/explore-iot-operations/mqtt-client-dotnet:latest
105-
imagePullPolicy: IfNotPresent
106-
volumeMounts:
107-
- name: mqtt-client-token
108-
mountPath: /var/run/secrets/tokens
109-
- name: aio-mq-ca-cert-chain
110-
mountPath: /certs/aio-mq-ca-cert/
111-
env:
112-
- name: IOT_MQ_HOST_NAME
113-
value: "aio-mq-dmqtt-frontend"
114-
- name: IOT_MQ_PORT
115-
value: "8883"
116-
- name: IOT_MQ_TLS_ENABLED
117-
value: "true"
105+
- name: mqtt-client-dotnet
106+
image: ghcr.io/azure-samples/explore-iot-operations/mqtt-client-dotnet:latest
107+
volumeMounts:
108+
- name: mqtt-client-token
109+
mountPath: /var/run/secrets/tokens/
110+
- name: aio-ca-trust-bundle
111+
mountPath: /var/run/certs/aio-mq-ca-cert/
112+
env:
113+
- name: hostname
114+
value: "aio-mq-dmqtt-frontend"
115+
- name: tcpPort
116+
value: "8883"
117+
- name: useTls
118+
value: "true"
119+
- name: caFile
120+
value: "/var/run/certs/aio-mq-ca-cert/ca.crt"
121+
- name: satAuthFile
122+
value: "/var/run/secrets/tokens/mqtt-client-token"
118123
```
119124

120-
The token is mounted into the container at the path specified in `containers[].volumeMount.mountPath`
121-
122125
To run the sample, follow the instructions in its [README](https://github.com/Azure-Samples/explore-iot-operations/tree/main/samples/mqtt-client-dotnet).
123126

124127
## Related content

0 commit comments

Comments
 (0)