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

fix: sync with changes from of common-lib #196

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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.
3 changes: 3 additions & 0 deletions .gitignore-e
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
kubelink
.DS_Store
144 changes: 144 additions & 0 deletions App.go-e
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2024. Devtron Inc.
*
* 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 main

import (
"context"
"fmt"
"github.com/devtron-labs/common-lib/constants"
"github.com/devtron-labs/common-lib/middlewares"
"github.com/devtron-labs/common-lib/pubsub-lib/metrics"
grpcUtil "github.com/devtron-labs/common-lib/utils/grpc"
"github.com/devtron-labs/kubelink/api/router"
client "github.com/devtron-labs/kubelink/grpc"
"github.com/devtron-labs/kubelink/internals/middleware"
"github.com/devtron-labs/kubelink/pkg/k8sInformer"
"github.com/devtron-labs/kubelink/pkg/service"
"github.com/go-pg/pg"
"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"
"net/http"
"runtime/debug"
"time"
)

type App struct {
Logger *zap.SugaredLogger
ServerImpl *service.ApplicationServiceServerImpl
router *router.RouterImpl
k8sInformer k8sInformer.K8sInformer
db *pg.DB
server *http.Server
grpcServer *grpc.Server
cfg *grpcUtil.Configuration
}

func NewApp(Logger *zap.SugaredLogger,
ServerImpl *service.ApplicationServiceServerImpl,
router *router.RouterImpl,
k8sInformer k8sInformer.K8sInformer,
db *pg.DB, cfg *grpcUtil.Configuration) *App {
return &App{
Logger: Logger,
ServerImpl: ServerImpl,
router: router,
k8sInformer: k8sInformer,
db: db,
cfg: cfg,
}
}

func (app *App) Start() {
port := 50051 //TODO: extract from environment variable
httpPort := 50052
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Panic(err)
}

grpcPanicRecoveryHandler := func(p any) (err error) {
metrics.IncPanicRecoveryCount("grpc", "", "", "")
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.cfg.KubelinkMaxRecvMsgSize * 1024 * 1024), // GRPC Request size
grpc.MaxSendMsgSize(app.cfg.KubelinkMaxSendMsgSize * 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
}
app.router.Router.Use(middleware.PrometheusMiddleware)
app.router.InitRouter()
app.grpcServer = grpc.NewServer(opts...)
client.RegisterApplicationServiceServer(app.grpcServer, app.ServerImpl)
grpcPrometheus.EnableHandlingTimeHistogram()
grpcPrometheus.Register(app.grpcServer)
go func() {
app.server = &http.Server{Addr: fmt.Sprintf(":%d", httpPort), Handler: app.router.Router}
app.router.Router.Use(middlewares.Recovery)
err := app.server.ListenAndServe()
if err != nil {
log.Fatal("error in starting http server", err)
}
}()
app.Logger.Infow("starting server on ", "port", port)

err = app.grpcServer.Serve(listener)
if err != nil {
app.Logger.Fatalw("failed to listen: %v", "err", err)
}

}

func (app *App) Stop() {

app.Logger.Infow("kubelink shutdown initiating")

timeoutContext, _ := context.WithTimeout(context.Background(), 5*time.Second)
app.Logger.Infow("closing router")
err := app.server.Shutdown(timeoutContext)
if err != nil {
app.Logger.Errorw("error in mux router shutdown", "err", err)
}

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

app.Logger.Infow("closing db connection")
err = app.db.Close()
if err != nil {
app.Logger.Errorw("error in closing db connection", "err", err)
}

app.Logger.Infow("housekeeping done. exiting now")
}
22 changes: 22 additions & 0 deletions Dockerfile-e
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:1.22 AS build-env

RUN apt update
RUN apt install git gcc musl-dev make -y
RUN go install github.com/google/wire/cmd/wire@latest

WORKDIR /go/src/github.com/devtron-labs/kubelink
ADD . /go/src/github.com/devtron-labs/kubelink/
RUN GOOS=linux make

FROM ubuntu:22.04@sha256:1b8d8ff4777f36f19bfe73ee4df61e3a0b789caeff29caa019539ec7c9a57f95
RUN apt update
RUN apt install ca-certificates -y
RUN apt clean autoclean
RUN apt autoremove -y && rm -rf /var/lib/apt/lists/*
COPY --from=build-env /go/src/github.com/devtron-labs/kubelink/kubelink .

RUN useradd -ms /bin/bash devtron
RUN chown -R devtron:devtron ./kubelink
USER devtron

CMD ["./kubelink"]
21 changes: 21 additions & 0 deletions LICENSE-e
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Devtron Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
44 changes: 44 additions & 0 deletions Main.go-e
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024. Devtron Inc.
*
* 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 main

import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
)

func main() {
app, err := InitializeApp()
if err != nil {
log.Panic(err)
}
// gracefulStop start
var gracefulStop = make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
go func() {
sig := <-gracefulStop
fmt.Printf("caught sig: %+v", sig)
app.Stop()
os.Exit(0)
}()
// gracefulStop end
app.Start()
}
29 changes: 29 additions & 0 deletions Makefile-e
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

all: build

TAG?=latest
FLAGS=
ENVVAR=
GOOS?=darwin
REGISTRY?=686244538589.dkr.ecr.us-east-2.amazonaws.com
BASEIMAGE?=alpine:3.9
GIT_COMMIT =$(shell sh -c 'git log --pretty=format:'%h' -n 1')
BUILD_TIME= $(shell sh -c 'date -u '+%Y-%m-%dT%H:%M:%SZ'')
GOFLAGS:= $(GOFLAGS) -buildvcs=false

include $(ENV_FILE)
export

build: clean wire
$(ENVVAR) GOOS=$(GOOS) go build \
-o kubelink \

wire:
wire

clean:
rm -rf kubelink

run: build
./kubelink

85 changes: 85 additions & 0 deletions Wire.go-e
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//go:build wireinject
// +build wireinject

/*
* Copyright (c) 2024. Devtron Inc.
*
* 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 main

import (
"github.com/devtron-labs/common-lib/helmLib/registry"
"github.com/devtron-labs/common-lib/monitoring"
"github.com/devtron-labs/common-lib/utils/grpc"
"github.com/devtron-labs/common-lib/utils/k8s"
"github.com/devtron-labs/kubelink/api/router"
globalConfig "github.com/devtron-labs/kubelink/config"
"github.com/devtron-labs/kubelink/converter"
"github.com/devtron-labs/kubelink/internals/lock"
"github.com/devtron-labs/kubelink/internals/logger"
"github.com/devtron-labs/kubelink/pkg/asyncProvider"
repository "github.com/devtron-labs/kubelink/pkg/cluster"
"github.com/devtron-labs/kubelink/pkg/k8sInformer"
"github.com/devtron-labs/kubelink/pkg/service"
commonHelmService "github.com/devtron-labs/kubelink/pkg/service/commonHelmService"
fluxService "github.com/devtron-labs/kubelink/pkg/service/fluxService"
helmApplicationService "github.com/devtron-labs/kubelink/pkg/service/helmApplicationService"
"github.com/devtron-labs/kubelink/pkg/sql"
"github.com/google/wire"
)

func InitializeApp() (*App, error) {
wire.Build(
NewApp,
sql.PgSqlWireSet,
logger.NewSugaredLogger,
k8s.GetRuntimeConfig,
k8s.NewK8sUtil,
wire.Bind(new(k8s.K8sService), new(*k8s.K8sServiceImpl)),
lock.NewChartRepositoryLocker,
commonHelmService.NewK8sServiceImpl,
wire.Bind(new(commonHelmService.K8sService), new(*commonHelmService.K8sServiceImpl)),
commonHelmService.NewCommonHelmServiceImpl,
wire.Bind(new(commonHelmService.CommonHelmService), new(*commonHelmService.CommonHelmServiceImpl)),
helmApplicationService.NewHelmAppServiceImpl,
wire.Bind(new(helmApplicationService.HelmAppService), new(*helmApplicationService.HelmAppServiceImpl)),
converter.NewConverterImpl,
wire.Bind(new(converter.ClusterBeanConverter), new(*converter.ClusterBeanConverterImpl)),
fluxService.NewFluxApplicationServiceImpl,
wire.Bind(new(fluxService.FluxApplicationService), new(*fluxService.FluxApplicationServiceImpl)),
service.NewApplicationServiceServerImpl,
router.NewRouter,
asyncProvider.NewAsyncRunnable,
k8sInformer.Newk8sInformerImpl,
wire.Bind(new(k8sInformer.K8sInformer), new(*k8sInformer.K8sInformerImpl)),
repository.NewClusterRepositoryImpl,
wire.Bind(new(repository.ClusterRepository), new(*repository.ClusterRepositoryImpl)),
globalConfig.GetHelmReleaseConfig,
//k8sInformer.GetHelmReleaseConfig,
grpc.GetConfiguration,
//pubsub_lib.NewPubSubClientServiceImpl,
//cache.NewClusterCacheImpl,
//wire.Bind(new(cache.ClusterCache), new(*cache.ClusterCacheImpl)),
//cache.GetClusterCacheConfig,
registry.NewSettingsFactoryImpl,
wire.Bind(new(registry.SettingsFactory), new(*registry.SettingsFactoryImpl)),

registry.NewDefaultSettingsGetter,
wire.Bind(new(registry.DefaultSettingsGetter), new(*registry.DefaultSettingsGetterImpl)),

monitoring.NewMonitoringRouter,
)
return &App{}, nil
}
Loading