-
Notifications
You must be signed in to change notification settings - Fork 700
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds interplex as a caching provider (#1328)
* feat: adds interplex as a caching provider Signed-off-by: AlexsJones <[email protected]> * chore: refactored cache input to lower Signed-off-by: AlexsJones <[email protected]> * chore: refactored cache input to lower Signed-off-by: AlexsJones <[email protected]> --------- Signed-off-by: AlexsJones <[email protected]>
- Loading branch information
1 parent
a841568
commit d6d80ee
Showing
9 changed files
with
255 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2023 The K8sGPT Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fatih/color" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/cache" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
// listCmd represents the list command | ||
var getCmd = &cobra.Command{ | ||
Use: "get", | ||
Short: "Get the current cache", | ||
Long: `Returns the current remote cache being used`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
// load remote cache if it is configured | ||
c, err := cache.GetCacheConfiguration() | ||
if err != nil { | ||
color.Red("Error: %v", err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("Current remote cache is: %s", c.GetName()) | ||
}, | ||
} | ||
|
||
func init() { | ||
CacheCmd.AddCommand(getCmd) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package cache | ||
|
||
import ( | ||
rpc "buf.build/gen/go/interplex-ai/schemas/grpc/go/protobuf/schema/v1/schemav1grpc" | ||
schemav1 "buf.build/gen/go/interplex-ai/schemas/protocolbuffers/go/protobuf/schema/v1" | ||
"context" | ||
"errors" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
var _ ICache = (*InterplexCache)(nil) | ||
|
||
type InterplexCache struct { | ||
configuration InterplexCacheConfiguration | ||
client InterplexClient | ||
cacheServiceClient rpc.CacheServiceClient | ||
noCache bool | ||
} | ||
|
||
type InterplexCacheConfiguration struct { | ||
ConnectionString string `mapstructure:"connectionString" yaml:"connectionString,omitempty"` | ||
} | ||
|
||
type InterplexClient struct { | ||
} | ||
|
||
func (c *InterplexCache) Configure(cacheInfo CacheProvider) error { | ||
|
||
if cacheInfo.Interplex.ConnectionString == "" { | ||
return errors.New("connection string is required") | ||
} | ||
c.configuration.ConnectionString = cacheInfo.Interplex.ConnectionString | ||
return nil | ||
} | ||
|
||
func (c *InterplexCache) Store(key string, data string) error { | ||
|
||
conn, err := grpc.NewClient(c.configuration.ConnectionString, grpc.WithInsecure(), grpc.WithBlock()) | ||
defer conn.Close() | ||
if err != nil { | ||
return err | ||
} | ||
serviceClient := rpc.NewCacheServiceClient(conn) | ||
c.cacheServiceClient = serviceClient | ||
req := schemav1.SetRequest{ | ||
Key: key, | ||
Value: data, | ||
} | ||
_, err = c.cacheServiceClient.Set(context.Background(), &req) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *InterplexCache) Load(key string) (string, error) { | ||
conn, err := grpc.NewClient(c.configuration.ConnectionString, grpc.WithInsecure(), grpc.WithBlock()) | ||
defer conn.Close() | ||
if err != nil { | ||
return "", err | ||
} | ||
serviceClient := rpc.NewCacheServiceClient(conn) | ||
c.cacheServiceClient = serviceClient | ||
req := schemav1.GetRequest{ | ||
Key: key, | ||
} | ||
resp, err := c.cacheServiceClient.Get(context.Background(), &req) | ||
// check if response is cache error not found | ||
if err != nil { | ||
return "", err | ||
} | ||
return resp.Value, nil | ||
} | ||
|
||
func (InterplexCache) List() ([]CacheObjectDetails, error) { | ||
//TODO implement me | ||
return nil, errors.New("not implemented") | ||
} | ||
|
||
func (InterplexCache) Remove(key string) error { | ||
|
||
return errors.New("not implemented") | ||
} | ||
|
||
func (c *InterplexCache) Exists(key string) bool { | ||
if _, err := c.Load(key); err != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (c *InterplexCache) IsCacheDisabled() bool { | ||
return c.noCache | ||
} | ||
|
||
func (InterplexCache) GetName() string { | ||
//TODO implement me | ||
return "interplex" | ||
} | ||
|
||
func (c *InterplexCache) DisableCache() { | ||
c.noCache = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cache | ||
|
||
import ( | ||
rpc "buf.build/gen/go/interplex-ai/schemas/grpc/go/protobuf/schema/v1/schemav1grpc" | ||
schemav1 "buf.build/gen/go/interplex-ai/schemas/protocolbuffers/go/protobuf/schema/v1" | ||
"context" | ||
"errors" | ||
"google.golang.org/grpc" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestInterplexCache(t *testing.T) { | ||
cache := &InterplexCache{ | ||
configuration: InterplexCacheConfiguration{ | ||
ConnectionString: "localhost:50051", | ||
}, | ||
} | ||
|
||
// Mock GRPC server setup | ||
go func() { | ||
lis, err := net.Listen("tcp", ":50051") | ||
if err != nil { | ||
t.Fatalf("failed to listen: %v", err) | ||
} | ||
s := grpc.NewServer() | ||
rpc.RegisterCacheServiceServer(s, &mockCacheService{}) | ||
if err := s.Serve(lis); err != nil { | ||
t.Fatalf("failed to serve: %v", err) | ||
} | ||
}() | ||
|
||
t.Run("TestStore", func(t *testing.T) { | ||
err := cache.Store("key1", "value1") | ||
if err != nil { | ||
t.Errorf("Error storing value: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("TestLoad", func(t *testing.T) { | ||
value, err := cache.Load("key1") | ||
if err != nil { | ||
t.Errorf("Error loading value: %v", err) | ||
} | ||
if value != "value1" { | ||
t.Errorf("Expected value1, got %v", value) | ||
} | ||
}) | ||
|
||
t.Run("TestExists", func(t *testing.T) { | ||
exists := cache.Exists("key1") | ||
if !exists { | ||
t.Errorf("Expected key1 to exist") | ||
} | ||
}) | ||
} | ||
|
||
type mockCacheService struct { | ||
rpc.UnimplementedCacheServiceServer | ||
data map[string]string | ||
} | ||
|
||
func (m *mockCacheService) Set(ctx context.Context, req *schemav1.SetRequest) (*schemav1.SetResponse, error) { | ||
if m.data == nil { | ||
m.data = make(map[string]string) | ||
} | ||
m.data[req.Key] = req.Value | ||
return &schemav1.SetResponse{}, nil | ||
} | ||
|
||
func (m *mockCacheService) Get(ctx context.Context, req *schemav1.GetRequest) (*schemav1.GetResponse, error) { | ||
value, exists := m.data[req.Key] | ||
if !exists { | ||
return nil, errors.New("key not found") | ||
} | ||
return &schemav1.GetResponse{Value: value}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters