Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Kubecon sync develop #47

Open
wants to merge 23 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
663f602
expose silver surfer as a mvc via grpc code incorporated in this commit
prakash100198 Nov 4, 2024
a5ad2e5
ClusterUpgradeReadService.go with logic in grpc handler
prakash100198 Nov 4, 2024
4e351e5
some minor refact
prakash100198 Nov 4, 2024
dc627db
small prod bug fix
prakash100198 Nov 4, 2024
66b2e62
small prod bug fix-2
prakash100198 Nov 4, 2024
e84412e
small prod bug fix-3
prakash100198 Nov 4, 2024
6cb269b
ResourceNamespace added in final SummaryValidationResult which was no…
prakash100198 Nov 4, 2024
b604e5c
change service.proto filename to silverSurferService.proto as it was …
prakash100198 Nov 4, 2024
c10eba5
change docker file for build
prakash100198 Nov 4, 2024
5497267
change go version
prakash100198 Nov 4, 2024
9cd7b3c
change alpine version
prakash100198 Nov 4, 2024
819eaef
fix
prakash100198 Nov 4, 2024
b651006
expose USE_AS_CLI,
prakash100198 Nov 4, 2024
b361b5d
code revie incorporation :- 1. add prometheus 2. add new dockerfile a…
prakash100198 Nov 5, 2024
08f1ab7
inject common lib, and also introduce cluster config in service.proto…
prakash100198 Nov 5, 2024
f12ae38
go mod tidy and go mod vendor
prakash100198 Nov 5, 2024
997edb1
commit staged files
prakash100198 Nov 5, 2024
934ad52
change service.proto package name
prakash100198 Nov 5, 2024
9ebe0ba
NewClusterViaInClusterConfig -> NewClusterFromEnvOrConfig changed nam…
prakash100198 Nov 5, 2024
9c2e693
info log
prakash100198 Nov 5, 2024
99da5eb
OpenApiSpecNotFoundError when k8s verion is not correct
prakash100198 Nov 6, 2024
c04bc37
return error when got error from LoadFromUrl instead of exiting
prakash100198 Nov 6, 2024
f445433
Merge pull request #45 from devtron-labs/expose-with-grpc
kartik-579 Nov 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
19 changes: 19 additions & 0 deletions DockerfileMVC
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Binary Build
FROM golang:1.21-alpine3.17 AS build-env
RUN echo $GOPATH
RUN apk add --no-cache git gcc musl-dev
RUN apk add --update make
RUN mkdir /silver-surfer
WORKDIR /silver-surfer
ADD . /silver-surfer/
RUN rm -rf ./bin
RUN mkdir ./bin
RUN GOOS=linux go build -o ./bin/kubedd-mvc ./app

# Prod Build
FROM alpine:3.17
RUN apk add --no-cache ca-certificates
RUN apk update
RUN apk add git
COPY --from=build-env /silver-surfer/bin .
ENTRYPOINT ["./kubedd-mvc"]
115 changes: 115 additions & 0 deletions app/App.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package main

import (
"fmt"
"github.com/caarlos0/env"
"github.com/devtron-labs/silver-surfer/app/api"
"github.com/devtron-labs/silver-surfer/app/constants"
grpc2 "github.com/devtron-labs/silver-surfer/app/grpc"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/status"
"log"
"net"
"os"
"runtime/debug"
"time"
)

type App struct {
grpcServer *grpc.Server
StartupConfig *StartupConfig
logger *zap.SugaredLogger
GrpcHandler *api.GrpcHandlerImpl
}

func NewApp(
logger *zap.SugaredLogger,
GrpcHandler *api.GrpcHandlerImpl,
) *App {
return &App{
logger: logger,
GrpcHandler: GrpcHandler,
}
}

type StartupConfig struct {
GrpcPort int `env:"SERVER_GRPC_PORT" envDefault:"8111"`
GrpcMaxRecvMsgSize int `env:"GRPC_MAX_RECEIVE_MSG_SIZE" envDefault:"20"` // In mb
GrpcMaxSendMsgSize int `env:"GRPC_MAX_SEND_MSG_SIZE" envDefault:"4"` // In mb
}

func (app *App) Start() {
// Parse config
app.StartupConfig = &StartupConfig{}
err := env.Parse(app.StartupConfig)
if err != nil {
app.logger.Errorw("failed to parse configuration")
os.Exit(2)
}

// Start gRPC server
err = app.initGrpcServer(app.StartupConfig.GrpcPort)
if err != nil {
app.logger.Errorw("error starting grpc server", "err", err)
os.Exit(2)
}
}

func (app *App) initGrpcServer(port int) error {
app.logger.Infow("gRPC server starting", "port", app.StartupConfig.GrpcPort)

//listen on the port
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("failed to start grpcServer %v", err)
return err
}

grpcPanicRecoveryHandler := func(p any) (err error) {
app.logger.Error(constants.PanicLogIdentifier, "recovered from panic", "panic", p, "stack", string(debug.Stack()))
return status.Errorf(codes.Internal, "%s", p)
}
recoveryOption := recovery.WithRecoveryHandler(grpcPanicRecoveryHandler)
opts := []grpc.ServerOption{
grpc.MaxRecvMsgSize(app.StartupConfig.GrpcMaxRecvMsgSize * 1024 * 1024), // GRPC Request size
grpc.MaxSendMsgSize(app.StartupConfig.GrpcMaxSendMsgSize * 1024 * 1024), // GRPC Response size
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionAge: 10 * time.Second,
}),
grpc.ChainStreamInterceptor(
grpcPrometheus.StreamServerInterceptor,
recovery.StreamServerInterceptor(recoveryOption)), // panic interceptor, should be at last
grpc.ChainUnaryInterceptor(
grpcPrometheus.UnaryServerInterceptor,
recovery.UnaryServerInterceptor(recoveryOption)), // panic interceptor, should be at last
}
// create a new gRPC grpcServer
app.grpcServer = grpc.NewServer(opts...)

// register Silver Surfer service
grpc2.RegisterSilverSurferServiceServer(app.grpcServer, app.GrpcHandler)
grpcPrometheus.EnableHandlingTimeHistogram()
grpcPrometheus.Register(app.grpcServer)
// start listening on address
if err = app.grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to start: %v", err)
return err
}
return nil
}

func (app *App) Stop() {

app.logger.Infow("silver-surfer shutdown initiating")

// Gracefully stop the gRPC server
app.logger.Info("Stopping gRPC server...")
app.grpcServer.GracefulStop()

app.logger.Infow("housekeeping done. exiting now")
}
26 changes: 26 additions & 0 deletions app/Wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build wireinject
// +build wireinject

package main

import (
"github.com/devtron-labs/common-lib/utils/k8s"
"github.com/devtron-labs/silver-surfer/app/api"
"github.com/devtron-labs/silver-surfer/app/logger"
"github.com/devtron-labs/silver-surfer/app/service"
"github.com/google/wire"
)

func InitializeApp() (*App, error) {
wire.Build(
NewApp,
logger.NewSugaredLogger,
api.NewGrpcHandlerImpl,
service.NewClusterUpgradeReadServiceImpl,
wire.Bind(new(service.ClusterUpgradeReadService), new(*service.ClusterUpgradeReadServiceImpl)),
k8s.GetRuntimeConfig,
k8s.NewK8sUtil,
wire.Bind(new(k8s.K8sService), new(*k8s.K8sServiceImpl)),
)
return &App{}, nil
}
93 changes: 93 additions & 0 deletions app/adaptors/adaptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package adaptors

import (
"github.com/devtron-labs/common-lib/utils/k8s"
"github.com/devtron-labs/common-lib/utils/remoteConnection/bean"
"github.com/devtron-labs/silver-surfer/app/grpc"
"github.com/devtron-labs/silver-surfer/pkg"
)

func ConvertSummaryValidationResultToGrpcObj(req []pkg.SummaryValidationResult) []*grpc.SummaryValidationResult {
resp := make([]*grpc.SummaryValidationResult, 0, len(req))
for _, item := range req {
svr := &grpc.SummaryValidationResult{
FileName: item.FileName,
Kind: item.Kind,
APIVersion: item.APIVersion,
ResourceName: item.ResourceName,
ResourceNamespace: item.ResourceNamespace,
Deleted: item.Deleted,
Deprecated: item.Deprecated,
LatestAPIVersion: item.LatestAPIVersion,
IsVersionSupported: int32(item.IsVersionSupported),
ErrorsForOriginal: ConvertSummarySchemaErrorToGrpcObj(item.ErrorsForOriginal),
ErrorsForLatest: ConvertSummarySchemaErrorToGrpcObj(item.ErrorsForLatest),
DeprecationForOriginal: ConvertSummarySchemaErrorToGrpcObj(item.DeprecationForOriginal),
DeprecationForLatest: ConvertSummarySchemaErrorToGrpcObj(item.DeprecationForLatest),
}
resp = append(resp, svr)
}
return resp
}

func ConvertSummarySchemaErrorToGrpcObj(req []*pkg.SummarySchemaError) []*grpc.SummarySchemaError {
resp := make([]*grpc.SummarySchemaError, 0, len(req))
for _, item := range req {
if item != nil {
sse := &grpc.SummarySchemaError{
Path: item.Path,
SchemaField: item.SchemaField,
Reason: item.Reason,
}
resp = append(resp, sse)
}
}
return resp
}

func ConvertGrpcObjToClusterConfig(req *grpc.ClusterConfig) *k8s.ClusterConfig {
if req != nil {
return &k8s.ClusterConfig{
ClusterName: req.ClusterName,
Host: req.ApiServerUrl,
BearerToken: req.Token,
InsecureSkipTLSVerify: req.InsecureSkipTLSVerify,
KeyData: req.KeyData,
CertData: req.CertData,
CAData: req.CaData,
ClusterId: int(req.ClusterId),
RemoteConnectionConfig: ConvertGrpcObjToRemoteConnectionConfig(req.RemoteConnectionConfig),
}
}
return &k8s.ClusterConfig{}
}

func ConvertGrpcObjToRemoteConnectionConfig(req *grpc.RemoteConnectionConfig) *bean.RemoteConnectionConfigBean {
if req != nil {
return &bean.RemoteConnectionConfigBean{
ConnectionMethod: bean.RemoteConnectionMethod(req.RemoteConnectionMethod),
ProxyConfig: ConvertGrpcObjToProxyConfig(req.ProxyConfig),
SSHTunnelConfig: ConvertGrpcObjToSSHTunnelConfig(req.SSHTunnelConfig),
}
}
return &bean.RemoteConnectionConfigBean{}
}

func ConvertGrpcObjToProxyConfig(req *grpc.ProxyConfig) *bean.ProxyConfig {
if req != nil {
return &bean.ProxyConfig{ProxyUrl: req.ProxyUrl}
}
return &bean.ProxyConfig{}
}

func ConvertGrpcObjToSSHTunnelConfig(req *grpc.SSHTunnelConfig) *bean.SSHTunnelConfig {
if req != nil {
return &bean.SSHTunnelConfig{
SSHServerAddress: req.SSHServerAddress,
SSHUsername: req.SSHUsername,
SSHPassword: req.SSHPassword,
SSHAuthKey: req.SSHAuthKey,
}
}
return &bean.SSHTunnelConfig{}
}
36 changes: 36 additions & 0 deletions app/api/GrpcHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package api

import (
"context"
"github.com/devtron-labs/silver-surfer/app/adaptors"
"github.com/devtron-labs/silver-surfer/app/grpc"
"github.com/devtron-labs/silver-surfer/app/service"
"go.uber.org/zap"
)

type GrpcHandlerImpl struct {
grpc.UnimplementedSilverSurferServiceServer
logger *zap.SugaredLogger
clusterUpgradeReadService service.ClusterUpgradeReadService
}

func NewGrpcHandlerImpl(
logger *zap.SugaredLogger,
clusterUpgradeReadService service.ClusterUpgradeReadService,
) *GrpcHandlerImpl {
return &GrpcHandlerImpl{
logger: logger,
clusterUpgradeReadService: clusterUpgradeReadService,
}
}

func (impl *GrpcHandlerImpl) GetClusterUpgradeSummaryValidationResult(ctx context.Context, request *grpc.ClusterUpgradeRequest) (*grpc.ClusterUpgradeResponse, error) {
impl.logger.Infow("scan cluster resources compatibility for k8s version upgrade request", "request", request)
summaryValidationResult, err := impl.clusterUpgradeReadService.GetClusterUpgradeSummaryValidationResult(request.TargetK8SVersion, request.ClusterConfig)
if err != nil {
impl.logger.Errorw("error in getting cluster upgrade summary validation result", "targetK8sVersion", request.TargetK8SVersion, "err", err)
return nil, err
}
svr := adaptors.ConvertSummaryValidationResultToGrpcObj(summaryValidationResult)
return &grpc.ClusterUpgradeResponse{Results: svr}, nil
}
6 changes: 6 additions & 0 deletions app/constants/Constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package constants

const (
PanicLogIdentifier = "DEVTRON_PANIC_RECOVER"
OutputJson = "json"
)
Loading