Skip to content

Commit

Permalink
Merge pull request #28 from jenting/label-annotation-selector-optional
Browse files Browse the repository at this point in the history
Label annotation selector optional
  • Loading branch information
joe-elliott authored Apr 21, 2020
2 parents f4a8194 + c09bd33 commit 49d39c1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
13 changes: 6 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"os"
"time"

"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/joe-elliott/cert-exporter/src/args"
"github.com/joe-elliott/cert-exporter/src/checkers"
"github.com/joe-elliott/cert-exporter/src/exporters"

"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
Expand Down Expand Up @@ -44,8 +44,8 @@ func init() {
flag.Var(&secretsLabelSelector, "secrets-label-selector", "Label selector to find secrets to publish as metrics.")
flag.Var(&secretsAnnotationSelector, "secrets-annotation-selector", "Annotation selector to find secrets to publish as metrics.")
flag.StringVar(&secretsNamespace, "secrets-namespace", "", "Kubernetes namespace to list secrets.")
flag.Var(&includeSecretsDataGlobs, "secrets-include-glob", "Globs to match against secret data keys (Default \"*\").")
flag.Var(&excludeSecretsDataGlobs, "secrets-exclude-glob", "Globs to match against secret data keys.")
flag.Var(&includeSecretsDataGlobs, "secrets-include-glob", "Secret globs to include when looking for secret data keys (Default \"*\").")
flag.Var(&excludeSecretsDataGlobs, "secrets-exclude-glob", "Secret globs to exclude when looking for secret data keys.")
}

func main() {
Expand All @@ -63,11 +63,10 @@ func main() {
go configChecker.StartChecking()
}

if len(secretsLabelSelector) > 0 || len(secretsAnnotationSelector) > 0 {
if len(secretsLabelSelector) > 0 || len(secretsAnnotationSelector) > 0 || len(includeSecretsDataGlobs) > 0 {
if len(includeSecretsDataGlobs) == 0 {
includeSecretsDataGlobs = args.GlobArgs([]string{"*"})
}

configChecker := checkers.NewSecretChecker(pollingPeriod, secretsLabelSelector, includeSecretsDataGlobs, excludeSecretsDataGlobs, secretsAnnotationSelector, secretsNamespace, kubeconfigPath, &exporters.SecretExporter{})
go configChecker.StartChecking()
}
Expand Down
10 changes: 2 additions & 8 deletions src/checkers/periodicCertChecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/golang/glog"

"github.com/joe-elliott/cert-exporter/src/exporters"
"github.com/joe-elliott/cert-exporter/src/metrics"
)
Expand Down Expand Up @@ -35,17 +36,14 @@ func (p *PeriodicCertChecker) StartChecking() {

for {
glog.Info("Begin periodic check")

for _, match := range p.getMatches() {

if !p.includeFile(match) {
continue
}

glog.Infof("Publishing metrics for %v", match)
glog.Infof("Publishing %v node metrics %v", p.nodeName, match)

err := p.exporter.ExportMetrics(match, p.nodeName)

if err != nil {
metrics.ErrorTotal.Inc()
glog.Errorf("Error on %v: %v", match, err)
Expand All @@ -58,11 +56,8 @@ func (p *PeriodicCertChecker) StartChecking() {

func (p *PeriodicCertChecker) getMatches() []string {
ret := make([]string, 0)

for _, includeGlob := range p.includeCertGlobs {

matches, err := filepath.Glob(includeGlob)

if err != nil {
metrics.ErrorTotal.Inc()
glog.Errorf("Glob failed on %v: %v", includeGlob, err)
Expand All @@ -78,7 +73,6 @@ func (p *PeriodicCertChecker) getMatches() []string {
func (p *PeriodicCertChecker) includeFile(file string) bool {
for _, excludeGlob := range p.excludeCertGlobs {
exclude, err := filepath.Match(excludeGlob, file)

if err != nil {
metrics.ErrorTotal.Inc()
glog.Errorf("Match failed on %v,%v: %v", excludeGlob, file, err)
Expand Down
28 changes: 12 additions & 16 deletions src/checkers/periodicSecretChecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"path/filepath"
"time"

api_v1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/golang/glog"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"

"github.com/golang/glog"
"github.com/joe-elliott/cert-exporter/src/exporters"
"github.com/joe-elliott/cert-exporter/src/metrics"
)
Expand All @@ -27,7 +27,7 @@ type PeriodicSecretChecker struct {
}

// NewSecretChecker is a factory method that returns a new PeriodicSecretChecker
func NewSecretChecker(period time.Duration, labelSelectors []string, includeSecretsDataGlobs []string, excludeSecretsDataGlobs []string, annotationSelectors []string, namespace string, kubeconfigPath string, e *exporters.SecretExporter) *PeriodicSecretChecker {
func NewSecretChecker(period time.Duration, labelSelectors, includeSecretsDataGlobs, excludeSecretsDataGlobs, annotationSelectors []string, namespace, kubeconfigPath string, e *exporters.SecretExporter) *PeriodicSecretChecker {
return &PeriodicSecretChecker{
period: period,
labelSelectors: labelSelectors,
Expand Down Expand Up @@ -58,24 +58,22 @@ func (p *PeriodicSecretChecker) StartChecking() {
for {
glog.Info("Begin periodic check")

var secrets []api_v1.Secret
var secrets []corev1.Secret
if len(p.labelSelectors) > 0 {
for _, labelSelector := range p.labelSelectors {
var s *api_v1.SecretList
s, err = client.CoreV1().Secrets(p.namespace).List(v1.ListOptions{
var s *corev1.SecretList
s, err = client.CoreV1().Secrets(p.namespace).List(metav1.ListOptions{
LabelSelector: labelSelector,
})

if err != nil {
break
}

secrets = append(secrets, s.Items...)
}
} else {
var s *api_v1.SecretList
s, err = client.CoreV1().Secrets(p.namespace).List(v1.ListOptions{})

var s *corev1.SecretList
s, err = client.CoreV1().Secrets(p.namespace).List(metav1.ListOptions{})
if err == nil {
secrets = s.Items
}
Expand All @@ -92,9 +90,7 @@ func (p *PeriodicSecretChecker) StartChecking() {

if len(p.annotationSelectors) > 0 {
matches := false

annotations := secret.GetAnnotations()

for _, selector := range p.annotationSelectors {
_, ok := annotations[selector]
if ok {
Expand All @@ -107,7 +103,7 @@ func (p *PeriodicSecretChecker) StartChecking() {
continue
}
}
glog.Infof("Annotations matched. Parsing Secret.")
glog.Infof("Annotations matched. Parsing Secret.")

for name, bytes := range secret.Data {
include, exclude := false, false
Expand Down Expand Up @@ -139,14 +135,14 @@ func (p *PeriodicSecretChecker) StartChecking() {
}

if include && !exclude {
glog.Infof("Publishing %v/%v metrics %v", secret.Name, secret.Namespace, name)
err = p.exporter.ExportMetrics(bytes, name, secret.Name, secret.Namespace)

if err != nil {
glog.Errorf("Error exporting secret %v", err)
metrics.ErrorTotal.Inc()
}
} else {
glog.Infof("Ignoring %v. Does not match %v or matches %v.", name, p.includeSecretsDataGlobs, p.excludeSecretsDataGlobs)
glog.Infof("Ignoring %v. Does not match %v or matches %v.", name, p.includeSecretsDataGlobs, p.excludeSecretsDataGlobs)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/exporters/kubeConfigExporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *KubeConfigExporter) ExportMetrics(file, nodeName string) error {
return nil
}

func pathToFileFromKubeConfig(file string, kubeConfigFile string) string {
func pathToFileFromKubeConfig(file, kubeConfigFile string) string {
if !path.IsAbs(file) {
kubeConfigPath := path.Dir(kubeConfigFile)
file = path.Join(kubeConfigPath, file)
Expand Down
2 changes: 1 addition & 1 deletion src/exporters/secretExporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type SecretExporter struct {
}

// ExportMetrics exports the provided PEM file
func (c *SecretExporter) ExportMetrics(bytes []byte, keyName string, secretName string, secretNamespace string) error {
func (c *SecretExporter) ExportMetrics(bytes []byte, keyName, secretName, secretNamespace string) error {
metric, err := secondsToExpiryFromCertAsBytes(bytes)
if err != nil {
return err
Expand Down

0 comments on commit 49d39c1

Please sign in to comment.