k8sgpt/pkg/server/config.go
Matthis 12146bf356
feat: rework cache package - add gcs cache - add cache purge command (#750)
* feat: rework cache pkg

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: Completion of cache pkg rework. Added cache purge command.

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* doc: add purgin command note

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* fix: disable cache if noCache is set

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: improve GetCacheConfiguration lisibility & transform add method to addOrUpdate

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: transform server mode to work with new cache configuration

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* fix: use 'switch' instead 'if' to evaluate Cache from grpc

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: add mutually exclusive flags for command options

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* doc: update readme.md

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: return err on bucket creation failed

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: update dependencies

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

---------

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
Signed-off-by: Matthis <matthish29@gmail.com>
2023-11-18 22:08:38 +01:00

61 lines
1.8 KiB
Go

package server
import (
"context"
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
"github.com/k8sgpt-ai/k8sgpt/pkg/cache"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (h *handler) AddConfig(ctx context.Context, i *schemav1.AddConfigRequest) (*schemav1.AddConfigResponse, error,
) {
resp, err := h.syncIntegration(ctx, i)
if err != nil {
return resp, err
}
if i.Cache != nil {
var err error
var remoteCache cache.CacheProvider
switch i.Cache.GetCacheType().(type) {
case *schemav1.Cache_AzureCache:
remoteCache, err = cache.NewCacheProvider("azure", "", "", i.Cache.GetAzureCache().StorageAccount, i.Cache.GetAzureCache().ContainerName, "")
case *schemav1.Cache_S3Cache:
remoteCache, err = cache.NewCacheProvider("s3", i.Cache.GetS3Cache().BucketName, i.Cache.GetS3Cache().Region, "", "", "")
case *schemav1.Cache_GcsCache:
remoteCache, err = cache.NewCacheProvider("gcs", i.Cache.GetGcsCache().BucketName, i.Cache.GetGcsCache().Region, "", "", i.Cache.GetGcsCache().GetProjectId())
default:
return resp, status.Error(codes.InvalidArgument, "Invalid cache configuration")
}
if err != nil {
return resp, err
}
err = cache.AddRemoteCache(remoteCache)
if err != nil {
return resp, err
}
}
return resp, nil
}
func (h *handler) RemoveConfig(ctx context.Context, i *schemav1.RemoveConfigRequest) (*schemav1.RemoveConfigResponse, error,
) {
err := cache.RemoveRemoteCache()
if err != nil {
return &schemav1.RemoveConfigResponse{}, err
}
// Remove any integrations is a TBD as it would be nice to make this more granular
// Currently integrations can be removed in the AddConfig sync
return &schemav1.RemoveConfigResponse{
Status: "Successfully removed the remote cache",
}, nil
}