示例#1
0
    def test_associate_pods(self):
        deps = K8Kat.deps().names('d11').go()
        self.assertEqual(self.the_pod_app_lbs(deps), ['d11'])

        deps = K8Kat.deps().ns('n1', 'n2').go()
        self.assertEqual(self.the_pod_app_lbs(deps),
                         ['d0', 'd11', 'd12', 'd21'])
示例#2
0
    def test_associate_svcs(self):
        deps = K8Kat.deps().names('d0').go()
        self.assertEqual(self.the_svc_names(deps), [])

        deps = K8Kat.deps().names('d11').go()
        self.assertEqual(self.the_svc_names(deps), ['d11'])

        deps = K8Kat.deps().names('d11', 'd12').go()
        self.assertEqual(self.the_svc_names(deps), ['d11'])

        deps = K8Kat.deps().ns('n1', 'n2').go()
        self.assertEqual(self.the_svc_names(deps), ['d11', 'd21'])
示例#3
0
    def test_names(self):
        result = K8Kat.deps().names('d11').go()
        self.assertEqual([dep.name for dep in result], ['d11'])

        result = K8Kat.deps().names('d11', 'd12').go()
        self.assertCountEqual([dep.name for dep in result], ['d11', 'd12'])

        result = K8Kat.deps().names('d11', 'd22').go()
        self.assertCountEqual([dep.name for dep in result], ['d11', 'd22'])

        result = K8Kat.deps().names('d11', 'd22').go()
        self.assertCountEqual([dep.name for dep in result], ['d11', 'd22'])
示例#4
0
    def test_lbs_inc_any(self):
        result = K8Kat.deps().ns('n1').lbs_inc_any(c='c', l1='v1').go()
        self.assertCountEqual([dep.name for dep in result], ['d11', 'd12'])

        result = K8Kat.deps().ns('n1', 'n2').lbs_inc_any(c='c', l1='v1').go()
        self.assertCountEqual([dep.name for dep in result],
                              ['d11', 'd12', 'd21'])

        q = K8Kat.deps().ns('n1', 'n2').lbs_inc_any(c='c', l1='v1')
        result = q.lbs_inc_each(l1='v1').go()
        self.assertCountEqual([dep.name for dep in result], ['d11', 'd21'])

        q = K8Kat.deps().ns('n1', 'n2').lbs_inc_each(l1='v1')
        result = q.lbs_inc_any(c='c', l1='v1').go()
        self.assertCountEqual([dep.name for dep in result], ['d11', 'd21'])
示例#5
0
def label_matrix():
    get = lambda *keys: [request.args.get(k) for k in keys]
    _type, ns, name = get('matcher_type', 'matcher_ns', 'matcher_name')
    collection = K8Kat.deps() if _type == 'deployment' else K8Kat.svcs()
    matcher = collection.ns(ns).find(name)
    result = ResUtils.label_matrix(matcher)
    return jsonify(result)
示例#6
0
def params_to_deps():
    q = K8Kat.deps()

    ns_white = request.args.get('ns_filter_type', 'whitelist')
    ns_white = True if ns_white == 'whitelist' else False
    ns_filters = request.args.get('ns_filters')
    ns_filters = ns_filters and ns_filters.split(',') or None

    lb_white = request.args.get('lb_filter_type', 'blacklist')
    lb_white = True if lb_white == 'whitelist' else False
    lb_filters = request.args.get('lb_filters')
    lb_filters = lb_filters and eq_strs_to_tups(lb_filters)

    if ns_filters is not None:
        q = q.ns(ns_filters) if ns_white else q.not_ns(ns_filters)

    if lb_filters is not None:
        q = q.lbs_inc_each(lb_filters) if lb_white else q.lbs_exc_each(
            lb_filters)

    deps = q.go()

    if request.args.get('svcs') == 'true':
        DepComposer.associate_svcs(deps)

    if request.args.get('pods') == 'true':
        DepComposer.associate_pods(deps)

    return deps
示例#7
0
 def dep_by_ns() -> List[Dict[str, str]]:
     from k8_kat.base.k8_kat import K8Kat
     deps = K8Kat.deps().not_ns('kube-system').go()
     output = []
     for name in set([dep.name for dep in deps]):
         appears_in = set([dep.ns for dep in deps if dep.name == name])
         output.append(dict(name=name, namespaces=list(appears_in)))
     return output
示例#8
0
def validate_labels(ns, name):
    dep = K8Kat.deps().ns(ns).find(name)
    v = DepWarnings
    return jsonify(
        dict(template_covers=v.check_pod_template_inclusive(dep),
             no_eavesdrop=v.check_no_pods_eavesdrop(dep),
             labels_unique=v.check_labels_unique(dep),
             pods_same_ns=v.check_pods_in_same_ns(dep)))
示例#9
0
 def stdSetUpClass(cls, step_class):
     cls.dep = K8Kat.deps().ns(TESTING_NS).find(TESTING_DEP_NM)
     cls.svc = K8Kat.svcs().ns(TESTING_NS).find(TESTING_SVC_NM)
     cls.step = step_class(
         from_port=cls.svc.from_port,
         dep_ns=TESTING_NS,
         svc_name=TESTING_SVC_NM,
         dep_name=TESTING_DEP_NM,
     )
示例#10
0
    def test_lbs_inc_each(self):
        pass
        result = K8Kat.deps().ns(['n1']).lbs_inc_each(l1='v1').go()
        self.assertCountEqual([dep.name for dep in result], ['d11'])

        result = K8Kat.deps().ns('n1').lbs_inc_each([('c', 'c')]).go()
        self.assertCountEqual([dep.name for dep in result], ['d11', 'd12'])

        result = K8Kat.deps().ns('n1', 'n2').lbs_inc_each(l1='v1').go()
        self.assertCountEqual([dep.name for dep in result], ['d11', 'd21'])

        q = [('l1', 'v1'), ('c', 'c')]
        result = K8Kat.deps().ns('n1', 'n2').lbs_inc_each(q).go()
        self.assertCountEqual([dep.name for dep in result], ['d11'])

        result = K8Kat.deps().ns('n1', 'n2').lbs_inc_each(c='c').go()
        self.assertCountEqual([dep.name for dep in result], ['d11', 'd12'])

        q = [('x', 'y'), ('c', 'c')]
        result = K8Kat.deps().ns('n1', 'n2').lbs_inc_each(q).go()
        self.assertCountEqual([dep.name for dep in result], [])
示例#11
0
def show(ns, name):
    dep = K8Kat.deps().ns(ns).find(name).with_friends()
    serialized = dep.serialize(Ser.with_pods_and_svcs)
    return jsonify(serialized)
示例#12
0
def scale_replicas():
    j = request.json
    ns, name, scale_to = j['dep_namespace'], j['dep_name'], j['scale_to']
    K8Kat.deps().ns(ns).find(name).scale(int(scale_to))
    return jsonify(data=dict(status='working'))
示例#13
0
def new_image():
    j = request.json
    ns, name, img_name = j['dep_namespace'], j['dep_name'], j['target_name']
    K8Kat.deps().ns(ns).find(name).replace_image(img_name)
    return jsonify(data=dict(status='working'))
示例#14
0
def image_reload():
    j = request.json
    ns, name = j['dep_namespace'], j['dep_name']
    K8Kat.deps().ns(ns).find(name).restart_pods()
    return jsonify(data=dict(status='working'))
示例#15
0
 def check_labels_unique(dep):
     twins = K8Kat.deps().ns(dep.ns).lbs_inc_each(dep.labels)
     return len(twins.go()) == 0
示例#16
0
def deployment_pods(ns, name):
    dep = K8Kat.deps().ns(ns).find(name)
    serialized = [PodSerialization.standard(pod) for pod in dep.pods()]
    return jsonify(dict(data=serialized))
示例#17
0
def deployment_services(ns, name):
    dep = K8Kat.deps().ns(ns).find(name)
    svcs = dep.svcs()
    serialized = [SvcSerialization.with_endpoints(svc) for svc in svcs]
    return jsonify(dict(data=serialized))
示例#18
0
 def __init__(self, **args):
     super().__init__()
     self.from_port = args['from_port']
     self.dep = K8Kat.deps().ns(args['dep_ns']).find(args['dep_name'])
     self.svc = K8Kat.svcs().ns(args['dep_ns']).find(args['svc_name'])
     self._stunt_pod = None
示例#19
0
 def check_no_pods_eavesdrop(dep):
     template = dep.template_labels.items()
     other_ns_deps = K8Kat.deps().ns(dep.ns).not_names(dep.name)
     drops = lambda o_dep: dep.pod_select_labels.items() <= template
     eavesdroppers = [d for d in other_ns_deps if drops(d)]
     return len(eavesdroppers) == 0