This is an automated email from the ASF dual-hosted git repository.

kichan pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/trafficserver-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new 4eea483  Add a flag for enabling snippet (#258)
4eea483 is described below

commit 4eea4832cf785d919738622b2a1ee4ff01d8980e
Author: Kit Chan <[email protected]>
AuthorDate: Mon Nov 4 05:05:37 2024 -1000

    Add a flag for enabling snippet (#258)
    
    * Update TUTORIAL.md
    
    * Update ats-deployment.yaml
    
    * Update entry.sh
    
    * Update connect_redis.lua
    
    * Update entry.sh
    
    * Update connect_redis_test.lua
    
    * Update connect_redis_test.lua
    
    * Update connect_redis_test.lua
    
    * Update connect_redis_test.lua
    
    * Update connect_redis.lua
    
    * Update connect_redis_test.lua
    
    * Update connect_redis.lua
---
 bin/entry.sh                                       |  5 +++
 docs/TUTORIAL.md                                   |  2 +-
 pluginats/connect_redis.lua                        | 52 +++++++++++++---------
 pluginats/connect_redis_test.lua                   |  8 ++--
 .../data/setup/traffic-server/ats-deployment.yaml  |  2 +
 5 files changed, 44 insertions(+), 25 deletions(-)

diff --git a/bin/entry.sh b/bin/entry.sh
index 16fd10c..d47aa9a 100755
--- a/bin/entry.sh
+++ b/bin/entry.sh
@@ -32,6 +32,11 @@ if [ ! -f "${EXTRA_PLUGIN_FNAME}" ]; then
        cat $EXTRA_PLUGIN_FNAME >> /opt/ats/etc/trafficserver/plugin.config
 fi
 
+# replace lua plugin parameters to plugin.config if snippet is allowed
+if [ ! -z "${SNIPPET}" ]; then
+       sed -i 's/tslua.so 
\/opt\/ats\/var\/pluginats\/connect_redis.lua/tslua.so 
\/opt\/ats\/var\/pluginats\/connect_redis.lua snippet/' 
/opt/ats/etc/trafficserver/plugin.config
+fi
+
 # start redis
 redis-server /opt/ats/etc/redis.conf 
 
diff --git a/docs/TUTORIAL.md b/docs/TUTORIAL.md
index 361662d..1e5d9e4 100644
--- a/docs/TUTORIAL.md
+++ b/docs/TUTORIAL.md
@@ -44,7 +44,7 @@ You can specifiy the list of namespaces to look for ingress 
object by providing
 
 #### Snippet
 
-You can attach [ATS lua 
script](https://docs.trafficserver.apache.org/en/9.2.x/admin-guide/plugins/lua.en.html)
 to an ingress object and ATS will execute it for requests matching the routing 
rules defined in the ingress object. 
+You can attach [ATS lua 
script](https://docs.trafficserver.apache.org/en/9.2.x/admin-guide/plugins/lua.en.html)
 to an ingress object and ATS will execute it for requests matching the routing 
rules defined in the ingress object. This can be enabled by providing an 
environment variable called `SNIPPET` in the deployment. 
 
 #### Ingress Class
 
diff --git a/pluginats/connect_redis.lua b/pluginats/connect_redis.lua
index 0888bc3..16dbdcd 100644
--- a/pluginats/connect_redis.lua
+++ b/pluginats/connect_redis.lua
@@ -22,6 +22,15 @@ local redis = require 'redis'
 -- connecting to unix domain socket
 local client = redis.connect('unix:///opt/ats/var/run/redis/redis.sock')
 
+local snippet_enabled = false
+
+function __init__(argtb)
+  if (#argtb) > 0 then
+    ts.debug("Parameter is given. Snippet is enabled.")
+    snippet_enabled = true
+  end
+end
+
 -- helper function to split a string
 function ipport_split(s, delimiter)
   result = {}
@@ -186,31 +195,32 @@ function do_global_read_request()
     end
   end
 
-  for _, svc in ipairs(svcs) do
-    if svc == nil then
-      ts.error("Redis Lookup Failure: svc == nil for hostpath")
-      return 0
-    end
-    if string.sub(svc, 1, 1) == "$" then
-      ts.debug("snippet")
-      client:select(1)
-      local snippets = client:smembers(svc)
-
-      if snippets == nil then
-        ts.error("Redis Lookup Failure: snippets == nil for hostpath")
+  if snippet_enabled == true then
+    for _, svc in ipairs(svcs) do
+      if svc == nil then
+        ts.error("Redis Lookup Failure: svc == nil for hostpath")
         return 0
       end
-
-      local snippet = snippets[1]
-      if snippet == nil then
-        ts.error("Redis Lookup Failure: snippet == nil for hostpath")
-        return 0
+      if string.sub(svc, 1, 1) == "$" then
+        ts.debug("snippet")
+        client:select(1)
+        local snippets = client:smembers(svc)
+
+        if snippets == nil then
+          ts.error("Redis Lookup Failure: snippets == nil for hostpath")
+          return 0
+        end
+
+        local snippet = snippets[1]
+        if snippet == nil then
+          ts.error("Redis Lookup Failure: snippet == nil for hostpath")
+          return 0
+        end
+
+        local f = loadstring(snippet)
+        f()
       end
-
-      local f = loadstring(snippet)
-      f()
     end
   end
-
 end
 
diff --git a/pluginats/connect_redis_test.lua b/pluginats/connect_redis_test.lua
index d142beb..f785486 100644
--- a/pluginats/connect_redis_test.lua
+++ b/pluginats/connect_redis_test.lua
@@ -112,7 +112,7 @@ describe("Unit tests - Lua", function()
       client:select(0)
       
client:sadd("trafficserver-test-2:appsvc1:8080","172.17.0.3#8080#http","172.17.0.5#8080#http")
       --require 'pl.pretty'.dump(client)
-
+                        
       stub(ts, "add_package_cpath")
       stub(ts, "add_package_path")
       stub(ts, "debug")
@@ -139,10 +139,12 @@ describe("Unit tests - Lua", function()
       
client:sadd("E+http://test.edge.com/app1","$trafficserver-test-3/app-ingress/411990";)
       snippet = "ts.debug('Debug msg example')\nts.error('Error msg 
example')\n-- ts.hook(TS_LUA_HOOK_SEND_RESPONSE_HDR, function()\n--   
ts.client_response.header['Location'] = 'https://test.edge.com/app2'\n-- 
end)\nts.http.skip_remapping_set(0)\nts.http.set_resp(301, 
'Redirect')\nts.debug('Uncomment the above lines to redirect http request to 
https')\nts.debug('Modification for testing')\n"
       client:sadd("$trafficserver-test-3/app-ingress/411990",snippet) 
-
+                        
       --require 'pl.pretty'.dump(client)
       require "connect_redis"
-      local result = do_global_read_request()
+      local input_args = {"snippet"}
+      local result = __init__(input_args)
+      result = do_global_read_request()
 
       assert.stub(ts.error).was.called_with("Error msg example")
       assert.stub(ts.http.skip_remapping_set).was.called_with(0)
diff --git a/tests/data/setup/traffic-server/ats-deployment.yaml 
b/tests/data/setup/traffic-server/ats-deployment.yaml
index e11a7b2..0216d86 100644
--- a/tests/data/setup/traffic-server/ats-deployment.yaml
+++ b/tests/data/setup/traffic-server/ats-deployment.yaml
@@ -65,6 +65,8 @@ spec:
                   fieldPath: metadata.namespace
 #            - name: INGRESS_CLASS
 #              value: "ats"
+            - name: SNIPPET
+              value: "1"
             - name: POD_TLS_PATH
               value: "/etc/ats/ssl"
           ports:

Reply via email to