Skip to content

Commit

Permalink
fix: linter issues (#19)
Browse files Browse the repository at this point in the history
* fix: linter issues

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* fixes

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

---------

Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Jan 23, 2024
1 parent 1ff693c commit 40c991d
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 58 deletions.
9 changes: 5 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ linters:
- errname
- exportloopref
- gci
- gochecknoinits
# - gochecknoinits
- gofmt
- gofumpt
- goimports
Expand Down Expand Up @@ -43,6 +43,7 @@ run:
timeout: 15m
skip-files:
- ".+\\.generated.go"
output:
format: colored-line-number
sort-results: true

output:
format: colored-line-number
sort-results: true
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import (
func main() {
logs.InitLogs()
defer logs.FlushLogs()

if len(os.Getenv("GOMAXPROCS")) == 0 {
runtime.GOMAXPROCS(runtime.NumCPU())
}

cmd := app.NewPolicyServer(genericapiserver.SetupSignalHandler())
if err := cmd.Execute(); err != nil {
panic(err)
Expand Down
33 changes: 21 additions & 12 deletions pkg/api/cpolr.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ func (c *cpolrStore) Create(ctx context.Context, obj runtime.Object, createValid
}

if !isDryRun {
err := c.createCpolr(cpolr)
if err != nil {
if err := c.createCpolr(cpolr); err != nil {
return &v1alpha2.ClusterPolicyReport{}, errors.NewBadRequest(fmt.Sprintf("cannot create cluster policy report: %s", err.Error()))
}
c.broadcaster.Action(watch.Added, obj)
if err := c.broadcaster.Action(watch.Added, obj); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}

return obj, nil
Expand All @@ -133,8 +134,12 @@ func (c *cpolrStore) Update(ctx context.Context, name string, objInfo rest.Updat
oldObj, _ := c.getCpolr(name)
updatedObject, _ := objInfo.UpdatedObject(ctx, oldObj)
cpolr := updatedObject.(*v1alpha2.ClusterPolicyReport)
c.updatePolr(cpolr, true)
c.broadcaster.Action(watch.Added, updatedObject)
if err := c.updatePolr(cpolr, true); err != nil {
klog.ErrorS(err, "failed to update resource")
}
if err := c.broadcaster.Action(watch.Added, updatedObject); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
return updatedObject, true, nil
}

Expand Down Expand Up @@ -167,11 +172,12 @@ func (c *cpolrStore) Update(ctx context.Context, name string, objInfo rest.Updat
}

if !isDryRun {
err := c.createCpolr(cpolr)
if err != nil {
if err := c.createCpolr(cpolr); err != nil {
return &v1alpha2.ClusterPolicyReport{}, false, errors.NewBadRequest(fmt.Sprintf("cannot create cluster policy report: %s", err.Error()))
}
c.broadcaster.Action(watch.Modified, updatedObject)
if err := c.broadcaster.Action(watch.Modified, updatedObject); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}

return updatedObject, true, nil
Expand All @@ -193,12 +199,13 @@ func (c *cpolrStore) Delete(ctx context.Context, name string, deleteValidation r
}

if !isDryRun {
err = c.deletePolr(cpolr)
if err != nil {
if err = c.deletePolr(cpolr); err != nil {
klog.ErrorS(err, "failed to delete cpolr", "name", name)
return &v1alpha2.ClusterPolicyReport{}, false, errors.NewBadRequest(fmt.Sprintf("failed to delete clusterpolicyreport: %s", err.Error()))
}
c.broadcaster.Action(watch.Deleted, cpolr)
if err := c.broadcaster.Action(watch.Deleted, cpolr); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}

return cpolr, true, nil // TODO: Add protobuf in wgpolicygroup
Expand Down Expand Up @@ -226,7 +233,9 @@ func (c *cpolrStore) DeleteCollection(ctx context.Context, deleteValidation rest
klog.ErrorS(err, "Failed to delete cpolr", "name", cpolr.GetName())
return &v1alpha2.ClusterPolicyReportList{}, errors.NewBadRequest(fmt.Sprintf("Failed to delete cluster policy report: %s", cpolr.GetName()))
}
c.broadcaster.Action(watch.Deleted, obj)
if err := c.broadcaster.Action(watch.Deleted, obj); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
}
return cpolrList, nil
Expand Down
1 change: 0 additions & 1 deletion pkg/api/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apiserver/pkg/registry/rest"
genericapiserver "k8s.io/apiserver/pkg/server"

"sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/api/wgpolicyk8s.io/v1alpha2"
)

Expand Down
20 changes: 9 additions & 11 deletions pkg/api/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import (
"k8s.io/component-base/metrics"
)

var (
metricFreshness = metrics.NewHistogramVec(
&metrics.HistogramOpts{
Namespace: "reports_server",
Subsystem: "api",
Name: "reports_server_export_time",
Help: "serve of reports exported",
Buckets: metrics.ExponentialBuckets(1, 1.364, 20),
},
[]string{},
)
var metricFreshness = metrics.NewHistogramVec(
&metrics.HistogramOpts{
Namespace: "reports_server",
Subsystem: "api",
Name: "reports_server_export_time",
Help: "serve of reports exported",
Buckets: metrics.ExponentialBuckets(1, 1.364, 20),
},
[]string{},
)

func RegisterAPIMetrics(registrationFunc func(metrics.Registerable) error) error {
Expand Down
24 changes: 18 additions & 6 deletions pkg/api/polr.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ func (p *polrStore) Create(ctx context.Context, obj runtime.Object, createValida
if err != nil {
return &v1alpha2.PolicyReport{}, errors.NewBadRequest(fmt.Sprintf("cannot create policy report: %s", err.Error()))
}
p.broadcaster.Action(watch.Added, obj)
if err := p.broadcaster.Action(watch.Added, obj); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}

return obj, nil
Expand All @@ -142,8 +144,12 @@ func (p *polrStore) Update(ctx context.Context, name string, objInfo rest.Update
oldObj, _ := p.getPolr(name, namespace)
updatedObject, _ := objInfo.UpdatedObject(ctx, oldObj)
polr := updatedObject.(*v1alpha2.PolicyReport)
p.updatePolr(polr, true)
p.broadcaster.Action(watch.Added, updatedObject)
if err := p.updatePolr(polr, true); err != nil {
klog.ErrorS(err, "failed to update resource")
}
if err := p.broadcaster.Action(watch.Added, updatedObject); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
return updatedObject, true, nil
}

Expand Down Expand Up @@ -184,7 +190,9 @@ func (p *polrStore) Update(ctx context.Context, name string, objInfo rest.Update
if err != nil {
return &v1alpha2.PolicyReport{}, false, errors.NewBadRequest(fmt.Sprintf("cannot create policy report: %s", err.Error()))
}
p.broadcaster.Action(watch.Modified, updatedObject)
if err := p.broadcaster.Action(watch.Modified, updatedObject); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}

return updatedObject, true, nil
Expand Down Expand Up @@ -212,7 +220,9 @@ func (p *polrStore) Delete(ctx context.Context, name string, deleteValidation re
klog.ErrorS(err, "failed to delete polr", "name", name, "namespace", klog.KRef("", namespace))
return &v1alpha2.PolicyReport{}, false, errors.NewBadRequest(fmt.Sprintf("failed to delete policyreport: %s", err.Error()))
}
p.broadcaster.Action(watch.Deleted, polr)
if err := p.broadcaster.Action(watch.Deleted, polr); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}

return polr, true, nil // TODO: Add protobuf in wgpolicygroup
Expand Down Expand Up @@ -241,7 +251,9 @@ func (p *polrStore) DeleteCollection(ctx context.Context, deleteValidation rest.
klog.ErrorS(err, "Failed to delete polr", "name", polr.GetName(), "namespace", klog.KRef("", namespace))
return &v1alpha2.PolicyReportList{}, errors.NewBadRequest(fmt.Sprintf("Failed to delete policy report: %s/%s", polr.Namespace, polr.GetName()))
}
p.broadcaster.Action(watch.Deleted, obj)
if err := p.broadcaster.Action(watch.Deleted, obj); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
}
return polrList, nil
Expand Down
6 changes: 0 additions & 6 deletions pkg/app/policyserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,17 @@ func runCommand(o *opts.Options, stopCh <-chan struct{}) error {
fmt.Println(version.Get().GitVersion)
os.Exit(0)
}

errors := o.Validate()
if len(errors) > 0 {
return errors[0]
}

config, err := o.ServerConfig()

if err != nil {
return err
}

s, err := config.Complete()

if err != nil {
return err
}

return s.RunUntil(stopCh)
}
5 changes: 2 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"net/http"

"github.com/kyverno/policy-reports/pkg/storage"
"k8s.io/component-base/metrics"
"k8s.io/klog/v2"

genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/apiserver/pkg/server/healthz"
"k8s.io/component-base/metrics"
"k8s.io/klog/v2"
)

func RegisterServerMetrics(registrationFunc func(metrics.Registerable) error) error {
Expand Down
4 changes: 1 addition & 3 deletions pkg/storage/inmemory/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/api/wgpolicyk8s.io/v1alpha2"
)

var (
groupResource = v1alpha2.SchemeGroupVersion.WithResource("policyreportsa").GroupResource()
)
var groupResource = v1alpha2.SchemeGroupVersion.WithResource("policyreportsa").GroupResource()

type inMemoryDb struct {
sync.Mutex
Expand Down
18 changes: 8 additions & 10 deletions pkg/storage/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ import (
"k8s.io/component-base/metrics"
)

var (
pointsStored = metrics.NewGaugeVec(
&metrics.GaugeOpts{
Namespace: "reports_server",
Subsystem: "storage",
Name: "policy_reports_database",
Help: "Database of policy reports",
},
[]string{"type"},
)
var pointsStored = metrics.NewGaugeVec(
&metrics.GaugeOpts{
Namespace: "reports_server",
Subsystem: "storage",
Name: "policy_reports_database",
Help: "Database of policy reports",
},
[]string{"type"},
)

// RegisterStorageMetrics registers a gauge metric for the number of metrics
Expand Down

0 comments on commit 40c991d

Please sign in to comment.