forked from k8sgpt-ai/k8sgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add configuration api route (k8sgpt-ai#459)
* feat: add configuration api route Signed-off-by: Matthis Holleville <matthish29@gmail.com> * feat: rename cache methods Signed-off-by: Matthis Holleville <matthish29@gmail.com> --------- Signed-off-by: Matthis Holleville <matthish29@gmail.com> Signed-off-by: David Sabatie <david.sabatie@notrenet.com>
- Loading branch information
1 parent
4562b38
commit 4de618e
Showing
9 changed files
with
150 additions
and
102 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
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,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 | ||
} |
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,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 | ||
} |
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 |
---|---|---|
@@ -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 | ||
} |
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