Skip to content

Commit

Permalink
Remove example dependency on internal packages (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
genevieveluyt committed Jul 26, 2021
1 parent 9c59e6d commit 863e367
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion example_custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type myAuditor struct{}
//
// Params
// resource: Read-only. The resource to audit.
// resources: Read-only. A reference to all resources. Can be used for context.
// resources: Read-only. A reference to all resources. Can be used for context though most auditors don't need this.
//
// Return
// auditResults: The results for the audit. Each result can optionally include a PendingFix object to
Expand Down
32 changes: 28 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/Shopify/kubeaudit/auditors/apparmor"
"github.com/Shopify/kubeaudit/auditors/image"
"github.com/Shopify/kubeaudit/config"
"github.com/Shopify/kubeaudit/internal/k8s"

"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -79,7 +78,7 @@ func Example_auditLocal() {
}

// Run the audit in local mode
report, err := auditor.AuditLocal("", k8s.ClientOptions{})
report, err := auditor.AuditLocal("", kubeaudit.AuditOptions{})
if err != nil {
log.Fatal(err)
}
Expand All @@ -103,7 +102,7 @@ func Example_auditCluster() {
}

// Run the audit in cluster mode. Note this will fail if kubeaudit is not running within a cluster.
report, err := auditor.AuditCluster(k8s.ClientOptions{})
report, err := auditor.AuditCluster(kubeaudit.AuditOptions{})
if err != nil {
log.Fatal(err)
}
Expand All @@ -114,6 +113,19 @@ func Example_auditCluster() {

// ExampleAuditorSubset shows how to run kubeaudit with a subset of auditors
func Example_auditorSubset() {
// A sample Kubernetes manifest file
manifest := `
apiVersion: apps/v1
kind: Deployment
metadata:
name: myAuditor
spec:
template:
spec:
containers:
- name: myContainer
`

// Initialize the auditors you want to use
auditor, err := kubeaudit.New([]kubeaudit.Auditable{
apparmor.New(),
Expand All @@ -138,6 +150,18 @@ func Example_auditorSubset() {
// for those auditors.
func Example_config() {
configFile := "config/config.yaml"
// A sample Kubernetes manifest file
manifest := `
apiVersion: apps/v1
kind: Deployment
metadata:
name: myAuditor
spec:
template:
spec:
containers:
- name: myContainer
`

// Open the configuration file
reader, err := os.Open(configFile)
Expand Down Expand Up @@ -180,7 +204,7 @@ func Example_printOptions() {
log.Fatal(err)
}

report, err := auditor.AuditLocal("", k8s.ClientOptions{})
report, err := auditor.AuditLocal("", kubeaudit.AuditOptions{})
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 4 additions & 2 deletions kubeaudit.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ type Kubeaudit struct {
auditors []Auditable
}

type AuditOptions = k8s.ClientOptions

// New returns a new Kubeaudit instance
func New(auditors []Auditable, opts ...Option) (*Kubeaudit, error) {
if len(auditors) == 0 {
Expand Down Expand Up @@ -152,7 +154,7 @@ func (a *Kubeaudit) AuditManifest(manifest io.Reader) (*Report, error) {
}

// AuditCluster audits the Kubernetes resources found in the cluster in which Kubeaudit is running
func (a *Kubeaudit) AuditCluster(options k8s.ClientOptions) (*Report, error) {
func (a *Kubeaudit) AuditCluster(options AuditOptions) (*Report, error) {
if !k8s.IsRunningInCluster(k8s.DefaultClient) {
return nil, errors.New("failed to audit resources in cluster mode: not running in cluster")
}
Expand All @@ -174,7 +176,7 @@ func (a *Kubeaudit) AuditCluster(options k8s.ClientOptions) (*Report, error) {
}

// AuditLocal audits the Kubernetes resources found in the provided Kubernetes config file
func (a *Kubeaudit) AuditLocal(configpath string, options k8s.ClientOptions) (*Report, error) {
func (a *Kubeaudit) AuditLocal(configpath string, options AuditOptions) (*Report, error) {
clientset, err := k8s.NewKubeClientLocal(configpath)
if err == k8s.ErrNoReadableKubeConfig {
return nil, fmt.Errorf("failed to open kubeconfig file %s", configpath)
Expand Down

0 comments on commit 863e367

Please sign in to comment.