Skip to content

Commit

Permalink
Antrea Prometheus integration (#236)
Browse files Browse the repository at this point in the history
Integrate with Prometheus monitoring solution.
Integration of the Prometheus client into Antrea controller and agent
allows the exposure of various metrics to Prometheus server.
In addition to Antrea's own set of metrics, Prometheus client will also
expose metrics which are defined by various components which are part of
the Antrea ecosystem, e.g golang, Prometheus itself etc.
  • Loading branch information
ksamoray committed Apr 23, 2020
1 parent 24442bb commit 0ab4ff6
Show file tree
Hide file tree
Showing 20 changed files with 356 additions and 78 deletions.
13 changes: 9 additions & 4 deletions build/yamls/antrea-eks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ data:
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-agent` container must be set to the same value.
#apiPort: 10350
# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener
#enablePrometheusMetrics: false
antrea-cni.conflist: |
{
"cniVersion":"0.3.0",
Expand All @@ -354,12 +357,15 @@ data:
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
#apiPort: 10349
# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener.
#enablePrometheusMetrics: false
kind: ConfigMap
metadata:
annotations: {}
labels:
app: antrea
name: antrea-config-t4m46b8f6h
name: antrea-config-td846cf4bm
namespace: kube-system
---
apiVersion: v1
Expand Down Expand Up @@ -459,7 +465,7 @@ spec:
key: node-role.kubernetes.io/master
volumes:
- configMap:
name: antrea-config-t4m46b8f6h
name: antrea-config-td846cf4bm
name: antrea-config
- hostPath:
path: /var/log/antrea
Expand Down Expand Up @@ -555,7 +561,6 @@ spec:
name: antrea-agent
ports:
- containerPort: 10350
hostPort: 10350
name: api
protocol: TCP
readinessProbe:
Expand Down Expand Up @@ -658,7 +663,7 @@ spec:
operator: Exists
volumes:
- configMap:
name: antrea-config-t4m46b8f6h
name: antrea-config-td846cf4bm
name: antrea-config
- hostPath:
path: /etc/cni/net.d
Expand Down
13 changes: 9 additions & 4 deletions build/yamls/antrea-gke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ data:
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-agent` container must be set to the same value.
#apiPort: 10350
# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener
#enablePrometheusMetrics: false
antrea-cni.conflist: |
{
"cniVersion":"0.3.0",
Expand All @@ -354,12 +357,15 @@ data:
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
#apiPort: 10349
# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener.
#enablePrometheusMetrics: false
kind: ConfigMap
metadata:
annotations: {}
labels:
app: antrea
name: antrea-config-5754dg84hf
name: antrea-config-h45gtb8dbg
namespace: kube-system
---
apiVersion: v1
Expand Down Expand Up @@ -459,7 +465,7 @@ spec:
key: node-role.kubernetes.io/master
volumes:
- configMap:
name: antrea-config-5754dg84hf
name: antrea-config-h45gtb8dbg
name: antrea-config
- hostPath:
path: /var/log/antrea
Expand Down Expand Up @@ -555,7 +561,6 @@ spec:
name: antrea-agent
ports:
- containerPort: 10350
hostPort: 10350
name: api
protocol: TCP
readinessProbe:
Expand Down Expand Up @@ -658,7 +663,7 @@ spec:
operator: Exists
volumes:
- configMap:
name: antrea-config-5754dg84hf
name: antrea-config-h45gtb8dbg
name: antrea-config
- hostPath:
path: /etc/cni/net.d
Expand Down
13 changes: 9 additions & 4 deletions build/yamls/antrea-ipsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ data:
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-agent` container must be set to the same value.
#apiPort: 10350
# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener
#enablePrometheusMetrics: false
antrea-cni.conflist: |
{
"cniVersion":"0.3.0",
Expand All @@ -354,12 +357,15 @@ data:
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
#apiPort: 10349
# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener.
#enablePrometheusMetrics: false
kind: ConfigMap
metadata:
annotations: {}
labels:
app: antrea
name: antrea-config-c7579447k2
name: antrea-config-d7m57h87ck
namespace: kube-system
---
apiVersion: v1
Expand Down Expand Up @@ -468,7 +474,7 @@ spec:
key: node-role.kubernetes.io/master
volumes:
- configMap:
name: antrea-config-c7579447k2
name: antrea-config-d7m57h87ck
name: antrea-config
- hostPath:
path: /var/log/antrea
Expand Down Expand Up @@ -596,7 +602,6 @@ spec:
name: antrea-agent
ports:
- containerPort: 10350
hostPort: 10350
name: api
protocol: TCP
readinessProbe:
Expand Down Expand Up @@ -699,7 +704,7 @@ spec:
operator: Exists
volumes:
- configMap:
name: antrea-config-c7579447k2
name: antrea-config-d7m57h87ck
name: antrea-config
- hostPath:
path: /etc/cni/net.d
Expand Down
13 changes: 9 additions & 4 deletions build/yamls/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ data:
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-agent` container must be set to the same value.
#apiPort: 10350
# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener
#enablePrometheusMetrics: false
antrea-cni.conflist: |
{
"cniVersion":"0.3.0",
Expand All @@ -354,12 +357,15 @@ data:
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
#apiPort: 10349
# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener.
#enablePrometheusMetrics: false
kind: ConfigMap
metadata:
annotations: {}
labels:
app: antrea
name: antrea-config-428d4tg64g
name: antrea-config-f95kf94mk9
namespace: kube-system
---
apiVersion: v1
Expand Down Expand Up @@ -459,7 +465,7 @@ spec:
key: node-role.kubernetes.io/master
volumes:
- configMap:
name: antrea-config-428d4tg64g
name: antrea-config-f95kf94mk9
name: antrea-config
- hostPath:
path: /var/log/antrea
Expand Down Expand Up @@ -555,7 +561,6 @@ spec:
name: antrea-agent
ports:
- containerPort: 10350
hostPort: 10350
name: api
protocol: TCP
readinessProbe:
Expand Down Expand Up @@ -658,7 +663,7 @@ spec:
operator: Exists
volumes:
- configMap:
name: antrea-config-428d4tg64g
name: antrea-config-f95kf94mk9
name: antrea-config
- hostPath:
path: /etc/cni/net.d
Expand Down
1 change: 0 additions & 1 deletion build/yamls/base/agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ spec:
fieldPath: spec.nodeName
ports:
- containerPort: 10350
hostPort: 10350
name: api
protocol: TCP
livenessProbe:
Expand Down
3 changes: 3 additions & 0 deletions build/yamls/base/conf/antrea-agent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-agent` container must be set to the same value.
#apiPort: 10350

# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener
#enablePrometheusMetrics: false
3 changes: 3 additions & 0 deletions build/yamls/base/conf/antrea-controller.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
# Note that if it's set to another value, the `containerPort` of the `api` port of the
# `antrea-controller` container must be set to the same value.
#apiPort: 10349

# Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener.
#enablePrometheusMetrics: false
11 changes: 10 additions & 1 deletion cmd/antrea-agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/vmware-tanzu/antrea/pkg/agent/controller/networkpolicy"
"github.com/vmware-tanzu/antrea/pkg/agent/controller/noderoute"
"github.com/vmware-tanzu/antrea/pkg/agent/interfacestore"
"github.com/vmware-tanzu/antrea/pkg/agent/metrics"
"github.com/vmware-tanzu/antrea/pkg/agent/openflow"
"github.com/vmware-tanzu/antrea/pkg/agent/querier"
"github.com/vmware-tanzu/antrea/pkg/agent/route"
Expand Down Expand Up @@ -161,11 +162,19 @@ func run(o *Options) error {
ovsBridgeClient,
networkPolicyController)

if o.config.EnablePrometheusMetrics {
metrics.InitializePrometheusMetrics(o.config.OVSBridge, ifaceStore, ofClient)
}

agentMonitor := monitor.NewAgentMonitor(crdClient, agentQuerier)

go agentMonitor.Run(stopCh)

apiServer, err := apiserver.New(agentQuerier, networkPolicyController, o.config.APIPort)
apiServer, err := apiserver.New(
agentQuerier,
networkPolicyController,
o.config.APIPort,
o.config.EnablePrometheusMetrics)
if err != nil {
return fmt.Errorf("error when creating agent API server: %v", err)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/antrea-agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@ type AgentConfig struct {
// APIPort is the port for the antrea-agent APIServer to serve on.
// Defaults to 10350.
APIPort int `yaml:"apiPort,omitempty"`
// Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener
// Defaults to false.
EnablePrometheusMetrics bool `yaml:"enablePrometheusMetrics,omitempty"`
}
3 changes: 3 additions & 0 deletions cmd/antrea-controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ type ControllerConfig struct {
// APIPort is the port for the antrea-controller APIServer to serve on.
// Defaults to 10349.
APIPort int `yaml:"apiPort,omitempty"`
// Enable metrics exposure via Prometheus. Initializes Prometheus metrics listener
// Defaults to false.
EnablePrometheusMetrics bool `yaml:"enablePrometheusMetrics,omitempty"`
}
15 changes: 13 additions & 2 deletions cmd/antrea-controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/vmware-tanzu/antrea/pkg/apiserver"
"github.com/vmware-tanzu/antrea/pkg/apiserver/openapi"
"github.com/vmware-tanzu/antrea/pkg/apiserver/storage"
"github.com/vmware-tanzu/antrea/pkg/controller/metrics"
"github.com/vmware-tanzu/antrea/pkg/controller/networkpolicy"
"github.com/vmware-tanzu/antrea/pkg/controller/networkpolicy/store"
"github.com/vmware-tanzu/antrea/pkg/controller/querier"
Expand Down Expand Up @@ -78,7 +79,8 @@ func run(o *Options) error {
addressGroupStore,
appliedToGroupStore,
networkPolicyStore,
controllerQuerier)
controllerQuerier,
o.config.EnablePrometheusMetrics)
if err != nil {
return fmt.Errorf("error creating API server config: %v", err)
}
Expand All @@ -100,6 +102,10 @@ func run(o *Options) error {

go apiServer.GenericAPIServer.PrepareRun().Run(stopCh)

if o.config.EnablePrometheusMetrics {
metrics.InitializePrometheusMetrics()
}

<-stopCh
klog.Info("Stopping Antrea controller")
return nil
Expand All @@ -110,13 +116,17 @@ func createAPIServerConfig(kubeconfig string,
addressGroupStore storage.Interface,
appliedToGroupStore storage.Interface,
networkPolicyStore storage.Interface,
controllerQuerier querier.ControllerQuerier) (*apiserver.Config, error) {
controllerQuerier querier.ControllerQuerier,
enableMetrics bool) (*apiserver.Config, error) {
// TODO:
// 1. Support user-provided certificate.
secureServing := genericoptions.NewSecureServingOptions().WithLoopback()
authentication := genericoptions.NewDelegatingAuthenticationOptions()
authorization := genericoptions.NewDelegatingAuthorizationOptions()

if enableMetrics {
authorization.WithAlwaysAllowPaths("/metrics")
}
// Set the PairName but leave certificate directory blank to generate in-memory by default
secureServing.ServerCert.CertDirectory = ""
secureServing.ServerCert.PairName = "antrea-apiserver"
Expand Down Expand Up @@ -146,6 +156,7 @@ func createAPIServerConfig(kubeconfig string,
openapi.GetOpenAPIDefinitions,
genericopenapi.NewDefinitionNamer(apiserver.Scheme))
serverConfig.OpenAPIConfig.Info.Title = "Antrea"
serverConfig.EnableMetrics = enableMetrics

return apiserver.NewConfig(
serverConfig,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/imdario/mergo v0.3.7 // indirect
github.com/j-keck/arping v1.0.0
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.4.1
github.com/spf13/cobra v0.0.5
Expand Down
23 changes: 2 additions & 21 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ import (
"github.com/vmware-tanzu/antrea/pkg/agent/types"
"github.com/vmware-tanzu/antrea/pkg/agent/util"
"github.com/vmware-tanzu/antrea/pkg/ovs/ovsconfig"
"github.com/vmware-tanzu/antrea/pkg/util/env"
)

const (
maxRetryForHostLink = 5
// nodeNameEnvKey is environment variable.
nodeNameEnvKey = "NODE_NAME"
// ipsecPSKEnvKey is environment variable.
ipsecPSKEnvKey = "ANTREA_IPSEC_PSK"
roundNumKey = "roundNum" // round number key in externalIDs.
Expand Down Expand Up @@ -436,7 +435,7 @@ func (i *Initializer) setupDefaultTunnelInterface(tunnelPortName string) error {
// initNodeLocalConfig retrieves node's subnet CIDR from node.spec.PodCIDR, which is used for IPAM and setup
// host gateway interface.
func (i *Initializer) initNodeLocalConfig() error {
nodeName, err := getNodeName()
nodeName, err := env.GetNodeName()
if err != nil {
return err
}
Expand Down Expand Up @@ -476,24 +475,6 @@ func (i *Initializer) initNodeLocalConfig() error {
return nil
}

// getNodeName returns the node's name used in Kubernetes, based on the priority:
// - Environment variable NODE_NAME, which should be set by Downward API
// - OS's hostname
func getNodeName() (string, error) {
nodeName := os.Getenv(nodeNameEnvKey)
if nodeName != "" {
return nodeName, nil
}
klog.Infof("Environment variable %s not found, using hostname instead", nodeNameEnvKey)
var err error
nodeName, err = os.Hostname()
if err != nil {
klog.Errorf("Failed to get local hostname: %v", err)
return "", err
}
return nodeName, nil
}

// readIPSecPSK reads the IPSec PSK value from environment variable
// ANTREA_IPSEC_PSK, when enableIPSecTunnel is set to true.
func (i *Initializer) readIPSecPSK() error {
Expand Down
Loading

0 comments on commit 0ab4ff6

Please sign in to comment.