jib源码分析之Step实现

栏目: 编程工具 · 发布时间: 5年前

内容简介:阅读本文前,建议事先了解下jib源码分析及应用以常用 的forBuildToDockerRegistry 包含的步骤来分析这个步骤的本质是 获取Credential ,也就是用户名和密码,无需远程访问。

简介(持续更新)

  • retrieveTargetRegistryCredentialsStep
  • pullAndCacheBaseImageLayersStep
  • PullAndCacheBaseImageLayerStep
  • pushBaseImageLayersStep
  • buildAndCacheApplicationLayers

阅读本文前,建议事先了解下jib源码分析及应用 jib源码分析之细节

以常用 的forBuildToDockerRegistry 包含的步骤来分析

retrieveTargetRegistryCredentialsStep

这个步骤的本质是 获取Credential ,也就是用户名和密码,无需远程访问。

class RetrieveRegistryCredentialsStep implements AsyncStep<Credential>, Callable<Credential> {
  	public Credential call() throws CredentialRetrievalException {
  		...
  		Optional<Credential> optionalCredential = credentialRetriever.retrieve();
  		...
  	}	
}

@FunctionalInterface
public interface CredentialRetriever {
  	Optional<Credential> retrieve() throws CredentialRetrievalException;
}

CredentialRetriever 是构建 RegistryImage 时拿到的

public class RegistryImage implements SourceImage, TargetImage {
	public RegistryImage addCredential(String username, String password) {
    	addCredentialRetriever(() -> Optional.of(Credential.basic(username, password)));
    	return this;
  	}
}

public class Credential {
	private final String username;
  		private final String password;
}

authenticatePushStep

Sends the authentication request and retrieves the Bearer authorization token.

这部分要参照官网 Token Authentication Specification

jib源码分析之Step实现

  1. Attempt to begin a push/pull operation with the registry.
  2. If the registry requires authorization it will return a 401 Unauthorized HTTP response with information on how to authenticate.
  3. The registry client makes a request to the authorization service for a Bearer token.
  4. The authorization service returns an opaque Bearer token representing the client’s authorized access.
  5. The client retries the original request with the Bearer token embedded in the request’s Authorization header.
  6. The Registry authorizes the client by validating the Bearer token and the claim set embedded within it and begins the push/pull session as usual.

authenticatePushStep 的结果是 得到一个 Authorization

public class Authorization {
  	private final String scheme;
  	private final String token;
}

Authorization 也可以根据Credential 直接构建。

PullBaseImageStep

try with no credentials ,行就直接结束了。If failed, then, retrieve base registry credentials and try with retrieved credentials. 构造一个retrieveBaseRegistryCredentialsStep,获取Credential,进而构建Authorization,然后再干活 pullBaseImage

jib源码分析之Step实现

对于返回值,schema version= 1时,则只是将得到的 Manifest 数据 转换为 Image<Layer> 。若schema version= 2,则需要拉取 一个blob(其中包括containerConfiguration),然后也转换为 Image<Layer> 并返回

可以看到,PullBaseImageStep 主要是拉取Image 元数据,如果本地没有base image 缓存,则要交给下一个Step

pullAndCacheBaseImageLayersStep

public ImmutableList<PullAndCacheBaseImageLayerStep> call() {
    BaseImageWithAuthorization pullBaseImageStepResult = NonBlockingSteps.get(pullBaseImageStep);
    ImmutableList<Layer> baseImageLayers = pullBaseImageStepResult.getBaseImage().getLayers();
    ...
  	ImmutableList.Builder<PullAndCacheBaseImageLayerStep> pullAndCacheBaseImageLayerStepsBuilder =
      	ImmutableList.builderWithExpectedSize(baseImageLayers.size());
  	for (Layer layer : baseImageLayers) {
    	pullAndCacheBaseImageLayerStepsBuilder.add(
        	new PullAndCacheBaseImageLayerStep(...,
            	layer.getBlobDescriptor().getDigest(),
            	pullBaseImageStepResult.getBaseImageAuthorization()));
  	}
  return pullAndCacheBaseImageLayerStepsBuilder.build();
  	}

pullAndCacheBaseImageLayersStep.call 真正干活的是PullAndCacheBaseImageLayerStep

PullAndCacheBaseImageLayerStep

public CachedLayer call() throws IOException, CacheCorruptedException {

...
    try (...) {
      	Cache cache = buildConfiguration.getBaseImageLayersCache();
      	// Checks if the layer already exists in the cache.
      	Optional<CachedLayer> optionalCachedLayer = cache.retrieve(layerDigest);
      	if (optionalCachedLayer.isPresent()) {
        	buildConfiguration.getEventDispatcher().dispatch(new ProgressEvent(progressAllocation, 1));
        	return optionalCachedLayer.get();
      	}
      	RegistryClient registryClient = ...
      	CachedLayer cachedLayer = cache.writeCompressedLayer(registryClient.pullBlob(layerDigest));
      	...
      	return cachedLayer;
    }
  	}

本地有则返回,无则下载

pushBaseImageLayersStep

实际工作的是 PushLayersStep

public ImmutableList<AsyncStep<PushBlobStep>> call() throws ExecutionException {
    try (...) {
        ImmutableList<? extends AsyncStep<? extends CachedLayer>> cachedLayer =
            NonBlockingSteps.get(cachedLayerStep);
        // Constructs a PushBlobStep for each layer.
        ImmutableList.Builder<AsyncStep<PushBlobStep>> pushBlobStepsBuilder = ImmutableList.builder();
        for (AsyncStep<? extends CachedLayer> cachedLayerStep : cachedLayer) {
          ListenableFuture<PushBlobStep> pushBlobStepFuture =
              Futures.whenAllSucceed(cachedLayerStep.getFuture())
                  .call(() -> makePushBlobStep(cachedLayerStep), listeningExecutorService);
          pushBlobStepsBuilder.add(() -> pushBlobStepFuture);
        }
        return pushBlobStepsBuilder.build();
    }
}

从 pullAndCacheBaseImageLayersStep 可以拿到一系列 PullAndCacheBaseImageLayerStep,对于每个PullAndCacheBaseImageLayerStep,拿到其执行结果 CachedLayer,然后构建 PushBlobStep

从这里也可以看到 ,jib 刚拉完base image,便又尝试重新push 了一下,意图何在呢?

buildAndCacheApplicationLayers

是一系列 BuildAndCacheApplicationLayerStep 的集合

jib 应用demo

Jib.from("busybox")
   .addLayer(Arrays.asList(Paths.get("helloworld.sh")), AbsoluteUnixPath.get("/")) 
   .setEntrypoint("sh", "/helloworld.sh")
   .containerize(
       Containerizer.to(RegistryImage.named("gcr.io/my-project/hello-from-jib")
                                     .addCredential("myusername", "mypassword")));

addLayer 的方法签名 JibContainerBuilder addLayer(List<Path> files, AbsoluteUnixPath pathInContainer) 表示将文件加入到 容器的特定目录下。最终一个 layerConfiguration 持有文件在host 上的地址、在容器内的地址和文件的访问权限(一个LayerEntry 可以表示的内容)

public CachedLayer call() throws IOException, CacheCorruptedException {
    ...
    try (...) {
        Cache cache = buildConfiguration.getApplicationLayersCache();
        // Don't build the layer if it exists already.
        Optional<CachedLayer> optionalCachedLayer = cache.retrieve(layerConfiguration.getLayerEntries());
        if (optionalCachedLayer.isPresent()) {
        	return optionalCachedLayer.get();
        }
        Blob layerBlob = new ReproducibleLayerBuilder(layerConfiguration.getLayerEntries()).build();
        CachedLayer cachedLayer =
          cache.writeUncompressedLayer(layerBlob, layerConfiguration.getLayerEntries());
        ...
        return cachedLayer;
    }
}

本地加入的layer 都是UncompressedLayer,先对其计算 digest 查询本地是否存在,如果已存在则直接返回对应的CachedLayer,否则写入到cache 目录 再返回。

对barge 流程的优化

不要 清理 jib-cache

个人微信订阅号

jib源码分析之Step实现


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

全景探秘游戏设计艺术

全景探秘游戏设计艺术

Jesse Schell / 吕阳、蒋韬、唐文 / 电子工业出版社 / 2010-6 / 69.00元

撬开你脑子里的那些困惑,让你重新认识游戏设计的真谛,人人都可以成为成功的游戏设计者!从更多的角度去审视你的游戏,从不完美的想法中跳脱出来,从枯燥的游戏设计理论中发现理论也可以这样好玩。本书主要内容包括:游戏的体验、构成游戏的元素、元素支撑的主题、游戏的改进、游戏机制、游戏中的角色、游戏设计团队、如何开发好的游戏、如何推销游戏、设计者的责任等。 本书适合任何游戏设计平台的游戏设计从业人员或即将......一起来看看 《全景探秘游戏设计艺术》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具