2023-09-22 16:51:15 +00:00
|
|
|
package catalogv2beta1
|
2023-09-07 15:37:15 +00:00
|
|
|
|
|
|
|
import "golang.org/x/exp/slices"
|
|
|
|
|
|
|
|
func (w *Workload) GetMeshPortName() (string, bool) {
|
|
|
|
var meshPort string
|
|
|
|
|
2023-10-12 01:23:16 +00:00
|
|
|
for portName, port := range w.GetPorts() {
|
2023-09-07 15:37:15 +00:00
|
|
|
if port.Protocol == Protocol_PROTOCOL_MESH {
|
|
|
|
meshPort = portName
|
|
|
|
return meshPort, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Workload) IsMeshEnabled() bool {
|
|
|
|
_, ok := w.GetMeshPortName()
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Workload) GetNonExternalAddressesForPort(portName string) []*WorkloadAddress {
|
|
|
|
var addresses []*WorkloadAddress
|
|
|
|
|
2023-10-12 01:23:16 +00:00
|
|
|
// If this port doesn't exist on a workload, return nil.
|
|
|
|
if _, ok := w.GetPorts()[portName]; !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range w.GetAddresses() {
|
|
|
|
if address.GetExternal() {
|
2023-09-07 15:37:15 +00:00
|
|
|
// Skip external addresses.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no ports, that means this port is selected.
|
|
|
|
// Otherwise, check if the port is explicitly selected by this address
|
2023-10-12 01:23:16 +00:00
|
|
|
if len(address.Ports) == 0 || slices.Contains(address.GetPorts(), portName) {
|
2023-09-07 15:37:15 +00:00
|
|
|
addresses = append(addresses, address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses
|
|
|
|
}
|
2023-09-15 16:31:22 +00:00
|
|
|
|
2023-10-12 01:23:16 +00:00
|
|
|
func (w *Workload) GetFirstNonExternalMeshAddress() *WorkloadAddress {
|
|
|
|
// Find mesh port.
|
|
|
|
meshPort, ok := w.GetMeshPortName()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the workload has a specific address for the mesh port.
|
|
|
|
meshAddresses := w.GetNonExternalAddressesForPort(meshPort)
|
|
|
|
|
|
|
|
// If there are no mesh addresses, return. This should be impossible.
|
|
|
|
if len(meshAddresses) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are more than one mesh address, use the first one in the list.
|
|
|
|
return meshAddresses[0]
|
|
|
|
}
|
|
|
|
|
2023-09-15 16:31:22 +00:00
|
|
|
func (w *Workload) GetPortsByProtocol() map[Protocol][]string {
|
|
|
|
if w == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-12 01:23:16 +00:00
|
|
|
out := make(map[Protocol][]string, len(w.GetPorts()))
|
|
|
|
for name, port := range w.GetPorts() {
|
2023-09-15 16:31:22 +00:00
|
|
|
out[port.GetProtocol()] = append(out[port.GetProtocol()], name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|