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

cluster: support configure grafana in server_configs #1703

Merged
merged 5 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 8 additions & 2 deletions components/dm/spec/topology_dm.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ type (
type (
// DMServerConfigs represents the server runtime configuration
DMServerConfigs struct {
Master map[string]interface{} `yaml:"master"`
Worker map[string]interface{} `yaml:"worker"`
Master map[string]interface{} `yaml:"master"`
Worker map[string]interface{} `yaml:"worker"`
Grafana map[string]string `yaml:"grafana"`
}

// Specification represents the specification of topology.yaml
Expand Down Expand Up @@ -838,3 +839,8 @@ func getPort(v reflect.Value) string {
}
return ""
}

// GetGrafanaConfig returns global grafana configurations
func (s *Specification) GetGrafanaConfig() map[string]string {
return s.ServerConfigs.Grafana
}
1 change: 1 addition & 0 deletions pkg/cluster/ansible/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ server_configs:
pump: {}
drainer: {}
cdc: {}
grafana: {}
tidb_servers: []
tikv_servers: []
tiflash_servers: []
Expand Down
1 change: 1 addition & 0 deletions pkg/cluster/ansible/test-data/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ topology:
pump: {}
drainer: {}
cdc: {}
grafana: {}
tidb_servers:
- host: 172.16.1.218
ssh_port: 9999
Expand Down
36 changes: 35 additions & 1 deletion pkg/cluster/spec/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"crypto/tls"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/pingcap/tiup/pkg/cluster/template/config"
"github.com/pingcap/tiup/pkg/cluster/template/scripts"
"github.com/pingcap/tiup/pkg/meta"
"gopkg.in/ini.v1"
)

// GrafanaSpec represents the Grafana topology specification in topology.yaml
Expand All @@ -38,6 +40,7 @@ type GrafanaSpec struct {
IgnoreExporter bool `yaml:"ignore_exporter,omitempty"`
Port int `yaml:"port" default:"3000"`
DeployDir string `yaml:"deploy_dir,omitempty"`
Config map[string]string `yaml:"config,omitempty" validate:"config:ignore"`
ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"`
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
Expand Down Expand Up @@ -181,11 +184,22 @@ func (i *GrafanaInstance) InitConfig(
return err
}

userConfig := i.topo.GetGrafanaConfig()
if userConfig == nil {
userConfig = make(map[string]string)
}
for k, v := range spec.Config {
userConfig[k] = v
}
err := mergeAdditionalGrafanaConf(fp, userConfig)
if err != nil {
return err
}

dst = filepath.Join(paths.Deploy, "conf", "grafana.ini")
if err := e.Transfer(ctx, fp, dst, false, 0, false); err != nil {
return err
}

if err := i.installDashboards(ctx, e, paths.Deploy, clusterName, clusterVersion); err != nil {
return errors.Annotate(err, "install dashboards")
}
Expand Down Expand Up @@ -330,3 +344,23 @@ func (i *GrafanaInstance) ScaleConfig(
i.topo = topo.Merge(i.topo)
return i.InitConfig(ctx, e, clusterName, clusterVersion, deployUser, paths)
}

func mergeAdditionalGrafanaConf(source string, addition map[string]string) error {
nexustar marked this conversation as resolved.
Show resolved Hide resolved
bytes, err := os.ReadFile(source)
if err != nil {
return err
}
result, err := ini.Load(bytes)
if err != nil {
return err
}
for k, v := range addition {
// convert "log.file.level to [log.file] level"
for i := len(k) - 1; i >= 0; i-- {
if k[i] == '.' {
result.Section(k[:i]).Key(k[i+1:]).SetValue(v)
}
}
}
return result.SaveTo(source)
}
7 changes: 7 additions & 0 deletions pkg/cluster/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type (
Pump map[string]interface{} `yaml:"pump"`
Drainer map[string]interface{} `yaml:"drainer"`
CDC map[string]interface{} `yaml:"cdc"`
Grafana map[string]string `yaml:"grafana"`
}

// Specification represents the specification of topology.yaml
Expand Down Expand Up @@ -153,6 +154,7 @@ type Topology interface {
TLSConfig(dir string) (*tls.Config, error)
Merge(that Topology) Topology
FillHostArch(hostArchmap map[string]string) error
GetGrafanaConfig() map[string]string

ScaleOutTopology
}
Expand Down Expand Up @@ -856,3 +858,8 @@ func setHostArch(field reflect.Value, hostArch map[string]string) error {

return nil
}

// GetGrafanaConfig returns global grafana configurations
func (s *Specification) GetGrafanaConfig() map[string]string {
return s.ServerConfigs.Grafana
}
3 changes: 3 additions & 0 deletions pkg/cluster/spec/spec_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func (t *TestTopology) CountDir(host string, dir string) int {
return 0
}

func (t *TestTopology) GetGrafanaConfig() map[string]string {
return nil
}
func TestSpec(t *testing.T) {
dir, err := os.MkdirTemp("", "test-*")
assert.Nil(t, err)
Expand Down