Skip to content

Commit

Permalink
Merge branch 'main' into CNFCERT-1088
Browse files Browse the repository at this point in the history
  • Loading branch information
bnshr committed Feb 5, 2025
2 parents b07bf64 + 1091350 commit 8dc7961
Show file tree
Hide file tree
Showing 19 changed files with 605 additions and 194 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ linters-settings:
- style
disabled-checks:
- whyNoLint
settings:
tooManyResultsChecker:
# Maximum number of results.
# Default: 5
maxResults: 10
gocyclo:
min-complexity: 20
mnd:
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,14 @@ build-image-local:
-t ${REGISTRY}/${CERTSUITE_IMAGE_NAME_LEGACY}:${IMAGE_TAG} \
-f Dockerfile .

# Acts differently depending on the host architecture
# Mac vs Linux
results-html:
curl -s -O --output-dir internal/results/html ${RESULTS_HTML_URL}
if [ "$(shell uname)" = "Darwin" ]; then \
curl -s -O --output-dir internal/results/html ${RESULTS_HTML_URL}; \
else \
curl -s -o internal/results/html/results.html ${RESULTS_HTML_URL}; \
fi

check-results:
./certsuite check results
9 changes: 9 additions & 0 deletions internal/clientsholder/clientsholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ func SetTestK8sClientsHolder(k8sClient kubernetes.Interface) {
clientsHolder.ready = true
}

func SetTestK8sDynamicClientsHolder(dynamicClient dynamic.Interface) {
clientsHolder.DynamicClient = dynamicClient
clientsHolder.ready = true
}

func SetTestClientGroupResources(groupResources []*metav1.APIResourceList) {
clientsHolder.GroupResources = groupResources
}

func ClearTestClientsHolder() {
clientsHolder.K8sClient = nil
clientsHolder.ready = false
Expand Down
106 changes: 0 additions & 106 deletions pkg/autodiscover/autodiscover.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"path"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -336,52 +335,6 @@ func DoAutoDiscover(config *configuration.TestConfiguration) DiscoveredTestData
return data
}

func getNetworkAttachmentDefinitions(client *clientsholder.ClientsHolder, namespaces []string) ([]nadClient.NetworkAttachmentDefinition, error) {
var nadList []nadClient.NetworkAttachmentDefinition

for _, ns := range namespaces {
nad, err := client.CNCFNetworkingClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, err
}

// Append the list of networkAttachmentDefinitions to the nadList slice
nadList = append(nadList, nad.Items...)
}

return nadList, nil
}

func getSriovNetworks(client *clientsholder.ClientsHolder, namespaces []string) (sriovNetworks []sriovNetworkOp.SriovNetwork, err error) {
var sriovNetworkList []sriovNetworkOp.SriovNetwork

for _, ns := range namespaces {
snl, err := client.SriovNetworkingClient.SriovNetworks(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, err
}

// Append the list of sriovNetworks to the sriovNetworks slice
sriovNetworkList = append(sriovNetworkList, snl.Items...)
}
return sriovNetworkList, nil
}

func getSriovNetworkNodePolicies(client *clientsholder.ClientsHolder, namespaces []string) (sriovNetworkNodePolicies []sriovNetworkOp.SriovNetworkNodePolicy, err error) {
var sriovNetworkNodePolicyList []sriovNetworkOp.SriovNetworkNodePolicy

for _, ns := range namespaces {
snnp, err := client.SriovNetworkingClient.SriovNetworkNodePolicies(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, err
}

// Append the list of sriovNetworkNodePolicies to the sriovNetworkNodePolicies slice
sriovNetworkNodePolicyList = append(sriovNetworkNodePolicyList, snnp.Items...)
}
return sriovNetworkNodePolicyList, nil
}

func namespacesListToStringList(namespaceList []configuration.Namespace) (stringList []string) {
for _, ns := range namespaceList {
stringList = append(stringList, ns.Name)
Expand Down Expand Up @@ -465,62 +418,3 @@ func getPodsOwnedByCsv(csvName, operatorNamespace string, client *clientsholder.
}
return managedPods, nil
}

// getOperandPodsFromTestCsvs returns a subset of pods whose owner CRs are managed by any of the testCsvs.
func getOperandPodsFromTestCsvs(testCsvs []*olmv1Alpha.ClusterServiceVersion, pods []corev1.Pod) ([]*corev1.Pod, error) {
// Helper var to store all the managed crds from the operators under test
// They map key is "Kind.group/version" or "Kind.APIversion", which should be the same.
// e.g.: "Subscription.operators.coreos.com/v1alpha1"
crds := map[string]*olmv1Alpha.ClusterServiceVersion{}

// First, iterate on each testCsv to fill the helper crds map.
for _, csv := range testCsvs {
ownedCrds := csv.Spec.CustomResourceDefinitions.Owned
if len(ownedCrds) == 0 {
continue
}

for i := range ownedCrds {
crd := &ownedCrds[i]

_, group, found := strings.Cut(crd.Name, ".")
if !found {
return nil, fmt.Errorf("failed to parse resources and group from crd name %q", crd.Name)
}

log.Info("CSV %q owns crd %v", csv.Name, crd.Kind+"/"+group+"/"+crd.Version)

crdPath := path.Join(crd.Kind, group, crd.Version)
crds[crdPath] = csv
}
}

// Now, iterate on every pod in the list to check whether they're owned by any of the CRs that
// the csvs are managing.
operandPods := []*corev1.Pod{}
for i := range pods {
pod := &pods[i]
owners, err := podhelper.GetPodTopOwner(pod.Namespace, pod.OwnerReferences)
if err != nil {
return nil, fmt.Errorf("failed to get top owners of pod %v/%v: %v", pod.Namespace, pod.Name, err)
}

for _, owner := range owners {
versionedCrdPath := path.Join(owner.Kind, owner.APIVersion)

var csv *olmv1Alpha.ClusterServiceVersion
if csv = crds[versionedCrdPath]; csv == nil {
// The owner is not a CR or it's not a CR owned by any operator under test
continue
}

log.Info("Pod %v/%v has owner CR %s of CRD %q (CSV %v)", pod.Namespace, pod.Name,
owner.Name, versionedCrdPath, csv.Name)

operandPods = append(operandPods, pod)
break
}
}

return operandPods, nil
}
26 changes: 26 additions & 0 deletions pkg/autodiscover/autodiscover_nads.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package autodiscover

import (
"context"

nadClient "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/redhat-best-practices-for-k8s/certsuite/internal/clientsholder"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func getNetworkAttachmentDefinitions(client *clientsholder.ClientsHolder, namespaces []string) ([]nadClient.NetworkAttachmentDefinition, error) {
var nadList []nadClient.NetworkAttachmentDefinition

for _, ns := range namespaces {
nad, err := client.CNCFNetworkingClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, err
}

// Append the list of networkAttachmentDefinitions to the nadList slice
nadList = append(nadList, nad.Items...)
}

return nadList, nil
}
1 change: 1 addition & 0 deletions pkg/autodiscover/autodiscover_nads_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package autodiscover
63 changes: 63 additions & 0 deletions pkg/autodiscover/autodiscover_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ package autodiscover
import (
"context"
"fmt"
"path"
"strings"

helmclient "github.com/mittwald/go-helm-client"
olmv1Alpha "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/typed/operators/v1alpha1"
"github.com/redhat-best-practices-for-k8s/certsuite/internal/log"
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/configuration"
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/podhelper"

olmpkgv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
olmpkgclient "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/client/clientset/versioned/typed/operators/v1"
"github.com/redhat-best-practices-for-k8s/certsuite/pkg/stringhelper"
"helm.sh/helm/v3/pkg/release"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appv1client "k8s.io/client-go/kubernetes/typed/apps/v1"
Expand Down Expand Up @@ -238,3 +242,62 @@ func getAllPackageManifests(olmPkgClient olmpkgclient.PackageManifestInterface)
}
return out
}

// getOperandPodsFromTestCsvs returns a subset of pods whose owner CRs are managed by any of the testCsvs.
func getOperandPodsFromTestCsvs(testCsvs []*olmv1Alpha.ClusterServiceVersion, pods []corev1.Pod) ([]*corev1.Pod, error) {
// Helper var to store all the managed crds from the operators under test
// They map key is "Kind.group/version" or "Kind.APIversion", which should be the same.
// e.g.: "Subscription.operators.coreos.com/v1alpha1"
crds := map[string]*olmv1Alpha.ClusterServiceVersion{}

// First, iterate on each testCsv to fill the helper crds map.
for _, csv := range testCsvs {
ownedCrds := csv.Spec.CustomResourceDefinitions.Owned
if len(ownedCrds) == 0 {
continue
}

for i := range ownedCrds {
crd := &ownedCrds[i]

_, group, found := strings.Cut(crd.Name, ".")
if !found {
return nil, fmt.Errorf("failed to parse resources and group from crd name %q", crd.Name)
}

log.Info("CSV %q owns crd %v", csv.Name, crd.Kind+"/"+group+"/"+crd.Version)

crdPath := path.Join(crd.Kind, group, crd.Version)
crds[crdPath] = csv
}
}

// Now, iterate on every pod in the list to check whether they're owned by any of the CRs that
// the csvs are managing.
operandPods := []*corev1.Pod{}
for i := range pods {
pod := &pods[i]
owners, err := podhelper.GetPodTopOwner(pod.Namespace, pod.OwnerReferences)
if err != nil {
return nil, fmt.Errorf("failed to get top owners of pod %v/%v: %v", pod.Namespace, pod.Name, err)
}

for _, owner := range owners {
versionedCrdPath := path.Join(owner.Kind, owner.APIVersion)

var csv *olmv1Alpha.ClusterServiceVersion
if csv = crds[versionedCrdPath]; csv == nil {
// The owner is not a CR or it's not a CR owned by any operator under test
continue
}

log.Info("Pod %v/%v has owner CR %s of CRD %q (CSV %v)", pod.Namespace, pod.Name,
owner.Name, versionedCrdPath, csv.Name)

operandPods = append(operandPods, pod)
break
}
}

return operandPods, nil
}
40 changes: 40 additions & 0 deletions pkg/autodiscover/autodiscover_sriov.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package autodiscover

import (
"context"

sriovNetworkOp "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/redhat-best-practices-for-k8s/certsuite/internal/clientsholder"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func getSriovNetworks(client *clientsholder.ClientsHolder, namespaces []string) (sriovNetworks []sriovNetworkOp.SriovNetwork, err error) {
var sriovNetworkList []sriovNetworkOp.SriovNetwork

for _, ns := range namespaces {
snl, err := client.SriovNetworkingClient.SriovNetworks(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, err
}

// Append the list of sriovNetworks to the sriovNetworks slice
sriovNetworkList = append(sriovNetworkList, snl.Items...)
}
return sriovNetworkList, nil
}

func getSriovNetworkNodePolicies(client *clientsholder.ClientsHolder, namespaces []string) (sriovNetworkNodePolicies []sriovNetworkOp.SriovNetworkNodePolicy, err error) {
var sriovNetworkNodePolicyList []sriovNetworkOp.SriovNetworkNodePolicy

for _, ns := range namespaces {
snnp, err := client.SriovNetworkingClient.SriovNetworkNodePolicies(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, err
}

// Append the list of sriovNetworkNodePolicies to the sriovNetworkNodePolicies slice
sriovNetworkNodePolicyList = append(sriovNetworkNodePolicyList, snnp.Items...)
}
return sriovNetworkNodePolicyList, nil
}
1 change: 1 addition & 0 deletions pkg/autodiscover/autodiscover_sriov_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package autodiscover
7 changes: 6 additions & 1 deletion pkg/podhelper/podhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ type TopOwner struct {
// Get the list of top owners of pods
func GetPodTopOwner(podNamespace string, podOwnerReferences []metav1.OwnerReference) (topOwners map[string]TopOwner, err error) {
topOwners = make(map[string]TopOwner)
err = followOwnerReferences(clientsholder.GetClientsHolder().GroupResources, clientsholder.GetClientsHolder().DynamicClient, topOwners, podNamespace, podOwnerReferences)
err = followOwnerReferences(
clientsholder.GetClientsHolder().GroupResources,
clientsholder.GetClientsHolder().DynamicClient,
topOwners,
podNamespace,
podOwnerReferences)
if err != nil {
return topOwners, fmt.Errorf("could not get top owners, err: %v", err)
}
Expand Down
Loading

0 comments on commit 8dc7961

Please sign in to comment.