-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(e2e): use a fake metric service for e2e tests
- Loading branch information
1 parent
42f1ac3
commit e3051fd
Showing
12 changed files
with
1,345 additions
and
47 deletions.
There are no files selected for viewing
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,29 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# 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. | ||
|
||
FROM google-go.pkg.dev/golang:1.22.3@sha256:efc6b824652199ede8bc25e2d5a57076e0e1ffbe90735dcbda3ee956fe33b612 as buildbase | ||
WORKDIR /app | ||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
COPY cmd cmd | ||
COPY pkg pkg | ||
COPY vendor vendor | ||
|
||
FROM buildbase as appbase | ||
RUN CGO_ENABLED=1 GOEXPERIMENT=boringcrypto \ | ||
go build -tags boring -mod=vendor -o fake-metric-service cmd/fake-metric-service/*.go | ||
|
||
FROM gke.gcr.io/gke-distroless/libc:gke_distroless_20240307.00_p0@sha256:4f834e207f2721977094aeec4c9daee7032c5daec2083c0be97760f4306e4f88 | ||
COPY --from=appbase /app/fake-metric-service /bin/fake-metric-service | ||
ENTRYPOINT ["/bin/fake-metric-service"] |
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,123 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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 main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"net" | ||
"net/http" | ||
"os" | ||
"sync" | ||
|
||
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb" | ||
"github.com/GoogleCloudPlatform/prometheus-engine/pkg/e2e" | ||
"github.com/go-logr/logr" | ||
"go.uber.org/zap/zapcore" | ||
"google.golang.org/grpc" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
) | ||
|
||
func main() { | ||
logVerbosity := 0 | ||
probeAddr := ":8080" | ||
metricServiceAddr := ":8081" | ||
|
||
flag.IntVar(&logVerbosity, "v", logVerbosity, "Logging verbosity") | ||
flag.StringVar(&probeAddr, "probe-addr", probeAddr, "Address to outputs probe statuses (e.g. /readyz and /livez)") | ||
flag.StringVar(&metricServiceAddr, "metric-service-addr", metricServiceAddr, "Address to serve a mock metric service server.") | ||
flag.Parse() | ||
|
||
logger := zap.New(zap.Level(zapcore.Level(-logVerbosity))) | ||
ctrl.SetLogger(logger) | ||
|
||
ctx := signals.SetupSignalHandler() | ||
if err := run(ctx, logger, probeAddr, metricServiceAddr); err != nil { | ||
logger.Error(err, "exit with error") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func run(ctx context.Context, logger logr.Logger, probeAddr, metricServiceAddr string) error { | ||
listener, err := net.Listen("tcp", metricServiceAddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
wg := sync.WaitGroup{} | ||
errs := make(chan error, 1) | ||
|
||
logger.Info("starting server...") | ||
|
||
{ | ||
s := grpc.NewServer() | ||
monitoringpb.RegisterMetricServiceServer(s, e2e.NewFakeMetricServer()) | ||
|
||
wg.Add(1) | ||
go func() { | ||
for range ctx.Done() { | ||
s.GracefulStop() | ||
return | ||
} | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
if err := s.Serve(listener); err != nil { | ||
errs <- err | ||
} | ||
}() | ||
} | ||
|
||
{ | ||
wg.Add(1) | ||
mux := http.NewServeMux() | ||
mux.Handle("/readyz", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
mux.Handle("/livez", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
server := http.Server{ | ||
Addr: probeAddr, | ||
Handler: mux, | ||
} | ||
go func() { | ||
for range ctx.Done() { | ||
// Start new context because ours is done. | ||
if err := server.Shutdown(context.Background()); err != nil { | ||
errs <- err | ||
} | ||
} | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
if err := server.ListenAndServe(); err != nil { | ||
errs <- err | ||
} | ||
}() | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(errs) | ||
}() | ||
|
||
for err := range errs { | ||
return err | ||
} | ||
return nil | ||
} |
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,105 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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 deploy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/GoogleCloudPlatform/prometheus-engine/e2e/kube" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func CreateFakeMetricService(ctx context.Context, kubeClient client.Client, namespace, name, image string) error { | ||
labels := map[string]string{ | ||
"app.kubernetes.io/name": "fake-metric-service", | ||
} | ||
if err := kubeClient.Create(ctx, &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: labels, | ||
}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: labels, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "server", | ||
Image: image, | ||
Ports: []corev1.ContainerPort{ | ||
{ | ||
Name: "metric-service", | ||
ContainerPort: 8081, | ||
}, | ||
}, | ||
LivenessProbe: &corev1.Probe{ | ||
ProbeHandler: corev1.ProbeHandler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: "livez", | ||
Port: intstr.FromInt(8080), | ||
Scheme: "HTTP", | ||
}, | ||
}, | ||
}, | ||
ReadinessProbe: &corev1.Probe{ | ||
ProbeHandler: corev1.ProbeHandler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: "readyz", | ||
Port: intstr.FromInt(8080), | ||
Scheme: "HTTP", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); err != nil { | ||
return fmt.Errorf("create metric-service deployment: %w", err) | ||
} | ||
if err := kubeClient.Create(ctx, &corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Spec: corev1.ServiceSpec{ | ||
Selector: labels, | ||
Ports: []corev1.ServicePort{ | ||
{ | ||
Port: 8080, | ||
TargetPort: intstr.FromString("metric-service"), | ||
}, | ||
}, | ||
}, | ||
}); err != nil { | ||
return fmt.Errorf("create metric-service service: %w", err) | ||
} | ||
return kube.WaitForDeploymentReady(ctx, kubeClient, namespace, name) | ||
} | ||
|
||
func FakeMetricServiceEndpoint() string { | ||
return "metric-service.default.svc.cluster.local:8081" | ||
} |
Oops, something went wrong.