Skip to content

Commit f4eae5d

Browse files
tahmed-devqiluo-msft
authored andcommitted
[telemetry] Call sonic-cfggen Once (sonic-net#4901)
sonic-cfggen call is slow and this is taking place in the SONiC boot up process. The change uses templates to assemble all required vars into single template file. With this change, telemetry now calls once into sonic-cfggen. signed-off-by: Tamer Ahmed <[email protected]>
1 parent 153f880 commit f4eae5d

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

dockers/docker-base-buster/Dockerfile.j2

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ RUN apt-get update && \
6464
net-tools \
6565
# for arm arch: Installing j2cli dependency package MarkupSafe from source relies on weeksetuptools and wheel
6666
python-setuptools \
67-
python-wheel
67+
python-wheel \
68+
# for processing/handling json files in bash environment
69+
jq
6870

6971
# For templating
7072
RUN pip install j2cli

dockers/docker-base-stretch/Dockerfile.j2

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ RUN apt-get update && \
6262
net-tools \
6363
# for arm arch: Installing j2cli dependency package MarkupSafe from source relies on weeksetuptools and wheel
6464
python-setuptools \
65-
python-wheel
65+
python-wheel \
66+
# for processing json files in bash environment
67+
jq
6668

6769
# For templating
6870
RUN pip install j2cli

dockers/docker-sonic-telemetry/Dockerfile.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ RUN apt-get clean -y && \
2222
apt-get autoremove -y && \
2323
rm -rf /debs
2424

25-
COPY ["start.sh", "telemetry.sh", "dialout.sh", "/usr/bin/"]
25+
COPY ["start.sh", "telemetry.sh", "dialout.sh", "telemetry_vars.j2", "/usr/bin/"]
2626
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
2727
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
2828
COPY ["critical_processes", "/etc/supervisor"]

dockers/docker-sonic-telemetry/telemetry.sh

+19-16
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,38 @@
22

33
# Try to read telemetry and certs config from ConfigDB.
44
# Use default value if no valid config exists
5-
X509=`sonic-cfggen -d -v "DEVICE_METADATA['x509']"`
6-
gnmi=`sonic-cfggen -d -v "TELEMETRY['gnmi']"`
7-
certs=`sonic-cfggen -d -v "TELEMETRY['certs']"`
5+
TELEMETRY_VARS=$(sonic-cfggen -d -t telemetry_vars.j2)
6+
TELEMETRY_VARS=${TELEMETRY_VARS//[\']/\"}
7+
X509=$(echo $TELEMETRY_VARS | jq -r '.x509')
8+
GNMI=$(echo $TELEMETRY_VARS | jq -r '.gnmi')
9+
CERTS=$(echo $TELEMETRY_VARS | jq -r '.certs')
810

911
TELEMETRY_ARGS=" -logtostderr"
1012
export CVL_SCHEMA_PATH=/usr/sbin/schema
1113

12-
if [ -n "$certs" ]; then
13-
SERVER_CRT=`sonic-cfggen -d -v "TELEMETRY['certs']['server_crt']"`
14-
SERVER_KEY=`sonic-cfggen -d -v "TELEMETRY['certs']['server_key']"`
14+
if [ -n "$CERTS" ]; then
15+
SERVER_CRT=$(echo $CERTS | jq -r '.server_crt')
16+
SERVER_KEY=$(echo $CERTS | jq -r '.server_key')
1517
if [ -z $SERVER_CRT ] || [ -z $SERVER_KEY ]; then
1618
TELEMETRY_ARGS+=" --insecure"
1719
else
1820
TELEMETRY_ARGS+=" --server_crt $SERVER_CRT --server_key $SERVER_KEY "
1921
fi
2022

21-
CA_CRT=`sonic-cfggen -d -v "TELEMETRY['certs']['ca_crt']"`
23+
CA_CRT=$(echo $CERTS | jq -r '.ca_crt')
2224
if [ ! -z $CA_CRT ]; then
2325
TELEMETRY_ARGS+=" --ca_crt $CA_CRT"
2426
fi
2527
elif [ -n "$X509" ]; then
26-
SERVER_CRT=`sonic-cfggen -d -v "DEVICE_METADATA['x509']['server_crt']"`
27-
SERVER_KEY=`sonic-cfggen -d -v "DEVICE_METADATA['x509']['server_key']"`
28+
SERVER_CRT=$(echo $X509 | jq -r '.server_crt')
29+
SERVER_KEY=$(echo $X509 | jq -r '.server_key')
2830
if [ -z $SERVER_CRT ] || [ -z $SERVER_KEY ]; then
2931
TELEMETRY_ARGS+=" --insecure"
3032
else
3133
TELEMETRY_ARGS+=" --server_crt $SERVER_CRT --server_key $SERVER_KEY "
3234
fi
3335

34-
CA_CRT=`sonic-cfggen -d -v "DEVICE_METADATA['x509']['ca_crt']"`
36+
CA_CRT=$(echo $X509 | jq -r '.ca_crt')
3537
if [ ! -z $CA_CRT ]; then
3638
TELEMETRY_ARGS+=" --ca_crt $CA_CRT"
3739
fi
@@ -40,19 +42,20 @@ else
4042
fi
4143

4244
# If no configuration entry exists for TELEMETRY, create one default port
43-
if [ -z "$gnmi" ]; then
44-
sonic-db-cli CONFIG_DB hset "TELEMETRY|gnmi" port 8080
45+
if [ -z "$GNMI" ]; then
46+
PORT=8080
47+
sonic-db-cli CONFIG_DB hset "TELEMETRY|gnmi" port $PORT
48+
else
49+
PORT=$(echo $GNMI | jq -r '.port')
4550
fi
46-
47-
PORT=`sonic-cfggen -d -v "TELEMETRY['gnmi']['port']"`
4851
TELEMETRY_ARGS+=" --port $PORT"
4952

50-
CLIENT_AUTH=`sonic-cfggen -d -v "TELEMETRY['gnmi']['client_auth']"`
53+
CLIENT_AUTH=$(echo $GNMI | jq -r '.client_auth')
5154
if [ -z $CLIENT_AUTH ] || [ $CLIENT_AUTH == "false" ]; then
5255
TELEMETRY_ARGS+=" --allow_no_client_auth"
5356
fi
5457

55-
LOG_LEVEL=`sonic-cfggen -d -v "TELEMETRY['gnmi']['log_level']"`
58+
LOG_LEVEL=$(echo $GNMI | jq -r '.log_level')
5659
if [ ! -z $LOG_LEVEL ]; then
5760
TELEMETRY_ARGS+=" -v=$LOG_LEVEL"
5861
else
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"certs": "{% if "certs" in TELEMETRY.keys() %}{{ TELEMETRY["certs"] }}{% endif %}",
3+
"gnmi" : "{% if "gnmi" in TELEMETRY.keys() %}{{ TELEMETRY["gnmi"] }}{% endif %}",
4+
"x509" : "{% if "x509" in DEVICE_METADATA.keys() %}{{ DEVICE_METADATA["x509"] }}{% endif %}"
5+
}

0 commit comments

Comments
 (0)