diff --git a/charts/komoplane/templates/deployment.yaml b/charts/komoplane/templates/deployment.yaml index f2bb8e6..dab292e 100644 --- a/charts/komoplane/templates/deployment.yaml +++ b/charts/komoplane/templates/deployment.yaml @@ -46,6 +46,10 @@ spec: env: - name: DEBUG value: {{- ternary " '1'" "" .Values.komoplane.debug }} + - name: KP_MR_CACHE_TTL + value: {{ .Values.komoplane.mrCacheTTL | default "1m" }} + - name: KP_MRD_CACHE_TTL + value: {{ .Values.komoplane.mrdCacheTTL | default "5m" }} ports: - name: http containerPort: 8090 diff --git a/charts/komoplane/values.yaml b/charts/komoplane/values.yaml index a08e92f..3e9d28b 100644 --- a/charts/komoplane/values.yaml +++ b/charts/komoplane/values.yaml @@ -1,3 +1,9 @@ +komoplane: + # Flag for setting environment to debug mode + debug: false + mrCacheTTL: 1m # cache list of MRs for this time + mrdCacheTTL: 5m # cache list of MRDs for this time + replicaCount: 1 image: @@ -6,7 +12,7 @@ image: # Overrides the image tag whose default is the chart appVersion. tag: "" -imagePullSecrets: [] +imagePullSecrets: [ ] nameOverride: "" fullnameOverride: "" @@ -25,11 +31,6 @@ resources: cpu: 1 memory: 1Gi -komoplane: - # Flag for setting environment to debug mode - debug: false - - ## @param.updateStrategy.type Set up update strategy for installation. ## Set to Recreate if you use persistent volume that cannot be mounted by more than one pods to make sure the pods is destroyed first. @@ -45,10 +46,10 @@ updateStrategy: type: RollingUpdate -podLabels: {} -podAnnotations: {} +podLabels: { } +podAnnotations: { } -podSecurityContext: +podSecurityContext: fsGroup: 2000 securityContext: @@ -57,7 +58,7 @@ securityContext: runAsUser: 1000 capabilities: drop: - - ALL + - ALL service: type: ClusterIP @@ -66,13 +67,13 @@ service: ingress: enabled: false className: "" - annotations: {} + annotations: { } hosts: - host: chart-example.local paths: - path: / pathType: ImplementationSpecific - tls: [] + tls: [ ] autoscaling: enabled: false @@ -80,12 +81,12 @@ autoscaling: maxReplicas: 100 targetCPUUtilizationPercentage: 80 -nodeSelector: {} +nodeSelector: { } extraArgs: - --bind=0.0.0.0 -tolerations: [] +tolerations: [ ] -affinity: {} +affinity: { } diff --git a/pkg/backend/controller.go b/pkg/backend/controller.go index bc17733..2160e84 100644 --- a/pkg/backend/controller.go +++ b/pkg/backend/controller.go @@ -44,6 +44,7 @@ type Controller struct { ctx context.Context apiExt *apiextensionsv1.ApiextensionsV1Client mrdCache *ttlcache.Cache[bool, []*v1.CustomResourceDefinition] // TODO: extract this into separate entity + mrCache *ttlcache.Cache[bool, *unstructured.UnstructuredList] } type ConditionedObject interface { @@ -326,25 +327,33 @@ func (c *Controller) getDynamicResource(ref *v12.ObjectReference, res Conditione } func (c *Controller) GetManageds(ec echo.Context) error { - MRDs, err := c.getCachedMRDs(ec) - if err != nil { - return err - } - res := &unstructured.UnstructuredList{Items: []unstructured.Unstructured{}} - for _, mrd := range MRDs { - gvk := schema.GroupVersionKind{ - Group: mrd.Spec.Group, - Version: mrd.Spec.Versions[0].Name, - Kind: mrd.Spec.Names.Plural, - } - items, err := c.CRDs.List(c.ctx, gvk) + cacheItem := c.mrCache.Get(true) + if cacheItem == nil { + log.Debugf("Missed cache for MRs, reloading...") + MRDs, err := c.getCachedMRDs(ec) if err != nil { - log.Warnf("Failed to list CRD: %v: %v", mrd.GroupVersionKind(), err) - continue + return err } - res.Items = append(res.Items, items.Items...) + for _, mrd := range MRDs { + gvk := schema.GroupVersionKind{ + Group: mrd.Spec.Group, + Version: mrd.Spec.Versions[0].Name, + Kind: mrd.Spec.Names.Plural, + } + items, err := c.CRDs.List(c.ctx, gvk) + if err != nil { + log.Warnf("Failed to list CRD: %v: %v", mrd.GroupVersionKind(), err) + continue + } + + res.Items = append(res.Items, items.Items...) + } + c.mrCache.Set(true, res, ttlcache.DefaultTTL) + } else { + log.Debugf("Cache hit for MRs") + res = cacheItem.Value() } return ec.JSONPretty(http.StatusOK, res, " ") @@ -675,6 +684,7 @@ func NewController(ctx context.Context, cfg *rest.Config, ns string, version str } mrdCacheTTL := durationFromEnv("KP_MRD_CACHE_TTL", 5*time.Minute) + mrCacheTTL := durationFromEnv("KP_MR_CACHE_TTL", 1*time.Minute) controller := Controller{ ctx: ctx, @@ -687,12 +697,18 @@ func NewController(ctx context.Context, cfg *rest.Config, ns string, version str CurVer: version, }, - mrdCache: ttlcache.New[bool, []*v1.CustomResourceDefinition]( + mrdCache: ttlcache.New( ttlcache.WithTTL[bool, []*v1.CustomResourceDefinition](mrdCacheTTL), + ttlcache.WithDisableTouchOnHit[bool, []*v1.CustomResourceDefinition](), + ), + mrCache: ttlcache.New( + ttlcache.WithTTL[bool, *unstructured.UnstructuredList](mrCacheTTL), + ttlcache.WithDisableTouchOnHit[bool, *unstructured.UnstructuredList](), ), } go controller.mrdCache.Start() // starts automatic expired item deletion + go controller.mrCache.Start() // starts automatic expired item deletion return &controller, nil }