Skip to content

Commit

Permalink
Cache list of MRs for 1m, allow setting TTL via values
Browse files Browse the repository at this point in the history
  • Loading branch information
undera committed Oct 20, 2023
1 parent ba1909e commit b8172c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 31 deletions.
4 changes: 4 additions & 0 deletions charts/komoplane/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 16 additions & 15 deletions charts/komoplane/values.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -6,7 +12,7 @@ image:
# Overrides the image tag whose default is the chart appVersion.
tag: ""

imagePullSecrets: []
imagePullSecrets: [ ]
nameOverride: ""
fullnameOverride: ""

Expand All @@ -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.
Expand All @@ -45,10 +46,10 @@ updateStrategy:
type: RollingUpdate


podLabels: {}
podAnnotations: {}
podLabels: { }
podAnnotations: { }

podSecurityContext:
podSecurityContext:
fsGroup: 2000

securityContext:
Expand All @@ -57,7 +58,7 @@ securityContext:
runAsUser: 1000
capabilities:
drop:
- ALL
- ALL

service:
type: ClusterIP
Expand All @@ -66,26 +67,26 @@ service:
ingress:
enabled: false
className: ""
annotations: {}
annotations: { }
hosts:
- host: chart-example.local
paths:
- path: /
pathType: ImplementationSpecific
tls: []
tls: [ ]

autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 100
targetCPUUtilizationPercentage: 80

nodeSelector: {}
nodeSelector: { }

extraArgs:
- --bind=0.0.0.0

tolerations: []
tolerations: [ ]

affinity: {}
affinity: { }

48 changes: 32 additions & 16 deletions pkg/backend/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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, " ")
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down

0 comments on commit b8172c7

Please sign in to comment.