Files
k8sgpt/pkg/cache/cache.go
Aris Boutselis 23ac52d5ff feat: add Azure remote cache (#690)
* feat: add Azure remote cache

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* feat: add serve mode support and update buf schema

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* fix: map structure name

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* chore: add a new cache type to make code readable

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* docs: update docs to reflect new remote cache type

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* fix(deps): update module github.com/prometheus/client_golang to v1.17.0 (#687)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* fix(deps): update module buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go to v1.3.0-20231002095256-194bc640518b.1 (#692)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update module helm.sh/helm/v3 to v3.13.0 (#688)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix: security warning around printing provider details in https://github.com/k8sgpt-ai/k8sgpt/security/code-scanning/1 (#695)

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* fix(deps): update module buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go to v1.31.0-20231002095256-194bc640518b.1 (#693)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update module github.com/sashabaranov/go-openai to v1.15.4 (#689)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* fix(deps): update module github.com/aws/aws-sdk-go to v1.45.20 (#685)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update amannn/action-semantic-pull-request action to v5.3.0 (#683)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* fix(deps): update module github.com/aws/aws-sdk-go to v1.45.21 (#696)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update module github.com/aws/aws-sdk-go to v1.45.22 (#697)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* fix(deps): update module github.com/aws/aws-sdk-go to v1.45.23 (#699)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* fix(deps): update module github.com/aws/aws-sdk-go to v1.45.24 (#701)

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

---------

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>
Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Aris Boutselis <aris.boutselis@senseon.io>
Co-authored-by: Aris Boutselis <arisboutselis08@gmail.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
2023-10-22 16:08:39 +01:00

116 lines
2.8 KiB
Go

package cache
import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/spf13/viper"
)
type CacheType string
const (
Azure CacheType = "azure"
S3 CacheType = "s3"
FileBased CacheType = "file"
)
type ICache interface {
Store(key string, data string) error
Load(key string) (string, error)
List() ([]string, error)
Exists(key string) bool
IsCacheDisabled() bool
}
func New(noCache bool, remoteCache CacheType) ICache {
switch remoteCache {
case S3:
return NewS3Cache(noCache)
case Azure:
return NewAzureCache(noCache)
case FileBased:
return &FileBasedCache{
noCache: noCache,
}
default:
return &FileBasedCache{
noCache: noCache,
}
}
}
// CacheProvider is the configuration for the cache provider when using a remote cache
type CacheProvider struct {
BucketName string `mapstructure:"bucketname" yaml:"bucketname,omitempty"`
Region string `mapstructure:"region" yaml:"region,omitempty"`
StorageAccount string `mapstructure:"storageaccount" yaml:"storageaccount,omitempty"`
ContainerName string `mapstructure:"container" yaml:"container,omitempty"`
}
// NewCacheProvider constructs a new cache struct
func NewCacheProvider(bucketname, region, storageaccount, containername string) CacheProvider {
return CacheProvider{
BucketName: bucketname,
Region: region,
StorageAccount: storageaccount,
ContainerName: containername,
}
}
// If we have set a remote cache, return the remote cache type
func RemoteCacheEnabled() (CacheType, error) {
// load remote cache if it is configured
var cache CacheProvider
err := viper.UnmarshalKey("cache", &cache)
if err != nil {
return "", err
}
if cache.BucketName != "" && cache.Region != "" {
return S3, nil
} else if cache.StorageAccount != "" && cache.ContainerName != "" {
return Azure, nil
}
return FileBased, nil
}
func AddRemoteCache(cache CacheProvider) error {
var cacheInfo CacheProvider
err := viper.UnmarshalKey("cache", &cacheInfo)
if err != nil {
return err
}
cacheInfo.BucketName = cache.BucketName
cacheInfo.Region = cache.Region
cacheInfo.StorageAccount = cache.StorageAccount
cacheInfo.ContainerName = cache.ContainerName
viper.Set("cache", cacheInfo)
err = viper.WriteConfig()
if err != nil {
return err
}
return nil
}
func RemoveRemoteCache() error {
var cacheInfo CacheProvider
err := viper.UnmarshalKey("cache", &cacheInfo)
if err != nil {
return status.Error(codes.Internal, "cache unmarshal")
}
if cacheInfo.BucketName == "" && cacheInfo.ContainerName == "" && cacheInfo.StorageAccount == "" {
return status.Error(codes.Internal, "no remote cache configured")
}
cacheInfo = CacheProvider{}
viper.Set("cache", cacheInfo)
err = viper.WriteConfig()
if err != nil {
return status.Error(codes.Internal, "unable to write config")
}
return nil
}