사용자 지정 플러그인에서 HCP Packer 지원 활성화
사용자 지정 플러그인에서 HCP Packer 지원 활성화
이 페이지는 사용자 지정 플러그인이 이미지 메타데이터를 HCP Packer 레지스트리에 게시할 수 있도록 플러그인을 갱신하는 방법을 설명해요. 외부 Packer 플러그인을 만드는 방법은 사용자 지정 빌더와 사용자 지정 포스트-프로세서 문서를 참고하세요.
출처: Packer 공식 문서
본문
메타데이터를 HCP Packer 레지스트리로 푸시하기 전에, Packer는 par.artifact.metadata 키를 사용해서 빌더 아티팩트에서 특정 컴포넌트가 레지스트리에 저장하고 싶어하는 이미지 메타데이터를 조회해요.
이미지 메타데이터를 관리하는 방법에 대한 자세한 내용과 예시는 HCP Packer GitHub 문서를 참고하세요.
HCP Packer는 활발히 개발 중이며, 플러그인 유지 관리자는 SDK 변경 사항을 잘 쫓아가면서 더 많은 HCP Packer 지원에 대비하는 것이 좋아요.
빌더 아티팩트 (Builder Artifact)
HCP Packer를 지원하려면 플러그인의 빌드 아티팩트에 변경이 필요해요. 아티팩트는 par.artifact.metadata 키 아래 상태(state)에 적절한 이미지 메타데이터를 보관해야 해요. 이 키는 image.ArtifactStateURI 상수가 제공해요.
HCP Packer 지원을 더 쉽게 만들기 위해 packer-plugin-sdk에 Image 패키지를 제공해요. FromArtifact 함수를 사용하면 아티팩트에 포함된 기본 정보로 Image를 쉽게 만들 수 있어요. 사용자 지정이 필요하다면 FromArtifact는 두 번째 인자로 오버라이드 함수를 받아서, 메타데이터에 다른 값을 설정하는 데 사용할 수 있어요.
이미지 메타데이터는 HCP Packer가 이 컴포넌트에 대해 저장해야 하는 모든 메타데이터를 포함해야 해요. 구조는 다음과 같아요:
// Image represents the metadata for some Artifact in the HCP Packer Registry.
type Image struct {
// ImageID is a unique reference identifier stored on the HCP Packer registry
// that can be used to get back the built artifact of a builder or post-processor.
ImageID string
// ProviderName represents the name of the top level cloud or service where the built artifact resides.
// For example "aws, azure, docker, gcp, and vsphere".
ProviderName string
// ProviderRegion represents the location of the built artifact.
// For cloud providers region usually maps to a cloud region or zone, but for things like the file builder,
// S3 bucket or vsphere cluster region can represent a path on the upstream datastore, or cluster.
ProviderRegion string
// Labels represents additional details about an image that a builder or post-processor may with to provide for a given build.
// Any additional metadata will be made available as build labels within a HCP Packer registry iteration.
Labels map[string]string
// SourceImageID is the cloud image ID of the image that was used as the
// source for this image. If set, the HCP Packer registry will be able
// to link the parent and child images for ancestry visualizations and
// dependency tracking.
SourceImageID string
}
여기서 ImageID, ProviderName, SourceImageID는 필수 필드예요.
FromArtifact 메서드를 사용하는 예시:
- 단순 형태:
func (a *Artifact) State(name string) interface{} {
if name == registryimage.ArtifactStateURI {
img, err := registryimage.FromArtifact(a)
if err != nil {
log.Printf("[DEBUG] error encountered when creating a registry image %v", err)
return nil
}
return img
}
return a.StateData[name]
}
- 오버라이드 사용:
func (a *Artifact) State(name string) interface{} {
if name == registryimage.ArtifactStateURI {
img, err := registryimage.FromArtifact(a,
registryimage.WithID(a.Name),
registryimage.WithRegion(a.Region.Name()),
registryimage.WithProvider("happy-cloud"),
registryimage.WithSourceID(a.SourceID),
registryimage.SetLabels(a.Labels),
)
if err != nil {
log.Printf("[DEBUG] error encountered when creating a registry image %v", err)
return nil
}
return img
}
return a.StateData[name]
}
추가 예시는 packer-plugin-sdk/packer/registry/image를 참고하세요.
SDK 버전 (SDK Version)
- HCP Packer 지원은 packer-plugin-sdk >= v0.2.7부터 사용할 수 있어요.
플러그인 예시 (Plugins Example)
다음 플러그인은 현재 HCP Packer를 지원하며 개발에 좋은 참고 자료가 돼요:
- packer-plugin-amazon
- packer-plugin-azure
- packer-plugin-vsphere
- packer-plugin-docker
- packer-plugin-googlecompute