import (
	"strconv"
)

expose: {
	type: "trait"
	annotations: {}
	description: "Expose port to enable web traffic for your component."
	attributes: {
		podDisruptive: false
		appliesToWorkloads: ["deployments.apps", "statefulsets.apps"]
		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
				}
				"""#
		}
	}
}
template: {
	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"
	}
}
