Skip to content

Commit

Permalink
Refactoring code layout
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjt committed Jan 15, 2024
1 parent 260582d commit 639fb8c
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 102 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
go-version: 1.21

- name: Run mapping generator
run: cd mappinggenerator && go run main.go
run: go run cmd/mappinggenerator/main.go

- name: Build config generator
run: go build -o docker-build/ocbconfigbuilder src/*.go
run: go build -o docker-build/ocbconfigbuilder cmd/configgenerator/*.go

- name: Build Docker image
run: docker build -f docker-build/Dockerfile --tag ghcr.io/${{ github.repository }}:latest docker-build/
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ This will result in a custom image that has the config file embedded inside, how
To regenerate the mappings

```shell
cd mappinggenerator
go run main.go
go run cmd/mappinggenerator/*
```

To build the configbuilder

```shell
go build -o docker-build/ocbconfigbuilder src/*.go
go build -o docker-build/ocbconfigbuilder cmd/configgenerator/*.go
```

To build the container image locally
Expand Down
18 changes: 18 additions & 0 deletions cmd/configgenerator/component_mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"embed"

"github.com/martinjt/ocb-config-builder/pkg/configmapping"
"gopkg.in/yaml.v3"
)

//go:embed component_mapping.yaml
var componentMappingFile embed.FS

func getComponentMapping() configmapping.ComponentMappingFile {
componentMappingBytes, _ := componentMappingFile.ReadFile("component_mapping.yaml")
var componentMapping configmapping.ComponentMappingFile
yaml.Unmarshal(componentMappingBytes, &componentMapping)
return componentMapping
}
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,6 @@ exporters:
syslog:
github_url: github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter
version: 0.90.1
tanzuobservability:
github_url: github.com/open-telemetry/opentelemetry-collector-contrib/exporter/tanzuobservabilityexporter
version: 0.90.1
tencentcloud_logservice:
github_url: github.com/open-telemetry/opentelemetry-collector-contrib/exporter/tencentcloudlogserviceexporter
version: 0.90.1
Expand Down
3 changes: 2 additions & 1 deletion src/main.go → cmd/configgenerator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/jessevdk/go-flags"
"github.com/martinjt/ocb-config-builder/pkg/collectorconfig"
)

var opts struct {
Expand All @@ -14,7 +15,7 @@ var opts struct {
func main() {
flags.Parse(&opts)

requiredComponents, err := getRequiredComponentsFromCollectorConfig(opts.InputConfig)
requiredComponents, err := collectorconfig.GetRequiredComponentsFromCollectorConfig(opts.InputConfig)
if err != nil {
fmt.Println(err)
return
Expand Down
4 changes: 3 additions & 1 deletion src/ocb_config.go → cmd/configgenerator/ocb_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"os"

"github.com/martinjt/ocb-config-builder/pkg/collectorconfig"
"github.com/martinjt/ocb-config-builder/pkg/configmapping"
"gopkg.in/yaml.v3"
)

Expand All @@ -26,7 +28,7 @@ type ModulePath struct {
GoMod string `yaml:"gomod"`
}

func (config *ocbConfig) addComponent(components requiredComponents, componentMapping ComponentMappingFile) {
func (config *ocbConfig) addComponent(components collectorconfig.RequiredComponents, componentMapping configmapping.ComponentMappingFile) {
for _, v := range components.Receivers {
config.Receivers = append(config.Receivers, ModulePath{GoMod: componentMapping.GetConfigType("receiver", v)})
}
Expand Down
38 changes: 13 additions & 25 deletions mappinggenerator/main.go → cmd/mappinggenerator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,14 @@ import (
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/martinjt/ocb-config-builder/pkg/configmapping"
"gopkg.in/yaml.v3"
)

type Metadata struct {
ConfigType string `yaml:"type"`
}

type ComponentMapping struct {
GithubUrl string `yaml:"github_url"`
Version string `yaml:"version"`
}

type ComponentMappingFile struct {
Receivers map[string]ComponentMapping `yaml:"receivers"`
Processors map[string]ComponentMapping `yaml:"processors"`
Exporters map[string]ComponentMapping `yaml:"exporters"`
Extensions map[string]ComponentMapping `yaml:"extensions"`
Connectors map[string]ComponentMapping `yaml:"connectors"`
}

func main() {

r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
Expand Down Expand Up @@ -61,8 +49,8 @@ func main() {
receivers := getCoreReceiverMapping()
processors := getCoreProcessorMapping()
exporters := getCoreExporterMapping()
extensions := make(map[string]ComponentMapping)
connectors := make(map[string]ComponentMapping)
extensions := make(map[string]configmapping.ComponentMapping)
connectors := make(map[string]configmapping.ComponentMapping)

tree.Files().ForEach(func(f *object.File) error {
if filepath.Base(f.Name) == "metadata.yaml" {
Expand All @@ -76,7 +64,7 @@ func main() {

yaml.Unmarshal([]byte(metadataContents), &metadata)

componentMapping := ComponentMapping{
componentMapping := configmapping.ComponentMapping{
GithubUrl: "github.com/open-telemetry/opentelemetry-collector-contrib/" + componentType + "/" + componentDir,
Version: "0.90.1",
}
Expand All @@ -93,13 +81,13 @@ func main() {
case "connector":
connectors[metadata.ConfigType] = componentMapping
default:
fmt.Println("Unknown component type: " + componentType)
fmt.Println("Unknown component type: " + componentType + "Filename: " + f.Name)
}
}
return nil
})

config := ComponentMappingFile{
config := configmapping.ComponentMappingFile{
Receivers: receivers,
Processors: processors,
Exporters: exporters,
Expand All @@ -109,20 +97,20 @@ func main() {

configBytes, _ := yaml.Marshal(&config)

os.WriteFile("../src/component_mapping.yaml", configBytes, 0644)
os.WriteFile("cmd/configgenerator/component_mapping.yaml", configBytes, 0644)
}

func getCoreProcessorMapping() map[string]ComponentMapping {
return map[string]ComponentMapping{
func getCoreProcessorMapping() map[string]configmapping.ComponentMapping {
return map[string]configmapping.ComponentMapping{
"batch": {
GithubUrl: "go.opentelemetry.io/collector/processor/batchprocessor",
Version: "0.90.1",
},
}
}

func getCoreExporterMapping() map[string]ComponentMapping {
return map[string]ComponentMapping{
func getCoreExporterMapping() map[string]configmapping.ComponentMapping {
return map[string]configmapping.ComponentMapping{
"logging": {
GithubUrl: "go.opentelemetry.io/collector/exporter/loggingexporter",
Version: "0.90.1",
Expand All @@ -138,8 +126,8 @@ func getCoreExporterMapping() map[string]ComponentMapping {
}
}

func getCoreReceiverMapping() map[string]ComponentMapping {
return map[string]ComponentMapping{
func getCoreReceiverMapping() map[string]configmapping.ComponentMapping {
return map[string]configmapping.ComponentMapping{
"otlp": {
GithubUrl: "go.opentelemetry.io/collector/receiver/otlpreceiver",
Version: "0.90.1",
Expand Down
3 changes: 2 additions & 1 deletion mappinggenerator/go.mod → go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module mappinggenerator
module github.com/martinjt/ocb-config-builder

go 1.21.5

require (
github.com/go-git/go-git/v5 v5.11.0
github.com/jessevdk/go-flags v1.5.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
3 changes: 3 additions & 0 deletions mappinggenerator/go.sum → go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -98,6 +100,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
6 changes: 0 additions & 6 deletions go.work

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package collectorconfig

import (
"os"
Expand All @@ -8,35 +8,35 @@ import (
"gopkg.in/yaml.v3"
)

type collectorConfigFile struct {
type CollectorConfigFile struct {
Receivers map[string]interface{} `yaml:"receivers"`
Processors map[string]interface{} `yaml:"processors"`
Exporters map[string]interface{} `yaml:"exporters"`
Extensions map[string]interface{} `yaml:"extensions"`
Connectors map[string]interface{} `yaml:"connectors"`
}

type requiredComponents struct {
type RequiredComponents struct {
Receivers []string
Processors []string
Exporters []string
Extensions []string
Connectors []string
}

func getRequiredComponentsFromCollectorConfig(filename string) (requiredComponents, error) {
func GetRequiredComponentsFromCollectorConfig(filename string) (RequiredComponents, error) {

bytes, err := os.ReadFile(filename)
if err != nil {
return requiredComponents{}, err
return RequiredComponents{}, err
}

var data collectorConfigFile
var data CollectorConfigFile

// Unmarshal the YAML string into the data map
yaml.Unmarshal(bytes, &data)

requiredComponents := requiredComponents{}
requiredComponents := RequiredComponents{}

for k := range data.Receivers {
requiredComponents.Receivers = append(requiredComponents.Receivers, getType(k))
Expand Down
19 changes: 2 additions & 17 deletions src/component_mapping.go → pkg/configmapping/mapping_file.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package main
package configmapping

import (
"embed"
"fmt"

"gopkg.in/yaml.v3"
)
import "fmt"

type ComponentMapping struct {
GithubUrl string `yaml:"github_url"`
Expand All @@ -20,16 +15,6 @@ type ComponentMappingFile struct {
Connectors map[string]ComponentMapping `yaml:"connectors"`
}

//go:embed component_mapping.yaml
var componentMappingFile embed.FS

func getComponentMapping() ComponentMappingFile {
componentMappingBytes, _ := componentMappingFile.ReadFile("component_mapping.yaml")
var componentMapping ComponentMappingFile
yaml.Unmarshal(componentMappingBytes, &componentMapping)
return componentMapping
}

func (c *ComponentMappingFile) GetConfigType(componentType string, componentTypeName string) string {
var component ComponentMapping
var found bool = false
Expand Down
16 changes: 0 additions & 16 deletions src/dist.yaml

This file was deleted.

9 changes: 0 additions & 9 deletions src/go.mod

This file was deleted.

10 changes: 0 additions & 10 deletions src/go.sum

This file was deleted.

2 changes: 1 addition & 1 deletion test/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM local/simple-collector-builder:0.90.1 as build
FROM local/simple-collector-builder:0.90.1-0.1 as build
COPY config-test.yaml /config/config.yaml
RUN /builder/build-collector.sh /config/config.yaml

Expand Down

0 comments on commit 639fb8c

Please sign in to comment.