-
Notifications
You must be signed in to change notification settings - Fork 9
/
naming.go
127 lines (100 loc) · 3.27 KB
/
naming.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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 runtime
import (
"context"
"errors"
"fmt"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
)
const (
LocalBaseDomain = "nslocal.host"
CloudBaseDomain = "nscloud.dev"
)
var (
NamingNoTLS = false // Set to true in CI.
WorkInProgressUseShortAlias = false
)
var errLogin = fnerrors.UsageError("Please run `ns login` to login.",
"Namespace automatically manages nscloud.dev-based sub-domains and issues SSL certificates on your behalf. To use these features, you'll need to login to Namespace using your Github account.")
func ComputeNaming(ctx context.Context, ws string, env cfg.Context, cluster Planner, source *schema.Naming) (*schema.ComputedNaming, error) {
result, err := computeNaming(ctx, ws, env, cluster, source)
if err != nil {
return nil, err
}
fmt.Fprintf(console.Debug(ctx), "computed naming: %+v\n", result)
return result, nil
}
func computeNaming(ctx context.Context, workspace string, env cfg.Context, cluster Planner, source *schema.Naming) (*schema.ComputedNaming, error) {
naming, err := computeInnerNaming(ctx, env, cluster, source)
if err != nil {
return nil, err
}
naming.MainModuleName = workspace
naming.UseShortAlias = naming.GetUseShortAlias() || WorkInProgressUseShortAlias
return naming, nil
}
func computeInnerNaming(ctx context.Context, rootenv cfg.Context, cluster Planner, source *schema.Naming) (*schema.ComputedNaming, error) {
base, err := cluster.ComputeBaseNaming(source)
if err != nil {
return nil, err
}
if base != nil {
return base, nil
}
env := rootenv.Environment()
if env.Purpose != schema.Environment_PRODUCTION {
return &schema.ComputedNaming{
Source: source,
BaseDomain: LocalBaseDomain,
Managed: schema.Domain_LOCAL_MANAGED,
}, nil
}
if !source.GetEnableNamespaceManaged() {
return &schema.ComputedNaming{}, nil
}
userAuth, err := fnapi.LoadUser()
if err != nil {
if errors.Is(err, fnapi.ErrRelogin) {
return nil, errLogin
}
return nil, err
}
org := userAuth.Org
if org == "" {
org = userAuth.Username
}
if env.Purpose == schema.Environment_PRODUCTION {
if orgOverride := source.GetWithOrg(); orgOverride != "" {
org = orgOverride
}
}
return &schema.ComputedNaming{
Source: source,
BaseDomain: fmt.Sprintf("%s.%s", org, CloudBaseDomain),
Managed: schema.Domain_CLOUD_MANAGED,
}, nil
}
func allocateName(ctx context.Context, srv Deployable, opts fnapi.AllocateOpts) (*schema.Certificate, error) {
opts.NoTLS = NamingNoTLS
opts.Scope = srv.GetPackageRef().AsPackageName()
nr, err := fnapi.AllocateName(ctx, opts)
if err != nil {
return nil, err
}
return certFromResource(nr), nil
}
func certFromResource(res *fnapi.NameResource) *schema.Certificate {
if res.Certificate.PrivateKey != nil && res.Certificate.CertificateBundle != nil {
return &schema.Certificate{
PrivateKey: res.Certificate.PrivateKey,
CertificateBundle: res.Certificate.CertificateBundle,
}
}
return nil
}