Search in sources :

Example 1 with IstioDestinationWeight

use of org.ballerinax.kubernetes.models.istio.IstioDestinationWeight in project kubernetes by ballerinax.

the class IstioVirtualServiceHandler method populateRouteList.

/**
 * Parse an route list to a yaml map.
 *
 * @param serviceName The name of the service.
 * @param routeModels       The list of destination weights
 * @return A list of yaml maps.
 */
private List<HTTPRouteDestination> populateRouteList(String serviceName, List<IstioDestinationWeight> routeModels) {
    if (routeModels == null) {
        routeModels = new LinkedList<>();
    }
    if (routeModels.size() == 0) {
        routeModels.add(new IstioDestinationWeight());
    }
    List<HTTPRouteDestination> destinationWeightList = new LinkedList<>();
    for (IstioDestinationWeight destinationWeightModel : routeModels) {
        HTTPRouteDestination routeDestination = new HTTPRouteDestinationBuilder().withWeight(destinationWeightModel.getWeight()).withDestination(populateDestination(serviceName, destinationWeightModel.getDestination())).build();
        destinationWeightList.add(routeDestination);
    }
    return destinationWeightList;
}
Also used : HTTPRouteDestinationBuilder(me.snowdrop.istio.api.networking.v1alpha3.HTTPRouteDestinationBuilder) IstioDestinationWeight(org.ballerinax.kubernetes.models.istio.IstioDestinationWeight) HTTPRouteDestination(me.snowdrop.istio.api.networking.v1alpha3.HTTPRouteDestination) LinkedList(java.util.LinkedList)

Example 2 with IstioDestinationWeight

use of org.ballerinax.kubernetes.models.istio.IstioDestinationWeight in project kubernetes by ballerinax.

the class IstioVirtualServiceAnnotationProcessor method processRoutesAnnotation.

/**
 * Process routes of http annotation to a model.
 *
 * @param routeArray The list of routes.
 * @return A list of istio destination weight models.
 * @throws KubernetesPluginException When an unknown field is found.
 */
private List<IstioDestinationWeight> processRoutesAnnotation(BLangListConstructorExpr routeArray) throws KubernetesPluginException {
    List<IstioDestinationWeight> destinationWeights = new LinkedList<>();
    for (ExpressionNode expression : routeArray.getExpressions()) {
        BLangRecordLiteral routeFields = (BLangRecordLiteral) expression;
        IstioDestinationWeight destinationWeight = new IstioDestinationWeight();
        for (BLangRecordLiteral.BLangRecordKeyValueField routeField : convertRecordFields(routeFields.getFields())) {
            switch(DestinationWeightConfig.valueOf(routeField.getKey().toString())) {
                case destination:
                    BLangRecordLiteral destinationFields = (BLangRecordLiteral) routeField.getValue();
                    IstioDestination destination = processDestinationAnnotation(destinationFields);
                    destinationWeight.setDestination(destination);
                    break;
                case weight:
                    destinationWeight.setWeight(getIntValue(routeField.getValue()));
                    break;
                default:
                    throw new KubernetesPluginException("unknown field found for istio virtual service: " + routeField.getKey().toString());
            }
        }
        destinationWeights.add(destinationWeight);
    }
    return destinationWeights;
}
Also used : IstioDestination(org.ballerinax.kubernetes.models.istio.IstioDestination) IstioDestinationWeight(org.ballerinax.kubernetes.models.istio.IstioDestinationWeight) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)2 IstioDestinationWeight (org.ballerinax.kubernetes.models.istio.IstioDestinationWeight)2 HTTPRouteDestination (me.snowdrop.istio.api.networking.v1alpha3.HTTPRouteDestination)1 HTTPRouteDestinationBuilder (me.snowdrop.istio.api.networking.v1alpha3.HTTPRouteDestinationBuilder)1 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)1 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)1 IstioDestination (org.ballerinax.kubernetes.models.istio.IstioDestination)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1