VirtualService 的校验脚本

Istio 的流量控制功能中,VirtualServiceDestinationRule 对象之间具有很强的关联性;另外这些规则需要用标签的形式落地到 Kubernetes 中的 ServicePod 中,很多时候因为操作不慎导致的一点小错误,例如标签错误、缺少 subset 定义、选择器冲突等,都会导致功能完全失效,因此就写了这个小脚本,从头到尾检查一遍相关配置。用法很简单:

./virtual-service-scanner.py flaskapp-default-v2
Validating VirtualService: flaskapp-default-v2
Destinations referenced: {'flaskapp.default.svc.cluster.local': ['v2', 'v3']}
Fetching all destination rules in current namespace.
Validating subsets of host flaskapp.default.svc.cluster.local
Service label is: {'app': 'flaskapp'}
Subset label is: {'version': 'v2'}
Pod selector: app=flaskapp,version=v2
1 pods found
Traceback (most recent call last):
  File "./virtual-service-scanner.py", line 148, in <module>
    main()
  File "./virtual-service-scanner.py", line 88, in main
    verify_virtualservice(args.object_name, kubectl_command)
  File "./virtual-service-scanner.py", line 117, in verify_virtualservice
    "Subset {} isn't defined.".format(subset_name)
AssertionError: Subset v3 isn't defined.

virtual-service-scanner.py [VirtualService]

会根据其中引用的 hostsubset 对其依赖的 DestinationRuleService 以及 Pod 进行逐一检查,如果其中有环节无法通过,脚本会出错退出,例如上面的例子中,VirtualService 引用了不存在的 subsetv3

项目地址:Github

相关

comments powered by Disqus