-
Notifications
You must be signed in to change notification settings - Fork 907
/
install.go
277 lines (255 loc) · 9.38 KB
/
install.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Copyright 2020 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/google/go-containerregistry/pkg/name"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
ctrl "sigs.k8s.io/controller-runtime"
"github.com/crossplane/crossplane-runtime/pkg/errors"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/logging"
v1 "github.com/crossplane/crossplane/apis/pkg/v1"
typedclient "github.com/crossplane/crossplane/internal/client/clientset/versioned/typed/pkg/v1"
"github.com/crossplane/crossplane/internal/version"
"github.com/crossplane/crossplane/internal/xpkg"
// Load all the auth plugins for the cloud providers.
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
const (
errPkgIdentifier = "invalid package image identifier"
errKubeConfig = "failed to get kubeconfig"
errKubeClient = "failed to create kube client"
errFmtPkgNotReadyTimeout = "%s is not ready in timeout duration"
errFmtWatchPkg = "Failed to watch for %s object"
)
const (
msgConfigurationReady = "Configuration is ready"
msgConfigurationNotReady = "Configuration is not ready"
msgConfigurationWaiting = "Waiting for the Configuration to be ready"
msgProviderReady = "Provider is ready"
msgProviderNotReady = "Provider is not ready"
msgProviderWaiting = "Waiting for the Provider to be ready"
)
// installCmd installs a package.
type installCmd struct {
Configuration installConfigCmd `cmd:"" help:"Install a Configuration package."`
Provider installProviderCmd `cmd:"" help:"Install a Provider package."`
}
// Run runs the install cmd.
func (c *installCmd) Run(b *buildChild) error {
return nil
}
// installConfigCmd installs a Configuration.
type installConfigCmd struct {
Package string `arg:"" help:"Image containing Configuration package."`
Name string `arg:"" optional:"" help:"Name of Configuration."`
Wait time.Duration `short:"w" help:"Wait for installation of package."`
RevisionHistoryLimit int64 `short:"r" help:"Revision history limit."`
ManualActivation bool `short:"m" help:"Enable manual revision activation policy."`
PackagePullSecrets []string `help:"List of secrets used to pull package."`
}
// Run runs the Configuration install cmd.
func (c *installConfigCmd) Run(k *kong.Context, logger logging.Logger) error { //nolint:gocyclo
rap := v1.AutomaticActivation
if c.ManualActivation {
rap = v1.ManualActivation
}
pkgName := c.Name
if pkgName == "" {
ref, err := name.ParseReference(c.Package)
if err != nil {
logger.Debug(errPkgIdentifier, "error", err)
return errors.Wrap(err, errPkgIdentifier)
}
pkgName = xpkg.ToDNSLabel(ref.Context().RepositoryStr())
}
logger = logger.WithValues("configurationName", pkgName)
packagePullSecrets := make([]corev1.LocalObjectReference, len(c.PackagePullSecrets))
for i, s := range c.PackagePullSecrets {
packagePullSecrets[i] = corev1.LocalObjectReference{
Name: s,
}
}
cr := &v1.Configuration{
ObjectMeta: metav1.ObjectMeta{
Name: pkgName,
},
Spec: v1.ConfigurationSpec{
PackageSpec: v1.PackageSpec{
Package: c.Package,
RevisionActivationPolicy: &rap,
RevisionHistoryLimit: &c.RevisionHistoryLimit,
PackagePullSecrets: packagePullSecrets,
},
},
}
kubeConfig, err := ctrl.GetConfig()
if err != nil {
logger.Debug(errKubeConfig, "error", err)
return errors.Wrap(err, errKubeConfig)
}
logger.Debug("Found kubeconfig")
kube, err := typedclient.NewForConfig(kubeConfig)
if err != nil {
logger.Debug(errKubeConfig, "error", err)
return errors.Wrap(err, errKubeClient)
}
logger.Debug("Created Kubernetes client")
res, err := kube.Configurations().Create(context.Background(), cr, metav1.CreateOptions{})
if err != nil {
logger.Debug("Failed to create configuration", "error", warnIfNotFound(err))
return errors.Wrap(warnIfNotFound(err), "cannot create configuration")
}
if c.Wait != 0 {
logger.Debug(msgConfigurationWaiting)
watchList := cache.NewListWatchFromClient(kube.RESTClient(), "configurations", corev1.NamespaceAll, fields.Everything())
waitSeconds := int64(c.Wait.Seconds())
watcher, err := watchList.Watch(metav1.ListOptions{Watch: true, TimeoutSeconds: &waitSeconds})
defer watcher.Stop()
if err != nil {
logger.Debug(fmt.Sprintf(errFmtWatchPkg, "Configuration"), "error", err)
return err
}
for {
event, ok := <-watcher.ResultChan()
if !ok {
logger.Debug(fmt.Sprintf(errFmtPkgNotReadyTimeout, "Configuration"))
return errors.Errorf(errFmtPkgNotReadyTimeout, "Configuration")
}
obj := (event.Object).(*v1.Configuration)
cond := obj.GetCondition(v1.TypeHealthy)
if obj.ObjectMeta.Name == pkgName && cond.Status == corev1.ConditionTrue {
logger.Debug(msgConfigurationReady)
break
}
logger.Debug(msgConfigurationNotReady)
}
}
_, err = fmt.Fprintf(k.Stdout, "%s/%s created\n", strings.ToLower(v1.ConfigurationGroupKind), res.GetName())
return err
}
// installProviderCmd install a Provider.
type installProviderCmd struct {
Package string `arg:"" help:"Image containing Provider package."`
Name string `arg:"" optional:"" help:"Name of Provider."`
Wait time.Duration `short:"w" help:"Wait for installation of package"`
RevisionHistoryLimit int64 `short:"r" help:"Revision history limit."`
ManualActivation bool `short:"m" help:"Enable manual revision activation policy."`
Config string `help:"Specify a ControllerConfig for this Provider."`
PackagePullSecrets []string `help:"List of secrets used to pull package."`
}
// Run runs the Provider install cmd.
func (c *installProviderCmd) Run(k *kong.Context, logger logging.Logger) error { //nolint:gocyclo
rap := v1.AutomaticActivation
if c.ManualActivation {
rap = v1.ManualActivation
}
pkgName := c.Name
if pkgName == "" {
ref, err := name.ParseReference(c.Package)
if err != nil {
logger.Debug(errPkgIdentifier, "error", err)
return errors.Wrap(err, errPkgIdentifier)
}
pkgName = xpkg.ToDNSLabel(ref.Context().RepositoryStr())
}
logger = logger.WithValues("providerName", pkgName)
packagePullSecrets := make([]corev1.LocalObjectReference, len(c.PackagePullSecrets))
for i, s := range c.PackagePullSecrets {
packagePullSecrets[i] = corev1.LocalObjectReference{
Name: s,
}
}
cr := &v1.Provider{
ObjectMeta: metav1.ObjectMeta{
Name: pkgName,
},
Spec: v1.ProviderSpec{
PackageSpec: v1.PackageSpec{
Package: c.Package,
RevisionActivationPolicy: &rap,
RevisionHistoryLimit: &c.RevisionHistoryLimit,
PackagePullSecrets: packagePullSecrets,
},
},
}
if c.Config != "" {
cr.Spec.ControllerConfigReference = &xpv1.Reference{
Name: c.Config,
}
}
kubeConfig, err := ctrl.GetConfig()
if err != nil {
logger.Debug(errKubeConfig, "error", err)
return errors.Wrap(err, errKubeConfig)
}
logger.Debug("Found kubeconfig")
kube, err := typedclient.NewForConfig(kubeConfig)
if err != nil {
logger.Debug(errKubeClient, "error", err)
return errors.Wrap(err, errKubeClient)
}
logger.Debug("Created kubernetes client")
res, err := kube.Providers().Create(context.Background(), cr, metav1.CreateOptions{})
if err != nil {
logger.Debug("Failed to create provider", "error", warnIfNotFound(err))
return errors.Wrap(warnIfNotFound(err), "cannot create provider")
}
if c.Wait != 0 {
logger.Debug(msgProviderWaiting)
watchList := cache.NewListWatchFromClient(kube.RESTClient(), "providers", corev1.NamespaceAll, fields.Everything())
waitSeconds := int64(c.Wait.Seconds())
watcher, err := watchList.Watch(metav1.ListOptions{Watch: true, TimeoutSeconds: &waitSeconds})
defer watcher.Stop()
if err != nil {
logger.Debug(fmt.Sprintf(errFmtWatchPkg, "Provider"), "error", err)
return err
}
for {
event, ok := <-watcher.ResultChan()
if !ok {
logger.Debug(fmt.Sprintf(errFmtPkgNotReadyTimeout, "Provider"))
return errors.Errorf(errFmtPkgNotReadyTimeout, "Provider")
}
obj := (event.Object).(*v1.Provider)
cond := obj.GetCondition(v1.TypeHealthy)
if obj.ObjectMeta.Name == pkgName && cond.Status == corev1.ConditionTrue {
logger.Debug(msgProviderReady, "pkgName", obj.ObjectMeta.Name)
break
}
logger.Debug(msgProviderNotReady)
}
}
_, err = fmt.Fprintf(k.Stdout, "%s/%s created\n", strings.ToLower(v1.ProviderGroupKind), res.GetName())
return err
}
func warnIfNotFound(err error) error {
serr, ok := err.(*apierrors.StatusError)
if !ok {
return err
}
if serr.ErrStatus.Code != http.StatusNotFound {
return err
}
return errors.WithMessagef(err, "kubectl-crossplane plugin %s might be out of date", version.New().GetVersionString())
}