-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Handle missing name on service port * Undo un-needed change
- Loading branch information
Showing
4 changed files
with
142 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package clusters | ||
|
||
import ( | ||
"github.com/admiral/admiral/pkg/controller/common" | ||
"istio.io/istio/pkg/log" | ||
"strconv" | ||
"strings" | ||
|
||
k8sV1 "k8s.io/api/core/v1" | ||
k8sAppsV1 "k8s.io/api/apps/v1" | ||
) | ||
|
||
func GetMeshPorts(clusterName string, destService *k8sV1.Service, | ||
destDeployment *k8sAppsV1.Deployment) map[string]uint32 { | ||
var ports = make(map[string]uint32) | ||
var meshPorts = destDeployment.Spec.Template.Annotations[common.SidecarEnabledPorts] | ||
if len(meshPorts) == 0 { | ||
log.Infof(LogFormat, "GetMeshPorts", "service", destService.Name, clusterName, "No mesh ports present, defaulting to first port") | ||
if destService.Spec.Ports != nil && len(destService.Spec.Ports) > 0 { | ||
var name = destService.Spec.Ports[0].Name | ||
if len(name) == 0 { | ||
name = common.Http | ||
} | ||
ports[name] = uint32(destService.Spec.Ports[0].Port) | ||
} | ||
return ports | ||
} | ||
|
||
meshPortsSplit := strings.Split(meshPorts, ",") | ||
var meshPortMap = make(map[uint32]uint32) | ||
for _, meshPort := range meshPortsSplit { | ||
port, err := strconv.ParseUint(meshPort, 10, 32) | ||
if err != nil { | ||
continue | ||
} | ||
meshPortMap[uint32(port)] = uint32(port) | ||
} | ||
for _, servicePort := range destService.Spec.Ports { | ||
if _, ok := meshPortMap[uint32(servicePort.Port)]; ok { | ||
log.Debugf(LogFormat, "GetMeshPorts", servicePort.Port, destService.Name, clusterName, "Adding mesh port") | ||
ports[common.Http] = uint32(servicePort.Port) | ||
} | ||
} | ||
return ports | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package clusters | ||
|
||
import ( | ||
"github.com/admiral/admiral/pkg/controller/common" | ||
k8sAppsV1 "k8s.io/api/apps/v1" | ||
v12 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"reflect" | ||
"strconv" | ||
"testing" | ||
|
||
k8sV1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestGetMeshPorts(t *testing.T) { | ||
|
||
annotatedPort := 8090 | ||
defaultServicePort := uint32(8080) | ||
|
||
defaultK8sSvcPortNoName := k8sV1.ServicePort{Port: int32(defaultServicePort)} | ||
defaultK8sSvcPort := k8sV1.ServicePort{Name: "default", Port: int32(defaultServicePort)} | ||
meshK8sSvcPort := k8sV1.ServicePort{Name: "mesh", Port: int32(annotatedPort)} | ||
|
||
serviceMeshPorts := []k8sV1.ServicePort{defaultK8sSvcPort, meshK8sSvcPort} | ||
|
||
serviceMeshPortsOnlyDefault := []k8sV1.ServicePort{defaultK8sSvcPortNoName} | ||
|
||
service := k8sV1.Service{ | ||
ObjectMeta: v1.ObjectMeta{Name: "server", Labels:map[string]string{"asset": "Intuit.platform.mesh.server"}}, | ||
Spec: k8sV1.ServiceSpec{Ports: serviceMeshPorts}, | ||
} | ||
deployment := k8sAppsV1.Deployment{ | ||
Spec: k8sAppsV1.DeploymentSpec{Template:v12.PodTemplateSpec{ | ||
ObjectMeta: v1.ObjectMeta{Annotations:map[string]string{common.SidecarEnabledPorts: strconv.Itoa(annotatedPort)}}, | ||
}}} | ||
|
||
ports := map[string]uint32{"http": uint32(annotatedPort)} | ||
|
||
portsFromDefaultSvcPort := map[string]uint32{"http": defaultServicePort} | ||
|
||
emptyPorts := map[string]uint32{} | ||
|
||
testCases := []struct { | ||
name string | ||
clusterName string | ||
service k8sV1.Service | ||
deployment k8sAppsV1.Deployment | ||
expected map[string]uint32 | ||
}{ | ||
{ | ||
name: "should return a port based on annotation", | ||
service: service, | ||
deployment: deployment, | ||
expected: ports, | ||
}, | ||
{ | ||
name: "should return a default port", | ||
service: k8sV1.Service{ | ||
ObjectMeta: v1.ObjectMeta{Name: "server", Labels:map[string]string{"asset": "Intuit.platform.mesh.server"}}, | ||
Spec: k8sV1.ServiceSpec{Ports: serviceMeshPortsOnlyDefault}, | ||
}, | ||
deployment: k8sAppsV1.Deployment{ | ||
Spec: k8sAppsV1.DeploymentSpec{Template:v12.PodTemplateSpec{ | ||
ObjectMeta: v1.ObjectMeta{Annotations:map[string]string{}}, | ||
}}}, | ||
expected: portsFromDefaultSvcPort, | ||
}, | ||
{ | ||
name: "should return empty ports", | ||
service: k8sV1.Service{ | ||
ObjectMeta: v1.ObjectMeta{Name: "server", Labels:map[string]string{"asset": "Intuit.platform.mesh.server"}}, | ||
Spec: k8sV1.ServiceSpec{Ports: nil}, | ||
}, | ||
deployment: k8sAppsV1.Deployment{ | ||
Spec: k8sAppsV1.DeploymentSpec{Template:v12.PodTemplateSpec{ | ||
ObjectMeta: v1.ObjectMeta{Annotations:map[string]string{}}, | ||
}}}, | ||
expected: emptyPorts, | ||
}, | ||
} | ||
|
||
for _, c := range testCases { | ||
t.Run(c.name, func(t *testing.T) { | ||
meshPorts := GetMeshPorts(c.clusterName, &c.service, &c.deployment) | ||
if !reflect.DeepEqual(meshPorts, c.expected) { | ||
t.Errorf("Wanted meshPorts: %v, got: %v", c.expected, meshPorts) | ||
} | ||
}) | ||
} | ||
} |