diff --git a/cmd/cache/add.go b/cmd/cache/add.go index e7d3832a6c..b97f5f2197 100644 --- a/cmd/cache/add.go +++ b/cmd/cache/add.go @@ -21,7 +21,6 @@ import ( "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/pkg/cache" "github.com/spf13/cobra" - "github.com/spf13/viper" ) var ( @@ -37,24 +36,7 @@ var addCmd = &cobra.Command{ - S3`, Run: func(cmd *cobra.Command, args []string) { fmt.Println(color.YellowString("Adding remote S3 based cache")) - - // Check to see whether there is cache information already - var cacheInfo cache.CacheProvider - err := viper.UnmarshalKey("cache", &cacheInfo) - if err != nil { - color.Red("Error: %v", err) - os.Exit(1) - } - if cacheInfo.BucketName != "" { - color.Yellow("Error: a cache is already configured, please remove it first") - os.Exit(1) - } - cacheInfo.BucketName = bucketname - cacheInfo.Region = region - - // Save the cache information - viper.Set("cache", cacheInfo) - err = viper.WriteConfig() + err := cache.AddRemoteCache(bucketname, region) if err != nil { color.Red("Error: %v", err) os.Exit(1) diff --git a/cmd/cache/remove.go b/cmd/cache/remove.go index 2127f01d72..56fd5b73cd 100644 --- a/cmd/cache/remove.go +++ b/cmd/cache/remove.go @@ -20,7 +20,6 @@ import ( "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/pkg/cache" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // removeCmd represents the remove command @@ -30,27 +29,11 @@ var removeCmd = &cobra.Command{ Long: `This command allows you to remove the remote cache and use the default filecache.`, Run: func(cmd *cobra.Command, args []string) { - // Remove the remote cache - var cacheInfo cache.CacheProvider - err := viper.UnmarshalKey("cache", &cacheInfo) + err := cache.RemoveRemoteCache(bucketname) if err != nil { color.Red("Error: %v", err) os.Exit(1) } - if cacheInfo.BucketName == "" { - color.Yellow("Error: no cache is configured") - os.Exit(1) - } - // Warn user this will delete the S3 bucket and prompt them to continue - color.Yellow("Warning: this will not delete the S3 bucket %s", cacheInfo.BucketName) - cacheInfo = cache.CacheProvider{} - viper.Set("cache", cacheInfo) - err = viper.WriteConfig() - if err != nil { - color.Red("Error: %v", err) - os.Exit(1) - } - color.Green("Successfully removed the remote cache") }, } diff --git a/go.mod b/go.mod index 791d89e3b1..64d4d94fb5 100644 --- a/go.mod +++ b/go.mod @@ -24,8 +24,8 @@ require ( require github.com/adrg/xdg v0.4.0 require ( - buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230515081240-6b5b845c638e.1 - buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230514071713-3d78cb8bbc06.1 + buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230524215339-41d88e13ab7e.1 + buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230524215339-41d88e13ab7e.1 github.com/aws/aws-sdk-go v1.44.267 ) diff --git a/go.sum b/go.sum index 53e9ea1a48..5cff87b5b7 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ -buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230515081240-6b5b845c638e.1 h1:u8CQODmTW0EYjXKt0ZSbA/FGuOkA+zRNicNCs97Ud/A= -buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230515081240-6b5b845c638e.1/go.mod h1:EB1h/5OvQWTeT9JJ2x0NLaboeFOOm3fqkYWKp5ojO7o= -buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.28.1-20230515081240-6b5b845c638e.4/go.mod h1:i/s4ALHwKvjA1oGNKpoHg0FpEOTbufoOm/NdTE6YQAE= -buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230514071713-3d78cb8bbc06.1 h1:V43+hDZXo4WaGqGZjNfaL8ZcAEvhusckIC/JBD/msz0= -buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230514071713-3d78cb8bbc06.1/go.mod h1:karV92RruD5davytOQq20lDyAqBnai8ajNolo98nu94= +buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230524215339-41d88e13ab7e.1 h1:lvzkvTlk64ZKgqBlyI6wTkRcOCRwERbgvNNC739L2uw= +buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230524215339-41d88e13ab7e.1/go.mod h1:gVjoI6SASls1kiAjJWFIYrT6midpN8pAT82q2EEVbGY= +buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.28.1-20230524215339-41d88e13ab7e.4/go.mod h1:i/s4ALHwKvjA1oGNKpoHg0FpEOTbufoOm/NdTE6YQAE= +buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230524215339-41d88e13ab7e.1 h1:Mx0Z+cXHStOU4lkemYYGhvNd40aU9g52sfS2W7D/gzA= +buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230524215339-41d88e13ab7e.1/go.mod h1:karV92RruD5davytOQq20lDyAqBnai8ajNolo98nu94= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 5ab4e7ff24..f004fc9633 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -1,6 +1,8 @@ package cache import ( + "errors" + "github.com/spf13/viper" ) @@ -39,3 +41,43 @@ func RemoteCacheEnabled() (bool, error) { } return false, nil } + +func AddRemoteCache(bucketName string, region string) error { + var cacheInfo CacheProvider + err := viper.UnmarshalKey("cache", &cacheInfo) + if err != nil { + return err + } + if cacheInfo.BucketName != "" { + return errors.New("Error: a cache is already configured, please remove it first") + } + cacheInfo.BucketName = bucketName + cacheInfo.Region = region + viper.Set("cache", cacheInfo) + err = viper.WriteConfig() + if err != nil { + return err + } + return nil +} + +func RemoveRemoteCache(bucketName string) error { + var cacheInfo CacheProvider + err := viper.UnmarshalKey("cache", &cacheInfo) + if err != nil { + return err + } + if cacheInfo.BucketName == "" { + return errors.New("Error: no cache is configured") + } + + cacheInfo = CacheProvider{} + viper.Set("cache", cacheInfo) + err = viper.WriteConfig() + if err != nil { + return err + } + + return nil + +} diff --git a/pkg/server/analyze.go b/pkg/server/analyze.go new file mode 100644 index 0000000000..ca5190c4d4 --- /dev/null +++ b/pkg/server/analyze.go @@ -0,0 +1,60 @@ +package server + +import ( + "context" + json "encoding/json" + + schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1" + "github.com/k8sgpt-ai/k8sgpt/pkg/analysis" +) + +func (h *handler) Analyze(ctx context.Context, i *schemav1.AnalyzeRequest) ( + *schemav1.AnalyzeResponse, + error, +) { + if i.Output == "" { + i.Output = "json" + } + + if i.Backend == "" { + i.Backend = "openai" + } + + if int(i.MaxConcurrency) == 0 { + i.MaxConcurrency = 10 + } + + config, err := analysis.NewAnalysis( + i.Backend, + i.Language, + i.Filters, + i.Namespace, + i.Nocache, + i.Explain, + int(i.MaxConcurrency), + ) + if err != nil { + return &schemav1.AnalyzeResponse{}, err + } + config.RunAnalysis() + + if i.Explain { + err := config.GetAIResults(i.Output, i.Anonymize) + if err != nil { + return &schemav1.AnalyzeResponse{}, err + } + } + + out, err := config.PrintOutput(i.Output) + if err != nil { + return &schemav1.AnalyzeResponse{}, err + } + var obj schemav1.AnalyzeResponse + + err = json.Unmarshal(out, &obj) + if err != nil { + return &schemav1.AnalyzeResponse{}, err + } + + return &obj, nil +} diff --git a/pkg/server/config.go b/pkg/server/config.go new file mode 100644 index 0000000000..0d6cf7edb3 --- /dev/null +++ b/pkg/server/config.go @@ -0,0 +1,37 @@ +package server + +import ( + "context" + "errors" + + schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1" + "github.com/k8sgpt-ai/k8sgpt/pkg/cache" +) + +func (h *handler) AddConfig(ctx context.Context, i *schemav1.AddConfigRequest) (*schemav1.AddConfigResponse, error, +) { + if i.Cache.BucketName == "" || i.Cache.Region == "" { + return nil, errors.New("BucketName & Region are required") + } + + err := cache.AddRemoteCache(i.Cache.BucketName, i.Cache.Region) + if err != nil { + return &schemav1.AddConfigResponse{}, err + } + + return &schemav1.AddConfigResponse{ + Status: "Configuration updated.", + }, nil +} + +func (h *handler) RemoveConfig(ctx context.Context, i *schemav1.RemoveConfigRequest) (*schemav1.RemoveConfigResponse, error, +) { + err := cache.RemoveRemoteCache(i.Cache.BucketName) + if err != nil { + return &schemav1.RemoveConfigResponse{}, err + } + + return &schemav1.RemoveConfigResponse{ + Status: "Successfully removed the remote cache", + }, nil +} diff --git a/pkg/server/handler.go b/pkg/server/handler.go index 9f685fc2e9..3751543b31 100644 --- a/pkg/server/handler.go +++ b/pkg/server/handler.go @@ -1,65 +1,9 @@ package server import ( - "context" - json "encoding/json" - rpc "buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go/schema/v1/schemav1grpc" - schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1" - "github.com/k8sgpt-ai/k8sgpt/pkg/analysis" ) type handler struct { - rpc.UnimplementedServerServer -} - -func (h *handler) Analyze(ctx context.Context, i *schemav1.AnalyzeRequest) ( - *schemav1.AnalyzeResponse, - error, -) { - if i.Output == "" { - i.Output = "json" - } - - if i.Backend == "" { - i.Backend = "openai" - } - - if int(i.MaxConcurrency) == 0 { - i.MaxConcurrency = 10 - } - - config, err := analysis.NewAnalysis( - i.Backend, - i.Language, - i.Filters, - i.Namespace, - i.Nocache, - i.Explain, - int(i.MaxConcurrency), - ) - if err != nil { - return &schemav1.AnalyzeResponse{}, err - } - config.RunAnalysis() - - if i.Explain { - err := config.GetAIResults(i.Output, i.Anonymize) - if err != nil { - return &schemav1.AnalyzeResponse{}, err - } - } - - out, err := config.PrintOutput(i.Output) - if err != nil { - return &schemav1.AnalyzeResponse{}, err - } - var obj schemav1.AnalyzeResponse - - err = json.Unmarshal(out, &obj) - if err != nil { - return &schemav1.AnalyzeResponse{}, err - } - - return &obj, nil + rpc.UnimplementedServerServiceServer } diff --git a/pkg/server/server.go b/pkg/server/server.go index b5f5cfca8d..842e79c2cb 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -69,7 +69,7 @@ func (s *Config) Serve() error { grpcServerUnaryInterceptor := grpc.UnaryInterceptor(logInterceptor(s.Logger)) grpcServer := grpc.NewServer(grpcServerUnaryInterceptor) reflection.Register(grpcServer) - rpc.RegisterServerServer(grpcServer, s.Handler) + rpc.RegisterServerServiceServer(grpcServer, s.Handler) if err := grpcServer.Serve( lis, ); err != nil && !errors.Is(err, http.ErrServerClosed) {