Search in sources :

Example 1 with RegistryConfig

use of org.eclipse.jkube.kit.common.RegistryConfig in project jkube by eclipse.

the class OpenshiftBuildService method checkOrCreatePullSecret.

private boolean checkOrCreatePullSecret(OpenShiftClient client, KubernetesListBuilder builder, String pullSecretName, ImageConfiguration imageConfig) throws Exception {
    BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();
    String fromImage;
    if (buildConfig.isDockerFileMode()) {
        fromImage = extractBaseFromDockerfile(jKubeConfiguration, buildConfig);
    } else {
        fromImage = extractBaseFromConfiguration(buildConfig);
    }
    String pullRegistry = EnvUtil.firstRegistryOf(new ImageName(fromImage).getRegistry(), jKubeConfiguration.getRegistryConfig().getRegistry(), jKubeConfiguration.getRegistryConfig().getRegistry());
    if (pullRegistry != null) {
        RegistryConfig registryConfig = jKubeConfiguration.getRegistryConfig();
        final AuthConfig authConfig = new AuthConfigFactory(log).createAuthConfig(false, registryConfig.isSkipExtendedAuth(), registryConfig.getAuthConfig(), registryConfig.getSettings(), null, pullRegistry, registryConfig.getPasswordDecryptionMethod());
        final Secret secret = Optional.ofNullable(pullSecretName).map(psn -> client.secrets().inNamespace(applicableOpenShiftNamespace).withName(psn).get()).orElse(null);
        if (secret != null) {
            log.info("Adding to Secret %s", pullSecretName);
            return updateSecret(client, pullSecretName, secret.getData());
        }
        if (authConfig != null) {
            JsonObject auths = new JsonObject();
            JsonObject auth = new JsonObject();
            JsonObject item = new JsonObject();
            String authString = authConfig.getUsername() + ":" + authConfig.getPassword();
            item.add("auth", new JsonPrimitive(Base64.encodeBase64String(authString.getBytes(StandardCharsets.UTF_8))));
            auth.add(pullRegistry, item);
            auths.add("auths", auth);
            String credentials = Base64.encodeBase64String(auths.toString().getBytes(StandardCharsets.UTF_8));
            Map<String, String> data = new HashMap<>();
            data.put(".dockerconfigjson", credentials);
            log.info("Creating Secret");
            builder.addNewSecretItem().withNewMetadata().withName(pullSecretName).endMetadata().withData(data).withType("kubernetes.io/dockerconfigjson").endSecretItem();
            return true;
        }
    }
    return false;
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) RegistryConfig(org.eclipse.jkube.kit.common.RegistryConfig) Secret(io.fabric8.kubernetes.api.model.Secret) BuildServiceConfig(org.eclipse.jkube.kit.config.service.BuildServiceConfig) Quantity(io.fabric8.kubernetes.api.model.Quantity) JsonObject(com.google.gson.JsonObject) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) OpenShiftBuildServiceUtils.createBuildArchive(org.eclipse.jkube.kit.config.service.openshift.OpenShiftBuildServiceUtils.createBuildArchive) Watcher(io.fabric8.kubernetes.client.Watcher) BuildConfigSpec(io.fabric8.openshift.api.model.BuildConfigSpec) AuthConfig(org.eclipse.jkube.kit.build.api.auth.AuthConfig) LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) Base64(org.apache.commons.codec.binary.Base64) ImageName(org.eclipse.jkube.kit.config.image.ImageName) EnvUtil(org.eclipse.jkube.kit.common.util.EnvUtil) Map(java.util.Map) Status(io.fabric8.kubernetes.api.model.Status) Build(io.fabric8.openshift.api.model.Build) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) AbstractImageBuildService(org.eclipse.jkube.kit.config.service.AbstractImageBuildService) RegistryConfig(org.eclipse.jkube.kit.common.RegistryConfig) RuntimeMode(org.eclipse.jkube.kit.config.resource.RuntimeMode) ResourceFileType(org.eclipse.jkube.kit.common.ResourceFileType) BuildUtil.extractBaseFromConfiguration(org.eclipse.jkube.kit.build.api.helper.BuildUtil.extractBaseFromConfiguration) BuildStrategy(io.fabric8.openshift.api.model.BuildStrategy) StandardCharsets(java.nio.charset.StandardCharsets) BuildConfigSpecBuilder(io.fabric8.openshift.api.model.BuildConfigSpecBuilder) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) JKubeServiceHub(org.eclipse.jkube.kit.config.service.JKubeServiceHub) Optional(java.util.Optional) Secret(io.fabric8.kubernetes.api.model.Secret) ClusterConfiguration(org.eclipse.jkube.kit.config.access.ClusterConfiguration) JKubeServiceException(org.eclipse.jkube.kit.config.service.JKubeServiceException) OpenShiftBuildServiceUtils.createBuildOutput(org.eclipse.jkube.kit.config.service.openshift.OpenShiftBuildServiceUtils.createBuildOutput) BuildUtil.extractBaseFromDockerfile(org.eclipse.jkube.kit.build.api.helper.BuildUtil.extractBaseFromDockerfile) OpenShiftBuildServiceUtils.computeS2IBuildName(org.eclipse.jkube.kit.config.service.openshift.OpenShiftBuildServiceUtils.computeS2IBuildName) OpenshiftHelper(org.eclipse.jkube.kit.common.util.OpenshiftHelper) ClusterAccess(org.eclipse.jkube.kit.config.access.ClusterAccess) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) BuildSource(io.fabric8.openshift.api.model.BuildSource) HashMap(java.util.HashMap) Watch(io.fabric8.kubernetes.client.Watch) AtomicReference(java.util.concurrent.atomic.AtomicReference) OpenshiftHelper.isOpenShift(org.eclipse.jkube.kit.common.util.OpenshiftHelper.isOpenShift) WatcherException(io.fabric8.kubernetes.client.WatcherException) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) AuthConfigFactory(org.eclipse.jkube.kit.build.service.docker.auth.AuthConfigFactory) JsonPrimitive(com.google.gson.JsonPrimitive) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) BuildOutput(io.fabric8.openshift.api.model.BuildOutput) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ObjectReference(io.fabric8.kubernetes.api.model.ObjectReference) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) KubernetesHelper(org.eclipse.jkube.kit.common.util.KubernetesHelper) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) File(java.io.File) ImageStreamService.resolveImageStreamName(org.eclipse.jkube.kit.config.service.openshift.ImageStreamService.resolveImageStreamName) TimeUnit(java.util.concurrent.TimeUnit) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) KitLogger(org.eclipse.jkube.kit.common.KitLogger) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) OpenShiftBuildServiceUtils.createBuildStrategy(org.eclipse.jkube.kit.config.service.openshift.OpenShiftBuildServiceUtils.createBuildStrategy) ImageName(org.eclipse.jkube.kit.config.image.ImageName) JsonPrimitive(com.google.gson.JsonPrimitive) HashMap(java.util.HashMap) AuthConfigFactory(org.eclipse.jkube.kit.build.service.docker.auth.AuthConfigFactory) JsonObject(com.google.gson.JsonObject) AuthConfig(org.eclipse.jkube.kit.build.api.auth.AuthConfig)

Example 2 with RegistryConfig

use of org.eclipse.jkube.kit.common.RegistryConfig in project jkube by eclipse.

the class OpenShiftBuildServiceTest method push_withValidImage_shouldLogWarning.

@Test
public void push_withValidImage_shouldLogWarning() throws JKubeServiceException {
    // Given
    when(jKubeServiceHub.getLog()).thenReturn(mockedKitLogger);
    // When
    new OpenshiftBuildService(jKubeServiceHub).push(Collections.singletonList(imageConfiguration), 0, new RegistryConfig(), false);
    // Then
    verify(mockedKitLogger, times(1)).warn("Image is pushed to OpenShift's internal registry during oc:build goal. Skipping...");
}
Also used : RegistryConfig(org.eclipse.jkube.kit.common.RegistryConfig) Test(org.junit.Test)

Example 3 with RegistryConfig

use of org.eclipse.jkube.kit.common.RegistryConfig in project jkube by eclipse.

the class OpenShiftBuildServiceTest method push_withEmptyList_shouldNotLogWarning.

@Test
public void push_withEmptyList_shouldNotLogWarning() throws JKubeServiceException {
    // Given
    when(jKubeServiceHub.getLog()).thenReturn(mockedKitLogger);
    // When
    new OpenshiftBuildService(jKubeServiceHub).push(Collections.emptyList(), 0, new RegistryConfig(), false);
    // Then
    verify(mockedKitLogger, times(0)).warn("Image is pushed to OpenShift's internal registry during oc:build goal. Skipping...");
}
Also used : RegistryConfig(org.eclipse.jkube.kit.common.RegistryConfig) Test(org.junit.Test)

Example 4 with RegistryConfig

use of org.eclipse.jkube.kit.common.RegistryConfig in project jkube by eclipse.

the class OpenshiftBuildServiceIntegrationTest method init.

@Before
public void init() throws Exception {
    final KitLogger logger = new KitLogger.StdoutLogger();
    baseDirectory = temporaryFolder.newFolder("openshift-build-service");
    target = temporaryFolder.newFolder("openshift-build-service", "target");
    final File emptyDockerBuildTar = new File(target, "docker-build.tar");
    FileUtils.touch(emptyDockerBuildTar);
    baseDir = baseDirectory.getAbsolutePath();
    projectName = "myapp";
    FileUtils.deleteDirectory(new File(baseDir, projectName));
    final File dockerFile = new File(baseDir, "Docker.tar");
    FileUtils.touch(dockerFile);
    openshiftHelper = mockStatic(OpenshiftHelper.class);
    openshiftHelper.when(() -> OpenshiftHelper.isOpenShift(eq(mockServer.getOpenshiftClient()))).thenReturn(true);
    jKubeBuildTarArchiver = mockConstruction(JKubeBuildTarArchiver.class, (mock, ctx) -> when(mock.createArchive(any(File.class), any(BuildDirs.class), eq(ArchiveCompression.none))).thenReturn(emptyDockerBuildTar));
    mockedResourceConfig = mock(ResourceConfig.class, RETURNS_DEEP_STUBS);
    when(mockedResourceConfig.getNamespace()).thenReturn("ns1");
    jKubeServiceHub = mock(JKubeServiceHub.class, RETURNS_DEEP_STUBS);
    when(jKubeServiceHub.getClusterAccess().createDefaultClient()).thenReturn(mockServer.getOpenshiftClient());
    when(jKubeServiceHub.getLog()).thenReturn(logger);
    when(jKubeServiceHub.getDockerServiceHub().getArchiveService()).thenReturn(new ArchiveService(AssemblyManager.getInstance(), logger));
    when(jKubeServiceHub.getBuildServiceConfig().getBuildDirectory()).thenReturn(target.getAbsolutePath());
    when(jKubeServiceHub.getConfiguration()).thenReturn(JKubeConfiguration.builder().outputDirectory(target.getAbsolutePath()).project(JavaProject.builder().baseDirectory(baseDirectory).buildDirectory(target).build()).registryConfig(RegistryConfig.builder().build()).build());
    image = ImageConfiguration.builder().name(projectName).build(BuildConfiguration.builder().from(projectName).build()).build();
    defaultConfig = BuildServiceConfig.builder().buildDirectory(baseDir).buildRecreateMode(BuildRecreateMode.none).s2iBuildNameSuffix("-s2i-suffix2").resourceConfig(mockedResourceConfig).jKubeBuildStrategy(JKubeBuildStrategy.s2i);
    defaultConfigSecret = defaultConfig.build().toBuilder().openshiftPullSecret("pullsecret-fabric8");
    dockerImageConfig = defaultConfig.build().toBuilder().buildOutputKind("DockerImage");
    dockerImageConfigSecret = defaultConfig.build().toBuilder().openshiftPullSecret("pullsecret-fabric8").openshiftPushSecret("pushsecret-fabric8").buildOutputKind("DockerImage");
    Serialization.jsonMapper().disable(SerializationFeature.INDENT_OUTPUT);
}
Also used : BuildServiceConfig(org.eclipse.jkube.kit.config.service.BuildServiceConfig) Quantity(io.fabric8.kubernetes.api.model.Quantity) BuildStrategyBuilder(io.fabric8.openshift.api.model.BuildStrategyBuilder) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) AssemblyManager(org.eclipse.jkube.kit.build.api.assembly.AssemblyManager) Map(java.util.Map) After(org.junit.After) Mockito.mockConstruction(org.mockito.Mockito.mockConstruction) Build(io.fabric8.openshift.api.model.Build) MockedConstruction(org.mockito.MockedConstruction) ArchiveCompression(org.eclipse.jkube.kit.common.archive.ArchiveCompression) ImmutableMap(com.google.common.collect.ImmutableMap) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) BuildRecreateMode(org.eclipse.jkube.kit.config.resource.BuildRecreateMode) RegistryConfig(org.eclipse.jkube.kit.common.RegistryConfig) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) StandardCharsets(java.nio.charset.StandardCharsets) MockedStatic(org.mockito.MockedStatic) JKubeServiceHub(org.eclipse.jkube.kit.config.service.JKubeServiceHub) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) JKubeServiceException(org.eclipse.jkube.kit.config.service.JKubeServiceException) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) OpenshiftHelper(org.eclipse.jkube.kit.common.util.OpenshiftHelper) JKubeBuildStrategy(org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) Assert.assertThrows(org.junit.Assert.assertThrows) Mockito.mockStatic(org.mockito.Mockito.mockStatic) HashMap(java.util.HashMap) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) Serialization(io.fabric8.kubernetes.client.utils.Serialization) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) ResourceConfig(org.eclipse.jkube.kit.config.resource.ResourceConfig) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) JavaProject(org.eclipse.jkube.kit.common.JavaProject) ImageStream(io.fabric8.openshift.api.model.ImageStream) Before(org.junit.Before) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) JKubeBuildTarArchiver(org.eclipse.jkube.kit.build.api.assembly.JKubeBuildTarArchiver) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) Assert.assertTrue(org.junit.Assert.assertTrue) ArchiveService(org.eclipse.jkube.kit.build.service.docker.ArchiveService) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BuildDirs(org.eclipse.jkube.kit.build.api.assembly.BuildDirs) File(java.io.File) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) KitLogger(org.eclipse.jkube.kit.common.KitLogger) Rule(org.junit.Rule) OpenShiftServer(io.fabric8.openshift.client.server.mock.OpenShiftServer) Assembly(org.eclipse.jkube.kit.common.Assembly) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) OpenshiftBuildConfig(org.eclipse.jkube.kit.config.resource.OpenshiftBuildConfig) TemporaryFolder(org.junit.rules.TemporaryFolder) Assert.assertEquals(org.junit.Assert.assertEquals) KitLogger(org.eclipse.jkube.kit.common.KitLogger) ArchiveService(org.eclipse.jkube.kit.build.service.docker.ArchiveService) OpenshiftHelper(org.eclipse.jkube.kit.common.util.OpenshiftHelper) JKubeBuildTarArchiver(org.eclipse.jkube.kit.build.api.assembly.JKubeBuildTarArchiver) ResourceConfig(org.eclipse.jkube.kit.config.resource.ResourceConfig) File(java.io.File) BuildDirs(org.eclipse.jkube.kit.build.api.assembly.BuildDirs) JKubeServiceHub(org.eclipse.jkube.kit.config.service.JKubeServiceHub) Before(org.junit.Before)

Aggregations

RegistryConfig (org.eclipse.jkube.kit.common.RegistryConfig)4 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)2 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)2 Quantity (io.fabric8.kubernetes.api.model.Quantity)2 Build (io.fabric8.openshift.api.model.Build)2 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)2 BuildConfigBuilder (io.fabric8.openshift.api.model.BuildConfigBuilder)2 ImageStreamBuilder (io.fabric8.openshift.api.model.ImageStreamBuilder)2 File (java.io.File)2 StandardCharsets (java.nio.charset.StandardCharsets)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Test (org.junit.Test)2 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 ObjectReference (io.fabric8.kubernetes.api.model.ObjectReference)1 Pod (io.fabric8.kubernetes.api.model.Pod)1