forked from juju/juju
/
policy.go
196 lines (175 loc) · 6.17 KB
/
policy.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/series"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/state/cloudimagemetadata"
"github.com/juju/juju/storage"
)
// NewPolicyFunc is the type of a function that,
// given a *State, returns a Policy for that State.
type NewPolicyFunc func(*State) Policy
// Policy is an interface provided to State that may
// be consulted by State to validate or modify the
// behaviour of certain operations.
//
// If a Policy implementation does not implement one
// of the methods, it must return an error that
// satisfies errors.IsNotImplemented, and will thus
// be ignored. Any other error will cause an error
// in the use of the policy.
type Policy interface {
// Prechecker returns a Prechecker or an error.
Prechecker() (environs.InstancePrechecker, error)
// ProviderConfigSchemaSource returns a config.ConfigSchemaSource
// for the cloud, or an error.
ProviderConfigSchemaSource(cloudName string) (config.ConfigSchemaSource, error)
// ConfigValidator returns a config.Validator or an error.
ConfigValidator() (config.Validator, error)
// ConstraintsValidator returns a constraints.Validator or an error.
ConstraintsValidator(context.ProviderCallContext) (constraints.Validator, error)
// InstanceDistributor returns an context.Distributor or an error.
InstanceDistributor() (context.Distributor, error)
// StorageProviderRegistry returns a storage.ProviderRegistry or an error.
StorageProviderRegistry() (storage.ProviderRegistry, error)
}
// precheckInstance calls the state's assigned policy, if non-nil, to obtain
// a Prechecker, and calls PrecheckInstance if a non-nil Prechecker is returned.
func (st *State) precheckInstance(
base Base,
cons constraints.Value,
placement string,
volumeAttachments []storage.VolumeAttachmentParams,
) error {
if st.policy == nil {
return nil
}
prechecker, err := st.policy.Prechecker()
if errors.IsNotImplemented(err) {
return nil
} else if err != nil {
return err
}
if prechecker == nil {
return errors.New("policy returned nil prechecker without an error")
}
mBase, err := series.ParseBase(base.OS, base.Channel)
if err != nil {
return errors.Trace(err)
}
return prechecker.PrecheckInstance(
context.CallContext(st),
environs.PrecheckInstanceParams{
Base: mBase,
Constraints: cons,
Placement: placement,
VolumeAttachments: volumeAttachments,
})
}
func (st *State) constraintsValidator() (constraints.Validator, error) {
// Default behaviour is to simply use a standard validator with
// no model specific behaviour built in.
var validator constraints.Validator
if st.policy != nil {
var err error
validator, err = st.policy.ConstraintsValidator(context.CallContext(st))
if errors.IsNotImplemented(err) {
validator = constraints.NewValidator()
} else if err != nil {
return nil, err
} else if validator == nil {
return nil, errors.New("policy returned nil constraints validator without an error")
}
} else {
validator = constraints.NewValidator()
}
// Add supported architectures gleaned from cloud image
// metadata to the validator's vocabulary.
model, err := st.Model()
if err != nil {
return nil, errors.Annotate(err, "getting model")
}
if region := model.CloudRegion(); region != "" {
cfg, err := model.ModelConfig()
if err != nil {
return nil, errors.Trace(err)
}
arches, err := st.CloudImageMetadataStorage.SupportedArchitectures(
cloudimagemetadata.MetadataFilter{
Stream: cfg.AgentStream(),
Region: region,
},
)
if err != nil {
return nil, errors.Annotate(err, "querying supported architectures")
}
// Also include any arches that belong to manually provisioned machines
// See LP1946639.
manualMachArches, err := st.GetManualMachineArches()
if err != nil {
return nil, errors.Annotate(err, "querying supported architectures for manual machines")
}
if supportedArches := manualMachArches.Union(set.NewStrings(arches...)); len(supportedArches) != 0 {
validator.UpdateVocabulary(constraints.Arch, supportedArches.SortedValues())
}
}
return validator, nil
}
// ResolveConstraints combines the given constraints with the environ constraints to get
// a constraints which will be used to create a new instance.
func (st *State) ResolveConstraints(cons constraints.Value) (constraints.Value, error) {
validator, err := st.constraintsValidator()
if err != nil {
return constraints.Value{}, err
}
modelCons, err := st.ModelConstraints()
if err != nil {
return constraints.Value{}, err
}
return validator.Merge(modelCons, cons)
}
// validateConstraints returns an error if the given constraints are not valid for the
// current model, and also any unsupported attributes.
func (st *State) validateConstraints(cons constraints.Value) ([]string, error) {
validator, err := st.constraintsValidator()
if err != nil {
return nil, err
}
return validator.Validate(cons)
}
// validate calls the state's assigned policy, if non-nil, to obtain
// a config.Validator, and calls Validate if a non-nil config.Validator is
// returned.
func (st *State) validate(cfg, old *config.Config) (valid *config.Config, err error) {
if st.policy == nil {
return cfg, nil
}
configValidator, err := st.policy.ConfigValidator()
if errors.IsNotImplemented(err) {
return cfg, nil
} else if err != nil {
return nil, err
}
if configValidator == nil {
return nil, errors.New("policy returned nil configValidator without an error")
}
return configValidator.Validate(cfg, old)
}
func (st *State) storageProviderRegistry() (storage.ProviderRegistry, error) {
if st.policy == nil {
return storage.StaticProviderRegistry{}, nil
}
return st.policy.StorageProviderRegistry()
}
func (st *State) environsProviderConfigSchemaSource(cloudName string) (config.ConfigSchemaSource, error) {
if st.policy == nil {
return nil, errors.NotImplementedf("config.ProviderConfigSchemaSource")
}
return st.policy.ProviderConfigSchemaSource(cloudName)
}