Skip to content
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

Integration Tests: Adds injector #7520

Merged
merged 22 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2b2c15a
Integration Tests: Adds injector binary
JoshVanL Feb 9, 2024
bcb0bed
Merge branch 'master' into test-integration-binary-injector
dapr-bot Feb 12, 2024
df35271
Merge branch 'master' into test-integration-binary-injector
dapr-bot Feb 14, 2024
e2c3f3d
Merge branch 'master' into test-integration-binary-injector
dapr-bot Feb 26, 2024
3d2f143
Merge branch 'master' into test-integration-binary-injector
dapr-bot Feb 26, 2024
f8efdd3
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 4, 2024
40435db
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 6, 2024
47bd15b
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 6, 2024
28e4c70
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 6, 2024
69d6933
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 6, 2024
0904813
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 7, 2024
2460b73
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 7, 2024
8e89a4e
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 7, 2024
27e4ef3
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 8, 2024
427d52e
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 9, 2024
d3883aa
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 11, 2024
371841f
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 12, 2024
f02a311
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 13, 2024
c2e256b
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 20, 2024
a4acd1d
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 25, 2024
24ef016
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 25, 2024
5980966
Merge branch 'master' into test-integration-binary-injector
dapr-bot Mar 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/injector/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func Run() {
}

inj, err := service.NewInjector(service.Options{
Port: opts.Port,
AuthUIDs: uids,
Config: cfg,
DaprClient: daprClient,
Expand Down
2 changes: 2 additions & 0 deletions cmd/injector/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
type Options struct {
HealthzPort int
Kubeconfig string
Port int
Logger logger.Options
Metrics *metrics.Options
}
Expand Down Expand Up @@ -53,6 +54,7 @@ func New(origArgs []string) *Options {
fs.SortFlags = true

fs.IntVar(&opts.HealthzPort, "healthz-port", 8080, "The port used for health checks")
fs.IntVar(&opts.Port, "port", 4000, "The port used for the injector service")

if home := homedir.HomeDir(); home != "" {
fs.StringVar(&opts.Kubeconfig, "kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
Expand Down
4 changes: 2 additions & 2 deletions pkg/injector/service/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
)

const (
port = 4000
getKubernetesServiceAccountTimeoutSeconds = 10
systemGroup = "system:masters"
serviceAccountUserInfoPrefix = "system:serviceaccount:"
Expand Down Expand Up @@ -72,6 +71,7 @@ type Options struct {
Config Config
DaprClient scheme.Interface
KubeClient kubernetes.Interface
Port int

ControlPlaneNamespace string
ControlPlaneTrustDomain string
Expand Down Expand Up @@ -140,7 +140,7 @@ func NewInjector(opts Options) (Injector, error) {
runtime.NewScheme(),
).UniversalDeserializer(),
server: &http.Server{
Addr: fmt.Sprintf(":%d", port),
Addr: fmt.Sprintf(":%d", opts.Port),
Handler: mux,
ReadHeaderTimeout: 10 * time.Second,
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/framework/binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
func BuildAll(t *testing.T) {
t.Helper()

binaryNames := []string{"daprd", "placement", "sentry", "operator"}
binaryNames := []string{"daprd", "placement", "sentry", "operator", "injector"}

var wg sync.WaitGroup
wg.Add(len(binaryNames))
Expand Down
155 changes: 155 additions & 0 deletions tests/integration/framework/process/injector/injector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
Copyright 2024 The Dapr Authors
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.
*/

package injector

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"testing"
"time"

"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"

"github.com/dapr/dapr/tests/integration/framework/binary"
"github.com/dapr/dapr/tests/integration/framework/process"
"github.com/dapr/dapr/tests/integration/framework/process/exec"
"github.com/dapr/dapr/tests/integration/framework/process/kubernetes"
"github.com/dapr/dapr/tests/integration/framework/util"
)

type Injector struct {
kubeapi *kubernetes.Kubernetes
exec process.Interface
freeport *util.FreePort

port int
namespace string
metricsPort int
healthzPort int
}

func New(t *testing.T, fopts ...Option) *Injector {
t.Helper()

fp := util.ReservePorts(t, 3)
opts := options{
logLevel: "info",
enableMetrics: true,
port: fp.Port(t, 0),
metricsPort: fp.Port(t, 1),
healthzPort: fp.Port(t, 2),
sidecarImage: "integration.dapr.io/dapr",
}

for _, fopt := range fopts {
fopt(&opts)
}

require.NotNil(t, opts.sentry, "sentry is required")
require.NotNil(t, opts.namespace, "namespace is required")

mobj, err := json.Marshal(new(admissionregistrationv1.MutatingWebhookConfiguration))
require.NoError(t, err)
kubeapi := kubernetes.New(t, kubernetes.WithBaseOperatorAPI(t,
spiffeid.RequireTrustDomainFromString(opts.sentry.TrustDomain(t)),
*opts.namespace,
opts.sentry.Port(),
),
kubernetes.WithPath("/apis/admissionregistration.k8s.io/v1/mutatingwebhookconfigurations/dapr-sidecar-injector", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.Write(mobj)
}),
)

args := []string{
"-log-level=" + opts.logLevel,
"-port=" + strconv.Itoa(opts.port),
"-metrics-port=" + strconv.Itoa(opts.metricsPort),
"-healthz-port=" + strconv.Itoa(opts.healthzPort),
"-kubeconfig=" + kubeapi.KubeconfigPath(t),
}

return &Injector{
kubeapi: kubeapi,
exec: exec.New(t,
binary.EnvValue("injector"), args,
append(
opts.execOpts,
exec.WithEnvVars(t,
"KUBERNETES_SERVICE_HOST", "anything",
"NAMESPACE", *opts.namespace,
"SIDECAR_IMAGE", opts.sidecarImage,
"DAPR_TRUST_ANCHORS_FILE", opts.sentry.TrustAnchorsFile(t),
"DAPR_CONTROL_PLANE_TRUST_DOMAIN", opts.sentry.TrustDomain(t),
"DAPR_SENTRY_ADDRESS", opts.sentry.Address(),
),
)...,
),
freeport: fp,
port: opts.port,
metricsPort: opts.metricsPort,
healthzPort: opts.healthzPort,
namespace: *opts.namespace,
}
}

func (i *Injector) Run(t *testing.T, ctx context.Context) {
i.kubeapi.Run(t, ctx)
i.freeport.Free(t)
i.exec.Run(t, ctx)
}

func (i *Injector) Cleanup(t *testing.T) {
i.exec.Cleanup(t)
i.kubeapi.Cleanup(t)
}

func (i *Injector) WaitUntilRunning(t *testing.T, ctx context.Context) {
client := util.HTTPClient(t)
//nolint:testifylint
assert.EventuallyWithT(t, func(t *assert.CollectT) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://localhost:%d/healthz", i.healthzPort), nil)
if !assert.NoError(t, err) {
return
}
resp, err := client.Do(req)
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
assert.Equal(t, http.StatusOK, resp.StatusCode)
}, time.Second*5, 10*time.Millisecond)
}

func (i *Injector) Port() int {
return i.port
}

func (i *Injector) Address() string {
return "localhost:" + strconv.Itoa(i.port)
}

func (i *Injector) MetricsPort() int {
return i.metricsPort
}

func (i *Injector) HealthzPort() int {
return i.healthzPort
}
92 changes: 92 additions & 0 deletions tests/integration/framework/process/injector/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2024 The Dapr Authors
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.
*/

package injector

import (
"github.com/dapr/dapr/tests/integration/framework/process/exec"
"github.com/dapr/dapr/tests/integration/framework/process/sentry"
)

// options contains the options for running Injector in integration tests.
type options struct {
execOpts []exec.Option

logLevel string
namespace *string
port int
enableMetrics bool
metricsPort int
healthzPort int

sidecarImage string

sentry *sentry.Sentry
}

// Option is a function that configures the process.
type Option func(*options)

func WithExecOptions(execOptions ...exec.Option) Option {
return func(o *options) {
o.execOpts = execOptions
}
}

func WithLogLevel(level string) Option {
return func(o *options) {
o.logLevel = level
}
}

func WithNamespace(namespace string) Option {
return func(o *options) {
o.namespace = &namespace
}
}

func WithPort(port int) Option {
return func(o *options) {
o.port = port
}
}

func WithMetricsPort(port int) Option {
return func(o *options) {
o.metricsPort = port
}
}

func WithEnableMetrics(enable bool) Option {
return func(o *options) {
o.enableMetrics = enable
}
}

func WithHealthzPort(port int) Option {
return func(o *options) {
o.healthzPort = port
}
}

func WithSidecarImage(image string) Option {
return func(o *options) {
o.sidecarImage = image
}
}

func WithSentry(sentry *sentry.Sentry) Option {
return func(o *options) {
o.sentry = sentry
}
}
5 changes: 5 additions & 0 deletions tests/integration/framework/process/kubernetes/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func WithClusterStatefulSetList(t *testing.T, ss *appsv1.StatefulSetList) Option
return handleClusterListResource(t, "/apis/apps/v1/statefulsets", ss)
}

func WithClusterServiceAccountList(t *testing.T, services *corev1.ServiceAccountList) Option {
return handleClusterListResource(t, "/api/v1/serviceaccounts", services)
}

func WithDaprConfigurationGet(t *testing.T, config *configapi.Configuration) Option {
return handleGetResource(t, "/apis/dapr.io/v1alpha1", "configurations", config.Namespace, config.Name, config)
}
Expand Down Expand Up @@ -118,6 +122,7 @@ func WithBaseOperatorAPI(t *testing.T, td spiffeid.TrustDomain, ns string, sentr
WithClusterServiceList(t, &corev1.ServiceList{TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "ServiceList"}}),
WithClusterStatefulSetList(t, &appsv1.StatefulSetList{TypeMeta: metav1.TypeMeta{APIVersion: "apps/v1", Kind: "StatefulSetList"}}),
WithClusterDeploymentList(t, &appsv1.DeploymentList{TypeMeta: metav1.TypeMeta{APIVersion: "apps/v1", Kind: "DeploymentList"}}),
WithClusterServiceAccountList(t, &corev1.ServiceAccountList{TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "ServiceAccountList"}}),
WithClusterDaprComponentList(t, &compapi.ComponentList{TypeMeta: metav1.TypeMeta{APIVersion: "dapr.io/v1alpha1", Kind: "ComponentList"}}),
WithClusterDaprHTTPEndpointList(t, &httpendapi.HTTPEndpointList{TypeMeta: metav1.TypeMeta{APIVersion: "dapr.io/v1alpha1", Kind: "HTTPEndpointList"}}),
WithClusterDaprResiliencyList(t, &resapi.ResiliencyList{TypeMeta: metav1.TypeMeta{APIVersion: "dapr.io/v1alpha1", Kind: "ResiliencyList"}}),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/framework/process/operator/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/dapr/dapr/tests/integration/framework/process/exec"
)

// options contains the options for running Sentry in integration tests.
// options contains the options for running Operator in integration tests.
type options struct {
execOpts []exec.Option

Expand Down
7 changes: 7 additions & 0 deletions tests/integration/framework/process/sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Sentry struct {
port int
healthzPort int
metricsPort int
trustDomain *string
}

func New(t *testing.T, fopts ...Option) *Sentry {
Expand Down Expand Up @@ -136,6 +137,7 @@ func New(t *testing.T, fopts ...Option) *Sentry {
port: opts.port,
metricsPort: opts.metricsPort,
healthzPort: opts.healthzPort,
trustDomain: opts.trustDomain,
}
}

Expand Down Expand Up @@ -190,6 +192,11 @@ func (s *Sentry) HealthzPort() int {
return s.healthzPort
}

func (s *Sentry) TrustDomain(t *testing.T) string {
require.NotNil(t, s.trustDomain)
return *s.trustDomain
}

// DialGRPC dials the sentry using the given context and returns a grpc client
// connection.
func (s *Sentry) DialGRPC(t *testing.T, ctx context.Context, sentryID string) *grpc.ClientConn {
Expand Down
Loading
Loading