-
Notifications
You must be signed in to change notification settings - Fork 181
Add e2e test for metrics registration #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,35 @@ | ||
| package e2e | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes" | ||
|
|
||
| "github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/operatorclient" | ||
| test "github.com/openshift/cluster-kube-apiserver-operator/test/library" | ||
| ) | ||
|
|
||
| func TestMetricsRegistration(t *testing.T) { | ||
| kubeConfig, err := test.NewClientConfigForTest() | ||
| require.NoError(t, err) | ||
| kubeClient, err := kubernetes.NewForConfig(kubeConfig) | ||
| require.NoError(t, err) | ||
|
|
||
| // get list of operator pods | ||
| pods, err := kubeClient.CoreV1().Pods(operatorclient.OperatorNamespace).List(metav1.ListOptions{}) | ||
| require.NoError(t, err) | ||
|
|
||
| // we just care about the one we expect | ||
| require.GreaterOrEqual(t, len(pods.Items), 1) | ||
| pod := pods.Items[0] | ||
|
|
||
| metrics, err := test.GetMetricsForPod(t, kubeConfig, &pod, 8443) | ||
| require.NoError(t, err) | ||
| t.Logf("Retrieved %d metrics.", len(metrics)) | ||
| if len(metrics) == 0 { | ||
| t.Fatal("No metrics retrieved.") | ||
| } | ||
| } | ||
This file contains hidden or 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,13 @@ | ||
| package library | ||
|
|
||
| import "net" | ||
|
|
||
| // FreeLocalTCPPort returns a local TCP port which is very likely to be unused. | ||
| func FreeLocalTCPPort() (int, error) { | ||
| l, err := net.Listen("tcp", ":0") | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| defer func() { _ = l.Close() }() | ||
| return l.Addr().(*net.TCPAddr).Port, nil | ||
| } |
This file contains hidden or 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,73 @@ | ||
| package library | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| v1 "k8s.io/api/core/v1" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/tools/clientcmd" | ||
| "k8s.io/client-go/tools/clientcmd/api" | ||
| ) | ||
|
|
||
| // GetMetricsForPod returns the metrics found at /metrics for a given pod. | ||
| func GetMetricsForPod(t *testing.T, kubeConfig *rest.Config, pod *v1.Pod, metricPort int) (map[string]string, error) { | ||
| data := bytes.NewBuffer(nil) | ||
|
|
||
| // function to get metrics at localhost:localPort/metrics | ||
| getMetricsFunc := func(localPort int) error { | ||
| // create config that uses the local port | ||
| config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( | ||
| clientcmd.NewDefaultClientConfigLoadingRules(), | ||
| &clientcmd.ConfigOverrides{ | ||
| ClusterInfo: api.Cluster{ | ||
| InsecureSkipTLSVerify: true, | ||
| Server: fmt.Sprintf("https://localhost:%d", localPort), | ||
| }, | ||
| }, | ||
| ).ClientConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| config = SetRESTConfigDefaults(config) | ||
| restClient, err := rest.RESTClientFor(config) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| t.Log("Retrieving metrics...") | ||
| stream, err := restClient.Get().RequestURI("/metrics").Stream() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer stream.Close() | ||
| _, err = io.Copy(data, stream) | ||
| return err | ||
| } | ||
|
|
||
| // we need to port-forward to the pod metric endpoint so that we can get metrics from from outside the cluster | ||
| err := ForwardPodPortAndExecute(t, kubeConfig, pod, metricPort, getMetricsFunc) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // parse raw metrics output into a map | ||
| metrics := map[string]string{} | ||
| scanner := bufio.NewScanner(data) | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
| entry := strings.SplitN(line, " ", 2) | ||
| metrics[entry[0]] = entry[1] | ||
| } | ||
| err = scanner.Err() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return metrics, nil | ||
| } |
This file contains hidden or 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,87 @@ | ||
| package library | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/tools/portforward" | ||
| "k8s.io/client-go/transport/spdy" | ||
| ) | ||
|
|
||
| // ForwardPodPortAndExecute forwards a local port to a pod port and executes the supplied func | ||
| func ForwardPodPortAndExecute(t *testing.T, kubeConfig *rest.Config, pod *corev1.Pod, podPort int, consumer func(localPort int) error) error { | ||
| // pod must be running | ||
| if pod.Status.Phase != corev1.PodRunning { | ||
| return fmt.Errorf("pod must be running") | ||
| } | ||
|
|
||
| //setup port forwarding | ||
| transport, upgrader, err := spdy.RoundTripperFor(kubeConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| restClient, err := rest.RESTClientFor(SetRESTConfigDefaults(kubeConfig)) | ||
| require.NoError(t, err) | ||
| request := restClient.Post().Resource("pods").Namespace(pod.Namespace).Name(pod.Name).SubResource("portforward") | ||
| localPort, err := FreeLocalTCPPort() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, request.URL()) | ||
| ports := []string{fmt.Sprintf("%d:%d", localPort, podPort)} | ||
| stop := make(chan struct{}, 1) | ||
| ready := make(chan struct{}, 1) | ||
| portForwarder, err := portforward.New(dialer, ports, stop, ready, bytes.NewBuffer(nil), ioutil.Discard) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // error from consumer function | ||
| var ferr error | ||
|
|
||
| // run consumer function | ||
| go func() { | ||
| // stop port forwarding when done | ||
| if stop != nil { | ||
| defer close(stop) | ||
| } | ||
| t.Log("Waiting for port forwarder to be ready...") | ||
| <-ready | ||
| t.Log("Port forwarder is ready.") | ||
| t.Log("Executing function...") | ||
| ferr = consumer(localPort) | ||
| }() | ||
|
|
||
| t.Log("Starting port forwarder...") | ||
| err = portForwarder.ForwardPorts() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| <-stop | ||
| t.Log("Port forwarder has stopped.") | ||
| return ferr | ||
| } | ||
|
|
||
| func SetRESTConfigDefaults(config *rest.Config) *rest.Config { | ||
| if config.GroupVersion == nil { | ||
| config.GroupVersion = &schema.GroupVersion{Group: "", Version: "v1"} | ||
| } | ||
| if config.NegotiatedSerializer == nil { | ||
| config.NegotiatedSerializer = scheme.Codecs | ||
| } | ||
| if len(config.UserAgent) == 0 { | ||
| config.UserAgent = rest.DefaultKubernetesUserAgent() | ||
| } | ||
| config.APIPath = "/api" | ||
| return config | ||
| } |
13 changes: 13 additions & 0 deletions
13
vendor/github.com/docker/spdystream/CONTRIBUTING.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this go to library-go as an e2e helper and just a oneliner here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intend to move to library-go once finalized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've opened openshift/library-go#596 to start.