4주차 과제 - 최원준

OpenStack openstack cached image clear 명령어 정리

1. Glance란?

https://docs.openstack.org/glance/latest/

  • 오픈스택의 이미지 서비스입니다.

  • OpenStack 클라우드에서 가상머신·볼륨 템플릿(이미지)을 • 등록(upload) • 저장(백엔드 스토어) • 검색 • 전송(download) 하는 중앙 이미지 레지스트리입니다.

2. Glance 이미지 캐시란?

  • Glance(이미지 서비스)는 각 API 노드에 로컬 이미지 캐시를 둘 수 있습니다.

  • 자주 요청되는 이미지 파일을 로컬 디스크에 저장해 백엔드 스토리지->API 노드 간 데이터 전송을 줄이고, 응답 속도를 높이는 목적입니다.

  • 단, 캐시는 자동으로 정리되지 않으므로 운영자가 주기적으로 관리하거나 수동으로 비울 필요가 있습니다.


3. openstack cached image clear 명령어의 역할

  • Glance 로컬 이미지 캐시에 저장된 모든 이미지 파일(cache)과 프리페치 대기열(queue)에 올라 있는 항목을 한 번에 삭제합니다.


4. 사용 방법 및 주요 옵션

$ openstack cached image clear [--target <scope>]

옵션 의미
(없음) / --target both 캐시된 이미지 + 대기열 항목 모두 삭제(기본)
--target cache 캐시된 이미지만 삭제, 대기열은 유지
--target queue 대기열 항목만 삭제, 캐시된 이미지는 유지

성공 시 별도 출력 없이 0으로 종료됩니다. 이후 openstack cached image list로 비워졌는지 확인할 수 있습니다.


5. 내부 동작 흐름

  1. CLI 매핑

    • openstack cached image clearopenstackclient.image.v2.cache.ClearCachedImage 클래스가 실행됩니다.
    class ClearCachedImage(command.Command):
        _description = _("Clear all images from cache, queue or both")
    
        def get_parser(self, prog_name):
            parser = super().get_parser(prog_name)
            parser.add_argument(
                "--cache",
                action="store_const",
                const="cache",
                default="both",
                dest="target",
                help=_("Clears all the cached images"),
            )
            parser.add_argument(
                "--queue",
                action="store_const",
                const="queue",
                default="both",
                dest="target",
                help=_("Clears all the queued images"),
            )
            return parser
    
        def take_action(self, parsed_args):
            image_client = self.app.client_manager.image
    
            target = parsed_args.target
            try:
                image_client.clear_cache(target)
            except Exception:
                msg = _("Failed to clear image cache")
                LOG.error(msg)
                raise exceptions.CommandError(msg)
    
    
  2. 파서 설정

    • -target 인자를 cache | queue | both 중 하나로 제한, 기본값 both.
  3. image_client.clear_cache(target) 실행

    • openstacksdk.openstack.image.v2._proxy.Proxy 클래스가 실행됩니다
    class Proxy(proxy.Proxy):
    		...
        # ====== CACHE MANAGEMENT======
        def clear_cache(self, target='both'):
            """Clear all images from cache, queue or both
    
            :param target: Specify which target you want to clear
                One of: ``both``(default), ``cache``, ``queue``.
            """
            cache = self._get_resource(_cache.Cache, None)
            return cache.clear(self, target)
    
    
  4. openstacksdk.openstack.image.v2.cache.Cache 클래스의 clear 가 실행됩니다

    from openstack import exceptions
    from openstack import resource
    from openstack import utils
    
    class Cache(resource.Resource):
        ...
        def clear(self, session, target='both'):  # type: ignore[override]
            """Clears the cache.
            :param session: The session to use for making this request
            :param target: Specify which target you want to clear
                One of: ``both``(default), ``cache``, ``queue``.
            :returns: The server response
            """
            headers = {}
            if target in ('cache', 'queue'):
                headers = {'x-image-cache-clear-target': target}
            elif target != "both":
                raise exceptions.InvalidRequest(
                    'Target must be "cache", "queue" or "both".'
                )
            response = session.delete(self.base_path, headers=headers)
            exceptions.raise_from_response(response)
            return response
    
    
    

참고

https://docs.openstack.org/glance/2025.1/user/

https://somaz.tistory.com/125