Search in sources :

Example 16 with Build

use of io.fabric8.openshift.api.model.Build in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetes method buildConfig.

/**
 * Builds the Config required by DefaultOpenShiftClient
 * Also sets the system properties
 * (1) to not refer .kube/config file and
 * (2) the client to use service account procedure to get authenticated and authorised
 *
 * @return {@link io.fabric8.kubernetes.client.Config} object to build the client
 * @throws ServiceDiscoveryException if an error occurs while building the config using externally stored token
 */
private Config buildConfig(Map<String, String> implParameters) throws ServiceDiscoveryException, APIMgtDAOException {
    System.setProperty(TRY_KUBE_CONFIG, "false");
    System.setProperty(TRY_SERVICE_ACCOUNT, "true");
    /*
         *  Common to both situations,
         *      - Token found inside APIM pod
         *      - Token stored in APIM resources/security folder }
         */
    ConfigBuilder configBuilder = new ConfigBuilder().withMasterUrl(implParameters.get(MASTER_URL)).withCaCertFile(implParameters.get(CA_CERT_PATH));
    /*
         *  Check if a service account token File Name is given in the configuration
         *      - if not : assume APIM is running inside a pod and look for the pod's token
         */
    String externalSATokenFileName = implParameters.get(EXTERNAL_SA_TOKEN_FILE_NAME);
    if (StringUtils.isEmpty(externalSATokenFileName)) {
        log.debug("Looking for service account token in " + POD_MOUNTED_SA_TOKEN_FILE_PATH);
        String podMountedSAToken = APIFileUtils.readFileContentAsText(implParameters.get(POD_MOUNTED_SA_TOKEN_FILE_PATH));
        return configBuilder.withOauthToken(podMountedSAToken).build();
    } else {
        log.info("Using externally stored service account token");
        return configBuilder.withOauthToken(resolveToken("encrypted" + externalSATokenFileName)).build();
    }
}
Also used : ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder)

Example 17 with Build

use of io.fabric8.openshift.api.model.Build in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testRemoveContainerBasedGatewayForException.

@Test(expected = ContainerBasedGatewayException.class)
public void testRemoveContainerBasedGatewayForException() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = new KubernetesGatewayImpl();
    kubernetesGateway.setClient(openShiftClient);
    kubernetesGateway.setValues(createImplParametersMap());
    Mockito.when(openShiftClient.services().inNamespace(NAMESPACE)).thenThrow(KubernetesClientException.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    kubernetesGateway.removeContainerBasedGateway("label", api);
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) API(org.wso2.carbon.apimgt.core.models.API) Test(org.junit.Test)

Example 18 with Build

use of io.fabric8.openshift.api.model.Build in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testCreateDeploymentResourceForKubernetesException.

@Test
public void testCreateDeploymentResourceForKubernetesException() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(getServiceResources(), getDeploymentResources());
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    ScalableResource scalableResource = Mockito.mock(ScalableResource.class);
    Service service = createService(openShiftClient, nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.create(Mockito.any())).thenReturn(service);
    String deploymentName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_DEPLOYMENT_SUFFIX;
    Mockito.when(openShiftClient.extensions().deployments().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withName(deploymentName)).thenReturn(scalableResource);
    Mockito.when(scalableResource.get()).thenThrow(KubernetesClientException.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    try {
        kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    } catch (ContainerBasedGatewayException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
    }
}
Also used : ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Service(io.fabric8.kubernetes.api.model.Service) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 19 with Build

use of io.fabric8.openshift.api.model.Build in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testCreateContainerGatewayForAlreadyAvailableResources.

@Test
public void testCreateContainerGatewayForAlreadyAvailableResources() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(getServiceResources(), getDeploymentResources(), getIngressResources());
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    ScalableResource scalableResource = Mockito.mock(ScalableResource.class);
    BaseOperation baseOperation = Mockito.mock(BaseOperation.class);
    String serviceName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_SERVICE_SUFFIX;
    Mockito.when(openShiftClient.services().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withName(serviceName)).thenReturn(baseOperation);
    Service service = Mockito.mock(Service.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(baseOperation.get()).thenReturn(service);
    String deploymentName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_DEPLOYMENT_SUFFIX;
    Mockito.when(openShiftClient.extensions().deployments().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withName(deploymentName)).thenReturn(scalableResource);
    Deployment deployment = Mockito.mock(Deployment.class, Mockito.RETURNS_DEEP_STUBS);
    String ingressName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_INGRESS_SUFFIX;
    Mockito.when(openShiftClient.extensions().ingresses().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withName(ingressName)).thenReturn(scalableResource);
    Ingress ingress = Mockito.mock(Ingress.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(scalableResource.get()).thenReturn(deployment, ingress);
    API api = SampleTestObjectCreator.createDefaultAPI().context("/test/context/").build();
    kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    Mockito.verify(openShiftClient, Mockito.times(4)).load(Mockito.any());
    Mockito.verify(openShiftClient, Mockito.times(2)).services();
    Mockito.verify(openShiftClient, Mockito.times(4)).extensions();
}
Also used : ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) API(org.wso2.carbon.apimgt.core.models.API) BaseOperation(io.fabric8.kubernetes.client.dsl.base.BaseOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 20 with Build

use of io.fabric8.openshift.api.model.Build in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method createMalformedServiceList.

/**
 * Create ServiceList with the given port type, but without a loadBalancer ingress
 *
 * @param portType http or https or a wrong port to check behavior
 * @return ServiceList containing one service of LoadBalancer type
 */
private ServiceList createMalformedServiceList(String portType) {
    ServicePort port = new ServicePortBuilder().withName(portType).withPort(80).withNodePort(30005).build();
    Service malformedService5 = new ServiceBuilder().withNewMetadata().withName("service5").withNamespace("prod").and().withNewSpec().withType("LoadBalancer").withClusterIP("1.1.1.5").withPorts(port).and().withNewStatus().withNewLoadBalancer().endLoadBalancer().and().build();
    List<Service> servicesList = new ArrayList<>();
    servicesList.add(malformedService5);
    return new ServiceListBuilder().withItems(servicesList).build();
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ServiceListBuilder(io.fabric8.kubernetes.api.model.ServiceListBuilder) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Aggregations

Test (org.junit.Test)255 ArrayList (java.util.ArrayList)74 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)69 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)68 HashMap (java.util.HashMap)67 File (java.io.File)53 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)51 IOException (java.io.IOException)50 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)45 Pod (io.fabric8.kubernetes.api.model.Pod)38 Map (java.util.Map)35 Service (io.fabric8.kubernetes.api.model.Service)34 FabricService (io.fabric8.api.FabricService)33 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)30 Container (io.fabric8.api.Container)29 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)28 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)27 List (java.util.List)26 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)25 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)25