github.com/oam-dev/kubevela@v1.9.11/vela-templates/definitions/internal/trait/expose.cue (about)

     1  import (
     2  	"strconv"
     3  	"strings"
     4  )
     5  
     6  expose: {
     7  	type: "trait"
     8  	annotations: {}
     9  	description: "Expose port to enable web traffic for your component."
    10  	attributes: {
    11  		podDisruptive: false
    12  		stage:         "PostDispatch"
    13  		appliesToWorkloads: ["deployments.apps", "statefulsets.apps"]
    14  		status: {
    15  			customStatus: #"""
    16  				message: *"" | string
    17  				service: context.outputs.service
    18  				if service.spec.type == "ClusterIP" {
    19  					message: "ClusterIP: \(service.spec.clusterIP)"
    20  				}
    21  				if service.spec.type == "LoadBalancer" {
    22  					status: service.status
    23  					isHealth: *false | bool
    24  					if status != _|_ if status.loadBalancer != _|_ if status.loadBalancer.ingress != _|_ if len(status.loadBalancer.ingress) > 0 if status.loadBalancer.ingress[0].ip != _|_ {
    25  						isHealth: true
    26  					}
    27  					if !isHealth {
    28  						message: "ExternalIP: Pending"
    29  					}
    30  					if isHealth {
    31  						message: "ExternalIP: \(status.loadBalancer.ingress[0].ip)"
    32  					}
    33  				}
    34  				"""#
    35  			healthPolicy: #"""
    36  				service: context.outputs.service
    37  				if service.spec.type == "LoadBalancer" {
    38  					status: service.status
    39  					isHealth: *false | bool
    40  					if status != _|_ if status.loadBalancer != _|_ if status.loadBalancer.ingress != _|_ if len(status.loadBalancer.ingress) > 0 if status.loadBalancer.ingress[0].ip != _|_ {
    41  						isHealth: true
    42  					}
    43  				}
    44  				if service.spec.type != "LoadBalancer" {
    45  					isHealth: true
    46  				}
    47  				"""#
    48  		}
    49  	}
    50  }
    51  template: {
    52  	outputs: service: {
    53  		apiVersion: "v1"
    54  		kind:       "Service"
    55  		metadata: name:        context.name
    56  		metadata: annotations: parameter.annotations
    57  		spec: {
    58  			if parameter["matchLabels"] == _|_ {
    59  				selector: "app.oam.dev/component": context.name
    60  			}
    61  			if parameter["matchLabels"] != _|_ {
    62  				selector: parameter["matchLabels"]
    63  			}
    64  
    65  			// compatible with the old way
    66  			if parameter["port"] != _|_ if parameter["ports"] == _|_ {
    67  				ports: [
    68  					for p in parameter.port {
    69  						name:       "port-" + strconv.FormatInt(p, 10)
    70  						port:       p
    71  						targetPort: p
    72  					},
    73  				]
    74  			}
    75  			if parameter["ports"] != _|_ {
    76  				ports: [ for v in parameter.ports {
    77  					port:       v.port
    78  					targetPort: v.port
    79  					if v.name != _|_ {
    80  						name: v.name
    81  					}
    82  					if v.name == _|_ {
    83  						_name: "port-" + strconv.FormatInt(v.port, 10)
    84  						name:  *_name | string
    85  						if v.protocol != "TCP" {
    86  							name: _name + "-" + strings.ToLower(v.protocol)
    87  						}
    88  					}
    89  					if v.nodePort != _|_ if parameter.type == "NodePort" {
    90  						nodePort: v.nodePort
    91  					}
    92  					if v.protocol != _|_ {
    93  						protocol: v.protocol
    94  					}
    95  				},
    96  				]
    97  			}
    98  			type: parameter.type
    99  		}
   100  	}
   101  	parameter: {
   102  		// +usage=Deprecated, the old way to specify the exposion ports
   103  		port?: [...int]
   104  
   105  		// +usage=Specify portsyou want customer traffic sent to
   106  		ports?: [...{
   107  			// +usage=Number of port to expose on the pod's IP address
   108  			port: int
   109  			// +usage=Name of the port
   110  			name?: string
   111  			// +usage=Protocol for port. Must be UDP, TCP, or SCTP
   112  			protocol: *"TCP" | "UDP" | "SCTP"
   113  			// +usage=exposed node port. Only Valid when exposeType is NodePort
   114  			nodePort?: int
   115  		}]
   116  
   117  		// +usage=Specify the annotations of the exposed service
   118  		annotations: [string]: string
   119  
   120  		matchLabels?: [string]: string
   121  
   122  		// +usage=Specify what kind of Service you want. options: "ClusterIP","NodePort","LoadBalancer","ExternalName"
   123  		type: *"ClusterIP" | "NodePort" | "LoadBalancer" | "ExternalName"
   124  	}
   125  }