Skip to content

Commit fa4a075

Browse files
feat: add configuration api route (#459)
* feat: add configuration api route Signed-off-by: Matthis Holleville <[email protected]> * feat: rename cache methods Signed-off-by: Matthis Holleville <[email protected]> --------- Signed-off-by: Matthis Holleville <[email protected]>
1 parent 49e120c commit fa4a075

File tree

9 files changed

+150
-102
lines changed

9 files changed

+150
-102
lines changed

cmd/cache/add.go

+1-19
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/fatih/color"
2222
"github.com/k8sgpt-ai/k8sgpt/pkg/cache"
2323
"github.com/spf13/cobra"
24-
"github.com/spf13/viper"
2524
)
2625

2726
var (
@@ -37,24 +36,7 @@ var addCmd = &cobra.Command{
3736
- S3`,
3837
Run: func(cmd *cobra.Command, args []string) {
3938
fmt.Println(color.YellowString("Adding remote S3 based cache"))
40-
41-
// Check to see whether there is cache information already
42-
var cacheInfo cache.CacheProvider
43-
err := viper.UnmarshalKey("cache", &cacheInfo)
44-
if err != nil {
45-
color.Red("Error: %v", err)
46-
os.Exit(1)
47-
}
48-
if cacheInfo.BucketName != "" {
49-
color.Yellow("Error: a cache is already configured, please remove it first")
50-
os.Exit(1)
51-
}
52-
cacheInfo.BucketName = bucketname
53-
cacheInfo.Region = region
54-
55-
// Save the cache information
56-
viper.Set("cache", cacheInfo)
57-
err = viper.WriteConfig()
39+
err := cache.AddRemoteCache(bucketname, region)
5840
if err != nil {
5941
color.Red("Error: %v", err)
6042
os.Exit(1)

cmd/cache/remove.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/fatih/color"
2121
"github.com/k8sgpt-ai/k8sgpt/pkg/cache"
2222
"github.com/spf13/cobra"
23-
"github.com/spf13/viper"
2423
)
2524

2625
// removeCmd represents the remove command
@@ -30,27 +29,11 @@ var removeCmd = &cobra.Command{
3029
Long: `This command allows you to remove the remote cache and use the default filecache.`,
3130
Run: func(cmd *cobra.Command, args []string) {
3231

33-
// Remove the remote cache
34-
var cacheInfo cache.CacheProvider
35-
err := viper.UnmarshalKey("cache", &cacheInfo)
32+
err := cache.RemoveRemoteCache(bucketname)
3633
if err != nil {
3734
color.Red("Error: %v", err)
3835
os.Exit(1)
3936
}
40-
if cacheInfo.BucketName == "" {
41-
color.Yellow("Error: no cache is configured")
42-
os.Exit(1)
43-
}
44-
// Warn user this will delete the S3 bucket and prompt them to continue
45-
color.Yellow("Warning: this will not delete the S3 bucket %s", cacheInfo.BucketName)
46-
cacheInfo = cache.CacheProvider{}
47-
viper.Set("cache", cacheInfo)
48-
err = viper.WriteConfig()
49-
if err != nil {
50-
color.Red("Error: %v", err)
51-
os.Exit(1)
52-
}
53-
5437
color.Green("Successfully removed the remote cache")
5538
},
5639
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ require (
2424
require github.com/adrg/xdg v0.4.0
2525

2626
require (
27-
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230515081240-6b5b845c638e.1
28-
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230514071713-3d78cb8bbc06.1
27+
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230524215339-41d88e13ab7e.1
28+
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230524215339-41d88e13ab7e.1
2929
github.com/aws/aws-sdk-go v1.44.267
3030
)
3131

go.sum

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230515081240-6b5b845c638e.1 h1:u8CQODmTW0EYjXKt0ZSbA/FGuOkA+zRNicNCs97Ud/A=
2-
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230515081240-6b5b845c638e.1/go.mod h1:EB1h/5OvQWTeT9JJ2x0NLaboeFOOm3fqkYWKp5ojO7o=
3-
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.28.1-20230515081240-6b5b845c638e.4/go.mod h1:i/s4ALHwKvjA1oGNKpoHg0FpEOTbufoOm/NdTE6YQAE=
4-
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230514071713-3d78cb8bbc06.1 h1:V43+hDZXo4WaGqGZjNfaL8ZcAEvhusckIC/JBD/msz0=
5-
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230514071713-3d78cb8bbc06.1/go.mod h1:karV92RruD5davytOQq20lDyAqBnai8ajNolo98nu94=
1+
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230524215339-41d88e13ab7e.1 h1:lvzkvTlk64ZKgqBlyI6wTkRcOCRwERbgvNNC739L2uw=
2+
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230524215339-41d88e13ab7e.1/go.mod h1:gVjoI6SASls1kiAjJWFIYrT6midpN8pAT82q2EEVbGY=
3+
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.28.1-20230524215339-41d88e13ab7e.4/go.mod h1:i/s4ALHwKvjA1oGNKpoHg0FpEOTbufoOm/NdTE6YQAE=
4+
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230524215339-41d88e13ab7e.1 h1:Mx0Z+cXHStOU4lkemYYGhvNd40aU9g52sfS2W7D/gzA=
5+
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230524215339-41d88e13ab7e.1/go.mod h1:karV92RruD5davytOQq20lDyAqBnai8ajNolo98nu94=
66
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
77
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
88
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=

pkg/cache/cache.go

+42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cache
22

33
import (
4+
"errors"
5+
46
"github.com/spf13/viper"
57
)
68

@@ -39,3 +41,43 @@ func RemoteCacheEnabled() (bool, error) {
3941
}
4042
return false, nil
4143
}
44+
45+
func AddRemoteCache(bucketName string, region string) error {
46+
var cacheInfo CacheProvider
47+
err := viper.UnmarshalKey("cache", &cacheInfo)
48+
if err != nil {
49+
return err
50+
}
51+
if cacheInfo.BucketName != "" {
52+
return errors.New("Error: a cache is already configured, please remove it first")
53+
}
54+
cacheInfo.BucketName = bucketName
55+
cacheInfo.Region = region
56+
viper.Set("cache", cacheInfo)
57+
err = viper.WriteConfig()
58+
if err != nil {
59+
return err
60+
}
61+
return nil
62+
}
63+
64+
func RemoveRemoteCache(bucketName string) error {
65+
var cacheInfo CacheProvider
66+
err := viper.UnmarshalKey("cache", &cacheInfo)
67+
if err != nil {
68+
return err
69+
}
70+
if cacheInfo.BucketName == "" {
71+
return errors.New("Error: no cache is configured")
72+
}
73+
74+
cacheInfo = CacheProvider{}
75+
viper.Set("cache", cacheInfo)
76+
err = viper.WriteConfig()
77+
if err != nil {
78+
return err
79+
}
80+
81+
return nil
82+
83+
}

pkg/server/analyze.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package server
2+
3+
import (
4+
"context"
5+
json "encoding/json"
6+
7+
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
8+
"github.com/k8sgpt-ai/k8sgpt/pkg/analysis"
9+
)
10+
11+
func (h *handler) Analyze(ctx context.Context, i *schemav1.AnalyzeRequest) (
12+
*schemav1.AnalyzeResponse,
13+
error,
14+
) {
15+
if i.Output == "" {
16+
i.Output = "json"
17+
}
18+
19+
if i.Backend == "" {
20+
i.Backend = "openai"
21+
}
22+
23+
if int(i.MaxConcurrency) == 0 {
24+
i.MaxConcurrency = 10
25+
}
26+
27+
config, err := analysis.NewAnalysis(
28+
i.Backend,
29+
i.Language,
30+
i.Filters,
31+
i.Namespace,
32+
i.Nocache,
33+
i.Explain,
34+
int(i.MaxConcurrency),
35+
)
36+
if err != nil {
37+
return &schemav1.AnalyzeResponse{}, err
38+
}
39+
config.RunAnalysis()
40+
41+
if i.Explain {
42+
err := config.GetAIResults(i.Output, i.Anonymize)
43+
if err != nil {
44+
return &schemav1.AnalyzeResponse{}, err
45+
}
46+
}
47+
48+
out, err := config.PrintOutput(i.Output)
49+
if err != nil {
50+
return &schemav1.AnalyzeResponse{}, err
51+
}
52+
var obj schemav1.AnalyzeResponse
53+
54+
err = json.Unmarshal(out, &obj)
55+
if err != nil {
56+
return &schemav1.AnalyzeResponse{}, err
57+
}
58+
59+
return &obj, nil
60+
}

pkg/server/config.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package server
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
8+
"github.com/k8sgpt-ai/k8sgpt/pkg/cache"
9+
)
10+
11+
func (h *handler) AddConfig(ctx context.Context, i *schemav1.AddConfigRequest) (*schemav1.AddConfigResponse, error,
12+
) {
13+
if i.Cache.BucketName == "" || i.Cache.Region == "" {
14+
return nil, errors.New("BucketName & Region are required")
15+
}
16+
17+
err := cache.AddRemoteCache(i.Cache.BucketName, i.Cache.Region)
18+
if err != nil {
19+
return &schemav1.AddConfigResponse{}, err
20+
}
21+
22+
return &schemav1.AddConfigResponse{
23+
Status: "Configuration updated.",
24+
}, nil
25+
}
26+
27+
func (h *handler) RemoveConfig(ctx context.Context, i *schemav1.RemoveConfigRequest) (*schemav1.RemoveConfigResponse, error,
28+
) {
29+
err := cache.RemoveRemoteCache(i.Cache.BucketName)
30+
if err != nil {
31+
return &schemav1.RemoveConfigResponse{}, err
32+
}
33+
34+
return &schemav1.RemoveConfigResponse{
35+
Status: "Successfully removed the remote cache",
36+
}, nil
37+
}

pkg/server/handler.go

+1-57
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,9 @@
11
package server
22

33
import (
4-
"context"
5-
json "encoding/json"
6-
74
rpc "buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go/schema/v1/schemav1grpc"
8-
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
9-
"github.com/k8sgpt-ai/k8sgpt/pkg/analysis"
105
)
116

127
type handler struct {
13-
rpc.UnimplementedServerServer
14-
}
15-
16-
func (h *handler) Analyze(ctx context.Context, i *schemav1.AnalyzeRequest) (
17-
*schemav1.AnalyzeResponse,
18-
error,
19-
) {
20-
if i.Output == "" {
21-
i.Output = "json"
22-
}
23-
24-
if i.Backend == "" {
25-
i.Backend = "openai"
26-
}
27-
28-
if int(i.MaxConcurrency) == 0 {
29-
i.MaxConcurrency = 10
30-
}
31-
32-
config, err := analysis.NewAnalysis(
33-
i.Backend,
34-
i.Language,
35-
i.Filters,
36-
i.Namespace,
37-
i.Nocache,
38-
i.Explain,
39-
int(i.MaxConcurrency),
40-
)
41-
if err != nil {
42-
return &schemav1.AnalyzeResponse{}, err
43-
}
44-
config.RunAnalysis()
45-
46-
if i.Explain {
47-
err := config.GetAIResults(i.Output, i.Anonymize)
48-
if err != nil {
49-
return &schemav1.AnalyzeResponse{}, err
50-
}
51-
}
52-
53-
out, err := config.PrintOutput(i.Output)
54-
if err != nil {
55-
return &schemav1.AnalyzeResponse{}, err
56-
}
57-
var obj schemav1.AnalyzeResponse
58-
59-
err = json.Unmarshal(out, &obj)
60-
if err != nil {
61-
return &schemav1.AnalyzeResponse{}, err
62-
}
63-
64-
return &obj, nil
8+
rpc.UnimplementedServerServiceServer
659
}

pkg/server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (s *Config) Serve() error {
6969
grpcServerUnaryInterceptor := grpc.UnaryInterceptor(logInterceptor(s.Logger))
7070
grpcServer := grpc.NewServer(grpcServerUnaryInterceptor)
7171
reflection.Register(grpcServer)
72-
rpc.RegisterServerServer(grpcServer, s.Handler)
72+
rpc.RegisterServerServiceServer(grpcServer, s.Handler)
7373
if err := grpcServer.Serve(
7474
lis,
7575
); err != nil && !errors.Is(err, http.ErrServerClosed) {

0 commit comments

Comments
 (0)