12.4. Fabric8 Karaf 機能の使用

Fabric8 は Apache Karaf のサポートを提供し、Kubernetes の OSGi アプリケーションの開発を容易にします。

Fabric8 の重要な機能を以下に示します。

  • Blueprint XML ファイルでプレースホルダーを解決するさまざまなストラテジー。
  • 環境変数
  • システムプロパティー
  • サービス
  • Kubernetes の ConfigMap
  • Kubernetes の Secret
  • Kubernetes の設定マップを使用して、OSGi 設定管理を動的に更新します。
  • OSGi サービスの Kubernetes ヘルスチェックを提供します。

12.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 サーバーにインストールされます。

12.4.2. Fabric8 Karaf Core バンドル機能の追加

fabric8-karaf-core バンドルは、Blueprint および ConfigAdmin エクステンションによって使用される機能を提供します。

手順

  1. プロジェクトの pom.xml ファイルを開き、fabric8-karaf-corestartupFeatures セクションに追加します。

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

    これにより、fabric8-karaf-core 機能がカスタム Karaf ディストリビューションに追加されます。

12.4.3. プロパティープレースホルダーサービスのオプション設定

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 にアクセスするには、サービスアカウントに view パーミッションが必要になります。view パーミッションをサービスアカウントに追加します。

    oc policy add-role-to-user view system:serviceaccount:$(oc project -q):default -n $(oc project -q)
  2. API 経由での Secret へのアクセスは制限されている可能性があるため、Secret を Pod にマウントします。
  3. Secret は Pod ではボリュームマウントとして使用でき、以下のように 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

12.4.4. カスタムのプロパティープレースホルダーリゾルバーの追加

カスタムのプレースホルダーリゾルバーを追加して、カスタムの暗号化などの特定の必要項目をサポートできます。また、PlaceholderResolver サービスを使用して、Blueprint および ConfigAdmin がリゾルバーを使用できるようにすることもできます。

手順

  1. 以下の mvn 依存関係をプロジェクトの pom.xml に追加します。

    pom.xml

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

  2. PropertiesFunction インターフェイスを実装し、CSR を使用して 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. 以下のように、設定管理でリゾルバーを参照することができます。

    properties

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

12.4.5. 解決ストラテジーのリスト

PlaceholderResolver サービスは、異なるプロパティープレースホルダー解決ストラテジーのコレクターとして動作します。デフォルトで提供される解決ストラテジーを以下の表に示します。

  1. 解決ストラテジーのリスト

接頭辞

説明

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 またはボリュームマウント経由) からプロパティーを検索します。

12.4.6. プロパティープレースホルダーサービスのオプションリスト

プロパティープレースホルダーサービスは以下のオプションをサポートします。

  1. プロパティープレースホルダーサービスのオプションリスト
名前デフォルト説明

fabric8.placeholder.prefix

$[

プレースホルダーの接頭辞。

fabric8.placeholder.suffix

]

プレースホルダーの接尾辞。

fabric8.k8s.secrets.path

null

Secret がマップされたパスのコンマ区切りリスト。

fabric8.k8s.secrets.api.enabled

false

API 経由で Secret の消費を有効または無効にする。