-
Notifications
You must be signed in to change notification settings - Fork 118
/
add_external.go
130 lines (113 loc) · 5.24 KB
/
add_external.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2019 Percona LLC
//
// 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 management
import (
"fmt"
"os"
"strings"
"github.com/AlekSi/pointer"
"github.com/percona/pmm/admin/agentlocal"
"github.com/percona/pmm/admin/commands"
"github.com/percona/pmm/api/managementpb/json/client"
"github.com/percona/pmm/api/managementpb/json/client/external"
)
const (
// DefaultGroupExternalExporter defines default group name for external exporter.
DefaultGroupExternalExporter = "external"
// DefaultServiceNameSuffix defines default service suffix for external exporter.
DefaultServiceNameSuffix = "-external"
)
var addExternalResultT = commands.ParseTemplate(`
External Service added.
Service ID : {{ .Service.ServiceID }}
Service name: {{ .Service.ServiceName }}
Group : {{ .Service.Group }}
`)
type addExternalResult struct {
Service *external.AddExternalOKBodyService `json:"service"`
}
func (res *addExternalResult) Result() {}
func (res *addExternalResult) String() string {
return commands.RenderTemplate(addExternalResultT, res)
}
// AddExternalCommand is used by Kong for CLI flags and commands.
type AddExternalCommand struct {
ServiceName string `default:"${hostname}${externalDefaultServiceName}" help:"Service name (autodetected default: ${hostname}${externalDefaultServiceName})"`
RunsOnNodeID string `name:"agent-node-id" help:"Node ID where agent runs (default is autodetected)"`
Username string `help:"External username"`
Password string `help:"External password"`
ServiceParamsSource string `help:"Path to file with service parameters"`
Scheme string `placeholder:"http or https" help:"Scheme to generate URI to exporter metrics endpoints"`
MetricsPath string `placeholder:"/metrics" help:"Path under which metrics are exposed, used to generate URI"`
ListenPort uint16 `placeholder:"port" required:"" help:"Listen port of external exporter for scraping metrics. (Required)"`
NodeID string `name:"service-node-id" help:"Node ID where service runs (default is autodetected)"`
Environment string `placeholder:"prod" help:"Environment name like 'production' or 'qa'"`
Cluster string `placeholder:"east-cluster" help:"Cluster name"`
ReplicationSet string `placeholder:"rs1" help:"Replication set name"`
CustomLabels map[string]string `mapsep:"," help:"Custom user-assigned labels"`
MetricsMode string `enum:"${metricsModesEnum}" default:"auto" help:"Metrics flow mode, can be push - agent will push metrics, pull - server scrape metrics from agent or auto - chosen by server"`
Group string `default:"${externalDefaultGroupExporter}" help:"Group name of external service (default: ${externalDefaultGroupExporter})"`
SkipConnectionCheck bool `help:"Skip exporter connection checks"`
}
func (cmd *AddExternalCommand) RunCmd() (commands.Result, error) {
customLabels := commands.ParseCustomLabels(cmd.CustomLabels)
if cmd.RunsOnNodeID == "" || cmd.NodeID == "" {
status, err := agentlocal.GetStatus(agentlocal.DoNotRequestNetworkInfo)
if err != nil {
return nil, err
}
if cmd.RunsOnNodeID == "" {
cmd.RunsOnNodeID = status.NodeID
}
if cmd.NodeID == "" {
cmd.NodeID = status.NodeID
}
}
hostname, _ := os.Hostname()
defaultServiceName := hostname + DefaultServiceNameSuffix
if cmd.Group != DefaultGroupExternalExporter && cmd.ServiceName == defaultServiceName {
cmd.ServiceName = fmt.Sprintf("%s-%s", strings.TrimSuffix(cmd.ServiceName, DefaultServiceNameSuffix), cmd.Group)
}
if cmd.MetricsPath != "" && !strings.HasPrefix(cmd.MetricsPath, "/") {
cmd.MetricsPath = fmt.Sprintf("/%s", cmd.MetricsPath)
}
params := &external.AddExternalParams{
Body: external.AddExternalBody{
RunsOnNodeID: cmd.RunsOnNodeID,
ServiceName: cmd.ServiceName,
Username: cmd.Username,
Password: cmd.Password,
Scheme: cmd.Scheme,
MetricsPath: cmd.MetricsPath,
ListenPort: int64(cmd.ListenPort),
NodeID: cmd.NodeID,
Environment: cmd.Environment,
Cluster: cmd.Cluster,
ReplicationSet: cmd.ReplicationSet,
CustomLabels: customLabels,
MetricsMode: pointer.ToString(strings.ToUpper(cmd.MetricsMode)),
Group: cmd.Group,
SkipConnectionCheck: cmd.SkipConnectionCheck,
ServiceParamsSource: cmd.ServiceParamsSource,
},
Context: commands.Ctx,
}
resp, err := client.Default.External.AddExternal(params)
if err != nil {
return nil, err
}
return &addExternalResult{
Service: resp.Payload.Service,
}, nil
}