This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #862 from phoenixking25/reporter
[MTB] added policy reporter
- Loading branch information
Showing
7 changed files
with
151 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
benchmarks/kubectl-mtb/internal/reporter/policy_reporter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package reporter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
v1alpha1 "github.com/kubernetes-sigs/wg-policy-prototypes/policy-report/api/v1alpha1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
"k8s.io/apimachinery/pkg/util/uuid" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/kubectl/pkg/scheme" | ||
) | ||
|
||
// PolicyReporter creates the policyreport object | ||
type PolicyReporter struct { | ||
policy v1alpha1.PolicyReport | ||
testSummaries []*TestSummary | ||
} | ||
|
||
// NewPolicyReporter returns the pointer of PolicyReporter | ||
func NewPolicyReporter() *PolicyReporter { | ||
policyName := "policy-" + string(uuid.NewUUID()) | ||
return &PolicyReporter{ | ||
policy: v1alpha1.PolicyReport{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "PolicyReport", | ||
APIVersion: "policy.kubernetes.io/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: policyName, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// SuiteWillBegin prints banner and total benchmarks to be run | ||
func (p *PolicyReporter) SuiteWillBegin(suiteSummary *SuiteSummary) { | ||
|
||
} | ||
|
||
// TestWillRun prints each test status | ||
func (p *PolicyReporter) TestWillRun(testSummary *TestSummary) { | ||
var status v1alpha1.PolicyStatus | ||
if testSummary.Validation && testSummary.Test { | ||
status = "Pass" | ||
} else if testSummary.Validation && !testSummary.Test { | ||
status = "Fail" | ||
} else if !testSummary.Validation { | ||
status = "Error" | ||
} else { | ||
status = "Skip" | ||
} | ||
p.policy.Results = append(p.policy.Results, &v1alpha1.PolicyReportResult{ | ||
Policy: testSummary.Benchmark.Title, | ||
Status: status, | ||
Scored: true, | ||
}) | ||
} | ||
|
||
// SuiteDidEnd prints end result summary of benchmark suite | ||
func (p *PolicyReporter) SuiteDidEnd(suiteSummary *SuiteSummary) { | ||
p.policy.Summary = v1alpha1.PolicyReportSummary{ | ||
Pass: suiteSummary.NumberOfPassedTests, | ||
Fail: suiteSummary.NumberOfFailedTests, | ||
Skip: suiteSummary.NumberOfSkippedTests, | ||
Error: suiteSummary.NumberOfFailedValidations, | ||
} | ||
|
||
CreatePolicy(suiteSummary.TenantAdminNamespace, p.policy) | ||
} | ||
|
||
// CreatePolicy creates the policy object | ||
func CreatePolicy(namespace string, policy v1alpha1.PolicyReport) { | ||
kubecfgFlags := genericclioptions.NewConfigFlags(false) | ||
|
||
config, err := kubecfgFlags.ToRESTConfig() | ||
if err != nil { | ||
errExit("", err) | ||
} | ||
|
||
v1alpha1.AddToScheme(scheme.Scheme) | ||
|
||
crdConfig := *config | ||
crdConfig.ContentConfig.GroupVersion = &v1alpha1.GroupVersion | ||
crdConfig.APIPath = "/apis" | ||
crdConfig.NegotiatedSerializer = serializer.NewCodecFactory(scheme.Scheme) | ||
crdConfig.UserAgent = rest.DefaultKubernetesUserAgent() | ||
|
||
c, err := rest.RESTClientFor(&crdConfig) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
result := v1alpha1.PolicyReport{} | ||
err = c. | ||
Post(). | ||
Namespace(namespace). | ||
Resource("policyreports"). | ||
Body(&policy). | ||
Do(context.TODO()). | ||
Into(&result) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} else { | ||
fmt.Println(result.ObjectMeta.Name, "is Created") | ||
} | ||
} | ||
|
||
func errExit(msg string, err error) { | ||
if err != nil { | ||
log.Fatalf("%s: %#v", msg, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters