summaryrefslogtreecommitdiffstats
path: root/share/starlingx_base/resource/k8s_infra_workload_helper.py
blob: eef6d3a10f006fe2a579ac25cdb41c0272160e46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright (c) 2017-2020 Wind River Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import logging
import json
import requests
import tarfile
from ruamel import yaml


from rest_framework import status
from rest_framework.response import Response

from django.conf import settings
from common.msapi import extsys
from newton_base.util import VimDriverUtils

logger = logging.getLogger(__name__)


# wrap calls to multicloud-k8s infra_workload API
class InfraWorkloadHelper:

    # def resettarfile(tarinfo):
    #     tarinfo.uid = tarinfo.gid = 0
    #     tarinfo.uname = tarinfo.gname = "root"
    #     return tarinfo

    @staticmethod
    def workload_create(self, vimid, workloadid, request):
        '''
        Deploy workload to target k8s via multicloud-k8s
        :param vimid:
        :param workloadid:
        :param request
        '''
        # resp_template = {
        #     "template_type": "HEAT",
        #     "workload_id": workloadid,
        #     "workload_status": "GET_FAILED",
        #     "workload_status_reason": "Exception occurs"
        # }
        # status_code = status.HTTP_500_INTERNAL_SERVER_ERROR

        # viminfo = VimDriverUtils.get_vim_info(vimid)
        workload_query = VimDriverUtils.get_query_part(request)
        workload_data = request.data

        # vf_module_model_customization_id = data.get("vf-module-model-customization-id", None)
        # vf_module_id = data.get("vf-module-id", "")
        user_directive = workload_data.get("user_directives", {})
        # oof_directive = data.get("oof_directives", {})
        # sdnc_directive = data.get("sdnc_directives", {})
        # template_type = data.get("template_type")
        # template_data = data.get("template_data", {})

        # 1, create profile if not exists
        # manifest.yaml content
        manifest_yaml = {
            "version": "v1",
            "type": {
                "values": "override_values.yaml"
            }
        }

        # override_values.yaml content
        override_values_yaml = ""

        # extract rb and profile info from user_directive
        rbname = "fakerbname"
        rbversion = "1"
        profilename = "p1"

        for attr in user_directive.get("attributes", []):
            aname = attr.get("attribute_name", None)
            avalue = attr.get("attribute_value", None)
            if aname == "override_values":
                # manifest_yaml = avalue["manifest_yaml"]
                # #override_values_yaml = avalue["override_values_yaml"]
                override_values_yaml = avalue
            elif aname == "definition-name":
                rbname = avalue
            elif aname == "definition-version":
                rbversion = avalue
            elif aname == "profile-name":
                profilename = avalue

        # package them into tarball
        basedir="/tmp/%s_%s_%s/" % (rbname, rbversion, profilename)
        manifest_yaml_filename="manifest.yaml"
        override_values_yaml_filename = "override_values.yaml"
        profile_filename = "profile.tar.gz"
        if not os.path.isdir(basedir):
            os.mkdir(basedir)
        logger.debug("k8s profile temp dir for %s,%s,%s is %s" % (rbname, rbversion, profilename, basedir))
        with open(basedir+manifest_yaml_filename, "w") as f:
            yaml.dump(manifest_yaml, f, Dumper=yaml.RoundTripDumper)
        with open(basedir+override_values_yaml_filename, "w") as f:
            #yaml.dump(override_values_yaml, f, Dumper=yaml.RoundTripDumper)
            f.write(override_values_yaml)

        tar = tarfile.open(basedir+profile_filename, "w:gz")
        # tar.add(basedir+manifest_yaml_filename, arcname=manifest_yaml_filename,filter=resettarfile)
        tar.add(basedir+manifest_yaml_filename, arcname=manifest_yaml_filename)
        tar.add(basedir+override_values_yaml_filename, arcname=override_values_yaml_filename)
        tar.close()

        # create profile and upload content
        create_rbprofile_json = {
            "rb-name": rbname,
            "rb-version": rbversion,
            "profile-name": profilename,
            "release-name": "r1",
            "namespace": "testnamespace1",
            "kubernetes-version": "1.16.2"
        }

        multicloudK8sUrl = "%s://%s:%s/api/multicloud-k8s/v1" % (
            settings.MSB_SERVICE_PROTOCOL, settings.MSB_SERVICE_ADDR, settings.MSB_SERVICE_PORT)
        profileUrl = multicloudK8sUrl+"/v1/rb/definition/%s/%s/profile" % (rbname, rbversion)

        #data = open('create_rbprofile.json')
        response = requests.post(profileUrl, data=json.dumps(create_rbprofile_json), verify=False)
        logger.debug("create profile, returns: %s,%s" % (response.content, response.status_code))

        profileContentUrl = profileUrl + "/%s/content" % (profilename)
        #profileContent = open(basedir+profile_filename, 'rb').read()
        with open(basedir+profile_filename, "rb") as profileContent:
            response = requests.post(profileContentUrl, data=profileContent.read(), verify=False)
            logger.debug("upload profile content, returns: %s,%s" % (response.content, response.status_code))

        # 2.forward infra_workload API requests with queries
        cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
        infraUrl = multicloudK8sUrl+"/%s/%s/infra_workload" % (cloud_owner, cloud_region_id)
        if workload_query:
            infraUrl += ("?%s" % workload_query)

        # should we forward headers ? TBD
        resp = requests.post(infraUrl, data=workload_data, verify=False)
        # resp_template["workload_status_reason"] = resp.content
        # status_code = resp.status_code
        return Response(data=resp.content, status=resp.status_code)


    @staticmethod
    def workload_delete(self, vimid, workloadid, request):
        '''
        remove workload
        '''
        # resp_template = {
        #     "template_type": "HEAT",
        #     "workload_id": workloadid,
        #     "workload_status": "GET_FAILED",
        #     "workload_status_reason": "Exception occurs"
        # }
        # status_code = status.HTTP_500_INTERNAL_SERVER_ERROR

        workload_query_str = VimDriverUtils.get_query_part(request)
        workload_data = request.data
        
        multicloudK8sUrl = "%s://%s:%s/api/multicloud-k8s/v1" % (
            settings.MSB_SERVICE_PROTOCOL, settings.MSB_SERVICE_ADDR, settings.MSB_SERVICE_PORT)

        # 1.forward infra_workload API requests with queries
        cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
        infraUrl = multicloudK8sUrl+"/%s/%s/infra_workload" % (cloud_owner, cloud_region_id)
        if workload_query_str:
            infraUrl += ("?%s" % workload_query_str)

        # should we forward headers ? TBD
        resp = requests.delete(infraUrl, data=workload_data, verify=False)
        # resp_template["workload_status_reason"] = resp.content
        # status_code = resp.status_code        
        return Response(data=resp.content, status=resp.status_code)


    @staticmethod
    def workload_detail(self, vimid, workloadid, request):
        '''
        get workload status
        '''
        # resp_template = {
        #     "template_type": "HEAT",
        #     "workload_id": workloadid,
        #     "workload_status": "GET_FAILED",
        #     "workload_status_reason": "Exception occurs"
        # }
        # status_code = status.HTTP_500_INTERNAL_SERVER_ERROR

        workload_query_str = VimDriverUtils.get_query_part(request)
        workload_data = request.data
        
        multicloudK8sUrl = "%s://%s:%s/api/multicloud-k8s/v1" % (
            settings.MSB_SERVICE_PROTOCOL, settings.MSB_SERVICE_ADDR, settings.MSB_SERVICE_PORT)

        # 1.forward infra_workload API requests with queries
        cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
        infraUrl = multicloudK8sUrl+"/%s/%s/infra_workload" % (cloud_owner, cloud_region_id)
        if workload_query_str:
            infraUrl += ("?%s" % workload_query_str)

        # should we forward headers ? TBD
        resp = requests.get(infraUrl, data=workload_data, verify=False)
        # resp_template["workload_status_reason"] = resp.content
        # status_code = resp.status_code
        return Response(data=resp.content, status=resp.status_code)