You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/04/18 07:15:19 UTC

[apisix] branch master updated: feat: inject kubernetes discovery environment variable (#6869)

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

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new ec2124276 feat: inject kubernetes discovery environment variable (#6869)
ec2124276 is described below

commit ec21242765e117e302e017aff0a6179cf2f4107a
Author: zhixiongdu <ro...@libssl.com>
AuthorDate: Mon Apr 18 15:15:12 2022 +0800

    feat: inject kubernetes discovery environment variable (#6869)
---
 apisix/cli/ops.lua       | 41 +++++++++++++++++++++++++++++++++++++++++
 t/cli/test_kubernetes.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)

diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua
index 28441266c..6da4fa900 100644
--- a/apisix/cli/ops.lua
+++ b/apisix/cli/ops.lua
@@ -644,6 +644,47 @@ Please modify "admin_key" in conf/config.yaml .
         end
     end
 
+    -- inject kubernetes discovery environment variable
+    if enabled_discoveries["kubernetes"] then
+
+        local kubernetes_conf = yaml_conf.discovery["kubernetes"]
+
+        local keys = {
+            kubernetes_conf.service.host,
+            kubernetes_conf.service.port,
+        }
+
+        if kubernetes_conf.client.token then
+            table_insert(keys, kubernetes_conf.client.token)
+        end
+
+        if kubernetes_conf.client.token_file then
+            table_insert(keys, kubernetes_conf.client.token_file)
+        end
+
+        local envs = {}
+
+        for _, key in ipairs(keys) do
+            if #key > 3 then
+                local first, second = str_byte(key, 1, 2)
+                if first == str_byte('$') and second == str_byte('{') then
+                    local last = str_byte(key, #key)
+                    if last == str_byte('}') then
+                        envs[str_sub(key, 3, #key - 1)] = ""
+                    end
+                end
+            end
+        end
+
+        if not sys_conf["envs"] then
+            sys_conf["envs"] = {}
+        end
+
+        for item in pairs(envs) do
+            table_insert(sys_conf["envs"], item)
+        end
+    end
+
     -- fix up lua path
     sys_conf["extra_lua_path"] = get_lua_path(yaml_conf.apisix.extra_lua_path)
     sys_conf["extra_lua_cpath"] = get_lua_path(yaml_conf.apisix.extra_lua_cpath)
diff --git a/t/cli/test_kubernetes.sh b/t/cli/test_kubernetes.sh
new file mode 100755
index 000000000..bc371ee01
--- /dev/null
+++ b/t/cli/test_kubernetes.sh
@@ -0,0 +1,48 @@
+#!/usr/bin/env bash
+
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+. ./t/cli/common.sh
+
+echo '
+discovery:
+    kubernetes:
+      service:
+        host: ${HOST_ENV}
+      client:
+        token: ${TOKEN_ENV}
+' > conf/config.yaml
+
+make init
+
+if ! grep "env HOST_ENV" conf/nginx.conf; then
+    echo "kubernetes discovery env inject failed"
+    exit 1
+fi
+
+if ! grep "env KUBERNETES_SERVICE_PORT" conf/nginx.conf; then
+    echo "kubernetes discovery env inject failed"
+    exit 1
+fi
+
+if ! grep "env TOKEN_ENV" conf/nginx.conf; then
+    echo "kubernetes discovery env inject failed"
+    exit 1
+fi
+
+echo "kubernetes discovery env inject success"