-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from etiennecoutaud/expose-metrics
pkg/{sdk,generator} Expose Prometheus metrics port
- Loading branch information
Showing
8 changed files
with
236 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package sdk | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
k8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
) | ||
|
||
// ExposeMetricsPort generate a Kubernetes Service to expose metrics port | ||
func ExposeMetricsPort() { | ||
http.Handle("/"+k8sutil.PrometheusMetricsPortName, promhttp.Handler()) | ||
go http.ListenAndServe(":"+strconv.Itoa(k8sutil.PrometheusMetricsPort), nil) | ||
|
||
service, err := k8sutil.InitOperatorService() | ||
if err != nil { | ||
logrus.Fatalf("Failed to init operator service: %v", err) | ||
} | ||
err = Create(service) | ||
if err != nil && !errors.IsAlreadyExists(err) { | ||
logrus.Infof("Failed to create operator service: %v", err) | ||
return | ||
} | ||
logrus.Infof("Metrics service %s created", service.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package k8sutil | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetOperatorName(t *testing.T) { | ||
type Output struct { | ||
operatorName string | ||
err error | ||
} | ||
|
||
type Scenario struct { | ||
name string | ||
envVarKey string | ||
envVarValue string | ||
expectedOutput Output | ||
} | ||
|
||
tests := []Scenario{ | ||
Scenario{ | ||
name: "Simple case", | ||
envVarKey: OperatorNameEnvVar, | ||
envVarValue: "myoperator", | ||
expectedOutput: Output{ | ||
operatorName: "myoperator", | ||
err: nil, | ||
}, | ||
}, | ||
Scenario{ | ||
name: "Unset env var", | ||
envVarKey: "", | ||
envVarValue: "", | ||
expectedOutput: Output{ | ||
operatorName: "", | ||
err: fmt.Errorf("%s must be set", OperatorNameEnvVar), | ||
}, | ||
}, | ||
Scenario{ | ||
name: "Empty env var", | ||
envVarKey: OperatorNameEnvVar, | ||
envVarValue: "", | ||
expectedOutput: Output{ | ||
operatorName: "", | ||
err: fmt.Errorf("%s must not be empty", OperatorNameEnvVar), | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
_ = os.Setenv(test.envVarKey, test.envVarValue) | ||
operatorName, err := GetOperatorName() | ||
if !(operatorName == test.expectedOutput.operatorName && reflect.DeepEqual(err, test.expectedOutput.err)) { | ||
t.Errorf("test %s failed, expected ouput: %s,%v; got: %s,%v", test.name, test.expectedOutput.operatorName, test.expectedOutput.err, operatorName, err) | ||
} | ||
_ = os.Unsetenv(test.envVarKey) | ||
} | ||
} |