11.4. 使用 Fabric8 Karaf 功能

Fabric8 为 Apache Karaf 提供支持,使开发用于 Kubernetes 的 OSGi 应用变得更加简单。

Fabric8 的主要功能如下:

  • 用于解析蓝图 XML 文件中的占位符的不同策略。
  • 环境变量
  • 系统属性
  • 服务
  • Kubernetes ConfigMap
  • Kubernetes Secret
  • 使用 Kubernetes 配置映射动态更新 OSGi 配置管理。
  • 为 OSGi 服务提供 Kubernetes 热检查。

11.4.1. 添加 Fabric8 Karaf 功能

要使用这些功能,请将 fabric8-karaf-features 依赖项添加到项目 POM 文件。

流程

  1. 打开项目的 pom.xml 文件,并添加 fabric8-karaf-features 依赖项。
<dependency>
  <groupId>io.fabric8</groupId>
  <artifactId>fabric8-karaf-features</artifactId>
  <version>${fabric8.version}</version>
  <classifier>features</classifier>
  <type>xml</type>
</dependency>

fabric8 karaf 功能将安装到 Karaf 服务器中。

11.4.2. 添加 Fabric8 Karaf 核心捆绑包功能

bundle fabric8-karaf-core 提供 Blueprint 和 ConfigAdmin 扩展使用的功能。

流程

  1. 打开项目的 pom.xml,并将 fabric8-karaf-core 添加到 startupFeatures 部分。

    <startupFeatures>
      ...
      <feature>fabric8-karaf-core</feature>
      ...
    </startupFeatures>

    这将在自定义 Karaf 分发中添加 fabric8-karaf-core 功能。

11.4.3. 设置属性 Placeholder 服务选项

bundle fabric8-karaf-core 使用以下接口导出服务 PlaceholderResolver

public interface PlaceholderResolver {
    /**
     * Resolve a placeholder using the strategy indicated by the prefix
     *
     * @param value the placeholder to resolve
     * @return the resolved value or null if not resolved
     */
    String resolve(String value);

    /**
     * Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template.
     *
     * @param source the string to replace in
     * @return the result of the replace operation
     */
    String replace(String value);

    /**
     * Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.
     *
     * @param value the builder to replace in
     * @rerurn true if altered
     */
    boolean replaceIn(StringBuilder value);

    /**
     * Replaces all the occurrences of variables within the given dictionary
     *
     * @param dictionary the dictionary to replace in
     * @rerurn true if altered
     */
    boolean replaceAll(Dictionary<String, Object> dictionary);

    /**
     * Replaces all the occurrences of variables within the given dictionary
     *
     * @param dictionary the dictionary to replace in
     * @rerurn true if altered
     */
    boolean replaceAll(Map<String, Object> dictionary);
}

PlaceholderResolver 服务充当不同属性占位符解析策略的收集器。默认情况下提供的解析策略列在表 解析策略 中。要设置属性占位符服务选项,您可以使用系统属性或环境变量或两者。

流程

  1. 要访问 OpenShift 上的 ConfigMap,服务帐户需要查看权限。为服务帐户添加查看权限。

    oc policy add-role-to-user view system:serviceaccount:$(oc project -q):default -n $(oc project -q)
  2. 将 secret 挂载到 Pod,以便通过 API 访问 secret。
  3. Pod 上作为卷挂载提供的 secret 映射到名为 secret 的目录,如下所示

    containers:
      -
       env:
       - name: FABRIC8_K8S_SECRETS_PATH
         value: /etc/secrets
         volumeMounts:
       - name: activemq-secret-volume
         mountPath: /etc/secrets/activemq
         readOnly: true
       - name: postgres-secret-volume
         mountPath: /etc/secrets/postgres
         readOnly: true
    
    volumes:
      - name: activemq-secret-volume
      secret:
      secretName: activemq
      - name: postgres-secret-volume
       secret:
      secretName: postgres

11.4.4. 添加自定义属性占位符解析器

您可以添加自定义占位符解析器来支持特定的需求,如自定义加密。您还可以使用 PlaceholderResolver 服务将解析器提供给 Blueprint 和 ConfigAdmin。

流程

  1. 将以下 mvn 依赖项添加到项目 pom.xml 中。

    pom.xml

    ---
    <dependency>
      <groupId>io.fabric8</groupId>
      <artifactId>fabric8-karaf-core</artifactId>
    </dependency>
    ---

  2. 实施 PropertiesFunction 接口,并使用 SCR 将其注册为 OSGi 服务。

    import io.fabric8.karaf.core.properties.function.PropertiesFunction;
    import org.apache.felix.scr.annotations.Component;
    import org.apache.felix.scr.annotations.ConfigurationPolicy;
    import org.apache.felix.scr.annotations.Service;
    
    @Component(
        immediate = true,
        policy = ConfigurationPolicy.IGNORE,
        createPid = false
    )
    @Service(PropertiesFunction.class)
    public class MyPropertiesFunction implements PropertiesFunction {
        @Override
        public String getName() {
            return "myResolver";
        }
    
        @Override
        public String apply(String remainder) {
            // Parse and resolve remainder
            return remainder;
        }
    }
  3. 您可以在配置管理中引用解析器,如下所示:

    属性

    my.property = $[myResolver:value-to-resolve]

11.4.5. 解析策略列表

PlaceholderResolver 服务充当不同属性占位符解析策略的收集器。默认情况下提供的解析策略列在表中。

  1. 解析策略列表

prefix

示例

描述

env

env:JAVA_HOME

从 OS 环境变量中查找属性。

'sys

sys:java.version

从 Java JVM 系统属性中查找属性。

'service

service:amq

使用服务命名约定,从 OS 环境变量中查找属性。

service.host

service.host:amq

使用服务命名规则从 OS 环境变量中查找属性,仅返回主机名部分。

service.port

service.port:amq

使用服务命名约定,从 OS 环境变量中查找属性,仅返回端口部分。

k8s:map

k8s:map:myMap/myKey

从 Kubernetes ConfigMap (通过 API)中查找属性

k8s:secret

k8s:secret:amq/password

从 Kubernetes Secret (通过 API 或卷挂载)中查找属性

11.4.6. Property Placeholder 服务选项列表

属性占位符服务支持以下选项:

  1. 属性占位符服务选项列表
Namedefault描述

fabric8.placeholder.prefix

$[

占位符前缀

fabric8.placeholder.suffix

]

占位符后缀

fabric8.k8s.secrets.path

null

以逗号分隔的路径列表,其中 secret 被映射

fabric8.k8s.secrets.api.enabled

false

通过 API 启用/禁用消耗 secret