Java Code Examples for io.fabric8.kubernetes.client.dsl.NonNamespaceOperation#createOrReplace()

The following examples show how to use io.fabric8.kubernetes.client.dsl.NonNamespaceOperation#createOrReplace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IstioExecutor.java    From istio-apim with Apache License 2.0 5 votes vote down vote up
/**
 * Create HTTPAPISpecBinding for the API
 *
 * @param apiName  Name of the API
 * @param endPoint Endpoint of the API
 */
public void createHttpAPISpecBinding(String apiName, String endPoint) {

    NonNamespaceOperation<HTTPAPISpecBinding, HTTPAPISpecBindingList, DoneableHTTPAPISpecBinding,
            io.fabric8.kubernetes.client.dsl.Resource<HTTPAPISpecBinding, DoneableHTTPAPISpecBinding>> bindingClient
            = client.customResource(httpAPISpecBindingCRD, HTTPAPISpecBinding.class, HTTPAPISpecBindingList.class,
            DoneableHTTPAPISpecBinding.class).inNamespace(appNamespace);

    String bindingName = Strings.toLowerCase(apiName) + "-binding";
    String apiSpecName = Strings.toLowerCase(apiName) + "-apispec";

    HTTPAPISpecBinding httpapiSpecBinding = new HTTPAPISpecBinding();
    ObjectMeta metadata = new ObjectMeta();
    metadata.setName(bindingName);
    httpapiSpecBinding.setMetadata(metadata);
    HTTPAPISpecBindingSpec httpapiSpecBindingSpec = new HTTPAPISpecBindingSpec();

    APISpec apiSpec = new APISpec();
    apiSpec.setName(apiSpecName);
    apiSpec.setNamespace(appNamespace);
    ArrayList<APISpec> apiSpecsList = new ArrayList<>();
    apiSpecsList.add(apiSpec);
    httpapiSpecBindingSpec.setApi_specs(apiSpecsList);

    Service service = new Service();
    service.setName(endPoint);
    service.setNamespace(appNamespace);
    ArrayList<Service> servicesList = new ArrayList<>();
    servicesList.add(service);
    httpapiSpecBindingSpec.setServices(servicesList);
    httpapiSpecBinding.setSpec(httpapiSpecBindingSpec);

    bindingClient.createOrReplace(httpapiSpecBinding);
    log.info("[HTTPAPISpecBinding] " + bindingName + " Created in the [Namespace] " + appNamespace + " for the"
            + " [API] " + apiName);
}
 
Example 2
Source File: IstioExecutor.java    From istio-apim with Apache License 2.0 5 votes vote down vote up
/**
 * Create Mixer rule for the API
 *
 * @param apiName     Name of the API
 * @param serviceName Istio service name
 */
private void createRule(String apiName, String serviceName) {

    NonNamespaceOperation<Rule, RuleList, DoneableRule,
            io.fabric8.kubernetes.client.dsl.Resource<Rule, DoneableRule>> ruleCRDClient
            = client.customResource(ruleCRD, Rule.class, RuleList.class,
            DoneableRule.class).inNamespace(istioSystemNamespace);
    String ruleName = Strings.toLowerCase(apiName) + "-rule";

    Rule rule = new Rule();
    ObjectMeta metadata = new ObjectMeta();
    metadata.setName(ruleName);
    rule.setMetadata(metadata);

    Action action = new Action();
    String handlerName = "wso2-handler." + istioSystemNamespace;
    action.setHandler(handlerName);
    ArrayList<String> instances = new ArrayList<>();
    instances.add("wso2-authorization");
    instances.add("wso2-metrics");
    action.setInstances(instances);

    ArrayList<Action> actions = new ArrayList<>();
    actions.add(action);

    RuleSpec ruleSpec = new RuleSpec();
    String matchValue = "context.reporter.kind == \"inbound\" && destination.namespace == \"" + appNamespace +
            "\" && destination.service.name == \"" + serviceName + "\"";
    ruleSpec.setMatch(matchValue);
    ruleSpec.setActions(actions);
    rule.setSpec(ruleSpec);

    ruleCRDClient.createOrReplace(rule);
    log.info("[Rule] " + ruleName + " Created in the [Namespace] " + istioSystemNamespace + " for the [API] "
            + apiName);

}
 
Example 3
Source File: IstioExecutor.java    From istio-apim with Apache License 2.0 4 votes vote down vote up
/**
 * Create HTTPAPISpec for the API
 *
 * @param apiName        Name of the API
 * @param apiContext     Context of the API
 * @param apiVersion     Version of the API
 * @param uriTemplates   URI templates of the API
 * @param resourceScopes Scopes of the resources of the API
 */
public void createHTTPAPISpec(String apiName, String apiContext, String apiVersion, Set<URITemplate> uriTemplates,
                              HashMap<String, String> resourceScopes) {

    NonNamespaceOperation<HTTPAPISpec, HTTPAPISpecList, DoneableHTTPAPISpec,
            io.fabric8.kubernetes.client.dsl.Resource<HTTPAPISpec, DoneableHTTPAPISpec>> apiSpecClient
            = client.customResource(httpAPISpecCRD, HTTPAPISpec.class, HTTPAPISpecList.class,
            DoneableHTTPAPISpec.class).inNamespace(appNamespace);

    String apiSpecName = Strings.toLowerCase(apiName) + "-apispec";

    HTTPAPISpec httpapiSpec = new HTTPAPISpec();
    ObjectMeta metadata = new ObjectMeta();
    metadata.setName(apiSpecName);
    httpapiSpec.setMetadata(metadata);

    HTTPAPISpecSpec httpapiSpecSpec = new HTTPAPISpecSpec();
    Map<String, Map<String, String>> attributeList = new HashMap<>();

    Map<String, String> apiService = new HashMap<>();
    apiService.put("stringValue", apiName);
    attributeList.put("api.service", apiService);

    Map<String, String> apiContextValue = new HashMap<>();
    apiContextValue.put("stringValue", apiContext);
    attributeList.put("api.context", apiContextValue);

    Map<String, String> apiVersionValue = new HashMap<>();
    apiVersionValue.put("stringValue", apiVersion);
    attributeList.put("api.version", apiVersionValue);

    Attributes attributes = new Attributes();
    attributes.setAttributes(attributeList);
    httpapiSpecSpec.setAttributes(attributes);
    httpapiSpecSpec.setPatterns(getPatterns(apiContext, apiVersion, uriTemplates, resourceScopes));
    httpapiSpec.setSpec(httpapiSpecSpec);

    apiSpecClient.createOrReplace(httpapiSpec);
    log.info("[HTTPAPISpec] " + apiSpecName + " Created in the [Namespace] " + appNamespace + " for the"
            + " [API] " + apiName);
}
 
Example 4
Source File: K8sManager.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Re-deploy an API
 *
 * @param apiId                   API Identifier
 * @param containerMgtInfoDetails Clusters which the API has published
 */
@Override
public void apiRepublish(API api, APIIdentifier apiId, Registry registry, JSONObject containerMgtInfoDetails)
        throws ParseException, APIManagementException {

    String apiName = apiId.getApiName();
    JSONObject propreties = (JSONObject) containerMgtInfoDetails.get(PROPERTIES);
    String jwtSecurity ="";
    String basicSecurity = "";
    String oauthSecurity = "";

    if (propreties.get(MASTER_URL) != null && propreties.get(SATOKEN) != null && propreties.get(NAMESPACE) != null) {
        Config config = new ConfigBuilder()
                .withMasterUrl(propreties.get(MASTER_URL).toString().replace("\\", ""))
                .withOauthToken(propreties.get(SATOKEN).toString())
                .withNamespace(propreties.get(NAMESPACE).toString())
                .withClientKeyPassphrase(System.getProperty(CLIENT_KEY_PASSPHRASE)).build();

        OpenShiftClient client = new DefaultOpenShiftClient(config);

        if(propreties.get(JWT_SECURITY_CR_NAME) != null){
            jwtSecurity = propreties.get(JWT_SECURITY_CR_NAME).toString();
        }
        if(propreties.get(OAUTH2_SECURITY_CR_NAME) != null){
            oauthSecurity = propreties.get(OAUTH2_SECURITY_CR_NAME).toString();
        }
        if(propreties.get(BASICAUTH_SECURITY_CR_NAME) != null){
            basicSecurity = propreties.get(BASICAUTH_SECURITY_CR_NAME).toString();
        }
        String[] configMapNames = deployConfigMap(api, apiId, registry, client,
                jwtSecurity, oauthSecurity, basicSecurity, true);

        CustomResourceDefinition crd = client.customResourceDefinitions().withName(API_CRD_NAME).get();

        NonNamespaceOperation<APICustomResourceDefinition, APICustomResourceDefinitionList,
                DoneableAPICustomResourceDefinition, Resource<APICustomResourceDefinition,
                DoneableAPICustomResourceDefinition>> crdClient = getCRDClient(client, crd);

        APICustomResourceDefinition apiCustomResourceDefinition = crdClient.withName(apiName.toLowerCase()).get();

        apiCustomResourceDefinition.getSpec().setUpdateTimeStamp(getTimeStamp());
        apiCustomResourceDefinition.getSpec().getDefinition().setSwaggerConfigmapNames(configMapNames);
        //update with interceptors
        Interceptors interceptors = new Interceptors();
        interceptors.setBallerina(new String[]{});
        interceptors.setJava(new String[]{});
        apiCustomResourceDefinition.getSpec().getDefinition().setInterceptors(interceptors);

        crdClient.createOrReplace(apiCustomResourceDefinition);
        log.info("Successfully Re-deployed the [API] " + apiName);
    } else {
        log.error("Error occurred while re-deploying the API in Kubernetes cluster");
    }
}
 
Example 5
Source File: K8sManager.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Represents the LC change Blocked --> Republish
 * Redeploy the API CR with "override : false"
 *
 * @param apiId                   API Identifier
 * @param containerMgtInfoDetails Clusters which the API has published
 * @param configMapName           Name of the Config Map
 */
@Override
public void changeLCStateBlockedToRepublished(APIIdentifier apiId, JSONObject containerMgtInfoDetails,
                                              String[] configMapName) {

    String apiName = apiId.getApiName();

    JSONObject propreties = (JSONObject) containerMgtInfoDetails.get(ContainerBasedConstants.PROPERTIES);

    if (propreties.get(MASTER_URL) != null && propreties.get(SATOKEN) != null && propreties.get(NAMESPACE) != null) {
        Config config = new ConfigBuilder()
                .withMasterUrl(propreties.get(MASTER_URL).toString().replace("\\", ""))
                .withOauthToken(propreties.get(SATOKEN).toString())
                .withNamespace(propreties.get(NAMESPACE).toString())
                .withClientKeyPassphrase(System.getProperty(CLIENT_KEY_PASSPHRASE)).build();

        OpenShiftClient client = new DefaultOpenShiftClient(config);
        applyAPICustomResourceDefinition(client, configMapName, Integer.parseInt(propreties.get(REPLICAS).toString())
                , apiId, false);
        CustomResourceDefinition crd = client.customResourceDefinitions().withName(API_CRD_NAME).get();

        NonNamespaceOperation<APICustomResourceDefinition, APICustomResourceDefinitionList,
                DoneableAPICustomResourceDefinition, Resource<APICustomResourceDefinition,
                DoneableAPICustomResourceDefinition>> crdClient = getCRDClient(client, crd);

        List<APICustomResourceDefinition> apiCustomResourceDefinitionList = crdClient.list().getItems();

        for (APICustomResourceDefinition apiCustomResourceDefinition : apiCustomResourceDefinitionList) {

            if (apiCustomResourceDefinition.getMetadata().getName().equals(apiName.toLowerCase())) {

                apiCustomResourceDefinition.getSpec().setOverride(false);
                crdClient.createOrReplace(apiCustomResourceDefinition);
                log.info("Successfully Re-Published the [API] " + apiName);
                return;
            }
        }

        log.error("The requested custom resource for the [API] " + apiName + " was not found");
    } else {
        log.error("Error occurred while re-publishing the API in Kubernetes cluster");
    }
}
 
Example 6
Source File: K8sManager.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Deploys the API Custom resource kind in kubernetes
 *
 * @param client         , Openshift client
 * @param configmapNames , Name of the configmap
 * @param replicas       , number of replicas
 * @param apiIdentifier  , APIIdentifier
 * @param override       , Checks whether the API CR needs to be overrode or not
 */
private void applyAPICustomResourceDefinition(OpenShiftClient client, String[] configmapNames, int replicas,
                                              APIIdentifier apiIdentifier, Boolean override) {

    CustomResourceDefinitionList customResourceDefinitionList = client.customResourceDefinitions().list();
    List<CustomResourceDefinition> customResourceDefinitionItems = customResourceDefinitionList.getItems();
    CustomResourceDefinition apiCustomResourceDefinition = null;

    for (CustomResourceDefinition crd : customResourceDefinitionItems) {
        ObjectMeta metadata = crd.getMetadata();

        if (metadata != null && metadata.getName().equals(API_CRD_NAME)) {

            apiCustomResourceDefinition = crd;
        }
    }

    if (apiCustomResourceDefinition != null) {
        log.info("Found [CRD] " + apiCustomResourceDefinition.getMetadata().getSelfLink());
    } else {

        log.error("Custom resource definition apis.wso2.com was not found in the specified cluster");
        return;
    }

    NonNamespaceOperation<APICustomResourceDefinition, APICustomResourceDefinitionList,
            DoneableAPICustomResourceDefinition, Resource<APICustomResourceDefinition,
            DoneableAPICustomResourceDefinition>> apiCrdClient = getCRDClient(client, apiCustomResourceDefinition);

    // assigning values and creating API cr
    Definition definition = new Definition();
    Interceptors interceptors = new Interceptors();
    interceptors.setBallerina(new String[]{});
    interceptors.setJava(new String[]{});
    definition.setType(SWAGGER);
    definition.setSwaggerConfigmapNames(configmapNames);
    definition.setInterceptors(interceptors);

    APICustomResourceDefinitionSpec apiCustomResourceDefinitionSpec = new APICustomResourceDefinitionSpec();
    apiCustomResourceDefinitionSpec.setDefinition(definition);
    apiCustomResourceDefinitionSpec.setMode(MODE);
    apiCustomResourceDefinitionSpec.setReplicas(replicas);
    apiCustomResourceDefinitionSpec.setOverride(override);
    apiCustomResourceDefinitionSpec.setUpdateTimeStamp("");

    Status status = new Status();

    APICustomResourceDefinition apiCustomResourceDef = new APICustomResourceDefinition();
    apiCustomResourceDef.setSpec(apiCustomResourceDefinitionSpec);
    apiCustomResourceDef.setStatus(status);
    apiCustomResourceDef.setApiVersion(API_VERSION);
    apiCustomResourceDef.setKind(CRD_KIND);
    ObjectMeta meta = new ObjectMeta();
    meta.setName(apiIdentifier.getApiName().toLowerCase());
    meta.setNamespace(client.getNamespace());
    apiCustomResourceDef.setMetadata(meta);
    apiCrdClient.createOrReplace(apiCustomResourceDef);

    log.info("Created [API-CR] apis.wso2.com/" + apiCustomResourceDef.getMetadata().getName() + " for the "
            + "[API] " + apiIdentifier.getApiName());
}