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

Implement store application data into postgreSQL #12

Merged
merged 7 commits into from
Jun 4, 2021
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
355 changes: 143 additions & 212 deletions cmd/application/handler.go

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions cmd/info/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,3 @@ func (s *Server) UpdateClusterStatus(ctx context.Context, in *pb.UpdateClusterSt
Error: nil,
}, nil
}

// ValidateLabelUniqueness check uniqueness of the label
func (s *Server) ValidateLabelUniqueness(ctx context.Context, in *pb.ValidateLabelUniquenessRequest) (*pb.ValidateLabelUniquenessResponse, error) {
return nil, nil
}
21 changes: 15 additions & 6 deletions cmd/server.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"gorm.io/driver/postgres"
"gorm.io/gorm"

"github.com/sktelecom/tks-contract/pkg/log"
app "github.com/sktelecom/tks-info/cmd/application"
Expand All @@ -17,12 +19,14 @@ import (
)

var (
// log = grpclog.Log

port = flag.Int("port", 9111, "The gRPC server port")
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
certFile = flag.String("cert_file", "", "The TLS cert file")
keyFile = flag.String("key_file", "", "The TLS key file")
port = flag.Int("port", 9111, "The gRPC server port")
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
certFile = flag.String("cert_file", "", "The TLS cert file")
keyFile = flag.String("key_file", "", "The TLS key file")
dbhost = flag.String("dbhost", "localhost", "host of postgreSQL")
dbport = flag.String("dbport", "5432", "port of postgreSQL")
dbuser = flag.String("dbuser", "postgres", "postgreSQL user")
dbpassword = flag.String("dbpassword", "password", "password for postgreSQL user")
)

func main() {
Expand Down Expand Up @@ -52,6 +56,11 @@ func main() {
}

s := grpc.NewServer(opts...)

dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=tks port=%s sslmode=disable TimeZone=Asia/Seoul",
*dbhost, *dbuser, *dbpassword, *dbport)
db, _ := gorm.Open(postgres.Open(dsn), &gorm.Config{})
app.Initialize(db)
pb.RegisterAppInfoServiceServer(s, &app.Server{})
pb.RegisterInfoServiceServer(s, &info.Server{})

Expand Down
203 changes: 69 additions & 134 deletions examples/application/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ import (
"context"
"flag"
"fmt"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

"github.com/google/uuid"
"github.com/sktelecom/tks-contract/pkg/log"
"github.com/sktelecom/tks-info/pkg/cert"
pb "github.com/sktelecom/tks-proto/pbgo"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)

var (
port = flag.Int("port", 9111, "The gRPC server port")
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
caFile = flag.String("ca_file", "", "The TLS ca file")
port = flag.Int("port", 9111, "The gRPC server port")
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
caFile = flag.String("ca_file", "", "The TLS ca file")
clusterID = uuid.New().String()
appGroupID string
)

func main() {
log.Info("Hello I'm a application client")
log.Info("new Cluster ID: ", clusterID)

flag.Parse()

Expand All @@ -46,155 +48,98 @@ func main() {
}
defer cc.Close()

doAddApp(cc)
doDeleteApp(cc)
doGetAppIDs(cc)
doGetAllAppsByClusterID(cc)
doGetAppsByName(cc)
doGetAppsByType(cc)
doGetApp(cc)
doCreateAppGroup(cc)
doGetAppGroupsByClusterID(cc)
doGetAppGroups(cc)
doGetAppGroup(cc)
doUpdateAppGroupStatus(cc)
doUpdateApp(cc)
doUpdateAppStatus(cc)
doUpdateEndpoints(cc)
doGetAppsByAppGroupID(cc)
doDeleteAppGroup(cc)
}

func doAddApp(cc *grpc.ClientConn) {
func doCreateAppGroup(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.AddAppRequest{
ClusterId: "ccc",
ServiceApp: &pb.ServiceApp{
AppName: "my_service_mesh",
Type: pb.AppType_SERVICE_MESH,
Owner: "ccc",
Status: pb.AppStatus_APP_RUNNING,
Endpoints: []*pb.Endpoint{
{
Type: pb.EpType_KIALI,
Url: "kiali.istio-system.svc.cluster.k1",
},
{
Type: pb.EpType_JAEGER,
Url: "jaeger.istio-system.svc.cluster.k1",
},
},
req := &pb.CreateAppGroupRequest{
ClusterId: clusterID,
AppGroup: &pb.AppGroup{
AppGroupName: "my_service_mesh",
Type: pb.AppGroupType_SERVICE_MESH,
Status: pb.AppGroupStatus_APP_GROUP_INSTALLING,
ExternalLabel: "service_mesh",
CreatedTs: timestamppb.New(time.Now()),
},
}
res, err := c.AddApp(context.Background(), req)
res, err := c.CreateAppGroup(context.Background(), req)
if err != nil {
log.Fatal("error while calling AddApp RPC", err)
log.Fatal("error while calling CreateAppGroup RPC", err)
}
log.Info("Response from AddApp: ", res.GetId())
appGroupID = res.GetId()
log.Info("Response from CreateAppGroup: ", res.GetId())
}

func doDeleteApp(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.DeleteAppRequest{
ClusterId: "ccc",
AppId: "111",
}
res, err := c.DeleteApp(context.Background(), req)
if err != nil {
log.Fatal("error while calling DeleteApp RPC", err)
}
log.Info("Response from DeleteApp: ", res.GetCode())
}

func doGetAppIDs(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.IDRequest{
Id: "ccc",
}
res, err := c.GetAppIDs(context.Background(), req)
if err != nil {
log.Fatal("error while calling GetAppId RPC", err)
}
log.Info("Response from GetAppId: ", res.GetIds())
}

func doGetAllAppsByClusterID(cc *grpc.ClientConn) {
func doGetAppGroupsByClusterID(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.IDRequest{
Id: "ccc",
Id: clusterID,
}
res, err := c.GetAllAppsByClusterID(context.Background(), req)
res, err := c.GetAppGroupsByClusterID(context.Background(), req)
if err != nil {
log.Fatal("error while calling GetAllAppsByClusterID RPC", err)
log.Fatal("error while calling GetAppGroupsByClusterID RPC", err)
}
log.Info("Response from GetAllAppsByClusterID: ", res)
log.Info("Response from GetAppGroupsByClusterID: ", res)
}

func doGetAppsByName(cc *grpc.ClientConn) {
func doGetAppGroups(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.GetAppsRequest{
ClusterId: "ccc",
AppName: "my",
req := &pb.GetAppGroupsRequest{
AppGroupName: "my_service_mesh",
Type: pb.AppGroupType_SERVICE_MESH,
}
res, err := c.GetAppsByName(context.Background(), req)
res, err := c.GetAppGroups(context.Background(), req)
if err != nil {
log.Fatal("error while calling GetAppsByName RPC", err)
log.Fatal("error while calling GetAppGroups RPC", err)
}
log.Info("Response from GetAppsByName: ", res)
log.Info("Response from GetAppGroups: ", res)
}

func doGetAppsByType(cc *grpc.ClientConn) {
func doGetAppGroup(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.GetAppsRequest{
ClusterId: "ccc",
Type: pb.AppType_SERVICE_MESH,
req := &pb.GetAppGroupRequest{
AppGroupId: appGroupID,
}
res, err := c.GetAppsByType(context.Background(), req)
res, err := c.GetAppGroup(context.Background(), req)
if err != nil {
log.Fatal("error while calling GetAppsByType RPC", err)
log.Fatal("error while calling GetAppGroup RPC", err)
}
log.Info("Response from GetAppsByType: ", res)
log.Info("Response from GetAppGroup: ", res)
}

func doGetApp(cc *grpc.ClientConn) {
func doUpdateAppGroupStatus(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.GetAppRequest{
ClusterId: "ccc",
AppId: "111",
req := &pb.UpdateAppGroupStatusRequest{
AppGroupId: appGroupID,
Status: pb.AppGroupStatus_APP_GROUP_ERROR,
}
res, err := c.GetApp(context.Background(), req)
res, err := c.UpdateAppGroupStatus(context.Background(), req)
if err != nil {
log.Fatal("error while calling GetApp RPC", err)
log.Fatal("error while calling UpdateAppGroupStatus RPC", err)
}
log.Info("Response from GetApp: ", res)
log.Info("Response from UpdateAppGroupStatus: ", res)
}

func doUpdateApp(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.UpdateAppRequest{
ClusterId: "ccc",
ServiceApp: &pb.ServiceApp{
AppId: "111",
AppName: "my_service_mesh",
Type: pb.AppType_SERVICE_MESH,
Owner: "ccc",
Status: pb.AppStatus_APP_RUNNING,
Endpoints: []*pb.Endpoint{
{
Type: pb.EpType_KIALI,
Url: "kiali.istio-system.svc.cluster.k1",
},
{
Type: pb.EpType_JAEGER,
Url: "jaeger.istio-system.svc.cluster.k1",
},
},
ExternalLabel: "service_mesh",
LastUpdatedTs: timestamppb.New(time.Now()),
},
AppGroupId: appGroupID,
AppType: pb.AppType_KIALI,
Endpoint: "https://localhost:20001",
Metadata: "",
}
res, err := c.UpdateApp(context.Background(), req)
if err != nil {
Expand All @@ -203,41 +148,31 @@ func doUpdateApp(cc *grpc.ClientConn) {
log.Info("Response from UpdateApp: ", res.GetCode())
}

func doUpdateAppStatus(cc *grpc.ClientConn) {
func doGetAppsByAppGroupID(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.UpdateAppStatusRequest{
ClusterId: "ccc",
AppId: "111",
Status: pb.AppStatus_APP_RUNNING,
req := &pb.IDRequest{
Id: appGroupID,
}
res, err := c.UpdateAppStatus(context.Background(), req)
res, err := c.GetAppsByAppGroupID(context.Background(), req)
if err != nil {
log.Fatal("error while calling UpdateAppStatus RPC", err)
log.Fatal("error while calling GetAppsByAppGroupID RPC", err)
}
log.Info("Response from GetAppsByAppGroupID: ")
for _, app := range res.Apps {
log.Info(fmt.Sprintf("id %s, type %d, endpoint %s", app.AppId, app.Type, app.Endpoint))
}
log.Info("Response from UpdateAppStatus: ", res.GetCode())
}

func doUpdateEndpoints(cc *grpc.ClientConn) {
func doDeleteAppGroup(cc *grpc.ClientConn) {
c := pb.NewAppInfoServiceClient(cc)

req := &pb.UpdateEndpointsRequest{
ClusterId: "ccc",
AppId: "111",
Endpoints: []*pb.Endpoint{
{
Type: pb.EpType_KIALI,
Url: "kiali.istio-system.svc.cluster.k1",
},
{
Type: pb.EpType_JAEGER,
Url: "jaeger.istio-system.svc.cluster.k1",
},
},
req := &pb.DeleteAppGroupRequest{
AppGroupId: appGroupID,
}
res, err := c.UpdateEndpoints(context.Background(), req)
res, err := c.DeleteAppGroup(context.Background(), req)
if err != nil {
log.Fatal("error while calling UpdateEndpoints RPC", err)
log.Fatal("error while calling DeleteApp RPC", err)
}
log.Info("Response from UpdateEndpoints: ", res.GetCode())
log.Info("Response from DeleteApp: ", res.GetCode())
}
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ go 1.16

require (
github.com/google/uuid v1.2.0
github.com/lib/pq v1.10.2 // indirect
github.com/sktelecom/tks-contract v0.1.0
github.com/sktelecom/tks-proto v0.0.4-0.20210422054948-9d4426f0b601
google.golang.org/grpc v1.37.0
github.com/sktelecom/tks-proto v0.0.5-0.20210601070539-28e30ba879cd
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
gorm.io/datatypes v1.0.1
gorm.io/driver/postgres v1.1.0
gorm.io/gorm v1.21.10
)

replace github.com/sktelecom/tks-info => ./
Loading