ImagePullSecretProvisioner implements ConfigurationProvisioner<KubernetesEnvironment> { @Override @Traced public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException { TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId); DockerAuthConfigs credentials = credentialsProvider.getCredentials(); if (credentials == null) { return; } Map<String, DockerAuthConfig> authConfigs = credentials.getConfigs(); if (authConfigs == null || authConfigs.isEmpty()) { return; } String encodedConfig = Base64.getEncoder().encodeToString(generateDockerCfg(authConfigs).getBytes()); Secret secret = new SecretBuilder() .addToData(".dockercfg", encodedConfig) .withType("kubernetes.io/dockercfg") .withNewMetadata() .withName(identity.getWorkspaceId() + SECRET_NAME_SUFFIX) .endMetadata() .build(); k8sEnv.getSecrets().put(secret.getMetadata().getName(), secret); k8sEnv .getPodsData() .values() .forEach(p -> addImagePullSecret(secret.getMetadata().getName(), p.getSpec())); } @Inject  ImagePullSecretProvisioner(
      UserSpecificDockerRegistryCredentialsProvider credentialsProvider); @Override @Traced void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity);  }
@Test public void doNotDoAnythingIfNoPrivateRegistries() throws Exception { when(dockerAuthConfigs.getConfigs()).thenReturn(Collections.emptyMap()); imagePullSecretProvisioner.provision(k8sEnv, runtimeIdentity); assertTrue(k8sEnv.getSecrets().isEmpty()); verifyZeroInteractions(podSpec); }
@Test public void addSecretAndReferenceInPod() throws Exception { when(dockerAuthConfigs.getConfigs()) .thenReturn( ImmutableMap.of( "reg1", new TestDockerAuthConfig().withUsername("username1").withPassword("password1"), "reg2", new TestDockerAuthConfig().withUsername("username2").withPassword("password2"))); imagePullSecretProvisioner.provision(k8sEnv, runtimeIdentity); verify(podSpec) .setImagePullSecrets(ImmutableList.of(newImagePullSecretRef, existingImagePullSecretRef)); Secret secret = k8sEnv.getSecrets().get(WORKSPACE_ID + SECRET_NAME_SUFFIX); assertNotNull(secret); assertEquals(secret.getType(), "kubernetes.io/dockercfg"); String dockerCfgData = secret.getData().get(".dockercfg"); assertNotNull(dockerCfgData); Gson gson = new Gson(); assertEquals( gson.toJson( gson.fromJson(new String(Base64.getDecoder().decode(dockerCfgData)), Map.class)), gson.toJson( gson.fromJson( "" + "{ \"https: + buildAuth("username1", "password1") + "\" }" + ", \"https: + buildAuth("username2", "password2") + "\" }" + "}", Map.class))); }