Search in sources :

Example 1 with ServiceModel

use of org.ballerinax.kubernetes.models.knative.ServiceModel in project kubernetes by ballerinax.

the class KnativeServiceAnnotationProcessor method processAnnotation.

@Override
public void processAnnotation(ServiceNode serviceNode, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    BLangService bService = (BLangService) serviceNode;
    for (BLangExpression attachedExpr : bService.getAttachedExprs()) {
        // If not anonymous endpoint throw error.
        if (attachedExpr instanceof BLangSimpleVarRef) {
            // throw new KubernetesPluginException("adding @knative:Service{} annotation to a service is only " +
            // "supported when the service has an anonymous listener");
            ServiceModel serviceModelAttched = processService(attachmentNode);
            serviceModelAttched.addPort(serviceModelAttched.getPort());
            if (KnativeUtils.isBlank(serviceModelAttched.getName())) {
                serviceModelAttched.setName(KnativeUtils.getValidName(serviceNode.getName().getValue()) + KNATIVE_SVC_POSTFIX);
            }
            return;
        }
    }
    ServiceModel serviceModel = processService(attachmentNode);
    if (KnativeUtils.isBlank(serviceModel.getName())) {
        serviceModel.setName(KnativeUtils.getValidName(serviceNode.getName().getValue()) + KNATIVE_SVC_POSTFIX);
    }
    // If service annotation port is not empty, then listener port is used for the k8s svc target port while
    // service annotation port is used for k8s port.
    // If service annotation port is empty, then listener port is used for both port and target port of the k8s
    // svc.
    BLangTypeInit bListener = (BLangTypeInit) bService.getAttachedExprs().get(0);
    if (serviceModel.getPort() == 8080) {
        if (extractPort(bListener) == 9090 || extractPort(bListener) == 9091 || extractPort(bListener) == 8013 || extractPort(bListener) == 8012) {
            throw new KubernetesPluginException("listner port is conflicts with knative port");
        }
        serviceModel.addPort(extractPort(bListener));
    }
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) ServiceModel(org.ballerinax.kubernetes.models.knative.ServiceModel) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 2 with ServiceModel

use of org.ballerinax.kubernetes.models.knative.ServiceModel in project kubernetes by ballerinax.

the class KnativeServiceAnnotationProcessor method processService.

private ServiceModel processService(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    ServiceModel serviceModel = new ServiceModel();
    List<BLangRecordLiteral.BLangRecordKeyValueField> keyValues = convertRecordFields(((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getFields());
    for (BLangRecordLiteral.BLangRecordKeyValueField keyValue : keyValues) {
        ServiceConfiguration serviceConfiguration = ServiceConfiguration.valueOf(keyValue.getKey().toString());
        switch(serviceConfiguration) {
            case name:
                serviceModel.setName(getValidName(getStringValue(keyValue.getValue())));
                break;
            case labels:
                serviceModel.setLabels(getMap(keyValue.getValue()));
                break;
            case annotations:
                serviceModel.setAnnotations(getMap(keyValue.getValue()));
                break;
            case dockerHost:
                serviceModel.setDockerHost(getStringValue(keyValue.getValue()));
                break;
            case dockerCertPath:
                serviceModel.setDockerCertPath(getStringValue(keyValue.getValue()));
                break;
            case registry:
                serviceModel.setRegistry(getStringValue(keyValue.getValue()));
                break;
            case username:
                serviceModel.setUsername(getStringValue(keyValue.getValue()));
                break;
            case password:
                serviceModel.setPassword(getStringValue(keyValue.getValue()));
                break;
            case baseImage:
                serviceModel.setBaseImage(getStringValue(keyValue.getValue()));
                break;
            case image:
                serviceModel.setImage(getStringValue(keyValue.getValue()));
                break;
            case buildImage:
                serviceModel.setBuildImage(getBooleanValue(keyValue.getValue()));
                break;
            case push:
                serviceModel.setPush(getBooleanValue(keyValue.getValue()));
                break;
            case cmd:
                serviceModel.setCmd(getStringValue(keyValue.getValue()));
                break;
            case copyFiles:
                serviceModel.setCopyFiles(getExternalFileMap(keyValue));
                break;
            case singleYAML:
                serviceModel.setSingleYAML(getBooleanValue(keyValue.getValue()));
                break;
            case namespace:
                KnativeContext.getInstance().getDataHolder().setNamespace(getStringValue(keyValue.getValue()));
                break;
            case replicas:
                serviceModel.setReplicas(getIntValue(keyValue.getValue()));
                break;
            case livenessProbe:
                serviceModel.setLivenessProbe(parseProbeConfiguration(keyValue.getValue()));
                break;
            case readinessProbe:
                serviceModel.setReadinessProbe(parseProbeConfiguration(keyValue.getValue()));
                break;
            case imagePullPolicy:
                serviceModel.setImagePullPolicy(getStringValue(keyValue.getValue()));
                break;
            case env:
                serviceModel.setEnv(getEnvVarMap(keyValue.getValue()));
                break;
            case podAnnotations:
                serviceModel.setPodAnnotations(getMap(keyValue.getValue()));
                break;
            case podTolerations:
                serviceModel.setPodTolerations(parsePodTolerationConfiguration(keyValue.getValue()));
                break;
            case dependsOn:
                serviceModel.setDependsOn(getDependsOn(keyValue));
                break;
            case imagePullSecrets:
                serviceModel.setImagePullSecrets(getImagePullSecrets(keyValue));
                break;
            case containerConcurrency:
                serviceModel.setContainerConcurrency(getIntValue(keyValue.getValue()));
                break;
            case timeoutSeconds:
                serviceModel.setTimeoutSeconds(getIntValue(keyValue.getValue()));
                break;
            case port:
                serviceModel.setPort(getIntValue(keyValue.getValue()));
                break;
            default:
                break;
        }
    }
    String dockerHost = System.getenv(DOCKER_HOST);
    if (!isBlank(dockerHost)) {
        serviceModel.setDockerHost(dockerHost);
    }
    String dockerCertPath = System.getenv(DOCKER_CERT_PATH);
    if (!isBlank(dockerCertPath)) {
        serviceModel.setDockerCertPath(dockerCertPath);
    }
    KnativeContext.getInstance().getDataHolder().setServiceModel(serviceModel);
    return serviceModel;
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.knative.ServiceModel) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 3 with ServiceModel

use of org.ballerinax.kubernetes.models.knative.ServiceModel in project kubernetes by ballerinax.

the class KnativeArtifactManager method populateDeploymentModel.

public void populateDeploymentModel() {
    ServiceModel serviceModel = knativeDataHolder.getServiceModel();
    knativeDataHolder.setServiceModel(serviceModel);
    String balxFileName = extractJarName(knativeDataHolder.getUberJarPath());
    if (isBlank(serviceModel.getName())) {
        if (balxFileName != null) {
            serviceModel.setName(getValidName(balxFileName) + DEPLOYMENT_POSTFIX);
        }
    }
    if (isBlank(serviceModel.getImage())) {
        serviceModel.setImage(balxFileName + DOCKER_LATEST_TAG);
    }
    serviceModel.addLabel(KubernetesConstants.KUBERNETES_SELECTOR_KEY, balxFileName);
    knativeDataHolder.setServiceModel(serviceModel);
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.knative.ServiceModel)

Example 4 with ServiceModel

use of org.ballerinax.kubernetes.models.knative.ServiceModel in project kubernetes by ballerinax.

the class KnativeContainerHandler method createArtifacts.

@Override
public void createArtifacts() throws KubernetesPluginException {
    // Service
    ServiceModel deploymentModel = knativeDataHolder.getServiceModel();
    Map<String, KnativeContainerModel> serviceModels = knativeDataHolder.getbListenerToK8sServiceMap();
    int count = 0;
    for (KnativeContainerModel serviceModel : serviceModels.values()) {
        count++;
        String balxFileName = extractJarName(KnativeContext.getInstance().getDataHolder().getUberJarPath());
        serviceModel.addLabel(KubernetesConstants.KUBERNETES_SELECTOR_KEY, balxFileName);
        serviceModel.setSelector(balxFileName);
        generate(serviceModel);
        deploymentModel.addPort(serviceModel.getTargetPort());
    }
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.knative.ServiceModel) KnativeContainerModel(org.ballerinax.kubernetes.models.knative.KnativeContainerModel)

Example 5 with ServiceModel

use of org.ballerinax.kubernetes.models.knative.ServiceModel in project kubernetes by ballerinax.

the class KnativeConfigMapHandler method createArtifacts.

@Override
public void createArtifacts() throws KubernetesPluginException {
    // configMap
    int count = 0;
    Collection<ConfigMapModel> configMapModels = knativeDataHolder.getConfigMapModelSet();
    if (configMapModels.size() > 0) {
        OUT.println();
    }
    for (ConfigMapModel configMapModel : configMapModels) {
        count++;
        if (!isBlank(configMapModel.getBallerinaConf())) {
            if (configMapModel.getData().size() != 1) {
                throw new KubernetesPluginException("there can be only 1 ballerina config file");
            }
            ServiceModel serviceModel = knativeDataHolder.getServiceModel();
            serviceModel.setCommandArgs(" --b7a.config.file=${CONFIG_FILE}");
            EnvVarValueModel envVarValueModel = new EnvVarValueModel(configMapModel.getMountPath() + BALLERINA_CONF_FILE_NAME);
            serviceModel.addEnv("CONFIG_FILE", envVarValueModel);
            knativeDataHolder.setServiceModel(serviceModel);
        }
        generate(configMapModel);
        OUT.print("\t@knative:ConfigMap \t\t\t - complete " + count + "/" + configMapModels.size() + "\r");
    }
}
Also used : ConfigMapModel(org.ballerinax.kubernetes.models.knative.ConfigMapModel) ServiceModel(org.ballerinax.kubernetes.models.knative.ServiceModel) EnvVarValueModel(org.ballerinax.kubernetes.models.knative.EnvVarValueModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Aggregations

ServiceModel (org.ballerinax.kubernetes.models.knative.ServiceModel)7 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)3 BLangTypeInit (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit)2 DockerGenException (org.ballerinax.docker.generator.exceptions.DockerGenException)1 ConfigMapModel (org.ballerinax.kubernetes.models.knative.ConfigMapModel)1 EnvVarValueModel (org.ballerinax.kubernetes.models.knative.EnvVarValueModel)1 KnativeContainerModel (org.ballerinax.kubernetes.models.knative.KnativeContainerModel)1 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)1 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1