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

Return active configs on registration #22

Merged
merged 3 commits into from
Jul 17, 2024
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
6 changes: 3 additions & 3 deletions service/.golangci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
run:
skip-dirs:
- ^tratcontroller/.*$
- ^tratcontroller$
- tratcontroller
- ^tratteriacontroller/.*$
- ^tratteriacontroller$
- tratteriacontroller

linters:
disable-all: true
Expand Down
8 changes: 6 additions & 2 deletions service/api/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ func (h *Handlers) RegistrationHandler(w http.ResponseWriter, r *http.Request) {

h.Logger.Info("Received a registration request.", zap.String("service", serviceName))

registrationResponse := h.Service.RegisterAgent(registrationRequest.IpAddress, registrationRequest.Port, serviceName, registrationRequest.NameSpace)
registrationResponse, err := h.Service.RegisterAgent(registrationRequest.IpAddress, registrationRequest.Port, serviceName, registrationRequest.NameSpace)
if err != nil {
h.Logger.Error("Failed to register service.", zap.Error(err))
http.Error(w, "Failed to register service", http.StatusInternalServerError)

// TODO: return rules belonging to this service
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand Down
39 changes: 32 additions & 7 deletions service/api/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/spiffe/go-spiffe/v2/workloadapi"
"github.com/tratteria/tconfigd/common"
"github.com/tratteria/tconfigd/dataplaneregistry"
"github.com/tratteria/tconfigd/tratteriacontroller"
tratteria1alpha1 "github.com/tratteria/tconfigd/tratteriacontroller/pkg/apis/tratteria/v1alpha1"
"go.uber.org/zap"
)

Expand All @@ -21,31 +23,54 @@ const (

type Service struct {
dataPlaneRegistryManager dataplaneregistry.Manager
tratteriaController *tratteriacontroller.TratteriaController
x509Source *workloadapi.X509Source
tratteriaSpiffeId spiffeid.ID
logger *zap.Logger
}

func NewService(dataPlaneRegistryManager dataplaneregistry.Manager, x509Source *workloadapi.X509Source, tratteriaSpiffeId spiffeid.ID, logger *zap.Logger) *Service {
func NewService(dataPlaneRegistryManager dataplaneregistry.Manager, tratteriaController *tratteriacontroller.TratteriaController, x509Source *workloadapi.X509Source, tratteriaSpiffeId spiffeid.ID, logger *zap.Logger) *Service {
return &Service{
dataPlaneRegistryManager: dataPlaneRegistryManager,
tratteriaController: tratteriaController,
x509Source: x509Source,
tratteriaSpiffeId: tratteriaSpiffeId,
logger: logger,
}
}

type registrationResponse struct {
HeartBeatIntervalMinutes int `json:"heartBeatIntervalMinutes"`
HeartBeatIntervalMinutes int `json:"heartBeatIntervalMinutes"`
VerificationRules *tratteria1alpha1.VerificationRules `json:"verificationRules,omitempty"`
GenerationRules *tratteria1alpha1.GenerationRules `json:"generationRules,omitempty"`
}

func (s *Service) RegisterAgent(ipaddress string, port int, serviceName string, namespace string) *registrationResponse {
// TODO: return rules belonging to the service
s.dataPlaneRegistryManager.Register(ipaddress, port, serviceName, namespace)

return &registrationResponse{
func (s *Service) RegisterAgent(ipaddress string, port int, serviceName string, namespace string) (*registrationResponse, error) {
response := &registrationResponse{
HeartBeatIntervalMinutes: common.DATA_PLANE_HEARTBEAT_INTERVAL_MINUTES,
}

if serviceName == common.TRATTERIA_SERVICE_NAME {
generationRules, err := s.tratteriaController.Controller.GetActiveGenerationRules(namespace)
if err != nil {
return nil, err
}

response.GenerationRules = generationRules

return response, nil
}

verificationRules, err := s.tratteriaController.Controller.GetActiveVerificationRules(serviceName, namespace)
if err != nil {
return nil, err
}

response.VerificationRules = verificationRules

s.dataPlaneRegistryManager.Register(ipaddress, port, serviceName, namespace)

return response, nil
}

func (s *Service) RegisterHeartBeat(ipaddress string, port int, serviceName string, namespace string) {
Expand Down
4 changes: 3 additions & 1 deletion service/api/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ import (
"github.com/tratteria/tconfigd/api/handler"
"github.com/tratteria/tconfigd/api/pkg/service"
"github.com/tratteria/tconfigd/dataplaneregistry"
"github.com/tratteria/tconfigd/tratteriacontroller"
)

const API_PORT = 8443

type API struct {
DataPlaneRegistryManager dataplaneregistry.Manager
TratteriaController *tratteriacontroller.TratteriaController
X509Source *workloadapi.X509Source
TratteriaSpiffeId spiffeid.ID
Logger *zap.Logger
}

func (api *API) Run() error {
service := service.NewService(api.DataPlaneRegistryManager, api.X509Source, api.TratteriaSpiffeId, api.Logger)
service := service.NewService(api.DataPlaneRegistryManager, api.TratteriaController, api.X509Source, api.TratteriaSpiffeId, api.Logger)
handler := handler.NewHandlers(service, api.Logger)
router := mux.NewRouter()

Expand Down
29 changes: 13 additions & 16 deletions service/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/tratteria/tconfigd/configdispatcher"
"github.com/tratteria/tconfigd/dataplaneregistry"
"github.com/tratteria/tconfigd/spiffe"
"github.com/tratteria/tconfigd/tratcontroller"
"github.com/tratteria/tconfigd/tratteriacontroller"
"github.com/tratteria/tconfigd/webhook"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -66,12 +66,21 @@ func main() {
logger.Fatal("Error getting tconfigd spiffe id.", zap.Error(err))
}

agentsManager := dataplaneregistry.NewRegistry()
configdispatcher := configdispatcher.NewConfigDispatcher(agentsManager, x509Source)
dataPlaneRegistryManager := dataplaneregistry.NewRegistry()
configdispatcher := configdispatcher.NewConfigDispatcher(dataPlaneRegistryManager, x509Source)

tratteriaController := &tratteriacontroller.TratteriaController{
ConfigDispatcher: configdispatcher,
}

if err := tratteriaController.Run(); err != nil {
logger.Fatal("Failed to start TraT Controller server.", zap.Error(err))
}

go func() {
apiServer := &api.API{
DataPlaneRegistryManager: agentsManager,
DataPlaneRegistryManager: dataPlaneRegistryManager,
TratteriaController: tratteriaController,
X509Source: x509Source,
TratteriaSpiffeId: spiffeid.ID(config.TratteriaSpiffeId),
Logger: logger,
Expand All @@ -98,18 +107,6 @@ func main() {
}
}()

go func() {
logger.Info("Starting TraT Controller...")

tratController := &tratcontroller.TraTController{
ConfigDispatcher: configdispatcher,
}

if err := tratController.Run(); err != nil {
logger.Fatal("Failed to start TraT Controller server.", zap.Error(err))
}
}()

<-ctx.Done()

logger.Info("Shutting down tconfigd...")
Expand Down
2 changes: 1 addition & 1 deletion service/configdispatcher/configdispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/tratteria/tconfigd/common"
"github.com/tratteria/tconfigd/dataplaneregistry"
"github.com/tratteria/tconfigd/tratcontroller/pkg/apis/tratteria/v1alpha1"
"github.com/tratteria/tconfigd/tratteriacontroller/pkg/apis/tratteria/v1alpha1"

"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/workloadapi"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"

tratteria1alpha1 "github.com/tratteria/tconfigd/tratcontroller/pkg/apis/tratteria/v1alpha1"
clientset "github.com/tratteria/tconfigd/tratcontroller/pkg/generated/clientset/versioned"
tratteriascheme "github.com/tratteria/tconfigd/tratcontroller/pkg/generated/clientset/versioned/scheme"
informers "github.com/tratteria/tconfigd/tratcontroller/pkg/generated/informers/externalversions/tratteria/v1alpha1"
listers "github.com/tratteria/tconfigd/tratcontroller/pkg/generated/listers/tratteria/v1alpha1"
tratteria1alpha1 "github.com/tratteria/tconfigd/tratteriacontroller/pkg/apis/tratteria/v1alpha1"
clientset "github.com/tratteria/tconfigd/tratteriacontroller/pkg/generated/clientset/versioned"
tratteriascheme "github.com/tratteria/tconfigd/tratteriacontroller/pkg/generated/clientset/versioned/scheme"
informers "github.com/tratteria/tconfigd/tratteriacontroller/pkg/generated/informers/externalversions/tratteria/v1alpha1"
listers "github.com/tratteria/tconfigd/tratteriacontroller/pkg/generated/listers/tratteria/v1alpha1"
)

type Status string
Expand Down Expand Up @@ -213,3 +213,37 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
return nil
}
}

func (c *Controller) GetActiveVerificationRules(serviceName string, namespace string) (*tratteria1alpha1.VerificationRules, error) {
verificationTokenRule, err := c.getActiveVerificationTokenRule(namespace)
if err != nil {
return nil, err
}

verificationEndpointRules, err := c.getActiveVerificationEndpointRules(serviceName, namespace)
if err != nil {
return nil, err
}

return &tratteria1alpha1.VerificationRules{
VerificationTokenRule: verificationTokenRule,
VerificationEndpointRules: verificationEndpointRules,
}, nil
}

func (c *Controller) GetActiveGenerationRules(namespace string) (*tratteria1alpha1.GenerationRules, error) {
generationTokenRule, err := c.getActiveGenerationTokenRule(namespace)
if err != nil {
return nil, err
}

generationEndpointRules, err := c.getActiveGenerationEndpointRules(namespace)
if err != nil {
return nil, err
}

return &tratteria1alpha1.GenerationRules{
GenerationTokenRule: generationTokenRule,
GenerationEndpointRules: generationEndpointRules,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"

tratteria1alpha1 "github.com/tratteria/tconfigd/tratcontroller/pkg/apis/tratteria/v1alpha1"
tratteria1alpha1 "github.com/tratteria/tconfigd/tratteriacontroller/pkg/apis/tratteria/v1alpha1"
)

func (c *Controller) handleTratteriaConfig(ctx context.Context, key string) error {
Expand Down Expand Up @@ -126,3 +128,47 @@ func (c *Controller) updateSuccessTratteriaConfigStatus(ctx context.Context, tra

return updateErr
}

func (c *Controller) getActiveVerificationTokenRule(namespace string) (*tratteria1alpha1.VerificationTokenRule, error) {
tratteriaConfigs, err := c.tratteriaConfigsLister.TratteriaConfigs(namespace).List(labels.Everything())
if err != nil {
klog.Error("Failed to list TratteriaConfigs in namespace:", namespace, err)

return nil, err
}

for _, config := range tratteriaConfigs {
if config.Status.Status == "DONE" {
verificationTokenRule, err := config.GetVerificationTokenRule()
if err != nil {
return nil, err
}

return verificationTokenRule, nil
}
}

return nil, nil
}

func (c *Controller) getActiveGenerationTokenRule(namespace string) (*tratteria1alpha1.GenerationTokenRule, error) {
tratteriaConfigs, err := c.tratteriaConfigsLister.TratteriaConfigs(namespace).List(labels.Everything())
if err != nil {
klog.Error("Failed to list TratteriaConfigs in namespace:", namespace, err)

return nil, err
}

for _, config := range tratteriaConfigs {
if config.Status.Status == "DONE" {
generationTokenRule, err := config.GetGenerationTokenRule()
if err != nil {
return nil, err
}

return generationTokenRule, nil
}
}

return nil, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"

tratteria1alpha1 "github.com/tratteria/tconfigd/tratcontroller/pkg/apis/tratteria/v1alpha1"
tratteria1alpha1 "github.com/tratteria/tconfigd/tratteriacontroller/pkg/apis/tratteria/v1alpha1"
)

func (c *Controller) handleTraT(ctx context.Context, key string) error {
Expand Down Expand Up @@ -129,3 +131,51 @@ func (c *Controller) updateSuccessTratStatus(ctx context.Context, trat *tratteri

return updateErr
}

func (c *Controller) getActiveVerificationEndpointRules(serviceName string, namespace string) ([]*tratteria1alpha1.VerificationEndpointRule, error) {
traTs, err := c.traTsLister.TraTs(namespace).List(labels.Everything())
if err != nil {
klog.Error("Failed to list TraTs in namespace:", namespace, err)

return nil, err
}

var endpointVerificationRules []*tratteria1alpha1.VerificationEndpointRule

for _, trat := range traTs {
if trat.Status.Status == "DONE" {
endpointVerificationRule, err := trat.GetVerificationEndpointRules()
if err != nil {
return nil, err
}
if serviceEndpointVerificationRule := endpointVerificationRule[serviceName]; serviceEndpointVerificationRule != nil {
endpointVerificationRules = append(endpointVerificationRules, serviceEndpointVerificationRule)
}
}
}

return endpointVerificationRules, nil
}

func (c *Controller) getActiveGenerationEndpointRules(namespace string) ([]*tratteria1alpha1.GenerationEndpointRule, error) {
traTs, err := c.traTsLister.TraTs(namespace).List(labels.Everything())
if err != nil {
klog.Error("Failed to list TraTs in namespace:", namespace, err)

return nil, err
}

var endpointGenerationRules []*tratteria1alpha1.GenerationEndpointRule

for _, trat := range traTs {
if trat.Status.Status == "DONE" {
endpointGenerationRule, err := trat.GetGenerationEndpointRule()
if err != nil {
return nil, err
}
endpointGenerationRules = append(endpointGenerationRules, endpointGenerationRule)
}
}

return endpointGenerationRules, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-

source "${CODEGEN_PKG}/kube_codegen.sh"

THIS_PKG="github.com/tratteria/tconfigd/tratcontroller"
THIS_PKG="github.com/tratteria/tconfigd/tratteriacontroller"

kube::codegen::gen_helpers \
--boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

tratteria "github.com/tratteria/tconfigd/tratcontroller/pkg/apis/tratteria"
tratteria "github.com/tratteria/tconfigd/tratteriacontroller/pkg/apis/tratteria"
)

// SchemeGroupVersion is group version used to register these objects
Expand Down
Loading
Loading