|
8 | 8 | "strings" |
9 | 9 |
|
10 | 10 | corev1 "k8s.io/api/core/v1" |
11 | | - v1 "k8s.io/api/core/v1" |
12 | 11 | networkingv1 "k8s.io/api/networking/v1" |
13 | 12 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
14 | 13 | "k8s.io/client-go/kubernetes" |
@@ -45,8 +44,14 @@ func main() { |
45 | 44 | policiesByNamespace := make(map[string][]networkingv1.NetworkPolicy) |
46 | 45 | servicesByNamespace := make(map[string][]corev1.Service) |
47 | 46 |
|
| 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 | + |
48 | 53 | // Iterate over namespaces and store policies/services |
49 | | - for _, ns := range namespaces.Items { |
| 54 | + for _, ns := range namespacePointers { |
50 | 55 | fmt.Printf("Writing policies and services for namespace %s...\n", ns.Name) |
51 | 56 |
|
52 | 57 | // Get network policies |
@@ -238,10 +243,10 @@ func checkExternalTrafficPolicyServices(namespaces *corev1.NamespaceList, servic |
238 | 243 |
|
239 | 244 | // Check if are there services with externalTrafficPolicy=Cluster (applicable if Type=NodePort or Type=LoadBalancer) |
240 | 245 | 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 { |
242 | 247 | externalTrafficPolicy := service.Spec.ExternalTrafficPolicy |
243 | 248 | // 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 { |
245 | 250 | // Any service with externalTrafficPolicy=Cluster is at risk so need to elimate any services that are incorrectly flagged |
246 | 251 | servicesAtRisk = append(servicesAtRisk, fmt.Sprintf("%s/%s", namespace.Name, service.Name)) |
247 | 252 | // If the service has no selector add it to the noSelectorServices list |
@@ -304,7 +309,7 @@ func hasIngressPolicies(policies []networkingv1.NetworkPolicy) bool { |
304 | 309 | return false |
305 | 310 | } |
306 | 311 |
|
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 { |
308 | 313 | for _, policy := range policiesListAtNamespace { |
309 | 314 | for _, ingress := range policy.Spec.Ingress { |
310 | 315 | // 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 |
359 | 364 | return true |
360 | 365 | } |
361 | 366 |
|
362 | | -func checkServiceTargetPortMatchPolicyPorts(servicePorts []v1.ServicePort, policyPorts []networkingv1.NetworkPolicyPort) bool { |
| 367 | +func checkServiceTargetPortMatchPolicyPorts(servicePorts []corev1.ServicePort, policyPorts []networkingv1.NetworkPolicyPort) bool { |
363 | 368 | ingressPorts := []string{} |
364 | 369 | for _, port := range policyPorts { |
365 | 370 | ingressPorts = append(ingressPorts, fmt.Sprintf("%d/%s", port.Port.IntVal, string(*port.Protocol))) |
|
0 commit comments