/
controller.go
225 lines (203 loc) · 7.17 KB
/
controller.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package common
import (
stdcontext "context"
"fmt"
"io"
"strings"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/retry"
"github.com/juju/juju/api"
"github.com/juju/juju/api/client/block"
caasprovider "github.com/juju/juju/caas/kubernetes/provider"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/network"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/jujuclient"
"github.com/juju/juju/rpc/params"
)
var (
bootstrapReadyPollDelay = 3 * time.Second
bootstrapReadyPollCount = 60
blockAPI = getBlockAPI
)
type listBlocksAPI interface {
List() ([]params.Block, error)
Close() error
}
// getBlockAPI returns a block api for listing blocks.
func getBlockAPI(c *modelcmd.ModelCommandBase) (listBlocksAPI, error) {
// Set a short dial timeout so WaitForAgentInitialisation can check
// ctx.Done() in its retry loop.
dialOpts := api.DefaultDialOpts()
dialOpts.Timeout = 6 * time.Second
root, err := c.NewAPIRootWithDialOpts(&dialOpts)
if err != nil {
return nil, errors.Trace(err)
}
return block.NewClient(root), nil
}
// tryAPI attempts to open the API and makes a trivial call
// to check if the API is available yet.
func tryAPI(c *modelcmd.ModelCommandBase) error {
client, err := blockAPI(c)
if err == nil {
_, err = client.List()
closeErr := client.Close()
if closeErr != nil {
logger.Debugf("Error closing client: %v", closeErr)
}
}
return err
}
// WaitForAgentInitialisation polls the bootstrapped controller with a read-only
// command which will fail until the controller is fully initialised.
// TODO(wallyworld) - add a bespoke command to maybe the admin facade for this purpose.
func WaitForAgentInitialisation(
ctx environs.BootstrapContext,
c *modelcmd.ModelCommandBase,
isCAASController bool,
controllerName string,
) (err error) {
if ctx.Context().Err() != nil {
return errors.Errorf("unable to contact api server: (%v)", ctx.Context().Err())
}
// Make a best effort to find the new controller address so we can print it.
var addressInfo string
controller, err := c.ClientStore().ControllerByName(controllerName)
if err == nil && len(controller.APIEndpoints) > 0 {
addr, err := network.ParseMachineHostPort(controller.APIEndpoints[0])
if err == nil {
addressInfo = fmt.Sprintf(" at %s", addr.Host())
}
}
ctx.Infof("Contacting Juju controller%s to verify accessibility...", addressInfo)
var apiAttempts int
err = retry.Call(retry.CallArgs{
Clock: clock.WallClock,
Attempts: bootstrapReadyPollCount,
Delay: bootstrapReadyPollDelay,
Stop: ctx.Context().Done(),
NotifyFunc: func(lastErr error, attempts int) {
apiAttempts = attempts
},
IsFatalError: func(err error) bool {
return errors.Is(err, &unknownError{}) ||
retry.IsRetryStopped(err) ||
errors.Is(err, stdcontext.Canceled)
},
Func: func() error {
retryErr := tryAPI(c)
if retryErr == nil {
msg := fmt.Sprintf("\nBootstrap complete, controller %q is now available", controllerName)
if isCAASController {
msg += fmt.Sprintf(" in namespace %q", caasprovider.DecideControllerNamespace(controllerName))
} else {
msg += fmt.Sprintf("\nController machines are in the %q model", bootstrap.ControllerModelName)
}
ctx.Infof(msg)
return nil
}
// As the API server is coming up, it goes through a number of steps.
// Initially the upgrade steps run, but the api server allows some
// calls to be processed during the upgrade, but not the list blocks.
// Logins are also blocked during space discovery.
// It is also possible that the underlying database causes connections
// to be dropped as it is initialising, or reconfiguring. These can
// lead to EOF or "connection is shut down" error messages. We skip
// these too, hoping that things come back up before the end of the
// retry poll count.
cause := errors.Cause(retryErr)
errorMessage := cause.Error()
switch {
case cause == io.EOF,
strings.HasSuffix(errorMessage, "no such host"), // wait for dns getting resolvable, aws elb for example.
strings.HasSuffix(errorMessage, "connection refused"),
strings.HasSuffix(errorMessage, "target machine actively refused it."), // Winsock message for connection refused
strings.HasSuffix(errorMessage, "connection is shut down"),
strings.HasSuffix(errorMessage, "i/o timeout"),
strings.HasSuffix(errorMessage, "network is unreachable"),
strings.HasSuffix(errorMessage, "deadline exceeded"),
strings.HasSuffix(errorMessage, "no api connection available"):
ctx.Verbosef("Still waiting for API to become available: %v", retryErr)
return retryErr
case params.ErrCode(retryErr) == params.CodeUpgradeInProgress:
ctx.Verbosef("Still waiting for API to become available: %v", retryErr)
return retryErr
default:
return &unknownError{
err: retryErr,
}
}
},
})
switch {
case err == nil:
return nil
case errors.Is(err, &unknownError{}):
err = errors.Cause(err)
default:
err = retry.LastError(err)
}
return errors.Annotatef(err, "unable to contact api server after %d attempts", apiAttempts)
}
// unknownError is used to wrap errors that we don't know how to handle.
type unknownError struct {
err error
}
// Is implements errors.Is, so that we can identify this error type.
func (e *unknownError) Is(other error) bool {
_, ok := other.(*unknownError)
return ok
}
// Cause implements errors.Cause, so that we can unwrap this error type.
func (e *unknownError) Cause() error {
return e.err
}
// Error implements error.Error.
func (e *unknownError) Error() string {
return e.err.Error()
}
// BootstrapEndpointAddresses returns the addresses of the bootstrapped instance.
func BootstrapEndpointAddresses(
environ environs.InstanceBroker, callContext context.ProviderCallContext,
) ([]network.ProviderAddress, error) {
instances, err := environ.AllRunningInstances(callContext)
if err != nil {
return nil, errors.Trace(err)
}
if n := len(instances); n != 1 {
return nil, errors.Errorf("expected one instance, got %d", n)
}
netAddrs, err := instances[0].Addresses(callContext)
if err != nil {
return nil, errors.Annotate(err, "failed to get bootstrap instance addresses")
}
return netAddrs, nil
}
// ValidateIaasController returns an error if the controller
// is not an IAAS controller.
func ValidateIaasController(c modelcmd.CommandBase, cmdName, controllerName string, store jujuclient.ClientStore) error {
// Ensure controller model is cached.
controllerModel := jujuclient.JoinOwnerModelName(
names.NewUserTag(environs.AdminUser), bootstrap.ControllerModelName)
_, err := c.ModelUUIDs(store, controllerName, []string{controllerModel})
if err != nil {
return errors.Annotatef(err, "cannot get controller model uuid")
}
details, err := store.ModelByName(controllerName, controllerModel)
if err != nil {
return errors.Trace(err)
}
if details.ModelType == model.IAAS {
return nil
}
return errors.Errorf("Juju command %q not supported on container controllers", cmdName)
}