Beispiel #1
0
 def service_spec(self):
     # check_key_raise('ports', self.data)
     ports = check_key('ports', self.data, [{}])
     nodePort = check_key('nodePort', ports[0], None)
     self.body.spec = dict(
         # [{"name": "https","protocol": "TCP","port": 443,"targetPort": 6443}]
         ports=check_key('ports', self.data, []),
         selector=self.basic,
         type=(None, "NodePort")[nodePort is not None])
     return self.body.spec
Beispiel #2
0
 def __init__(self, api_client=None, body=None, data=None):
     if api_client is None:
         api_client = client.ExtensionsV1beta1Api()
     if body is None:
         body = client.V1Deployment()
     if data is None:
         raise Exception("data is None")
     self.data = data
     self.api_client = api_client
     self.body = body
     self.username = current_user.username
     self.basic = check_key('lables', data, {
         'app': check_key('name', data),
         'namespace': self.username
     })
Beispiel #3
0
 def __init__(self, api_client=None, body=None, data=None):
     if api_client is None:
         api_client = client.AutoscalingV2beta1Api()
     if body is None:
         body = client.V2beta1HorizontalPodAutoscaler()
     if data is None:
         raise Exception("data is None")
     self.data = data
     self.api_client = api_client
     self.body = body
     self.username = current_user.username
     self.basic = check_key('lables', data, {
         'app': check_key('name', data),
         'namespace': self.username
     })
Beispiel #4
0
def register():
    if request.json and request.method == 'POST':
        data = request.get_json()
        email = check_key('email', data)
        if data['username'] is None or data['password'] is None:
            abort(400)
        if User.query.filter_by(username=data['username']).first() is not None:
            abort(400)
        v1 = client.CoreV1Api()
        body = client.V1Namespace()
        body.kind = 'Namespace'
        body.metadata = dict(name=data['username'], lables={
            'name': data['username']
        })
        pretty = 'true'
        try:
            api_response = v1.create_namespace(body, pretty=pretty)
            print(api_response)
        except ApiException as e:
            print("Exception when calling CoreV1Api->create_namespace: %s\n" % e)
            return jsonify({'code': 500, 'msg': '用户已存在'})
        user_data = User(username=data['username'])
        user_data.email = email
        user_data.hash_password(data['password'])
        db.session.add(user_data)
        db.session.commit()
        return jsonify({'code': 200, 'msg': '帐号注册成功'})
    else:
        return jsonify({'code': 500, 'msg': '请求方式错误'})
Beispiel #5
0
 def auto_scale_spec(self):
     self.body.spec = {
         'maxReplicas':
         check_key_raise('maxReplicas', self.data),
         'minReplicas':
         check_key_raise('minReplicas', self.data),
         'scaleTargetRef':
         scale_target_ref(check_key_raise('name', self.data)),
         'metrics': [{
             'type': check_key('type', self.data, 'Resource'),
             'resource': {
                 'name':
                 check_key('resource_name', self.data, 'cpu'),
                 'targetAverageUtilization':
                 check_key_raise('cpu_threshold', self.data),
             }
         }]
     }
     return self.body.spec
Beispiel #6
0
 def deploy_spec(self):
     self.body.spec = {
         'replicas': check_key('replicas', self.data, 1),
         'selector': {
             'matchLabels': self.basic,
         },
         'template': {
             'metadata': {
                 'labels': self.basic,
             },
             'spec': {
                 'containers': [{
                     'name':
                     self.data['name'],
                     'image':
                     self.data['image'],
                     'command':
                     check_key('command', self.data, []),
                     # [{"containerPort": 80, "protocol": "TCP"}]
                     'ports':
                     check_key('deploy_ports', self.data, None),
                     'resources': {
                         # requests资源的最小申请量,limits资源最大允许使用量
                         'requests':
                         check_key('requests', self.data, {
                             'cpu': '0.05',
                             'memory': '16Mi'
                         }),
                         'limits':
                         check_key('limits', self.data, {
                             'cpu': '0.1',
                             'memory': '1Gi'
                         }),
                     },
                     'imagePullPolicy':
                     check_key('imagePullPolicy', self.data,
                               'IfNotPresent'),
                     'restartPolicy':
                     check_key('restartPolicy', self.data, 'Always'),
                     # [{"name": "MYSQL_SERVICE_HOST", "value": "mysql"}]
                     'env':
                     check_key('env', self.data, None),
                 }]
             }
         }
     }
     return self.body.spec
Beispiel #7
0
def node_list():
    v1 = client.CoreV1Api()
    ret = v1.list_node()
    data = request.get_json()
    state = check_key('state', data)
    if state is not None and state == 'online':
        res = []
        for item in ret.items:
            if item.status.conditions[-1].status == "True":
                res.append(item)
    elif state is not None and state == 'offline':
        res = []
        for item in ret.items:
            if item.status.conditions[-1].status == "False":
                res.append(item)
    else:
        res = ret.items
    return jsonify({
        'code':
        200,
        'msg':
        'ok',
        'data': [{
            'name':
            i.metadata.name,
            'ip':
            i.status.addresses[0].address,
            'image':
            i.status.node_info.os_image,
            'version':
            i.status.node_info.kernel_version,
            'cpu':
            i.status.allocatable['cpu'],
            'memory':
            int(i.status.allocatable['memory'][0:-2]) / 1024 / 1024,
            'create_time':
            i.metadata.creation_timestamp.strftime("%Y-%m-%d %H:%M:%S"),
        } for i in res]
    })