Skip to content

Commit 843741b

Browse files
committed
update for lint errors with repeat imports and using slice of pointers for service and policy maps
1 parent 69a71be commit 843741b

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
corev1 "k8s.io/api/core/v1"
11-
v1 "k8s.io/api/core/v1"
1211
networkingv1 "k8s.io/api/networking/v1"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
"k8s.io/client-go/kubernetes"
@@ -45,8 +44,14 @@ func main() {
4544
policiesByNamespace := make(map[string][]networkingv1.NetworkPolicy)
4645
servicesByNamespace := make(map[string][]corev1.Service)
4746

47+
// Copy namespaces.Items into a slice of pointers
48+
namespacePointers := make([]*corev1.Namespace, len(namespaces.Items))
49+
for i := range namespaces.Items {
50+
namespacePointers[i] = &namespaces.Items[i]
51+
}
52+
4853
// Iterate over namespaces and store policies/services
49-
for _, ns := range namespaces.Items {
54+
for _, ns := range namespacePointers {
5055
fmt.Printf("Writing policies and services for namespace %s...\n", ns.Name)
5156

5257
// Get network policies
@@ -238,10 +243,10 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic
238243

239244
// Check if are there services with externalTrafficPolicy=Cluster (applicable if Type=NodePort or Type=LoadBalancer)
240245
for _, service := range serviceListAtNamespace {
241-
if service.Spec.Type == v1.ServiceTypeLoadBalancer || service.Spec.Type == v1.ServiceTypeNodePort {
246+
if service.Spec.Type == corev1.ServiceTypeLoadBalancer || service.Spec.Type == corev1.ServiceTypeNodePort {
242247
externalTrafficPolicy := service.Spec.ExternalTrafficPolicy
243248
// If the service has externalTrafficPolicy is set to "Cluster" add it to the servicesAtRisk list (ExternalTrafficPolicy: "" defaults to Cluster)
244-
if externalTrafficPolicy != v1.ServiceExternalTrafficPolicyTypeLocal {
249+
if externalTrafficPolicy != corev1.ServiceExternalTrafficPolicyTypeLocal {
245250
// Any service with externalTrafficPolicy=Cluster is at risk so need to elimate any services that are incorrectly flagged
246251
servicesAtRisk = append(servicesAtRisk, fmt.Sprintf("%s/%s", namespace.Name, service.Name))
247252
// If the service has no selector add it to the noSelectorServices list
@@ -304,7 +309,7 @@ func hasIngressPolicies(policies []networkingv1.NetworkPolicy) bool {
304309
return false
305310
}
306311

307-
func checkServiceRisk(service v1.Service, namespace string, policiesListAtNamespace []networkingv1.NetworkPolicy, safeServices []string) []string {
312+
func checkServiceRisk(service corev1.Service, namespace string, policiesListAtNamespace []networkingv1.NetworkPolicy, safeServices []string) []string {
308313
for _, policy := range policiesListAtNamespace {
309314
for _, ingress := range policy.Spec.Ingress {
310315
// Check if there is an allow all ingress policy that matches labels the service is safe
@@ -359,7 +364,7 @@ func checkPolicyMatchServiceLabels(serviceLabels, policyLabels map[string]string
359364
return true
360365
}
361366

362-
func checkServiceTargetPortMatchPolicyPorts(servicePorts []v1.ServicePort, policyPorts []networkingv1.NetworkPolicyPort) bool {
367+
func checkServiceTargetPortMatchPolicyPorts(servicePorts []corev1.ServicePort, policyPorts []networkingv1.NetworkPolicyPort) bool {
363368
ingressPorts := []string{}
364369
for _, port := range policyPorts {
365370
ingressPorts = append(ingressPorts, fmt.Sprintf("%d/%s", port.Port.IntVal, string(*port.Protocol)))

0 commit comments

Comments
 (0)