Skip to content

Commit 776ca72

Browse files
jianxiaoguoduanhongyi
authored andcommitted
chore(controller): add volume expand support use patch
1 parent 0384bd0 commit 776ca72

7 files changed

Lines changed: 85 additions & 10 deletions

File tree

charts/controller/templates/controller-ingress.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ spec:
2525
{{- end }}
2626
backend:
2727
service:
28-
name: drycc-controller
28+
name: drycc-controller-api
2929
port:
3030
number: 80
3131
{{- if .Values.global.certManagerEnabled }}

rootfs/api/models/volume.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def expand(self, size):
4646
"size": self._get_size(self.size),
4747
"storage_class": settings.DRYCC_APP_STORAGE_CLASS,
4848
}
49-
self._scheduler.pvc.put(self.app.id, self.name, **kwargs)
49+
self._scheduler.pvc.patch(self.app.id, self.name, **kwargs)
5050
except KubeException as e:
5151
msg = 'There was a problem expand the volume ' \
5252
'{} for {}'.format(self.name, self.app_id)

rootfs/api/tests/test_volume.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ def test_volume_expand(self, mock_requests):
8686
data = {'name': 'myvolume1', 'size': '500G'}
8787
self.client.post('/v2/apps/{}/volumes'.format(app_id), data=data)
8888

89-
# Put
89+
# Patch
9090
url = '/v2/apps/{app_id}/volumes/{volume}'.format(app_id=app_id,
9191
volume='myvolume1')
92-
response = self.client.put(url, {'name': 'myvolume1', 'size': '100G'})
92+
response = self.client.patch(url, {'name': 'myvolume1', 'size': '100G'})
9393
self.assertEqual(response.status_code, 400)
9494

95-
response = self.client.put(url, {'name': 'myvolume1', 'size': '1024G'})
95+
response = self.client.patch(url, {'name': 'myvolume1', 'size': '1024G'})
9696
self.assertEqual(response.status_code, 200)
9797
# Fetch
9898
url = '/v2/apps/{app_id}/volumes'.format(app_id=app_id)

rootfs/api/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
url(r"^apps/(?P<id>{})/volumes/?$".format(settings.APP_URL_REGEX),
8080
views.AppVolumesViewSet.as_view({'get': 'list', 'post': 'create'})),
8181
url(r"^apps/(?P<id>{})/volumes/(?P<name>[-_\w]+)/?$".format(settings.APP_URL_REGEX),
82-
views.AppVolumesViewSet.as_view({'put': 'expand', 'delete': 'destroy'})),
82+
views.AppVolumesViewSet.as_view({'patch': 'expand', 'delete': 'destroy'})),
8383
url(r"^apps/(?P<id>{})/volumes/(?P<name>[-_\w]+)/path/?$".format(settings.APP_URL_REGEX),
8484
views.AppVolumeMountPathViewSet.as_view({'patch': 'path'})),
8585
# application resources

rootfs/bin/boot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55

66
# fail hard and fast even on pipelines
7-
set -eo pipefail
7+
set -e
88

99
# set debug based on envvar
1010
[[ $DRYCC_DEBUG == "true" ]] && set -x

rootfs/scheduler/mock.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,79 @@ def put(request, context):
799799
return request.json()
800800

801801

802+
def patch(request, context):
803+
"""Process a PATCH request to the kubernetes API"""
804+
url = cache_key(request.url)
805+
# type is the second last element
806+
resource_type = get_type(request.url)
807+
# check if the namespace being posted to exists
808+
if resource_type != 'namespaces':
809+
namespace = get_namespace(url, resource_type)
810+
if cache.get(namespace) is None:
811+
context.status_code = 404
812+
context.reason = 'Not Found'
813+
return {}
814+
815+
# figure out main resource if in subresource
816+
original_url = url
817+
subresource, resource_type, url = is_subresource(resource_type, url)
818+
if subresource != resource_type:
819+
cache.set(original_url, request.json(), None)
820+
821+
item = cache.get(url)
822+
if item is None:
823+
context.status_code = 404
824+
context.reason = 'Not Found'
825+
return {}
826+
# raise a 503 when we want to intentionally test for it
827+
elif item['metadata']['name'] == 'image-pull-failed-test':
828+
context.status_code = 503
829+
context.reason = 'Network Unreachable'
830+
return {}
831+
832+
data = request.json()
833+
834+
# merge new data into old but keep labels separate in case they changed
835+
if 'labels' in data['metadata']:
836+
labels = data['metadata'].pop('labels')
837+
item['metadata'].update(data['metadata'])
838+
data['metadata'] = item['metadata']
839+
# make sure only new labels are used
840+
data['metadata']['labels'] = labels
841+
842+
# split out deployments and replicasets? due to upsert_pods
843+
if resource_type in ['replicationcontrollers', 'replicasets', 'deployments']:
844+
if subresource == 'scale':
845+
# has minimal info so need to copy data
846+
replicas = data['spec']['replicas']
847+
data = copy.deepcopy(item) # full copy
848+
data['spec']['replicas'] = replicas
849+
850+
if 'status' not in data:
851+
# just use what was set last time
852+
data['status'] = {'observedGeneration': item['status']['observedGeneration']}
853+
854+
data['metadata']['resourceVersion'] += 1
855+
data['metadata']['generation'] += 1
856+
data['status']['observedGeneration'] += 1
857+
858+
# Update the individual resource
859+
cache.set(url, data, None)
860+
861+
if resource_type in ['replicationcontrollers', 'replicasets']:
862+
upsert_pods(data, url)
863+
elif resource_type == 'deployments':
864+
manage_replicasets(data, url)
865+
else:
866+
# Update the individual resource
867+
cache.set(url, data, None)
868+
869+
context.status_code = 200
870+
context.reason = 'OK'
871+
872+
return request.json()
873+
874+
802875
def delete(request, context):
803876
"""Process a DELETE request to the kubernetes API"""
804877
url = cache_key(request.url)
@@ -892,6 +965,8 @@ def mock_kubernetes(request, context):
892965
response = get(request, context)
893966
elif request.method == 'PUT':
894967
response = put(request, context)
968+
elif request.method == 'PATCH':
969+
response = patch(request, context)
895970
elif request.method == 'DELETE':
896971
response = delete(request, context)
897972

rootfs/scheduler/resources/pvc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ def create(self, namespace, name, **kwargs):
6565
"create persistentvolumeclaim {}".format(namespace))
6666
return response
6767

68-
def put(self, namespace, name, **kwargs):
68+
def patch(self, namespace, name, **kwargs):
6969
url = self.api('/namespaces/{}/persistentvolumeclaims/{}', namespace,
7070
name)
7171
data = self.manifest(namespace, name, **kwargs)
72-
response = self.http_put(url, json=data)
72+
response = self.http_patch(url, json=data, headers={"Content-Type": "application/merge-patch+json"}) # noqa
7373
if self.unhealthy(response.status_code):
7474
self.log(namespace, 'template used: {}'.format(json.dumps(data, indent=4)), 'DEBUG') # noqa
75-
raise KubeHTTPException(response, 'update HorizontalPodAutoscaler "{}"', name)
75+
raise KubeHTTPException(response, 'update persistentvolumeclaims "{}"', name)
7676
return response
7777

7878
def delete(self, namespace, name):

0 commit comments

Comments
 (0)