diff --git a/README.md b/README.md index e29096e..4edfc18 100644 --- a/README.md +++ b/README.md @@ -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; diff --git a/cmd/app/webhook.go b/cmd/app/webhook.go index dafa84a..45e33de 100644 --- a/cmd/app/webhook.go +++ b/cmd/app/webhook.go @@ -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" @@ -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" @@ -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") } @@ -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") } diff --git a/deploy/monitor.yaml b/deploy/monitor.yaml new file mode 100644 index 0000000..5b34bdd --- /dev/null +++ b/deploy/monitor.yaml @@ -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 \ No newline at end of file diff --git a/go.mod b/go.mod index f37ebb0..3394b78 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 367c95c..4440145 100644 --- a/go.sum +++ b/go.sum @@ -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=