/
core.go
136 lines (109 loc) · 3.89 KB
/
core.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
128
129
130
131
132
133
134
135
136
package env
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/go-autorest/autorest"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/containerservice"
"github.com/Azure/ARO-RP/pkg/util/instancemetadata"
"github.com/Azure/ARO-RP/pkg/util/liveconfig"
)
type ServiceComponent string
const (
COMPONENT_RP ServiceComponent = "RP"
COMPONENT_GATEWAY ServiceComponent = "GATEWAY"
COMPONENT_MONITOR ServiceComponent = "MONITOR"
COMPONENT_DBTOKEN ServiceComponent = "DBTOKEN"
COMPONENT_OPERATOR ServiceComponent = "OPERATOR"
COMPONENT_MIRROR ServiceComponent = "MIRROR"
COMPONENT_PORTAL ServiceComponent = "PORTAL"
COMPONENT_UPDATE_OCP_VERSIONS ServiceComponent = "UPDATE_OCP_VERSIONS"
COMPONENT_DEPLOY ServiceComponent = "DEPLOY"
COMPONENT_TOOLING ServiceComponent = "TOOLING"
)
// Core collects basic configuration information which is expected to be
// available on any PROD service VMSS (i.e. instance metadata, MSI authorizer,
// etc.)
type Core interface {
IsLocalDevelopmentMode() bool
IsCI() bool
NewMSITokenCredential() (azcore.TokenCredential, error)
NewMSIAuthorizer(...string) (autorest.Authorizer, error)
NewLiveConfigManager(context.Context) (liveconfig.Manager, error)
instancemetadata.InstanceMetadata
Component() string
Logger() *logrus.Entry
}
type core struct {
instancemetadata.InstanceMetadata
isLocalDevelopmentMode bool
isCI bool
component ServiceComponent
componentLog *logrus.Entry
}
func (c *core) IsLocalDevelopmentMode() bool {
return c.isLocalDevelopmentMode
}
func (c *core) IsCI() bool {
return c.isCI
}
func (c *core) Component() string {
return string(c.component)
}
func (c *core) Logger() *logrus.Entry {
return c.componentLog
}
func (c *core) NewLiveConfigManager(ctx context.Context) (liveconfig.Manager, error) {
msiAuthorizer, err := c.NewMSIAuthorizer(c.Environment().ResourceManagerScope)
if err != nil {
return nil, err
}
mcc := containerservice.NewManagedClustersClient(c.Environment(), c.SubscriptionID(), msiAuthorizer)
if c.isLocalDevelopmentMode {
return liveconfig.NewDev(c.Location(), mcc), nil
}
return liveconfig.NewProd(c.Location(), mcc), nil
}
func NewCore(ctx context.Context, log *logrus.Entry, component ServiceComponent) (Core, error) {
// assign results of package-level functions to struct's environment flags
isLocalDevelopmentMode := IsLocalDevelopmentMode()
isCI := IsCI()
componentLog := log.WithField("component", strings.ReplaceAll(strings.ToLower(string(component)), "_", "-"))
if isLocalDevelopmentMode {
log.Info("running in local development mode")
}
im, err := instancemetadata.New(ctx, log, isLocalDevelopmentMode)
if err != nil {
return nil, err
}
log.Infof("InstanceMetadata: running on %s", im.Environment().Name)
return &core{
InstanceMetadata: im,
isLocalDevelopmentMode: isLocalDevelopmentMode,
isCI: isCI,
component: component,
componentLog: componentLog,
}, nil
}
// NewCoreForCI returns an env.Core which respects RP_MODE but always uses
// AZURE_* environment variables instead of IMDS. This is used for entrypoints
// which may run on CI VMs. CI VMs don't currently have MSI and hence cannot
// resolve their tenant ID, and also may access resources in a different tenant
// (e.g. AME).
func NewCoreForCI(ctx context.Context, log *logrus.Entry) (Core, error) {
isLocalDevelopmentMode := IsLocalDevelopmentMode()
if isLocalDevelopmentMode {
log.Info("running in local development mode")
}
im, err := instancemetadata.NewDev(false)
if err != nil {
return nil, err
}
return &core{
InstanceMetadata: im,
isLocalDevelopmentMode: isLocalDevelopmentMode,
}, nil
}