Helm 正是为这样的事情而设计的。它处理一组复杂的资源部署作为一个组等。
但如果我们仍在寻找一些简单的替代方案,那么使用 ant 怎么样?
如果您想在构建过程或测试过程中修改文件,那么您也可以使用 ant 任务。
使用 ant 您可以将所有环境值加载为属性,或者您可以简单地加载属性文件,例如:
<property environment="env" />
<property file="build.properties" />
然后你可以有一个目标,将模板文件转换成你想要的 yaml 文件。
<target name="generate_from_template">
<!-- Copy task to replaces values and create new file -->
<copy todir="${dest.dir}" verbose="true" overwrite="true" failonerror="true">
<!-- List of files to be processed -->
<fileset file="${source.dir}/xyz.template.yml" />
<!-- Mapper to transform filename. Removes '.template' from the file
name when copying the file to output directory -->
<mapper type="regexp" from="(.*).template(.*)" to="\1\2" />
<!-- Filter chain that replaces the template values with actual values
fetched from properties file -->
<filterchain>
<expandproperties />
</filterchain>
</copy>
</target>
当然,您可以使用fileset 而不是file,以防您想动态更改多个文件(嵌套或其他)的值
您的模板文件xyz.template.yml 应如下所示:
apiVersion: v1
kind: Service
metadata:
name: ${XYZ_RES_NAME}-ser
labels:
app: ${XYZ_RES_NAME}
version: v1
spec:
type: NodePort
ports:
- port: ${env.XYZ_RES_PORT}
protocol: TCP
selector:
app: ${XYZ_RES_NAME}
version: v1
env. 属性从环境变量加载,其他从属性文件加载
希望它有所帮助:)