From f044a504505aa20bff89cbdbbc3309812e0e9986 Mon Sep 17 00:00:00 2001 From: Bharath Horatti Date: Wed, 4 Oct 2023 12:58:15 +0530 Subject: [PATCH] feat(): Initial commit for slicegw edge server Signed-off-by: Bharath Horatti --- Dockerfile | 63 +++++ go.mod | 26 ++ go.sum | 66 +++++ main.go | 103 ++++++++ pkg/edgeservice/edge_server.go | 197 ++++++++++++++ pkg/edgeservice/edgeservice.pb.go | 351 +++++++++++++++++++++++++ pkg/edgeservice/edgeservice.proto | 45 ++++ pkg/edgeservice/edgeservice_grpc.pb.go | 107 ++++++++ pkg/logger/logger.go | 119 +++++++++ 9 files changed, 1077 insertions(+) create mode 100644 Dockerfile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 pkg/edgeservice/edge_server.go create mode 100644 pkg/edgeservice/edgeservice.pb.go create mode 100644 pkg/edgeservice/edgeservice.proto create mode 100644 pkg/edgeservice/edgeservice_grpc.pb.go create mode 100644 pkg/logger/logger.go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bf5d686 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,63 @@ +########################################################## +#Dockerfile +#Copyright (c) 2022 Avesha, Inc. All rights reserved. +# +#SPDX-License-Identifier: Apache-2.0 +# +#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. +########################################################## + +FROM amd64/golang:1.21.0-alpine3.17 as gobuilder + +# Install git. + +# Git is required for fetching the dependencies. + +RUN apk update && apk add --no-cache git make build-base + +# Set the Go source path + +WORKDIR / + +COPY . . + +# Build the binary. + +RUN go mod download &&\ + go env -w GOPRIVATE=github.com/kubeslice && \ + CGO_ENABLED=1 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o bin/kubeslice-gateway-edge main.go + + +# Build reduced image from base alpine + +FROM amd64/alpine:3.15 + +# tc - is needed for traffic control and shaping on the sidecar. it is part of the iproute2 + +RUN apk add --no-cache ca-certificates \ + iproute2 + +RUN apk add --no-cache ca-certificates \ + iptables + +# Copy our static executable. + +COPY --from=gobuilder bin/kubeslice-gateway-edge . + +EXPOSE 5000 + +EXPOSE 8080 + +# Or could be CMD + +ENTRYPOINT ["./kubeslice-gateway-edge"] \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b4a2553 --- /dev/null +++ b/go.mod @@ -0,0 +1,26 @@ +module github.com/kubeslice/slicegw-edge + +go 1.21 + +require ( + github.com/coreos/go-iptables v0.7.0 + github.com/lorenzosaino/go-sysctl v0.3.1 + go.uber.org/zap v1.25.0 + google.golang.org/grpc v1.58.0 + google.golang.org/protobuf v1.31.0 +) + +require ( + github.com/BurntSushi/toml v1.1.0 // indirect + github.com/golang/protobuf v1.5.3 // indirect + go.uber.org/multierr v1.10.0 // indirect + golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d // indirect + golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect + golang.org/x/mod v0.8.0 // indirect + golang.org/x/net v0.12.0 // indirect + golang.org/x/sys v0.10.0 // indirect + golang.org/x/text v0.11.0 // indirect + golang.org/x/tools v0.6.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect + honnef.co/go/tools v0.3.2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6410603 --- /dev/null +++ b/go.sum @@ -0,0 +1,66 @@ +github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I= +github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= +github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/coreos/go-iptables v0.7.0 h1:XWM3V+MPRr5/q51NuWSgU0fqMad64Zyxs8ZUoMsamr8= +github.com/coreos/go-iptables v0.7.0/go.mod h1:Qe8Bv2Xik5FyTXwgIbLAnv2sWSBmvWdFETJConOQ//Q= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/lorenzosaino/go-sysctl v0.3.1 h1:3phX80tdITw2fJjZlwbXQnDWs4S30beNcMbw0cn0HtY= +github.com/lorenzosaino/go-sysctl v0.3.1/go.mod h1:5grcsBRpspKknNS1qzt1eIeRDLrhpKZAtz8Fcuvs1Rc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c= +go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d h1:+W8Qf4iJtMGKkyAygcKohjxTk4JPsL9DpzApJ22m5Ic= +golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50= +golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 h1:bVf09lpb+OJbByTj913DRJioFFAjf/ZGxEz7MajTp2U= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM= +google.golang.org/grpc v1.58.0 h1:32JY8YpPMSR45K+c3o6b8VL73V+rR8k+DeMIr4vRH8o= +google.golang.org/grpc v1.58.0/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.3.2 h1:ytYb4rOqyp1TSa2EPvNVwtPQJctSELKaMyLfqNP4+34= +honnef.co/go/tools v0.3.2/go.mod h1:jzwdWgg7Jdq75wlfblQxO4neNaFFSvgc1tD5Wv8U0Yw= diff --git a/main.go b/main.go new file mode 100644 index 0000000..71131c0 --- /dev/null +++ b/main.go @@ -0,0 +1,103 @@ +/* Copyright (c) 2022 Avesha, Inc. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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" + "net" + "os" + "os/signal" + "sync" + "syscall" + + "github.com/kubeslice/slicegw-edge/pkg/edgeservice" + "github.com/kubeslice/slicegw-edge/pkg/logger" + + sysctl "github.com/lorenzosaino/go-sysctl" + "google.golang.org/grpc" +) + +var ( + log *logger.Logger = logger.NewLogger() +) + +func startGrpcServer(grpcPort string) error { + address := fmt.Sprintf(":%s", grpcPort) + log.Infof("Starting GRPC Server for SliceGw-Edge Pod at %v", address) + + ln, err := net.Listen("tcp", address) + if err != nil { + log.Errorf("Unable to listen on address: %v, err: %v", address, err.Error()) + return err + } + + srv := grpc.NewServer() + edgeservice.RegisterGwEdgeServiceServer(srv, &edgeservice.GwEdgeService{}) + + err = srv.Serve(ln) + if err != nil { + log.Errorf("Failed to start GRPC server: %v", err.Error()) + return err + } + + log.Infof("GRPC Server is exiting...") + + return nil +} + +// shutdownHandler triggers application shutdown. +func shutdownHandler(wg *sync.WaitGroup) { + // signChan channel is used to transmit signal notifications. + signChan := make(chan os.Signal, 1) + // Catch and relay certain signal(s) to signChan channel. + signal.Notify(signChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + // Blocking until a signal is sent over signChan channel. Progress to + // next line after signal + sig := <-signChan + log.Infof("Teardown started with ", sig, "signal") + + wg.Done() +} + +func main() { + var grpcPort string = "5000" + + // Get value of a net.ipv4.ip_forward using sysctl + val, err := sysctl.Get("net.ipv4.ip_forward") + if err != nil { + log.Fatalf("Retrive of ipv4.ip_forward errored %v", err) + } + if val != "1" { + // Set value of a net.ipv4.ip_forward to 1 using sysctl + err = sysctl.Set("net.ipv4.ip_forward", "1") + if err != nil { + log.Fatalf("Set of ipv4.ip_forward errored %v", err) + } + } + wg := &sync.WaitGroup{} + wg.Add(2) + + // Start the GRPC Server to communicate with slice controller. + go startGrpcServer(grpcPort) + + go shutdownHandler(wg) + + wg.Wait() + log.Infof("Slice Gateway Edge Server is exiting") +} diff --git a/pkg/edgeservice/edge_server.go b/pkg/edgeservice/edge_server.go new file mode 100644 index 0000000..6518d3c --- /dev/null +++ b/pkg/edgeservice/edge_server.go @@ -0,0 +1,197 @@ +package edgeservice + +import ( + "context" + "fmt" + "strings" + + "github.com/kubeslice/slicegw-edge/pkg/logger" + + "github.com/coreos/go-iptables/iptables" +) + +var ( + log *logger.Logger = logger.NewLogger() +) + +type GwEdgeService struct { + UnimplementedGwEdgeServiceServer +} + +type serviceInfo struct { + svcIP string + nodePort uint32 + targetPort uint32 +} + +// TODO: Change this to sync.Map +var serviceMap map[string]serviceInfo + +func updateNeeded(svcList []*SliceGwServiceInfo) bool { + if len(serviceMap) != len(svcList) { + return true + } + + for _, svcInfo := range svcList { + if updateNeededForSvc(svcInfo.GwSvcName, + serviceInfo{ + svcIP: svcInfo.GwSvcClusterIP, + nodePort: svcInfo.GwSvcNodePort, + targetPort: svcInfo.GwSvcTargetPort, + }) { + return true + } + } + + return false +} + +func updateNeededForSvc(svcName string, svcInfo serviceInfo) bool { + cachedInfo, found := serviceMap[svcName] + if !found { + return true + } + + if cachedInfo.svcIP != svcInfo.svcIP || cachedInfo.nodePort != svcInfo.nodePort || cachedInfo.targetPort != svcInfo.targetPort { + return true + } + + return false +} + +func deleteIpTablesRule(svcInfo serviceInfo) error { + ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4) + if err != nil { + log.Error(err, "Failed to init iptables handle") + return err + } + + rulespec := fmt.Sprintf("-p udp --dport %d -j DNAT --to-destination %s:%d", svcInfo.nodePort, svcInfo.svcIP, svcInfo.targetPort) + err = ipt.DeleteIfExists("nat", "PREROUTING", strings.Split(rulespec, " ")...) + if err != nil { + return err + } + + return nil +} + +func deleteIpTablesRuleForSvc(svcName string) error { + cachedInfo, found := serviceMap[svcName] + if !found { + return nil + } + + err := deleteIpTablesRule(cachedInfo) + if err != nil { + return err + } + + delete(serviceMap, svcName) + + return nil +} + +func addIpTablesRuleForSvc(svcName string, svcInfo serviceInfo) error { + ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4) + if err != nil { + log.Error(err, "Failed to init iptables handle") + return err + } + + // Be careful (learned it the hard way) while drafting the rulespec. Even a single unwanted, benign + // space will result in failed iptables API call. + rulespec := fmt.Sprintf("-p udp --dport %d -j DNAT --to-destination %s:%d", svcInfo.nodePort, svcInfo.svcIP, svcInfo.targetPort) + err = ipt.AppendUnique("nat", "PREROUTING", strings.Split(rulespec, " ")...) + if err != nil { + log.Error(err, "Failed to add iptables rule", "rulespec", rulespec) + return nil + } + + serviceMap[svcName] = svcInfo + + return nil +} + +func addIpTablesMasqRule() error { + ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4) + if err != nil { + log.Error(err, "Failed to init iptables handle") + return err + } + + // Be careful while drafting the rulespec. Even a single unwanted, benign + // space will result in failed iptables API call. + rulespec := fmt.Sprintf("-o %s -j MASQUERADE", "eth0") + err = ipt.AppendUnique("nat", "POSTROUTING", strings.Split(rulespec, " ")...) + if err != nil { + log.Error(err, "Failed to add iptables SNAT rule", "rulespec", rulespec) + return nil + } + + return nil +} + +func (s *GwEdgeService) UpdateSliceGwServiceMap(ctx context.Context, in *SliceGwServiceMap) (*GwEdgeResponse, error) { + log.Info("Received update from operator", "servicemap", in.SliceGwServiceList) + + if serviceMap == nil { + serviceMap = make(map[string]serviceInfo) + } + + if !updateNeeded(in.SliceGwServiceList) { + return &GwEdgeResponse{StatusMsg: "Success"}, nil + } + + // Check if any rule needs to be deleted. Service info present in cache but missing in the update message. + for svcName, cachedInfo := range serviceMap { + found := false + for _, svcInfo := range in.SliceGwServiceList { + if svcInfo.GwSvcName == svcName { + found = true + break + } + } + + if !found { + err := deleteIpTablesRuleForSvc(svcName) + if err != nil { + log.Error(err, "Failed to remove iptables rule for svc", "svcinfo", cachedInfo) + return &GwEdgeResponse{StatusMsg: "Failed to update iptables"}, err + } + } + } + + for _, sliceGwSvcInfo := range in.SliceGwServiceList { + nodePort := sliceGwSvcInfo.GwSvcNodePort + targetPort := sliceGwSvcInfo.GwSvcTargetPort + svcIP := sliceGwSvcInfo.GwSvcClusterIP + + // Check if an update is needed for this svc + if !updateNeededForSvc(sliceGwSvcInfo.GwSvcName, serviceInfo{ + svcIP: svcIP, + nodePort: nodePort, + targetPort: targetPort, + }) { + continue + } + + // Delete if rule is present for this svc + err := deleteIpTablesRuleForSvc(sliceGwSvcInfo.GwSvcName) + if err != nil { + log.Error(err, "Failed to remove iptables rule for svc", "svcName", sliceGwSvcInfo.GwSvcName) + return &GwEdgeResponse{StatusMsg: "Failed to update iptables"}, err + } + + err = addIpTablesRuleForSvc(sliceGwSvcInfo.GwSvcName, serviceInfo{ + svcIP: svcIP, + nodePort: nodePort, + targetPort: targetPort, + }) + if err != nil { + log.Error(err, "Failed to add iptables rule", "svcName", sliceGwSvcInfo.GwSvcName) + return &GwEdgeResponse{StatusMsg: "Failed to add rule to iptables"}, err + } + } + + return &GwEdgeResponse{StatusMsg: "Success"}, nil +} diff --git a/pkg/edgeservice/edgeservice.pb.go b/pkg/edgeservice/edgeservice.pb.go new file mode 100644 index 0000000..70ce366 --- /dev/null +++ b/pkg/edgeservice/edgeservice.pb.go @@ -0,0 +1,351 @@ +// Copyright (c) 2022 Avesha, Inc. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// +// 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. + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v4.24.3 +// source: edgeservice/edgeservice.proto + +package edgeservice + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// GwEdgeResponse represents the Sidecar response format. +type GwEdgeResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + StatusMsg string `protobuf:"bytes,1,opt,name=statusMsg,proto3" json:"statusMsg,omitempty"` +} + +func (x *GwEdgeResponse) Reset() { + *x = GwEdgeResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_edgeservice_edgeservice_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GwEdgeResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GwEdgeResponse) ProtoMessage() {} + +func (x *GwEdgeResponse) ProtoReflect() protoreflect.Message { + mi := &file_edgeservice_edgeservice_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GwEdgeResponse.ProtoReflect.Descriptor instead. +func (*GwEdgeResponse) Descriptor() ([]byte, []int) { + return file_edgeservice_edgeservice_proto_rawDescGZIP(), []int{0} +} + +func (x *GwEdgeResponse) GetStatusMsg() string { + if x != nil { + return x.StatusMsg + } + return "" +} + +type SliceGwServiceInfo struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + GwName string `protobuf:"bytes,1,opt,name=GwName,proto3" json:"GwName,omitempty"` + GwSvcName string `protobuf:"bytes,2,opt,name=GwSvcName,proto3" json:"GwSvcName,omitempty"` + GwSvcClusterIP string `protobuf:"bytes,3,opt,name=GwSvcClusterIP,proto3" json:"GwSvcClusterIP,omitempty"` + GwSvcTargetPort uint32 `protobuf:"varint,4,opt,name=GwSvcTargetPort,proto3" json:"GwSvcTargetPort,omitempty"` + GwSvcNodePort uint32 `protobuf:"varint,5,opt,name=GwSvcNodePort,proto3" json:"GwSvcNodePort,omitempty"` +} + +func (x *SliceGwServiceInfo) Reset() { + *x = SliceGwServiceInfo{} + if protoimpl.UnsafeEnabled { + mi := &file_edgeservice_edgeservice_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SliceGwServiceInfo) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SliceGwServiceInfo) ProtoMessage() {} + +func (x *SliceGwServiceInfo) ProtoReflect() protoreflect.Message { + mi := &file_edgeservice_edgeservice_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SliceGwServiceInfo.ProtoReflect.Descriptor instead. +func (*SliceGwServiceInfo) Descriptor() ([]byte, []int) { + return file_edgeservice_edgeservice_proto_rawDescGZIP(), []int{1} +} + +func (x *SliceGwServiceInfo) GetGwName() string { + if x != nil { + return x.GwName + } + return "" +} + +func (x *SliceGwServiceInfo) GetGwSvcName() string { + if x != nil { + return x.GwSvcName + } + return "" +} + +func (x *SliceGwServiceInfo) GetGwSvcClusterIP() string { + if x != nil { + return x.GwSvcClusterIP + } + return "" +} + +func (x *SliceGwServiceInfo) GetGwSvcTargetPort() uint32 { + if x != nil { + return x.GwSvcTargetPort + } + return 0 +} + +func (x *SliceGwServiceInfo) GetGwSvcNodePort() uint32 { + if x != nil { + return x.GwSvcNodePort + } + return 0 +} + +type SliceGwServiceMap struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + SliceName string `protobuf:"bytes,1,opt,name=sliceName,proto3" json:"sliceName,omitempty"` + SliceGwServiceList []*SliceGwServiceInfo `protobuf:"bytes,2,rep,name=sliceGwServiceList,proto3" json:"sliceGwServiceList,omitempty"` +} + +func (x *SliceGwServiceMap) Reset() { + *x = SliceGwServiceMap{} + if protoimpl.UnsafeEnabled { + mi := &file_edgeservice_edgeservice_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SliceGwServiceMap) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SliceGwServiceMap) ProtoMessage() {} + +func (x *SliceGwServiceMap) ProtoReflect() protoreflect.Message { + mi := &file_edgeservice_edgeservice_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SliceGwServiceMap.ProtoReflect.Descriptor instead. +func (*SliceGwServiceMap) Descriptor() ([]byte, []int) { + return file_edgeservice_edgeservice_proto_rawDescGZIP(), []int{2} +} + +func (x *SliceGwServiceMap) GetSliceName() string { + if x != nil { + return x.SliceName + } + return "" +} + +func (x *SliceGwServiceMap) GetSliceGwServiceList() []*SliceGwServiceInfo { + if x != nil { + return x.SliceGwServiceList + } + return nil +} + +var File_edgeservice_edgeservice_proto protoreflect.FileDescriptor + +var file_edgeservice_edgeservice_proto_rawDesc = []byte{ + 0x0a, 0x1d, 0x65, 0x64, 0x67, 0x65, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x65, 0x64, + 0x67, 0x65, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, + 0x0b, 0x65, 0x64, 0x67, 0x65, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x2e, 0x0a, 0x0e, + 0x47, 0x77, 0x45, 0x64, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1c, + 0x0a, 0x09, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x4d, 0x73, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x09, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x4d, 0x73, 0x67, 0x22, 0xc2, 0x01, 0x0a, + 0x12, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x47, 0x77, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, + 0x6e, 0x66, 0x6f, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x77, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x47, 0x77, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x47, + 0x77, 0x53, 0x76, 0x63, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, + 0x47, 0x77, 0x53, 0x76, 0x63, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x26, 0x0a, 0x0e, 0x47, 0x77, 0x53, + 0x76, 0x63, 0x43, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x49, 0x50, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x0e, 0x47, 0x77, 0x53, 0x76, 0x63, 0x43, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x49, + 0x50, 0x12, 0x28, 0x0a, 0x0f, 0x47, 0x77, 0x53, 0x76, 0x63, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, + 0x50, 0x6f, 0x72, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0f, 0x47, 0x77, 0x53, 0x76, + 0x63, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x50, 0x6f, 0x72, 0x74, 0x12, 0x24, 0x0a, 0x0d, 0x47, + 0x77, 0x53, 0x76, 0x63, 0x4e, 0x6f, 0x64, 0x65, 0x50, 0x6f, 0x72, 0x74, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x0d, 0x52, 0x0d, 0x47, 0x77, 0x53, 0x76, 0x63, 0x4e, 0x6f, 0x64, 0x65, 0x50, 0x6f, 0x72, + 0x74, 0x22, 0x82, 0x01, 0x0a, 0x11, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x47, 0x77, 0x53, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x4d, 0x61, 0x70, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x6c, 0x69, 0x63, 0x65, + 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x6c, 0x69, 0x63, + 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x4f, 0x0a, 0x12, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x47, 0x77, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x18, 0x02, 0x20, 0x03, 0x28, + 0x0b, 0x32, 0x1f, 0x2e, 0x65, 0x64, 0x67, 0x65, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, + 0x53, 0x6c, 0x69, 0x63, 0x65, 0x47, 0x77, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x49, 0x6e, + 0x66, 0x6f, 0x52, 0x12, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x47, 0x77, 0x53, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x32, 0x69, 0x0a, 0x0d, 0x47, 0x77, 0x45, 0x64, 0x67, 0x65, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x58, 0x0a, 0x17, 0x55, 0x70, 0x64, 0x61, 0x74, + 0x65, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x47, 0x77, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, + 0x61, 0x70, 0x12, 0x1e, 0x2e, 0x65, 0x64, 0x67, 0x65, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x2e, 0x53, 0x6c, 0x69, 0x63, 0x65, 0x47, 0x77, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4d, + 0x61, 0x70, 0x1a, 0x1b, 0x2e, 0x65, 0x64, 0x67, 0x65, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x2e, 0x47, 0x77, 0x45, 0x64, 0x67, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, + 0x00, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x3b, 0x65, 0x64, 0x67, 0x65, 0x73, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_edgeservice_edgeservice_proto_rawDescOnce sync.Once + file_edgeservice_edgeservice_proto_rawDescData = file_edgeservice_edgeservice_proto_rawDesc +) + +func file_edgeservice_edgeservice_proto_rawDescGZIP() []byte { + file_edgeservice_edgeservice_proto_rawDescOnce.Do(func() { + file_edgeservice_edgeservice_proto_rawDescData = protoimpl.X.CompressGZIP(file_edgeservice_edgeservice_proto_rawDescData) + }) + return file_edgeservice_edgeservice_proto_rawDescData +} + +var file_edgeservice_edgeservice_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_edgeservice_edgeservice_proto_goTypes = []interface{}{ + (*GwEdgeResponse)(nil), // 0: edgeservice.GwEdgeResponse + (*SliceGwServiceInfo)(nil), // 1: edgeservice.SliceGwServiceInfo + (*SliceGwServiceMap)(nil), // 2: edgeservice.SliceGwServiceMap +} +var file_edgeservice_edgeservice_proto_depIdxs = []int32{ + 1, // 0: edgeservice.SliceGwServiceMap.sliceGwServiceList:type_name -> edgeservice.SliceGwServiceInfo + 2, // 1: edgeservice.GwEdgeService.UpdateSliceGwServiceMap:input_type -> edgeservice.SliceGwServiceMap + 0, // 2: edgeservice.GwEdgeService.UpdateSliceGwServiceMap:output_type -> edgeservice.GwEdgeResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_edgeservice_edgeservice_proto_init() } +func file_edgeservice_edgeservice_proto_init() { + if File_edgeservice_edgeservice_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_edgeservice_edgeservice_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GwEdgeResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_edgeservice_edgeservice_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SliceGwServiceInfo); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_edgeservice_edgeservice_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SliceGwServiceMap); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_edgeservice_edgeservice_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_edgeservice_edgeservice_proto_goTypes, + DependencyIndexes: file_edgeservice_edgeservice_proto_depIdxs, + MessageInfos: file_edgeservice_edgeservice_proto_msgTypes, + }.Build() + File_edgeservice_edgeservice_proto = out.File + file_edgeservice_edgeservice_proto_rawDesc = nil + file_edgeservice_edgeservice_proto_goTypes = nil + file_edgeservice_edgeservice_proto_depIdxs = nil +} diff --git a/pkg/edgeservice/edgeservice.proto b/pkg/edgeservice/edgeservice.proto new file mode 100644 index 0000000..2ee0479 --- /dev/null +++ b/pkg/edgeservice/edgeservice.proto @@ -0,0 +1,45 @@ +/* Copyright (c) 2022 Avesha, Inc. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + */ + +syntax = "proto3"; + +package edgeservice; +option go_package = ".;edgeservice"; + + +// GwEdgeResponse represents the Sidecar response format. +message GwEdgeResponse { + string statusMsg = 1; +} + +message SliceGwServiceInfo { + string GwName = 1; + string GwSvcName = 2; + string GwSvcClusterIP = 3; + uint32 GwSvcTargetPort = 4; + uint32 GwSvcNodePort = 5; +} + +message SliceGwServiceMap { + string sliceName = 1; + repeated SliceGwServiceInfo sliceGwServiceList = 2; +} + +service GwEdgeService { + // Interface to get slice gateway service info + rpc UpdateSliceGwServiceMap(SliceGwServiceMap) returns (GwEdgeResponse) {} +} \ No newline at end of file diff --git a/pkg/edgeservice/edgeservice_grpc.pb.go b/pkg/edgeservice/edgeservice_grpc.pb.go new file mode 100644 index 0000000..d92d31e --- /dev/null +++ b/pkg/edgeservice/edgeservice_grpc.pb.go @@ -0,0 +1,107 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v4.24.3 +// source: edgeservice/edgeservice.proto + +package edgeservice + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// GwEdgeServiceClient is the client API for GwEdgeService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type GwEdgeServiceClient interface { + // Interface to get slice gateway service info + UpdateSliceGwServiceMap(ctx context.Context, in *SliceGwServiceMap, opts ...grpc.CallOption) (*GwEdgeResponse, error) +} + +type gwEdgeServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewGwEdgeServiceClient(cc grpc.ClientConnInterface) GwEdgeServiceClient { + return &gwEdgeServiceClient{cc} +} + +func (c *gwEdgeServiceClient) UpdateSliceGwServiceMap(ctx context.Context, in *SliceGwServiceMap, opts ...grpc.CallOption) (*GwEdgeResponse, error) { + out := new(GwEdgeResponse) + err := c.cc.Invoke(ctx, "/edgeservice.GwEdgeService/UpdateSliceGwServiceMap", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// GwEdgeServiceServer is the server API for GwEdgeService service. +// All implementations must embed UnimplementedGwEdgeServiceServer +// for forward compatibility +type GwEdgeServiceServer interface { + // Interface to get slice gateway service info + UpdateSliceGwServiceMap(context.Context, *SliceGwServiceMap) (*GwEdgeResponse, error) + mustEmbedUnimplementedGwEdgeServiceServer() +} + +// UnimplementedGwEdgeServiceServer must be embedded to have forward compatible implementations. +type UnimplementedGwEdgeServiceServer struct { +} + +func (UnimplementedGwEdgeServiceServer) UpdateSliceGwServiceMap(context.Context, *SliceGwServiceMap) (*GwEdgeResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method UpdateSliceGwServiceMap not implemented") +} +func (UnimplementedGwEdgeServiceServer) mustEmbedUnimplementedGwEdgeServiceServer() {} + +// UnsafeGwEdgeServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to GwEdgeServiceServer will +// result in compilation errors. +type UnsafeGwEdgeServiceServer interface { + mustEmbedUnimplementedGwEdgeServiceServer() +} + +func RegisterGwEdgeServiceServer(s grpc.ServiceRegistrar, srv GwEdgeServiceServer) { + s.RegisterService(&GwEdgeService_ServiceDesc, srv) +} + +func _GwEdgeService_UpdateSliceGwServiceMap_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SliceGwServiceMap) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GwEdgeServiceServer).UpdateSliceGwServiceMap(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/edgeservice.GwEdgeService/UpdateSliceGwServiceMap", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GwEdgeServiceServer).UpdateSliceGwServiceMap(ctx, req.(*SliceGwServiceMap)) + } + return interceptor(ctx, in, info, handler) +} + +// GwEdgeService_ServiceDesc is the grpc.ServiceDesc for GwEdgeService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var GwEdgeService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "edgeservice.GwEdgeService", + HandlerType: (*GwEdgeServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "UpdateSliceGwServiceMap", + Handler: _GwEdgeService_UpdateSliceGwServiceMap_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "edgeservice/edgeservice.proto", +} diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go new file mode 100644 index 0000000..2f5718d --- /dev/null +++ b/pkg/logger/logger.go @@ -0,0 +1,119 @@ +/* Copyright (c) 2022 Avesha, Inc. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 logger + +import ( + "os" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +var GlobalLogger *Logger + +// Logger : Logger type +type Logger struct { + handle *zap.SugaredLogger +} + +// Debugf : Log level type Debugf +func (logger *Logger) Debugf(format string, args ...interface{}) { + logger.handle.Debugf(format, args...) +} + +// Infof : Log level type Infof +func (logger *Logger) Infof(format string, args ...interface{}) { + logger.handle.Infof(format, args...) +} + +// Errorf : Log level type Errorf +func (logger *Logger) Errorf(format string, args ...interface{}) { + logger.handle.Errorf(format, args...) +} + +// Fatalf : Log level type Fatalf +func (logger *Logger) Fatalf(format string, args ...interface{}) { + logger.handle.Fatalf(format, args...) +} + +// Panicf : Log level type Panicf +func (logger *Logger) Panicf(format string, args ...interface{}) { + logger.handle.Panicf(format, args...) +} + +// Debug : Log level type Debug +func (logger *Logger) Debug(args ...interface{}) { + logger.handle.Debug(args...) +} + +// Info : Log level type Info +func (logger *Logger) Info(args ...interface{}) { + logger.handle.Info(args...) +} + +// Warn : Log level type Warn +func (logger *Logger) Warn(args ...interface{}) { + logger.handle.Warn(args...) +} + +// Error : Log level type Error +func (logger *Logger) Error(args ...interface{}) { + logger.handle.Error(args...) +} + +// Fatal : Log level type Fatal +func (logger *Logger) Fatal(args ...interface{}) { + logger.handle.Fatal(args...) +} + +// Panic : Log level type Panic +func (logger *Logger) Panic(args ...interface{}) { + logger.handle.Panic(args...) +} + +// NewLogger creates the new logger object. +func NewLogger() *Logger { + logLevel := os.Getenv("LOG_LEVEL") + if logLevel == "" { + logLevel = "INFO" + } + + logLevelMap := map[string]zapcore.Level{ + "DEBUG": zapcore.DebugLevel, + "INFO": zapcore.InfoLevel, + "ERROR": zapcore.ErrorLevel, + "WARN": zapcore.WarnLevel, + "FATAL": zapcore.FatalLevel, + "PANIC": zapcore.PanicLevel} + + logLvl := logLevelMap[logLevel] + + encoderConfig := zap.NewProductionEncoderConfig() + encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder + + consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig) + core := zapcore.NewTee( + zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), logLvl), + ) + logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1)).Sugar() + + defer logger.Sync() + + return &Logger{logger} +}