Skip to content

Commit

Permalink
feat: add metrics for policy (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusank authored Nov 30, 2022
1 parent 8227546 commit 7bd18a5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Three kind of policy are supported.

`OverridePolicy` is used to mutate object in the same namespace.
`ClusterOverridePolicy` is used to mutate object in any namespace.
`CLusterValidatePolciy` is used to validate object in any namespace.
`ClusterValidatePolciy` is used to validate object in any namespace.

For cluster scoped resource:
- Apply ClusterOverridePolicy by policies name in ascending;
Expand Down
33 changes: 31 additions & 2 deletions cmd/app/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/k-cloud-labs/pkg/utils/dynamiclister"
"github.com/k-cloud-labs/pkg/utils/informermanager"
"github.com/k-cloud-labs/pkg/utils/interrupter"
"github.com/k-cloud-labs/pkg/utils/metrics"
"github.com/k-cloud-labs/pkg/utils/overridemanager"
"github.com/k-cloud-labs/pkg/utils/templatemanager"
"github.com/k-cloud-labs/pkg/utils/templatemanager/templates"
Expand Down Expand Up @@ -298,8 +300,26 @@ func (s *setupManager) setupOverridePolicyManager() (err error) {
opInformer := s.informerManager.Informer(opGVR)
copInformer := s.informerManager.Informer(copGVR)

opInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
metrics.IncrPolicy("OverridePolicy")
},
DeleteFunc: func(obj interface{}) {
metrics.DecPolicy("OverridePolicy")
},
})

copInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
metrics.IncrPolicy("ClusterOverridePolicy")
},
DeleteFunc: func(obj interface{}) {
metrics.DecPolicy("ClusterOverridePolicy")
},
})

s.informerManager.Start()
if cache := s.informerManager.WaitForCacheSync(); !cache[opGVR] || !cache[copGVR] {
if result := s.informerManager.WaitForCacheSync(); !result[opGVR] || !result[copGVR] {
return errors.New("failed to sync override policy")
}

Expand All @@ -317,8 +337,17 @@ func (s *setupManager) setupValidatePolicyManager() (err error) {
}
cvpInformer := s.informerManager.Informer(cvpGVR)

cvpInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
metrics.IncrPolicy("ClusterValidatePolicy")
},
DeleteFunc: func(obj interface{}) {
metrics.DecPolicy("ClusterValidatePolicy")
},
})

s.informerManager.Start()
if cache := s.informerManager.WaitForCacheSync(); !cache[cvpGVR] {
if result := s.informerManager.WaitForCacheSync(); !result[cvpGVR] {
return errors.New("failed to sync validate policy")
}

Expand Down
13 changes: 13 additions & 0 deletions deploy/monitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kinitiras-exporter
namespace: kinitiras-system
spec:
endpoints:
- interval: 5s
port: metrics
scrapeTimeout: 2s
selector:
matchLabels:
controller: kinitiras-webhook
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/go-logr/logr v1.2.3
github.com/k-cloud-labs/pkg v0.4.0
github.com/k-cloud-labs/pkg v0.4.1
github.com/open-policy-agent/cert-controller v0.3.0
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/k-cloud-labs/pkg v0.4.0 h1:eAl+BFVEzHCytXRw80lYAhMXJjrJV32lyTrNCPXfM3c=
github.com/k-cloud-labs/pkg v0.4.0/go.mod h1:9/QKKo9w0/Y4dG9G8cqbNSidq/vuFJvB+RKhtiBJ2Bs=
github.com/k-cloud-labs/pkg v0.4.1 h1:uPbttHdkMBze+F1I9kbrL6EUjbhei0cf4ApN/XRF6iA=
github.com/k-cloud-labs/pkg v0.4.1/go.mod h1:xIgN1grjPSJY7/fKtA+4Q/gWsaVyjmT/AnOagIh2BJM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down

0 comments on commit 7bd18a5

Please sign in to comment.