Skip to content

Commit

Permalink
Restructure project folders structure (#147)
Browse files Browse the repository at this point in the history
* Restructure project folders structure

* Move some util function to their respective files

* Move back discovery and factsengine to internal
  • Loading branch information
arbulu89 authored Nov 24, 2022
1 parent f76e000 commit 64e3fd8
Show file tree
Hide file tree
Showing 93 changed files with 205 additions and 235 deletions.
6 changes: 3 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/trento-project/agent/internal"
"github.com/trento-project/agent/internal/agent"
"github.com/trento-project/agent/internal/discovery"
"github.com/trento-project/agent/internal/discovery/collector"
)
Expand All @@ -20,7 +20,7 @@ func validatePeriod(durationFlag string, minValue time.Duration) error {
return nil
}

func LoadConfig() (*internal.Config, error) {
func LoadConfig() (*agent.Config, error) {
minPeriodValues := map[string]time.Duration{
"cluster-discovery-period": discovery.ClusterDiscoveryMinPeriod,
"sapsystem-discovery-period": discovery.SAPDiscoveryMinPeriod,
Expand Down Expand Up @@ -71,7 +71,7 @@ func LoadConfig() (*internal.Config, error) {
DiscoveriesPeriodsConfig: discoveryPeriodsConfig,
}

return &internal.Config{
return &agent.Config{
InstanceName: hostname,
DiscoveriesConfig: discoveriesConfig,
// Feature flag to enable the facts engine
Expand Down
4 changes: 2 additions & 2 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal"
"github.com/trento-project/agent/internal/agent"
"github.com/trento-project/agent/internal/discovery"
"github.com/trento-project/agent/internal/discovery/collector"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ func (suite *AgentCmdTestSuite) SetupTest() {
func (suite *AgentCmdTestSuite) TearDownTest() {
_ = suite.cmd.Execute()

expectedConfig := &internal.Config{
expectedConfig := &agent.Config{
InstanceName: "some-hostname",
DiscoveriesConfig: &discovery.DiscoveriesConfig{
SSHAddress: "some-ssh-address",
Expand Down
28 changes: 22 additions & 6 deletions cmd/facts.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package cmd

import (
"bytes"
"encoding/json"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/trento-project/agent/internal"
"github.com/trento-project/agent/internal/factsengine/entities"
"github.com/trento-project/agent/internal/agent"
"github.com/trento-project/agent/internal/factsengine/gatherers"
"github.com/trento-project/agent/internal/utils"
"github.com/trento-project/agent/pkg/factsengine/entities"
)

func NewFactsCmd() *cobra.Command {
Expand Down Expand Up @@ -38,7 +40,7 @@ func NewFactsGatherCmd() *cobra.Command {
}
})

return internal.InitConfig("agent")
return agent.InitConfig("agent")
},
}

Expand All @@ -65,7 +67,7 @@ func NewFactsListCmd() *cobra.Command {
}
})

return internal.InitConfig("agent")
return agent.InitConfig("agent")
},
}

Expand Down Expand Up @@ -114,7 +116,7 @@ func gather(*cobra.Command, []string) {
cleanupAndFatal(err)
}

result, err := utils.PrettifyInterfaceToJSON(value[0])
result, err := prettifyInterfaceToJSON(value[0])
if err != nil {
cleanupAndFatal(err)
}
Expand Down Expand Up @@ -159,3 +161,17 @@ func list(*cobra.Command, []string) {
log.Printf(g)
}
}

func prettifyInterfaceToJSON(data interface{}) (string, error) {
jsonResult, err := json.Marshal(data)
if err != nil {
return "", errors.Wrap(err, "Error building the response")
}

var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, jsonResult, "", " "); err != nil {
return "", errors.Wrap(err, "Error indenting the json data")
}

return prettyJSON.String(), nil
}
6 changes: 3 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/trento-project/agent/internal"
"github.com/trento-project/agent/internal/agent"
)

func NewStartCmd() *cobra.Command {
Expand All @@ -36,7 +36,7 @@ func NewStartCmd() *cobra.Command {
}
})

return internal.InitConfig("agent")
return agent.InitConfig("agent")
},
}

Expand Down Expand Up @@ -136,7 +136,7 @@ func start(*cobra.Command, []string) {
log.Fatal("Failed to create the agent configuration: ", err)
}

a, err := internal.NewAgent(config)
a, err := agent.NewAgent(config)
if err != nil {
log.Fatal("Failed to create the agent: ", err)
}
Expand Down
29 changes: 25 additions & 4 deletions internal/agent.go → internal/agent/agent.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package internal
package agent

import (
"context"
"fmt"
"strings"
"time"

"github.com/google/uuid"
"github.com/pkg/errors"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/trento-project/agent/internal/discovery/collector"
"github.com/trento-project/agent/internal/factsengine"
"github.com/trento-project/agent/internal/factsengine/gatherers"
"github.com/trento-project/agent/internal/utils"
)

const machineIDPath = "/etc/machine-id"
Expand Down Expand Up @@ -156,7 +156,7 @@ func (a *Agent) startDiscoverTicker(ctx context.Context, d discovery.Discovery)
}
log.Infof("%s discovery tick output: %s", d.GetID(), result)
}
utils.Repeat(ctx, d.GetID(), tick, d.GetInterval())
repeat(ctx, d.GetID(), tick, d.GetInterval())

}

Expand All @@ -168,5 +168,26 @@ func (a *Agent) startHeartbeatTicker(ctx context.Context) {
}
}

utils.Repeat(ctx, "agent.heartbeat", tick, HeartbeatInterval)
repeat(ctx, "agent.heartbeat", tick, HeartbeatInterval)
}

// Repeat executes a function at a given interval.
// the first tick runs immediately
func repeat(ctx context.Context, operation string, tick func(), interval time.Duration) {
tick()

ticker := time.NewTicker(interval)
msg := fmt.Sprintf("Next execution for operation %s in %s", operation, interval)
log.Debugf(msg)

defer ticker.Stop()
for {
select {
case <-ticker.C:
tick()
log.Debugf(msg)
case <-ctx.Done():
return
}
}
}
2 changes: 1 addition & 1 deletion internal/agent_test.go → internal/agent/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package agent

import (
"testing"
Expand Down
40 changes: 32 additions & 8 deletions internal/config.go → internal/agent/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package internal
package agent

import (
"os"
"path"
"strings"

"github.com/hashicorp/go-hclog"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"github.com/trento-project/agent/internal/utils"
)

// InitConfig intializes the config for the application
Expand All @@ -24,11 +23,11 @@ import (
// /usr/etc/trento/${context}.yaml
// $HOME/.config/trento/${context}.yaml
func InitConfig(configName string) error {
BindEnv()
bindEnv()

viper.SetConfigType("yaml")
utils.SetLogLevel(viper.GetString("log-level"))
utils.SetLogFormatter("2006-01-02 15:04:05")
setLogLevel(viper.GetString("log-level"))
setLogFormatter("2006-01-02 15:04:05")

cfgFile := viper.GetString("config")
if cfgFile != "" {
Expand Down Expand Up @@ -73,8 +72,33 @@ func InitConfig(configName string) error {
return nil
}

func BindEnv() {
func bindEnv() {
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
viper.SetEnvPrefix("TRENTO")
viper.AutomaticEnv() // read in environment variables that match
}

func setLogLevel(level string) {
switch level {
case "error":
log.SetLevel(log.ErrorLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "debug":
log.SetLevel(log.DebugLevel)
default:
log.Warnln("Unrecognized minimum log level; using 'info' as default")
log.SetLevel(log.InfoLevel)
}
hclog.DefaultOptions.Level = hclog.LevelFromString(level)
}

func setLogFormatter(timestampFormat string) {
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = timestampFormat
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
hclog.DefaultOptions.TimeFormat = timestampFormat
}
2 changes: 1 addition & 1 deletion internal/heartbeat.go → internal/agent/heartbeat.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package agent

import "time"

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/cloud/mocks"
"github.com/trento-project/agent/internal/core/cloud/mocks"
"github.com/trento-project/agent/test/helpers"
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/cloud/mocks"
"github.com/trento-project/agent/internal/core/cloud/mocks"
"github.com/trento-project/agent/test/helpers"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/cloud/mocks"
"github.com/trento-project/agent/internal/core/cloud/mocks"
"github.com/trento-project/agent/test/helpers"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

log "github.com/sirupsen/logrus"
"github.com/trento-project/agent/internal/utils"
"github.com/trento-project/agent/pkg/utils"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/cloud/mocks"
utilsMocks "github.com/trento-project/agent/internal/utils/mocks"
"github.com/trento-project/agent/internal/core/cloud/mocks"
utilsMocks "github.com/trento-project/agent/pkg/utils/mocks"
)

type CloudMetadataTestSuite struct {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
// Now we mantain our own fork
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/trento-project/agent/internal/cloud"
"github.com/trento-project/agent/internal/cluster/cib"
"github.com/trento-project/agent/internal/cluster/crmmon"
"github.com/trento-project/agent/internal/utils"
"github.com/trento-project/agent/internal/core/cloud"
"github.com/trento-project/agent/internal/core/cluster/cib"
"github.com/trento-project/agent/internal/core/cluster/crmmon"
"github.com/trento-project/agent/pkg/utils"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/cluster/cib"
"github.com/trento-project/agent/internal/cluster/crmmon"
"github.com/trento-project/agent/internal/core/cluster/cib"
"github.com/trento-project/agent/internal/core/cluster/crmmon"
"github.com/trento-project/agent/test/helpers"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/cluster/sbd.go → internal/core/cluster/sbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
log "github.com/sirupsen/logrus"

"github.com/pkg/errors"
"github.com/trento-project/agent/internal/utils"
"github.com/trento-project/agent/pkg/utils"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/utils/mocks"
"github.com/trento-project/agent/pkg/utils/mocks"
"github.com/trento-project/agent/test/helpers"
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/pkg/errors"
sapcontrol "github.com/trento-project/agent/internal/sapsystem/sapcontrolapi"
sapcontrol "github.com/trento-project/agent/internal/core/sapsystem/sapcontrolapi"
)

type SAPControl struct {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 64e3fd8

Please sign in to comment.