Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

added support for --listen-metrics-url parameter #160

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 18 additions & 1 deletion apis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ type EtcdAdmConfig struct {
AdvertiseClientURLs URLList
ListenClientURLs URLList

LoopbackClientURL url.URL
LoopbackClientURL url.URL
AdditionalMetricsURL url.URL

// ServerCertSANs sets extra Subject Alternative Names for the etcd server signing cert.
ServerCertSANs []string
Expand Down Expand Up @@ -200,6 +201,9 @@ func setDynamicDefaults(cfg *EtcdAdmConfig) error {
if err := DefaultClientURLs(cfg); err != nil {
return err
}
if cfg.AdditionalMetricsURL.String() == "" {
DefaultAdditionalMetricsURL(cfg)
}
DefaultPeerCertSANs(cfg)
DefaultServerCertSANs(cfg)
return nil
Expand Down Expand Up @@ -300,6 +304,19 @@ func DefaultAdvertiseClientURLs(cfg *EtcdAdmConfig) error {
return nil
}

// DefaultAdditionalMetricsURL sets additional metrics url using node ip
func DefaultAdditionalMetricsURL(cfg *EtcdAdmConfig) error {
externalAddress, err := defaultExternalAddress()
if err != nil {
return fmt.Errorf("failed to set default DefaultAdditionalMetricsURL: %s", err)
}
cfg.AdditionalMetricsURL = url.URL{
Scheme: constants.DefaultAdditionalMetricsScheme,
Host: fmt.Sprintf("%s:%d", externalAddress.String(), constants.DefaultAdditionalMetricsPort),
}
return nil
}

// Returns the address associated with the host's default interface.
func defaultExternalAddress() (net.IP, error) {
ip, err := netutil.ResolveBindAddress(net.ParseIP("0.0.0.0"))
Expand Down
1 change: 1 addition & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,5 @@ func init() {
initCmd.PersistentFlags().BoolVar(&etcdAdmConfig.SkipHashCheck, "skip-hash-check", false, "Ignore snapshot integrity hash value (required if copied from data directory)")
initCmd.PersistentFlags().DurationVar(&etcdAdmConfig.DownloadConnectTimeout, "download-connect-timeout", constants.DefaultDownloadConnectTimeout, "Maximum time in seconds that you allow the connection to the server to take.")
initCmd.PersistentFlags().StringArrayVar(&etcdAdmConfig.EtcdDiskPriorities, "disk-priorities", constants.DefaultEtcdDiskPriorities, "Setting etcd disk priority")
initCmd.PersistentFlags().Var(util.URLValue{URL: &etcdAdmConfig.AdditionalMetricsURL}, "listen-metrics-url", "URL to expose additional /metrics endpoint without TLS")
}
2 changes: 2 additions & 0 deletions cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"sigs.k8s.io/etcdadm/etcd"
"sigs.k8s.io/etcdadm/initsystem"
"sigs.k8s.io/etcdadm/service"
"sigs.k8s.io/etcdadm/util"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -235,4 +236,5 @@ func init() {
joinCmd.PersistentFlags().StringVar(&etcdAdmConfig.InstallDir, "install-dir", constants.DefaultInstallDir, "install directory")
joinCmd.PersistentFlags().StringArrayVar(&etcdAdmConfig.EtcdDiskPriorities, "disk-priorities", constants.DefaultEtcdDiskPriorities, "Setting etcd disk priority")
joinCmd.PersistentFlags().BoolVar(&etcdAdmConfig.Retry, "retry", true, "Enable or disable backoff retry when join etcd member to cluster")
joinCmd.PersistentFlags().Var(util.URLValue{URL: &etcdAdmConfig.AdditionalMetricsURL}, "listen-metrics-url", "URL to expose additional /metrics endpoint without TLS")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the user passes in a string that isn't a valid URL?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use url.ParseRequestURI() function in URLValue Set function to parse it into a valid URL, if not it returns an error.

[root@ip-172-31-17-207 tmp]# ./etcdadm init --listen-metrics-url http//1.1.1.1
Error: invalid argument "http//1.1.1.1" for "--listen-metrics-url" flag: parse http//1.1.1.1: invalid URI for request

[root@ip-172-31-17-207 tmp]# ./etcdadm init --listen-metrics-url 1.1.1.1
Error: invalid argument "1.1.1.1" for "--listen-metrics-url" flag: parse 1.1.1.1: invalid URI for request

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining. I overlooked the use of .Var.

The UX looks good! (Do you think it would be nice if the error gives an example of a valid URI?)

}
6 changes: 6 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (
DefaultPeerPort = 2380
DefaultClientPort = 2379

DefaultAdditionalMetricsPort = 9379
DefaultAdditionalMetricsScheme = "http"

DefaultDownloadConnectTimeout = 10 * time.Second
DefaultEtcdRequestTimeout = 5 * time.Second

Expand Down Expand Up @@ -126,6 +129,9 @@ ETCD_PEER_TRUSTED_CA_FILE={{ .PeerTrustedCAFile }}
# Client/server configuration
ETCD_ADVERTISE_CLIENT_URLS={{ .AdvertiseClientURLs.String }}
ETCD_LISTEN_CLIENT_URLS={{ .ListenClientURLs.String }}
{{- if .AdditionalMetricsURL.String }}
ETCD_LISTEN_METRICS_URLS={{ .AdditionalMetricsURL.String }}
{{- end }}

ETCD_PEER_CLIENT_CERT_AUTH=true
ETCD_CERT_FILE={{ .CertFile }}
Expand Down
49 changes: 49 additions & 0 deletions util/url_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the Set and Type receivers used anywhere?

Copy link
Author

@suhrud-kumar suhrud-kumar May 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set is used to set argument passed to the assigned variable.

Since URLValue is an implementation of Value interface in pflag, all three functions are required - https://godoc.org/github.com/spf13/pflag#Value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I missed this.

Copyright 2018 The Kubernetes Authors.

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 util

import (
"net/url"
)

// URLValue struct definition
type URLValue struct {
URL *url.URL
}

// String reassembles the URL into a valid URL string
func (s URLValue) String() string {
if s.URL != nil {
return s.URL.String()
}
return ""
}

// Set sets the argument passed to URL Value
func (s URLValue) Set(val string) error {
if u, err := url.Parse(val); err != nil {
return err
} else {
dlipovetsky marked this conversation as resolved.
Show resolved Hide resolved
*s.URL = *u
}
return nil
}

// Type returns type of struct
func (s URLValue) Type() string {
return "URLValue"
}