Skip to content

Commit

Permalink
remove the dependency on hub namepsace from Reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
yana1205 committed Jul 20, 2023
1 parent 20bb2de commit 9334954
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (c *Composer) Compose(namespace string, componentObjects []oscal.ComponentO
},
ObjectMeta: metav1.ObjectMeta{
Name: "c2p-parameters",
Namespace: "c2p",
Namespace: namespace,
},
Data: parameters,
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/reporter/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ import (
)

func TestMarkdown(t *testing.T) {
tmplateData, err := embeddedResources.ReadFile("template.md")
assert.NoError(t, err, "Should not happen")

var report typereport.ComplianceReport
err = pkg.LoadYamlFileToK8sTypedObject(pkg.PathFromPkgDirectory("./testdata/reports/compliance-report.yaml"), &report)
err := pkg.LoadYamlFileToK8sTypedObject(pkg.PathFromPkgDirectory("./testdata/reports/compliance-report.yaml"), &report)
assert.NoError(t, err, "Should not happen")

markdown := NewMarkdown()
generated, err := markdown.Generate(string(tmplateData), report)
generated, err := markdown.Generate("", report)
assert.NoError(t, err, "Should not happen")

err = os.WriteFile(pkg.PathFromPkgDirectory("./testdata/reports/compliance-report.md"), generated, os.ModePerm)
Expand Down
17 changes: 12 additions & 5 deletions pkg/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,19 @@ func (r *Reporter) Generate() (typereport.ComplianceReport, error) {
}
reportComponents := []typereport.Component{}
for _, cdobj := range r.c2pParsed.ComponentObjects {
policySet := typeutils.FindByNamespaceAnnotation(r.policySets, r.c2pParsed.Namespace, pkg.ANNOTATION_COMPONENT_TITLE, cdobj.ComponentTitle)
policySets := typeutils.FilterByAnnotation(r.policySets, pkg.ANNOTATION_COMPONENT_TITLE, cdobj.ComponentTitle)
clusterNameSets := sets.NewString()
var policySet *typepolicy.PolicySet
if len(policySets) > 0 {
policySet = policySets[0]
}
if policySet != nil {
placements := []string{}
for _, placement := range policySet.Status.Placement {
placements = append(placements, placement.Placement)
}
for _, placement := range placements {
placementDecision := typeutils.FindByNamespaceLabel(r.placementDecisions, r.c2pParsed.Namespace, "cluster.open-cluster-management.io/placement", placement)
placementDecision := typeutils.FindByNamespaceLabel(r.placementDecisions, policySet.Namespace, "cluster.open-cluster-management.io/placement", placement)
for _, decision := range placementDecision.Status.Decisions {
clusterNameSets.Insert(decision.ClusterName)
}
Expand All @@ -132,7 +136,10 @@ func (r *Reporter) Generate() (typereport.ComplianceReport, error) {
})
} else {
policyId := rule.PolicyId
policy := typeutils.FindByNamespaceName(r.policies, r.c2pParsed.Namespace, policyId)
var policy *typepolicy.Policy
if policySet != nil {
policy = typeutils.FindByNamespaceName(r.policies, policySet.Namespace, policyId)
}
var reason string
var ruleStatus typereport.RuleStatus
if policy != nil {
Expand Down Expand Up @@ -204,7 +211,7 @@ func (r *Reporter) GenerateReasonsFromRawPolicies(policy typepolicy.Policy) []Re
reasons := []Reason{}
for _, status := range policy.Status.Status {
clusterName := status.ClusterName
policyPerCluster := typeutils.FindByNamespaceName(r.policies, clusterName, r.c2pParsed.Namespace+"."+policy.Name)
policyPerCluster := typeutils.FindByNamespaceName(r.policies, clusterName, policy.Namespace+"."+policy.Name)
if policyPerCluster == nil {
continue
}
Expand All @@ -228,7 +235,7 @@ func (r *Reporter) GenerateReasonsFromPolicyReports(policy typepolicy.Policy) []
reasons := []Reason{}
for _, status := range policy.Status.Status {
clusterName := status.ClusterName
policyReport := findPolicyReportByNamespaceName(r.policyReports, clusterName, r.c2pParsed.Namespace+"."+policy.Name)
policyReport := findPolicyReportByNamespaceName(r.policyReports, clusterName, policy.Namespace+"."+policy.Name)
clusterHistories := []typepolicy.ComplianceHistory{}
for _, result := range policyReport.Results {
clusterHistory := typepolicy.ComplianceHistory{
Expand Down
13 changes: 13 additions & 0 deletions pkg/types/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ type KubernetesObject interface {
GetLabel() map[string]string
}

func FilterByAnnotation[T KubernetesObject](list []T, annotationName string, annotationValue string) []T {
filtered := []T{}
for _, item := range list {
an, ok := item.GetAnnotation()[annotationName]
if ok {
if an == annotationValue {
filtered = append(filtered, item)
}
}
}
return filtered
}

func FindByNamespaceName[T KubernetesObject](list []T, namespace string, name string) T {
var result T
for _, item := range list {
Expand Down

0 comments on commit 9334954

Please sign in to comment.