Skip to content

Commit

Permalink
Add host to image table
Browse files Browse the repository at this point in the history
This patch solves two problems:
    1. When use "zun image-show" to see its details, we donot know
    which host the image is on.

    2. The unique constraint of image table in db is "repo" and "tag".
    If we pull ubuntu:test image on host1, an error will occur when
    we pull ubuntu:test on host2. Because the repo:tag that is the
    unique constrainthas has generated in db.

This patch adds host to image table and changes the unique constraint
to 'repo', 'tag', 'host'.

Change-Id: Id698d15fcc76ea9935d1d6ce4594459dd2ad9319
  • Loading branch information
weikeyou committed Aug 17, 2018
1 parent 1f7526f commit 72eac7c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions zun/api/controllers/v1/images.py
Expand Up @@ -144,6 +144,7 @@ def post(self, **image_dict):
image_dict['project_id'] = context.project_id
image_dict['user_id'] = context.user_id
repo_tag = image_dict.get('repo')
image_dict['host'] = host.hostname
image_dict['repo'], image_dict['tag'] = utils.parse_image_name(
repo_tag)
new_image = objects.Image(context, **image_dict)
Expand Down
@@ -0,0 +1,38 @@
# 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.

"""add host to image
Revision ID: a019998b09b5
Revises: a9c9fb54274a
Create Date: 2018-08-17 13:49:11.470002
"""

# revision identifiers, used by Alembic.
revision = 'a019998b09b5'
down_revision = 'a9c9fb54274a'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
op.add_column('image',
sa.Column('host', sa.String(length=255), nullable=True))
op.drop_constraint(constraint_name='uniq_image0repotag',
table_name='image', type_='unique')
op.create_unique_constraint(constraint_name='uniq_image0repotaghost',
table_name='image',
columns=['repo', 'tag', 'host'])
1 change: 1 addition & 0 deletions zun/db/sqlalchemy/models.py
Expand Up @@ -241,6 +241,7 @@ class Image(Base):
repo = Column(String(255))
tag = Column(String(255))
size = Column(String(255))
host = Column(String(255))


class ResourceProvider(Base):
Expand Down
6 changes: 4 additions & 2 deletions zun/objects/image.py
Expand Up @@ -19,8 +19,9 @@
@base.ZunObjectRegistry.register
class Image(base.ZunPersistentObject, base.ZunObject):
# Version 1.0: Initial version
# Version = '1.1': Add delete image
VERSION = '1.1'
# Version 1.1: Add delete image
# Version 1.2: Add host to image
VERSION = '1.2'

fields = {
'id': fields.IntegerField(),
Expand All @@ -31,6 +32,7 @@ class Image(base.ZunPersistentObject, base.ZunObject):
'repo': fields.StringField(nullable=True),
'tag': fields.StringField(nullable=True),
'size': fields.StringField(nullable=True),
'host': fields.StringField(nullable=True),
}

@staticmethod
Expand Down
1 change: 1 addition & 0 deletions zun/tests/unit/db/utils.py
Expand Up @@ -177,6 +177,7 @@ def get_test_image(**kwargs):
'user_id': kwargs.get('user_id', 'fake_user'),
'created_at': kwargs.get('created_at'),
'updated_at': kwargs.get('updated_at'),
'host': kwargs.get('host', 'host1'),
}


Expand Down
2 changes: 1 addition & 1 deletion zun/tests/unit/objects/test_objects.py
Expand Up @@ -346,7 +346,7 @@ class TestObject(test_base.TestCase, _TestObject):
object_data = {
'Container': '1.36-ad2bacdaa51afd0047e96003f93ff181',
'VolumeMapping': '1.3-14e3f9fc64e7afd751727c6ad3f32a94',
'Image': '1.1-330e6205c80b99b59717e1cfc6a79935',
'Image': '1.2-80504fdd797e9dd86128a91680e876ad',
'MyObj': '1.0-34c4b1aadefd177b13f9a2f894cc23cd',
'NUMANode': '1.0-cba878b70b2f8b52f1e031b41ac13b4e',
'NUMATopology': '1.0-b54086eda7e4b2e6145ecb6ee2c925ab',
Expand Down

0 comments on commit 72eac7c

Please sign in to comment.