|
| 1 | +package clusterconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + networkv1client "github.com/openshift/client-go/network/clientset/versioned/typed/network/v1" |
| 7 | + "github.com/openshift/insights-operator/pkg/record" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/client-go/kubernetes" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + assignMacVlanAnn = "pod.network.openshift.io/assign-macvlan" |
| 14 | + multicastEnabledAnn = "netnamespace.network.openshift.io/multicast-enabled" |
| 15 | +) |
| 16 | + |
| 17 | +// GatherNumberOfPodsAndNetnamespacesWithSDNAnnotations Collects number of Pods with the annotation: |
| 18 | +// `pod.network.openshift.io/assign-macvlan` |
| 19 | +// and also collects number of Netnamespaces with the annotation: |
| 20 | +// `netnamespace.network.openshift.io/multicast-enabled: "true"` |
| 21 | +// |
| 22 | +// ### Sample data |
| 23 | +// - docs/insights-archive-sample/aggregated/pods_and_netnamespaces_with_sdn_annotations.json |
| 24 | +// |
| 25 | +// ### Location in archive |
| 26 | +// - `aggregated/pods_and_netnamespaces_with_sdn_annotations.json` |
| 27 | +// |
| 28 | +// ### Config ID |
| 29 | +// `clusterconfig/pods_and_netnamespaces_with_sdn_annotations` |
| 30 | +// |
| 31 | +// ### Released version |
| 32 | +// - 4.17.0 |
| 33 | +// |
| 34 | +// ### Backported versions |
| 35 | +// |
| 36 | +// ### Changes |
| 37 | +// None |
| 38 | +func (g *Gatherer) GatherNumberOfPodsAndNetnamespacesWithSDNAnnotations(ctx context.Context) ([]record.Record, []error) { |
| 39 | + networkClient, err := networkv1client.NewForConfig(g.gatherKubeConfig) |
| 40 | + if err != nil { |
| 41 | + return nil, []error{err} |
| 42 | + } |
| 43 | + kubeCli, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig) |
| 44 | + if err != nil { |
| 45 | + return nil, []error{err} |
| 46 | + } |
| 47 | + |
| 48 | + return gatherNumberOfPodsAndNetnamespacesWithSDN(ctx, networkClient, kubeCli) |
| 49 | +} |
| 50 | + |
| 51 | +type dataRecord struct { |
| 52 | + NumberOfPods int `json:"pods_with_assign-macvlan_annotation"` |
| 53 | + NumberOfNetnamespaces int `json:"netnamespaces_with_multicast-enabled_annotation"` |
| 54 | +} |
| 55 | + |
| 56 | +func gatherNumberOfPodsAndNetnamespacesWithSDN(ctx context.Context, |
| 57 | + networkCli networkv1client.NetworkV1Interface, |
| 58 | + kubeCli kubernetes.Interface) ([]record.Record, []error) { |
| 59 | + var errs []error |
| 60 | + |
| 61 | + numberOfPods, err := getNumberOfPodsWithAnnotation(ctx, assignMacVlanAnn, kubeCli) |
| 62 | + if err != nil { |
| 63 | + errs = append(errs, err) |
| 64 | + } |
| 65 | + numberOfNetnamespaces, err := getNumberOfNetnamespacesWithAnnotation(ctx, multicastEnabledAnn, networkCli) |
| 66 | + if err != nil { |
| 67 | + errs = append(errs, err) |
| 68 | + } |
| 69 | + |
| 70 | + if numberOfNetnamespaces == 0 && numberOfPods == 0 { |
| 71 | + return nil, nil |
| 72 | + } |
| 73 | + |
| 74 | + return []record.Record{ |
| 75 | + { |
| 76 | + Name: "aggregated/pods_and_netnamespaces_with_sdn_annotations", |
| 77 | + Item: record.JSONMarshaller{Object: dataRecord{ |
| 78 | + NumberOfPods: numberOfPods, |
| 79 | + NumberOfNetnamespaces: numberOfNetnamespaces, |
| 80 | + }}, |
| 81 | + }, |
| 82 | + }, errs |
| 83 | +} |
| 84 | + |
| 85 | +// getNumberOfPodsWithAnnotation lists all the Pods in the cluster and counts the ones with provided annotation |
| 86 | +func getNumberOfPodsWithAnnotation(ctx context.Context, annotation string, kubeCli kubernetes.Interface) (int, error) { |
| 87 | + var continueValue string |
| 88 | + var numberOfPods int |
| 89 | + for { |
| 90 | + pods, err := kubeCli.CoreV1().Pods(metav1.NamespaceAll).List(ctx, metav1.ListOptions{ |
| 91 | + Limit: 500, |
| 92 | + Continue: continueValue, |
| 93 | + }) |
| 94 | + if err != nil { |
| 95 | + return 0, err |
| 96 | + } |
| 97 | + |
| 98 | + for i := range pods.Items { |
| 99 | + pod := pods.Items[i] |
| 100 | + if _, ok := pod.Annotations[annotation]; ok { |
| 101 | + numberOfPods++ |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if pods.Continue == "" { |
| 106 | + break |
| 107 | + } |
| 108 | + continueValue = pods.Continue |
| 109 | + } |
| 110 | + return numberOfPods, nil |
| 111 | +} |
| 112 | + |
| 113 | +// getNumberOfNetnamespacesWithAnnotation lists all the Netnamespaces in the cluster |
| 114 | +// and counts the ones with provided annotation |
| 115 | +func getNumberOfNetnamespacesWithAnnotation(ctx context.Context, |
| 116 | + annotation string, |
| 117 | + networkCli networkv1client.NetworkV1Interface) (int, error) { |
| 118 | + var numberOfNamespaces int |
| 119 | + var continueValue string |
| 120 | + |
| 121 | + for { |
| 122 | + netNamespaces, err := networkCli.NetNamespaces().List(ctx, metav1.ListOptions{ |
| 123 | + Limit: 500, |
| 124 | + Continue: continueValue, |
| 125 | + }) |
| 126 | + if err != nil { |
| 127 | + return 0, err |
| 128 | + } |
| 129 | + |
| 130 | + for i := range netNamespaces.Items { |
| 131 | + netNamespace := netNamespaces.Items[i] |
| 132 | + if v, ok := netNamespace.Annotations[annotation]; ok { |
| 133 | + if v == "true" { |
| 134 | + numberOfNamespaces++ |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if netNamespaces.Continue == "" { |
| 140 | + break |
| 141 | + } |
| 142 | + continueValue = netNamespaces.Continue |
| 143 | + } |
| 144 | + |
| 145 | + return numberOfNamespaces, nil |
| 146 | +} |
0 commit comments