Skip to content

Commit

Permalink
add-option-to-disable-k8s-client-cache (#1210)
Browse files Browse the repository at this point in the history
* add-option-to-disable-k8s-client-cache

* propagate

* schema

* fix-scheme

* debug

* fix-config

* remove-debug
  • Loading branch information
calvix authored Nov 15, 2023
1 parent 3dd7ead commit 8705d0b
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project's packages adheres to [Semantic Versioning](http://semver.org/s

## [Unreleased]

### Added

- Add option to disable k8s client cache.

## [6.9.0] - 2023-11-10

### Changed
Expand Down
18 changes: 18 additions & 0 deletions flag/service/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kubernetes

import (
"github.com/giantswarm/operatorkit/v8/pkg/flag/service/kubernetes/tls"
"github.com/giantswarm/operatorkit/v8/pkg/flag/service/kubernetes/watch"
)

// Kubernetes is a data structure to hold Kubernetes specific command line
// configuration flags.
type Kubernetes struct {
Address string
DisableClientCache string
InCluster string
KubeConfig string
KubeConfigPath string
TLS tls.TLS
Watch watch.Watch
}
3 changes: 1 addition & 2 deletions flag/service/service.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package service

import (
"github.com/giantswarm/operatorkit/v8/pkg/flag/service/kubernetes"

"github.com/giantswarm/app-operator/v6/flag/service/app"
"github.com/giantswarm/app-operator/v6/flag/service/appcatalog"
"github.com/giantswarm/app-operator/v6/flag/service/chart"
"github.com/giantswarm/app-operator/v6/flag/service/helm"
"github.com/giantswarm/app-operator/v6/flag/service/image"
"github.com/giantswarm/app-operator/v6/flag/service/kubernetes"
"github.com/giantswarm/app-operator/v6/flag/service/operatorkit"
"github.com/giantswarm/app-operator/v6/flag/service/provider"
)
Expand Down
1 change: 1 addition & 0 deletions helm/app-operator/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data:
registry: '{{ .Values.registry.domain }}'
kubernetes:
incluster: true
disableClientCache: {{ $.Values.kubernetes.disableClientCache }}
operatorkit:
resyncPeriod: '{{ .Values.operatorkit.resyncPeriod }}'
provider:
Expand Down
8 changes: 8 additions & 0 deletions helm/app-operator/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@
}
}
},
"kubernetes": {
"type": "object",
"properties": {
"disableClientCache": {
"type": "boolean"
}
}
},
"name": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions helm/app-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ helm:
provider:
kind: ""

kubernetes:
disableClientCache: false

userID: 1000
groupID: 1000

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func mainWithError() (err error) {
daemonCommand.PersistentFlags().String(f.Service.Helm.HTTP.ClientTimeout, "5s", "HTTP timeout for pulling chart tarballs.")
daemonCommand.PersistentFlags().String(f.Service.Image.Registry, "quay.io", "The container registry for pulling Tiller images.")
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.Address, "", "Address used to connect to Kubernetes. When empty in-cluster config is created.")
daemonCommand.PersistentFlags().Bool(f.Service.Kubernetes.DisableClientCache, false, "Disable Kubernetes client cache.")
daemonCommand.PersistentFlags().Bool(f.Service.Kubernetes.InCluster, true, "Whether to use the in-cluster config to authenticate with Kubernetes.")
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.KubeConfig, "", "KubeConfig used to connect to Kubernetes. When empty other settings are used.")
daemonCommand.PersistentFlags().String(f.Service.Kubernetes.TLS.CAFile, "", "Certificate authority file path to use to authenticate with Kubernetes.")
Expand Down
17 changes: 11 additions & 6 deletions service/internal/clientcache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Config struct {

// Settings.
HTTPClientTimeout time.Duration
DisableCache bool
}

type Resource struct {
Expand All @@ -41,6 +42,7 @@ type Resource struct {

// Settings.
httpClientTimeout time.Duration
disableCache bool
}

type clients struct {
Expand Down Expand Up @@ -73,6 +75,7 @@ func New(config Config) (*Resource, error) {

// Settings
httpClientTimeout: config.HTTPClientTimeout,
disableCache: config.DisableCache,
}

return r, nil
Expand All @@ -81,13 +84,15 @@ func New(config Config) (*Resource, error) {
func (r *Resource) GetClients(ctx context.Context, kubeConfig *v1alpha1.AppSpecKubeConfig) (*clients, error) {
k := fmt.Sprintf("%s/%s", kubeConfig.Secret.Namespace, kubeConfig.Secret.Name)

if v, ok := r.cache.Get(k); ok {
c, ok := v.(clients)
if !ok {
return nil, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", clients{}, v)
}
if !r.disableCache {
if v, ok := r.cache.Get(k); ok {
c, ok := v.(clients)
if !ok {
return nil, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", clients{}, v)
}

return &c, nil
return &c, nil
}
}

var c clients
Expand Down
1 change: 1 addition & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func New(config Config) (*Service, error) {
Logger: config.Logger,

HTTPClientTimeout: config.Viper.GetDuration(config.Flag.Service.Helm.HTTP.ClientTimeout),
DisableCache: config.Viper.GetBool(config.Flag.Service.Kubernetes.DisableClientCache),
}

clientCache, err = clientcache.New(c)
Expand Down

0 comments on commit 8705d0b

Please sign in to comment.