Skip to content

Commit c329191

Browse files
committed
added baseline service tests and updated logic for unsafe and noselector services with the edgecase of deny all + service no selector in mind
1 parent a2d413f commit c329191

File tree

2 files changed

+682
-20
lines changed

2 files changed

+682
-20
lines changed

tools/azure-npm-to-cilium-validator/azure-npm-to-cilium-validator.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ func getEgressPolicies(policiesByNamespace map[string][]*networkingv1.NetworkPol
162162
return egressPolicies
163163
}
164164

165-
func getExternalTrafficPolicyClusterServices(namespaces *corev1.NamespaceList, servicesByNamespace map[string][]*corev1.Service, policiesByNamespace map[string][]*networkingv1.NetworkPolicy) (unsafeServices, noSelectorServices []string) {
166-
var servicesAtRisk, safeServices []string
165+
func getExternalTrafficPolicyClusterServices(namespaces *corev1.NamespaceList, servicesByNamespace map[string][]*corev1.Service, policiesByNamespace map[string][]*networkingv1.NetworkPolicy) (unsafeServicesAtRisk, unsafeNoSelectorServices []string) {
166+
var servicesAtRisk, noSelectorServices, safeServices []string
167167

168168
for i := range namespaces.Items {
169169
namespace := &namespaces.Items[i]
@@ -195,10 +195,11 @@ func getExternalTrafficPolicyClusterServices(namespaces *corev1.NamespaceList, s
195195
}
196196
}
197197

198-
// Get the services that are at risk but not in the safe services or no selector services lists
199-
unsafeServices = difference(&servicesAtRisk, &safeServices, &noSelectorServices)
200-
201-
return unsafeServices, noSelectorServices
198+
// Remove all the safe services from the services at risk
199+
unsafeServicesAtRisk = difference(&servicesAtRisk, &safeServices)
200+
// Remove all the safe services from the no selector services
201+
unsafeNoSelectorServices = difference(&noSelectorServices, &safeServices)
202+
return unsafeServicesAtRisk, unsafeNoSelectorServices
202203
}
203204

204205
func hasIngressPolicies(policies []*networkingv1.NetworkPolicy) bool {
@@ -299,14 +300,11 @@ func checkServiceTargetPortMatchPolicyPorts(servicePorts *[]corev1.ServicePort,
299300
return true
300301
}
301302

302-
func difference(slice1, slice2, slice3 *[]string) []string {
303+
func difference(slice1, slice2 *[]string) []string {
303304
m := make(map[string]struct{})
304305
for _, s := range *slice2 {
305306
m[s] = struct{}{}
306307
}
307-
for _, s := range *slice3 {
308-
m[s] = struct{}{}
309-
}
310308
var diff []string
311309
for _, s := range *slice1 {
312310
if _, ok := m[s]; !ok {
@@ -347,13 +345,13 @@ func printMigrationSummary(namespaces *corev1.NamespaceList, policiesByNamespace
347345
fmt.Println("+------------------------------+-------------------------------+")
348346

349347
// Get services that have externalTrafficPolicy!=Local
350-
unsafeServices, noSelectorServices := getExternalTrafficPolicyClusterServices(namespaces, servicesByNamespace, policiesByNamespace)
348+
unsafeServicesAtRisk, unsafeNoSelectorServices := getExternalTrafficPolicyClusterServices(namespaces, servicesByNamespace, policiesByNamespace)
351349

352350
// Print the services that are at risk
353-
printUnsafeServices(&unsafeServices, &noSelectorServices)
351+
printUnsafeServices(&unsafeServicesAtRisk, &unsafeNoSelectorServices)
354352

355353
fmt.Println("+------------------------------+-------------------------------+")
356-
if len(ingressEndportNetworkPolicy) > 0 || len(egressEndportNetworkPolicy) > 0 || len(ingressPoliciesWithCIDR) > 0 || len(egressPoliciesWithCIDR) > 0 || len(egressPolicies) > 0 || len(unsafeServices) > 0 {
354+
if len(ingressEndportNetworkPolicy) > 0 || len(egressEndportNetworkPolicy) > 0 || len(ingressPoliciesWithCIDR) > 0 || len(egressPoliciesWithCIDR) > 0 || len(egressPolicies) > 0 || len(unsafeServicesAtRisk) > 0 {
357355
fmt.Println("\033[31m✘ Review above issues before migration.\033[0m")
358356
fmt.Println("Please see \033[32maka.ms/azurenpmtocilium\033[0m for instructions on how to evaluate/assess the above warnings marked by ❌.")
359357
fmt.Println("NOTE: rerun this script if any modifications (create/update/delete) are made to services or policies.")
@@ -416,27 +414,29 @@ func printEgressPolicies(egressPolicies *[]string) {
416414
}
417415
}
418416

419-
func printUnsafeServices(unsafeServices, noSelectorServices *[]string) {
420-
// If there is no unsafe services then migration is safe for services with extranalTrafficPolicy=Cluster
421-
if len(*unsafeServices) == 0 {
417+
func printUnsafeServices(unsafeServicesAtRisk, unsafeNoSelectorServices *[]string) {
418+
// If there is no unsafe services and services with no selectors then migration is safe for services with extranalTrafficPolicy=Cluster
419+
if len(*unsafeServicesAtRisk) == 0 {
422420
fmt.Printf("%-30s | %-30s \n", "Disruption for some", "✅")
423421
fmt.Printf("%-30s | %-30s \n", "Services with", "")
424422
fmt.Printf("%-30s | %-30s \n", "externalTrafficPolicy=Cluster", "")
425423
} else {
424+
// Remove all no selector services from unsafe services to prevent repeating the same flagged service
425+
*unsafeServicesAtRisk = difference(unsafeServicesAtRisk, unsafeNoSelectorServices)
426426
fmt.Printf("%-30s | %-30s \n", "Disruption for some", "❌")
427427
fmt.Printf("%-30s | %-30s \n", "Services with", "")
428428
fmt.Printf("%-30s | %-30s \n", "externalTrafficPolicy=Cluster", "")
429429
fmt.Println("Services affected:")
430430
// If there are any no selector services or unsafe services then print them as they could be impacted by migration
431-
if len(*noSelectorServices) > 0 {
432-
for _, service := range *noSelectorServices {
431+
if len(*unsafeNoSelectorServices) > 0 {
432+
for _, service := range *unsafeNoSelectorServices {
433433
serviceName := strings.Split(service, "/")[1]
434434
serviceNamespace := strings.Split(service, "/")[0]
435435
fmt.Printf("❌ Found Service: \033[31m%s\033[0m without selectors in namespace: \033[31m%s\033[0m\n", serviceName, serviceNamespace)
436436
}
437437
}
438-
if len(*unsafeServices) > 0 {
439-
for _, service := range *unsafeServices {
438+
if len(*unsafeServicesAtRisk) > 0 {
439+
for _, service := range *unsafeServicesAtRisk {
440440
serviceName := strings.Split(service, "/")[1]
441441
serviceNamespace := strings.Split(service, "/")[0]
442442
fmt.Printf("❌ Found Service: \033[31m%s\033[0m with selectors in namespace: \033[31m%s\033[0m\n", serviceName, serviceNamespace)

0 commit comments

Comments
 (0)