特定のパーセンテージよりも高いパーセンテージを強制し、整数値を拒否するためPodDisriptionBudgets
に、制約テンプレートを Kubernetes クラスターにデプロイしようとしています。maxUnavailable
ただし、整数または文字列maxUnavailable
になる可能性があるため、安全に評価する方法がわかりません。私が使用している制約テンプレートは次のとおりです。
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
name: pdbrequiredtolerance
spec:
crd:
spec:
names:
kind: PdbRequiredTolerance
validation:
# Schema for the `parameters` field
openAPIV3Schema:
properties:
minAllowed:
type: integer
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package pdbrequiredtolerance
# Check that maxUnavailable exists
violation[{"msg": msg }] {
not input.review.object.spec.maxUnavailable
msg := "You must use maxUnavailable on your PDB"
}
# Check that maxUnavailable is a string
violation[{"msg": msg}] {
not is_string(input.review.object.spec.maxUnavailable)
msg := "maxUnavailable must be a string"
}
# Check that maxUnavailable is a percentage
violation[{"msg": msg}] {
not endswith(input.review.object.spec.maxUnavailable,"%")
msg := "maxUnavailable must be a string ending with %"
}
# Check that maxUnavailable is in the accpetable range
violation[{"msg": msg}] {
percentage := split(input.review.object.spec.maxUnavailable, "%")
to_number(percentage[0]) < input.parameters.minAllowed
msg := sprintf("You must have maxUnavailable of %v percent or higher", [input.parameters.minAllowed])
}
値が高すぎる PDB を入力すると、予想されるエラーが表示されます。
Error from server ([pdb-must-have-max-unavailable] You must have maxUnavailable of 30 percent or higher)
ただし、整数値を持つ PDB を使用すると、次のようになります。
Error from server (admission.k8s.gatekeeper.sh: __modset_templates["admission.k8s.gatekeeper.sh"]["PdbRequiredTolerance"]_idx_0:14: eval_type_error: endswith: operand 1 must be string but got number)
これは、endswith
ルールが文字列を評価しようとしているためです。Gatekeeperでこれを回避する方法はありますか? 私が指定した両方の PDB は、有効な Kubernetes マニフェストです。この紛らわしいエラーをエンド ユーザーに返すつもりはありません。むしろ、整数を使用できないことを明確にしたいと思います。