-
Notifications
You must be signed in to change notification settings - Fork 9
/
environment.go
57 lines (48 loc) · 1.76 KB
/
environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package testing
import (
"context"
"google.golang.org/protobuf/types/known/anypb"
"namespacelabs.dev/foundation/internal/build/registry"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/internal/providers/nscloud"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
"namespacelabs.dev/foundation/internal/runtime/kubernetes/client"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/go-ids"
)
var (
UseNamespaceCloud = false
UseNamespaceBuildCluster = false
)
func PrepareEnv(ctx context.Context, sourceEnv cfg.Context, ephemeral bool) (cfg.Context, error) {
testInv := ids.NewRandomBase32ID(8)
testEnv := &schema.Environment{
Name: "test-" + testInv,
Purpose: schema.Environment_TESTING,
Runtime: "kubernetes",
Ephemeral: ephemeral,
}
var messages []*anypb.Any
if UseNamespaceBuildCluster {
msg, err := nscloud.EnsureBuildCluster(ctx, api.Methods)
if err != nil {
return nil, err
}
messages = append(messages, protos.WrapAnyOrDie(msg))
}
newCfg := sourceEnv.Configuration().Derive(testEnv.Name, func(previous cfg.ConfigurationSlice) cfg.ConfigurationSlice {
if UseNamespaceCloud {
messages = append(messages, protos.WrapAnysOrDie(
&client.HostEnv{Provider: "nscloud"},
®istry.Provider{Provider: "nscloud"},
)...)
}
// Prepend as the testing configurations should take precedence.
return cfg.ConfigurationSlice{Configuration: append(messages, previous.Configuration...)}
})
return cfg.MakeUnverifiedContext(newCfg, testEnv), nil
}