Search in sources :

Example 36 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project halyard by spinnaker.

the class KubernetesV2Service method getResourceYaml.

default String getResourceYaml(AccountDeploymentDetails<KubernetesAccount> details, GenerateService.ResolvedConfiguration resolvedConfiguration) {
    ServiceSettings settings = resolvedConfiguration.getServiceSettings(getService());
    String namespace = getNamespace(settings);
    List<ConfigSource> configSources = stageConfig(details, resolvedConfiguration);
    Map<String, String> env = configSources.stream().map(ConfigSource::getEnv).map(Map::entrySet).flatMap(Collection::stream).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
    env.putAll(settings.getEnv());
    List<String> volumes = configSources.stream().map(c -> {
        TemplatedResource volume = new JinjaJarResource("/kubernetes/manifests/volume.yml");
        volume.addBinding("name", c.getId());
        return volume.toString();
    }).collect(Collectors.toList());
    List<String> volumeMounts = configSources.stream().map(c -> {
        TemplatedResource volume = new JinjaJarResource("/kubernetes/manifests/volumeMount.yml");
        volume.addBinding("name", c.getId());
        volume.addBinding("mountPath", c.getMountPath());
        return volume.toString();
    }).collect(Collectors.toList());
    TemplatedResource probe;
    if (StringUtils.isNotEmpty(settings.getHealthEndpoint())) {
        probe = new JinjaJarResource("/kubernetes/manifests/execReadinessProbe.yml");
        probe.addBinding("command", getReadinessExecCommand(settings));
    } else {
        probe = new JinjaJarResource("/kubernetes/manifests/tcpSocketReadinessProbe.yml");
        probe.addBinding("port", settings.getPort());
    }
    TemplatedResource container = new JinjaJarResource("/kubernetes/manifests/container.yml");
    container.addBinding("name", getService().getCanonicalName());
    container.addBinding("imageId", settings.getArtifactId());
    container.addBinding("port", settings.getPort());
    container.addBinding("volumeMounts", volumeMounts);
    container.addBinding("probe", probe.toString());
    container.addBinding("env", env);
    List<String> containers = Collections.singletonList(container.toString());
    TemplatedResource podSpec = new JinjaJarResource("/kubernetes/manifests/podSpec.yml").addBinding("containers", containers).addBinding("volumes", volumes);
    return new JinjaJarResource("/kubernetes/manifests/deployment.yml").addBinding("name", getService().getCanonicalName()).addBinding("namespace", namespace).addBinding("replicas", settings.getTargetSize()).addBinding("podAnnotations", settings.getKubernetes().getPodAnnotations()).addBinding("podSpec", podSpec.toString()).toString();
}
Also used : ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) Arrays(java.util.Arrays) KubernetesUtil(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.KubernetesUtil) KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) HashMap(java.util.HashMap) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) SecretMountPair(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.v2.KubernetesV2Utils.SecretMountPair) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AccountDeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails) Map(java.util.Map) KubernetesImageDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesImageDescription) JinjaJarResource(com.netflix.spinnaker.halyard.core.resource.v1.JinjaJarResource) Strings(io.fabric8.utils.Strings) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) GenerateService(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration) Collectors(java.util.stream.Collectors) File(java.io.File) HasServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.HasServiceSettings) List(java.util.List) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) Paths(java.nio.file.Paths) KubernetesSharedServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.KubernetesSharedServiceSettings) Entry(java.util.Map.Entry) ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) Collections(java.util.Collections) JinjaJarResource(com.netflix.spinnaker.halyard.core.resource.v1.JinjaJarResource) HasServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.HasServiceSettings) KubernetesSharedServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.KubernetesSharedServiceSettings) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) HashMap(java.util.HashMap) Map(java.util.Map)

Example 37 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project halyard by spinnaker.

the class BakeDebianServiceProvider method getInstallCommand.

@Override
public String getInstallCommand(DeploymentDetails deploymentDetails, GenerateService.ResolvedConfiguration resolvedConfiguration, Map<String, String> installCommands, String startupCommand) {
    Map<String, Object> bindings = new HashMap<>();
    List<SpinnakerService.Type> serviceTypes = new ArrayList<>(installCommands.keySet()).stream().map(SpinnakerService.Type::fromCanonicalName).collect(Collectors.toList());
    List<String> upstartNames = getPrioritizedBakeableServices(serviceTypes).stream().filter(i -> resolvedConfiguration.getServiceSettings(i.getService()).getEnabled()).map(i -> ((BakeDebianService) i).getUpstartServiceName()).filter(Objects::nonNull).collect(Collectors.toList());
    List<String> systemdServiceConfigs = upstartNames.stream().map(n -> n + ".service").collect(Collectors.toList());
    List<String> serviceInstalls = serviceTypes.stream().map(t -> installCommands.get(t.getCanonicalName())).collect(Collectors.toList());
    TemplatedResource resource = new StringReplaceJarResource("/debian/init.sh");
    bindings.put("services", Strings.join(upstartNames, " "));
    bindings.put("systemd-service-configs", Strings.join(systemdServiceConfigs, " "));
    String upstartInit = resource.setBindings(bindings).toString();
    BillOfMaterials.ArtifactSources artifactSources = artifactService.getArtifactSources(deploymentDetails.getDeploymentName());
    resource = new StringReplaceJarResource("/debian/pre-bake.sh");
    bindings = new HashMap<>();
    bindings.put("debian-repository", artifactSourcesConfig.mergeWithBomSources(artifactSources).getDebianRepository());
    bindings.put("install-commands", String.join("\n", serviceInstalls));
    bindings.put("upstart-init", upstartInit);
    bindings.put("startup-file", Paths.get(startupScriptPath, "startup.sh").toString());
    bindings.put("startup-command", startupCommand);
    return resource.setBindings(bindings).toString();
}
Also used : DeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.DeploymentDetails) ArtifactSourcesConfig(com.netflix.spinnaker.halyard.config.config.v1.ArtifactSourcesConfig) Autowired(org.springframework.beans.factory.annotation.Autowired) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) HashMap(java.util.HashMap) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) GenerateService(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) BakeServiceProvider(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.bake.BakeServiceProvider) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Component(org.springframework.stereotype.Component) List(java.util.List) Paths(java.nio.file.Paths) StringReplaceJarResource(com.netflix.spinnaker.halyard.core.resource.v1.StringReplaceJarResource) Map(java.util.Map) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) RemoteAction(com.netflix.spinnaker.halyard.core.RemoteAction) BillOfMaterials(com.netflix.spinnaker.halyard.core.registry.v1.BillOfMaterials) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) Strings(io.fabric8.utils.Strings) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) HashMap(java.util.HashMap) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) BillOfMaterials(com.netflix.spinnaker.halyard.core.registry.v1.BillOfMaterials) StringReplaceJarResource(com.netflix.spinnaker.halyard.core.resource.v1.StringReplaceJarResource)

Example 38 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project curiostack by curioswitch.

the class CreateClientCertTask method exec.

@TaskAction
public void exec() {
    ImmutableClusterExtension cluster = getProject().getExtensions().getByType(ClusterExtension.class);
    String commonName = (String) getProject().getRootProject().findProperty("commonName");
    checkNotNull(commonName, "-PcommonName must be set");
    final KeyPairGenerator keygen;
    try {
        keygen = KeyPairGenerator.getInstance("ECDSA", BouncyCastleProvider.PROVIDER_NAME);
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        throw new IllegalStateException("Could not find RSA, can't happen.", e);
    }
    keygen.initialize(256, new SecureRandom());
    KeyPair keyPair = keygen.generateKeyPair();
    PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(new X500Principal("CN=" + commonName), keyPair.getPublic());
    final ContentSigner signer;
    try {
        signer = new JcaContentSignerBuilder("SHA256withECDSA").build(keyPair.getPrivate());
    } catch (OperatorCreationException e) {
        throw new IllegalStateException("Could not find signer, can't happen.", e);
    }
    PKCS10CertificationRequest csr = p10Builder.build(signer);
    StringWriter csrWriter = new StringWriter();
    try (JcaPEMWriter pemWriter = new JcaPEMWriter(csrWriter)) {
        pemWriter.writeObject(csr);
    } catch (IOException e) {
        throw new IllegalStateException("Could not encode csr, can't happen.", e);
    }
    String encodedCsr = Base64.getEncoder().encodeToString(csrWriter.toString().getBytes(StandardCharsets.UTF_8));
    String csrName = cluster.namespace() + "." + commonName + ".client.crt";
    Map<Object, Object> csrApiRequest = ImmutableMap.of("apiVersion", "certificates.k8s.io/v1beta1", "kind", "CertificateSigningRequest", "metadata", ImmutableMap.of("name", csrName), "spec", ImmutableMap.of("request", encodedCsr, "usages", ImmutableList.of("digital signature", "key encipherment", "server auth", "client auth")));
    final byte[] encodedApiRequest;
    try {
        encodedApiRequest = OBJECT_MAPPER.writeValueAsBytes(csrApiRequest);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Could not encode yaml", e);
    }
    ImmutableGcloudExtension config = getProject().getRootProject().getExtensions().getByType(GcloudExtension.class);
    String command = config.download() ? new File(config.platformConfig().gcloudBinDir(), "kubectl").getAbsolutePath() : "kubectl";
    getProject().exec(exec -> {
        exec.executable(command);
        exec.args("create", "-f", "-");
        exec.setStandardInput(new ByteArrayInputStream(encodedApiRequest));
    });
    getProject().exec(exec -> {
        exec.executable(command);
        exec.args("certificate", "approve", csrName);
    });
    // Need to wait a bit for certificate to propagate before fetching.
    try {
        TimeUnit.SECONDS.sleep(5);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    ByteArrayOutputStream certStream = new ByteArrayOutputStream();
    getProject().exec(exec -> {
        exec.executable(command);
        exec.args("get", "csr", csrName, "-o", "jsonpath={.status.certificate}");
        exec.setStandardOutput(certStream);
    });
    String certificate = new String(Base64.getDecoder().decode(certStream.toByteArray()), StandardCharsets.UTF_8);
    final JcaPKCS8Generator keyGenerator;
    final PemObject keyObject;
    try {
        keyGenerator = new JcaPKCS8Generator(keyPair.getPrivate(), null);
        keyObject = keyGenerator.generate();
    } catch (PemGenerationException e) {
        throw new IllegalStateException("Could not encode to pkcs8.", e);
    }
    StringWriter keyWriter = new StringWriter();
    try (JcaPEMWriter pemWriter = new JcaPEMWriter(keyWriter)) {
        pemWriter.writeObject(keyObject);
    } catch (IOException e) {
        throw new IllegalStateException("Could not encode csr, can't happen.", e);
    }
    String key = keyWriter.toString();
    KubernetesClient client = new DefaultKubernetesClient();
    Secret certificateSecret = new SecretBuilder().withMetadata(new ObjectMetaBuilder().withName(commonName + "-client-tls").withNamespace(cluster.namespace()).build()).withType("Opaque").withData(ImmutableMap.of("client.crt", Base64.getEncoder().encodeToString(certificate.getBytes(StandardCharsets.UTF_8)), "client-key.pem", Base64.getEncoder().encodeToString(key.getBytes(StandardCharsets.UTF_8)))).build();
    client.resource(certificateSecret).createOrReplace();
}
Also used : ImmutableGcloudExtension(org.curioswitch.gradle.plugins.gcloud.ImmutableGcloudExtension) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ImmutableClusterExtension(org.curioswitch.gradle.plugins.gcloud.ImmutableClusterExtension) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) StringWriter(java.io.StringWriter) JcaPKCS8Generator(org.bouncycastle.openssl.jcajce.JcaPKCS8Generator) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) KeyPair(java.security.KeyPair) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) JcaPKCS10CertificationRequestBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder) PemGenerationException(org.bouncycastle.util.io.pem.PemGenerationException) ContentSigner(org.bouncycastle.operator.ContentSigner) SecureRandom(java.security.SecureRandom) PKCS10CertificationRequestBuilder(org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder) JcaPKCS10CertificationRequestBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder) KeyPairGenerator(java.security.KeyPairGenerator) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Secret(io.fabric8.kubernetes.api.model.Secret) PemObject(org.bouncycastle.util.io.pem.PemObject) ByteArrayInputStream(java.io.ByteArrayInputStream) X500Principal(javax.security.auth.x500.X500Principal) PemObject(org.bouncycastle.util.io.pem.PemObject) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) NoSuchProviderException(java.security.NoSuchProviderException) JcaPEMWriter(org.bouncycastle.openssl.jcajce.JcaPEMWriter) File(java.io.File) TaskAction(org.gradle.api.tasks.TaskAction)

Example 39 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project alluxio by Alluxio.

the class ManagerProcessContext method applyMount.

/**
 * Apply mount hdfs command.
 *
 * @param req validated request
 * @return response
 */
public ApplyMountPointResponse.Payload applyMount(ApplyMountPointRequest req) {
    ApplyMountPointRequest.Payload p = req.getPayload();
    ApplyMountPointResponse.Payload.Builder resultBuilder = ApplyMountPointResponse.Payload.newBuilder();
    alluxio.hub.proto.MountPointInfo mountPointInfo = p.getNew();
    Map<String, String> properties = constructProp(mountPointInfo, false);
    MountPOptions.Builder optionBuilder = MountPOptions.newBuilder().setReadOnly(mountPointInfo.getReadOnly()).setShared(mountPointInfo.getShared()).putAllProperties(properties);
    boolean needToRemount = false;
    try {
        boolean needToMount = true;
        if (p.hasBefore()) {
            AlluxioURI originalMount = new AlluxioURI(p.getBefore().getAlluxioPath());
            if (originalMount.isRoot() && (!(p.getBefore().getAlluxioPath().equals(p.getNew().getAlluxioPath()) && p.getBefore().getUfsUri().equals(p.getNew().getUfsUri())))) {
                throw new IOException("Cannot change the mount location or ufs location of a root mount");
            }
            // if both mount uri and ufs uri has not changed
            if (originalMount.isRoot() || (p.getBefore().getAlluxioPath().equals(p.getNew().getAlluxioPath()) && p.getBefore().getUfsUri().equals(p.getNew().getUfsUri()))) {
                getFileSystem().updateMount(originalMount, optionBuilder.build());
                needToMount = false;
            } else {
                getFileSystem().unmount(originalMount);
                needToRemount = true;
            }
        }
        if (needToMount) {
            getFileSystem().mount(new AlluxioURI(mountPointInfo.getAlluxioPath()), new AlluxioURI(mountPointInfo.getUfsUri()), optionBuilder.build());
            needToRemount = false;
        }
        if (mountPointInfo.hasHdfsInfo() && mountPointInfo.getHdfsInfo().getEnableSync()) {
            getFileSystem().startSync(new AlluxioURI(mountPointInfo.getAlluxioPath()));
        }
        resultBuilder.setSuccess(true);
    } catch (Exception e) {
        if (needToRemount) {
            alluxio.hub.proto.MountPointInfo oldMountPoint = p.getBefore();
            MountPOptions.Builder oldOptionBuilder = MountPOptions.newBuilder().setReadOnly(oldMountPoint.getReadOnly()).setShared(oldMountPoint.getShared()).putAllProperties(constructProp(oldMountPoint, false));
            try {
                getFileSystem().mount(new AlluxioURI(oldMountPoint.getAlluxioPath()), new AlluxioURI(oldMountPoint.getUfsUri()), oldOptionBuilder.build());
            } catch (Exception ex) {
                LOG.warn("Failed to restore the mount point " + oldMountPoint.getAlluxioPath() + " to previous state");
                resultBuilder.addError(ValidationUtils.getErrorInfo(ex));
            }
        }
        LOG.warn("Failed applying mount settings due to " + ValidationUtils.getErrorInfo(e));
        resultBuilder.setSuccess(false);
        resultBuilder.addError(ValidationUtils.getErrorInfo(e));
        // =====================
        if (e instanceof InvalidPathException) {
            if (exceptionMatchesPattern(e, "mount point.*already exists.*")) {
                resultBuilder.setAdvice("Another file, directory, or mount point already exists at this" + " path. Check the Alluxio path you are mounting to again to make sure it" + " doesn't conflict with paths that already exist.");
            } else if (exceptionMatchesPattern(e, "mount point.*is a prefix of.*")) {
                resultBuilder.setAdvice("A mount point with a similar UFS URI is already mounted to" + " Alluxio. Make sure that your UFS URI doesn't have a namespace overlap with" + " another existing mount point");
            } else if (exceptionMatchesPattern(e, ".*path.*is invalid.*")) {
                resultBuilder.setAdvice("We couldn't parse your Alluxio path. Make sure to provide the" + " absolute path to where the new mount point should exist.");
            }
        } else if (e instanceof FileDoesNotExistException) {
            if (exceptionMatchesPattern(e, "file.*creation failed. component.*does not exist")) {
                resultBuilder.setAdvice("One of components in the path you are trying to mount to does" + " not exist. Make sure that all path components in the Alluxio namespace you" + " are trying to mount at exists, except for the last one.");
            }
        } else if (e instanceof AlluxioException) {
            if (exceptionMatchesPattern(e, ".*no underfilesystem factory found for.*")) {
                resultBuilder.setAdvice(String.format("Alluxio couldn't find a matching library with the" + " URI (%s) and version (%s) provided. This means that you're trying to mount a" + " URI with an unsupported scheme (<scheme>://) or, that Alluxio doesn't have" + " an available version of the UFS type you're trying to mount.", mountPointInfo.getUfsUri(), p.getNew().hasHdfsInfo() ? p.getNew().getHdfsInfo().getHdfsVersion() : "N/A"));
            } else if (exceptionMatchesPattern(e, ".*ufs path.*does not exist.*")) {
                resultBuilder.setAdvice("The UFS path you are trying to mount does not exist in the UFS." + " Please make sure the UFS path specified exists.");
            } else if (exceptionMatchesPattern(e, ".*java.net.unknownhostexception:.*")) {
                resultBuilder.setAdvice(String.format("We weren't able to resolve the hostname in your" + " HDFS uri %s. Please double check that you've typed the hostname correctly and" + " that the system DNS can resolve the hostname.", mountPointInfo.getUfsUri()));
            } else if (exceptionMatchesPattern(e, "call from.*to.*failed on connection exception.*")) {
                resultBuilder.setAdvice("We were able to resolve the hostname of your URI, but failed to" + " connect. Double check that the hostname and port are correct. Then, if it" + " still fails verify that there is no firewall blocking connections between" + " this machine and the UFS URI specified.");
            }
        }
    // END Advice handling
    // ===================
    }
    return resultBuilder.build();
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) MountPOptions(alluxio.grpc.MountPOptions) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) InvalidPathException(alluxio.exception.InvalidPathException) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) InvalidPathException(alluxio.exception.InvalidPathException) MountPointInfo(alluxio.wire.MountPointInfo) HDFSMountPointInfo(alluxio.hub.proto.HDFSMountPointInfo) ApplyMountPointRequest(alluxio.hub.proto.ApplyMountPointRequest) AlluxioURI(alluxio.AlluxioURI) AlluxioException(alluxio.exception.AlluxioException)

Example 40 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project zalenium by zalando.

the class KubernetesContainerMock method getMockedKubernetesContainerClient.

public static KubernetesContainerClient getMockedKubernetesContainerClient() {
    // Mocking the environment variable to return false for video recording enabled
    Environment environment = mock(Environment.class);
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_CPU_REQUEST", null)).thenReturn("250m");
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_CPU_LIMIT", null)).thenReturn("500m");
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_MEMORY_REQUEST", null)).thenReturn("1Gi");
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_MEMORY_LIMIT", null)).thenReturn("4Gi");
    String hostName;
    try {
        hostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        hostName = "";
    }
    KubernetesServer server = new KubernetesServer();
    server.before();
    Map<String, String> zaleniumPodLabels = new HashMap<>();
    zaleniumPodLabels.put("app", "zalenium");
    zaleniumPodLabels.put("role", "grid");
    Pod zaleniumPod = new PodBuilder().withNewMetadata().withLabels(zaleniumPodLabels).addToLabels(zaleniumPodLabels).withNamespace("test").withGenerateName(hostName).withName(hostName).and().build();
    String videosVolumeName = RandomStringUtils.randomAlphabetic(5).toLowerCase();
    String generalVolumeName = RandomStringUtils.randomAlphabetic(5).toLowerCase();
    VolumeMount volumeMountVideos = new VolumeMountBuilder().withName(videosVolumeName).withMountPath("/tmp/videos").build();
    VolumeMount volumeMountGeneral = new VolumeMountBuilder().withName(generalVolumeName).withMountPath("/tmp/mounted").build();
    Container zaleniumContainer = new ContainerBuilder().withVolumeMounts(volumeMountVideos, volumeMountGeneral).build();
    Volume videosVolume = new VolumeBuilder().withName(videosVolumeName).build();
    Volume generalVolume = new VolumeBuilder().withName(generalVolumeName).build();
    HostAlias hostAlias = new HostAliasBuilder().withHostnames("foo.local").withIp("127.0.0.1").build();
    Map<String, String> nodeSelector = new HashMap<>();
    nodeSelector.put("nodeLabelKey", "nodeLabelValue");
    PodSpec zaleniumPodSpec = new PodSpecBuilder().withContainers(zaleniumContainer).withVolumes(videosVolume, generalVolume).withHostAliases(hostAlias).withNodeSelector(nodeSelector).build();
    zaleniumPod.setSpec(zaleniumPodSpec);
    String podsPath = String.format("/api/v1/namespaces/test/pods/%s", hostName);
    server.expect().withPath(podsPath).andReturn(200, zaleniumPod).once();
    Map<String, String> dockerSeleniumPodLabels = new HashMap<>();
    dockerSeleniumPodLabels.put("createdBy", "zalenium");
    Pod dockerSeleniumPod = new PodBuilder().withNewMetadata().withLabels(dockerSeleniumPodLabels).withNamespace("test").withName(hostName).and().build();
    Container dockerSeleniumContainer = new ContainerBuilder().withEnv(new EnvVarBuilder().withName("NOVNC_PORT").withValue("40000").build()).build();
    PodSpec dockerSeleniumPodSpec = new PodSpecBuilder().withContainers(dockerSeleniumContainer).build();
    dockerSeleniumPod.setSpec(dockerSeleniumPodSpec);
    PodStatus dockerSeleniumPodStatus = new PodStatusBuilder().withPodIP("localhost").build();
    dockerSeleniumPod.setStatus(dockerSeleniumPodStatus);
    server.expect().withPath("/api/v1/namespaces/test/pods?labelSelector=createdBy%3Dzalenium").andReturn(200, new PodListBuilder().withItems(dockerSeleniumPod).build()).always();
    ServiceSpec serviceSpec = new ServiceSpecBuilder().withPorts(new ServicePortBuilder().withNodePort(40000).build()).build();
    Service service = new ServiceBuilder().withSpec(serviceSpec).build();
    server.expect().withPath("/api/v1/namespaces/test/services").andReturn(201, service).always();
    String bashCommand = String.format("/api/v1/namespaces/test/pods/%s/exec?command=bash&command=-c&command=", hostName);
    String tarCommand = String.format("/api/v1/namespaces/test/pods/%s/exec?command=tar&command=-C&command=", hostName);
    String commandSuffix = "&stdout=true&stderr=true";
    String expectedOutput = "test";
    List<String> execPaths = new ArrayList<>();
    execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%s/var/log/cont/&command=-c&command=.%s", tarCommand, commandSuffix));
    execPaths.add(String.format("%s/videos/&command=-c&command=.%s", tarCommand, commandSuffix));
    execPaths.add(String.format("%s/videos/&command=-C&command=.%s", tarCommand, commandSuffix));
    execPaths.add(String.format("%sstop-video%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%sstart-video%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%scleanup-container.sh%s", bashCommand, commandSuffix));
    String notifyComplete = bashCommand.concat("notify%20%27Zalenium%27,%20%27TEST%20COMPLETED%27,%20--icon=/home/seluser/images/completed.png").concat(commandSuffix);
    String notifyTimeout = bashCommand.concat("notify%20%27Zalenium%27,%20%27TEST%20TIMED%20OUT%27,%20--icon=/home/seluser/images/timeout.png").concat(commandSuffix);
    execPaths.add(notifyComplete);
    execPaths.add(notifyTimeout);
    for (String execPath : execPaths) {
        server.expect().withPath(execPath).andUpgradeToWebSocket().open(new OutputStreamMessage(expectedOutput)).done().once();
    }
    server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(201, new PodBuilder().build()).always();
    KubernetesClient client = server.getClient();
    return new KubernetesContainerClient(environment, KubernetesContainerClient::createDoneablePodDefaultImpl, client);
}
Also used : HashMap(java.util.HashMap) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) ArrayList(java.util.ArrayList) OutputStreamMessage(io.fabric8.kubernetes.client.server.mock.OutputStreamMessage) PodStatusBuilder(io.fabric8.kubernetes.api.model.PodStatusBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) HostAliasBuilder(io.fabric8.kubernetes.api.model.HostAliasBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) UnknownHostException(java.net.UnknownHostException) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Service(io.fabric8.kubernetes.api.model.Service) KubernetesServer(io.fabric8.kubernetes.client.server.mock.KubernetesServer) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) KubernetesContainerClient(de.zalando.ep.zalenium.container.kubernetes.KubernetesContainerClient) HostAlias(io.fabric8.kubernetes.api.model.HostAlias) ServiceSpecBuilder(io.fabric8.kubernetes.api.model.ServiceSpecBuilder) PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount)

Aggregations

IOException (java.io.IOException)9 File (java.io.File)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Container (io.fabric8.api.Container)5 JMXRequest (io.fabric8.api.commands.JMXRequest)4 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 FabricService (io.fabric8.api.FabricService)3 ContainerCreateConfig (io.fabric8.maven.docker.access.ContainerCreateConfig)3 Arguments (io.fabric8.maven.docker.config.Arguments)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Properties (java.util.Properties)3 TemplatedResource (com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource)2 ArtifactService (com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService)2 GenerateService (com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService)2 SpinnakerRuntimeSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings)2 Container (io.fabric8.kubernetes.api.model.Container)2