# Code generated by KubeVela templates. DO NOT EDIT. Please edit the original cue file.
# Definition source cue file: vela-templates/definitions/internal/expose.cue
apiVersion: core.oam.dev/v1beta1
kind: TraitDefinition
metadata:
  annotations:
    definition.oam.dev/description: Expose port to enable web traffic for your component.
  name: expose
  namespace: {{ include "systemDefinitionNamespace" . }}
spec:
  appliesToWorkloads:
    - deployments.apps
    - statefulsets.apps
  podDisruptive: false
  schematic:
    cue:
      template: |
        import (
        	"strconv"
        )

        outputs: service: {
        	apiVersion: "v1"
        	kind:       "Service"
        	metadata: name:        context.name
        	metadata: annotations: parameter.annotations
        	spec: {
        		selector: "app.oam.dev/component": context.name
        		ports: [
        			for p in parameter.port {
        				name:       "port-" + strconv.FormatInt(p, 10)
        				port:       p
        				targetPort: p
        			},
        		]
        		type: parameter.type
        	}
        }
        parameter: {
        	// +usage=Specify the exposion ports
        	port: [...int]
        	// +usage=Specify the annotaions of the exposed service
        	annotations: [string]: string
        	// +usage=Specify what kind of Service you want. options: "ClusterIP","NodePort","LoadBalancer","ExternalName"
        	type: *"ClusterIP" | "NodePort" | "LoadBalancer" | "ExternalName"
        }
  status:
    customStatus: |-
      message: *"" | string
      service: context.outputs.service
      if service.spec.type == "ClusterIP" {
      	message: "ClusterIP: \(service.spec.clusterIP)"
      }
      if service.spec.type == "LoadBalancer" {
      	status: service.status
      	isHealth: *false | bool
      	if status != _|_ if status.loadBalancer != _|_ if status.loadBalancer.ingress != _|_ if len(status.loadBalancer.ingress) > 0 if status.loadBalancer.ingress[0].ip != _|_ {
      		isHealth: true
      	}
      	if !isHealth {
      		message: "ExternalIP: Pending"
      	}
      	if isHealth {
      		message: "ExternalIP: \(status.loadBalancer.ingress[0].ip)"
      	}
      }
    healthPolicy: |-
      service: context.outputs.service
      if service.spec.type == "LoadBalancer" {
      	status: service.status
      	isHealth: *false | bool
      	if status != _|_ if status.loadBalancer != _|_ if status.loadBalancer.ingress != _|_ if len(status.loadBalancer.ingress) > 0 if status.loadBalancer.ingress[0].ip != _|_ {
      		isHealth: true
      	}
      }
      if service.spec.type != "LoadBalancer" {
      	isHealth: true
      }

