-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathprovider.go
815 lines (714 loc) · 29.9 KB
/
provider.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// Copyright 2016-2018, Pulumi Corporation.
//
// 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 provider
import (
"context"
"fmt"
"os"
"strings"
"github.com/golang/glog"
pbempty "github.com/golang/protobuf/ptypes/empty"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/pulumi/pulumi-kubernetes/pkg/await"
"github.com/pulumi/pulumi-kubernetes/pkg/client"
"github.com/pulumi/pulumi-kubernetes/pkg/openapi"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/resource/plugin"
"github.com/pulumi/pulumi/pkg/resource/provider"
"github.com/pulumi/pulumi/pkg/util/contract"
"github.com/pulumi/pulumi/pkg/util/rpcutil/rpcerror"
pulumirpc "github.com/pulumi/pulumi/sdk/proto/go"
"github.com/yudai/gojsondiff"
"google.golang.org/grpc/codes"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
clientapi "k8s.io/client-go/tools/clientcmd/api"
)
// --------------------------------------------------------------------------
// Kubernetes resource provider.
//
// Implements functionality for the Pulumi Kubernetes Resource Provider. This code is responsible
// for producing sensible responses for the gRPC server to send back to a client when it requests
// something to do with the Kubernetes resources it's meant to manage.
// --------------------------------------------------------------------------
const (
gvkDelimiter = ":"
)
type cancellationContext struct {
context context.Context
cancel context.CancelFunc
}
func makeCancellationContext() *cancellationContext {
var ctx, cancel = context.WithCancel(context.Background())
return &cancellationContext{
context: ctx,
cancel: cancel,
}
}
type kubeOpts struct {
rejectUnknownResources bool
}
type kubeProvider struct {
host *provider.HostClient
canceler *cancellationContext
client discovery.CachedDiscoveryInterface
pool dynamic.ClientPool
name string
version string
providerPrefix string
opts kubeOpts
}
var _ pulumirpc.ResourceProviderServer = (*kubeProvider)(nil)
func makeKubeProvider(
host *provider.HostClient, name, version string,
) (pulumirpc.ResourceProviderServer, error) {
return &kubeProvider{
host: host,
canceler: makeCancellationContext(),
name: name,
version: version,
providerPrefix: name + gvkDelimiter,
}, nil
}
// Configure configures the resource provider with "globals" that control its behavior.
func (k *kubeProvider) Configure(_ context.Context, req *pulumirpc.ConfigureRequest) (*pbempty.Empty, error) {
vars := req.GetVariables()
//
// Set simple configuration settings.
//
k.opts = kubeOpts{
rejectUnknownResources: vars["kubernetes:config:rejectUnknownResources"] == "true",
}
//
// Configure client-go using provided or ambient kubeconfig file.
//
// Compute config overrides.
overrides := &clientcmd.ConfigOverrides{
Context: clientapi.Context{
Cluster: vars["kubernetes:config:cluster"],
Namespace: vars["kubernetes:config:namespace"],
},
CurrentContext: vars["kubernetes:config:context"],
}
var kubeconfig clientcmd.ClientConfig
if configJSON, ok := vars["kubernetes:config:kubeconfig"]; ok {
config, err := clientcmd.Load([]byte(configJSON))
if err != nil {
return nil, fmt.Errorf("failed to parse kubeconfig: %v", err)
}
kubeconfig = clientcmd.NewDefaultClientConfig(*config, overrides)
} else {
// Use client-go to resolve the final configuration values for the client. Typically these
// values would would reside in the $KUBECONFIG file, but can also be altered in several
// places, including in env variables, client-go default values, and (if we allowed it) CLI
// flags.
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
kubeconfig = clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)
}
// Configure the discovery client.
conf, err := kubeconfig.ClientConfig()
if err != nil {
return nil, fmt.Errorf("Unable to read kubectl config: %v", err)
}
disco, err := discovery.NewDiscoveryClientForConfig(conf)
if err != nil {
return nil, err
}
// Cache the discovery information (OpenAPI schema, etc.) so we don't have to retrieve it for
// every request.
discoCache := client.NewMemcachedDiscoveryClient(disco)
mapper := discovery.NewDeferredDiscoveryRESTMapper(discoCache, dynamic.VersionInterfaces)
pathresolver := dynamic.LegacyAPIPathResolverFunc
// Create client pool, reusing one client per API group (e.g., one each for core, extensions,
// apps, etc.)
pool := dynamic.NewClientPool(conf, mapper, pathresolver)
k.client, k.pool = discoCache, pool
return &pbempty.Empty{}, nil
}
// Invoke dynamically executes a built-in function in the provider.
func (k *kubeProvider) Invoke(context.Context, *pulumirpc.InvokeRequest) (*pulumirpc.InvokeResponse, error) {
panic("Invoke not implemented")
}
// Check validates that the given property bag is valid for a resource of the given type and returns
// the inputs that should be passed to successive calls to Diff, Create, or Update for this
// resource. As a rule, the provider inputs returned by a call to Check should preserve the original
// representation of the properties as present in the program inputs. Though this rule is not
// required for correctness, violations thereof can negatively impact the end-user experience, as
// the provider inputs are using for detecting and rendering diffs.
func (k *kubeProvider) Check(ctx context.Context, req *pulumirpc.CheckRequest) (*pulumirpc.CheckResponse, error) {
//
// Behavior as of v0.12.x: We take two inputs:
//
// 1. req.News, the new resource inputs, i.e., the property bag coming from a custom resource like
// k8s.core.v1.Service
// 2. req.Olds, the last version submitted from a custom resource.
//
// `req.Olds` are ignored (and are sometimes nil). `req.News` are validated, and `.metadata.name`
// is given to it if it's not already provided.
//
// Utilities for determining whether a resource's GVK exists.
gvkExists := func(gvk schema.GroupVersionKind) bool {
knownGVKs := sets.NewString()
if knownGVKs.Has(gvk.String()) {
return true
}
gv := gvk.GroupVersion()
rls, err := k.client.ServerResourcesForGroupVersion(gv.String())
if err != nil {
if !errors.IsNotFound(err) {
glog.V(3).Infof("ServerResourcesForGroupVersion(%q) returned unexpected error %v", gv, err)
}
return false
}
for _, rl := range rls.APIResources {
knownGVKs.Insert(gv.WithKind(rl.Kind).String())
}
return knownGVKs.Has(gvk.String())
}
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Check(%s)", k.label(), urn)
glog.V(9).Infof("%s executing", label)
// Obtain old resource inputs. This is the old version of the resource(s) supplied by the user as
// an update.
oldResInputs := req.GetOlds()
olds, err := plugin.UnmarshalProperties(oldResInputs, plugin.MarshalOptions{
Label: fmt.Sprintf("%s.olds", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
oldInputs := propMapToUnstructured(olds)
// Obtain new resource inputs. This is the new version of the resource(s) supplied by the user as
// an update.
newResInputs := req.GetNews()
news, err := plugin.UnmarshalProperties(newResInputs, plugin.MarshalOptions{
Label: fmt.Sprintf("%s.news", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
newInputs := propMapToUnstructured(news)
var failures []*pulumirpc.CheckFailure
// If annotations with the prefix `pulumi.com/` exist, report that as error.
for k := range newInputs.GetAnnotations() {
if strings.HasPrefix(k, annotationInternalPrefix) {
failures = append(failures, &pulumirpc.CheckFailure{
Reason: fmt.Sprintf("annotation '%s' uses illegal prefix `pulumi.com/internal`", k),
})
}
}
// Adopt name from old object if appropriate.
//
// If the user HAS NOT assigned a name in the new inputs, we autoname it and mark the object as
// autonamed in `.metadata.annotations`. This makes it easier for `Diff` to decide whether this
// needs to be `DeleteBeforeReplace`'d. If the resource is marked `DeleteBeforeReplace`, then
// `Create` will allocate it a new name later.
if len(oldInputs.Object) > 0 {
// NOTE: If old inputs exist, they have a name, either provided by the user or filled in with a
// previous run of `Check`.
contract.Assert(oldInputs.GetName() != "")
adoptOldNameIfUnnamed(newInputs, oldInputs)
} else {
assignNameIfAutonamable(newInputs, urn.Name())
}
gvk, err := k.gvkFromURN(urn)
if err != nil {
return nil, err
}
// HACK: Do not validate against OpenAPI spec if there is a computed value. The OpenAPI spec
// does not know how to deal with the placeholder values for computed values.
if !hasComputedValue(newInputs) {
// Get OpenAPI schema for the GVK.
err = openapi.ValidateAgainstSchema(k.client, newInputs)
// Validate the object according to the OpenAPI schema.
if err != nil {
resourceNotFound := errors.IsNotFound(err) ||
strings.Contains(err.Error(), "is not supported by the server")
if resourceNotFound && gvkExists(gvk) {
failures = append(failures, &pulumirpc.CheckFailure{
Reason: fmt.Sprintf(" Found API Group, but it did not contain a schema for '%s'", gvk),
})
} else if k.opts.rejectUnknownResources {
// If the schema doesn't exist, it could still be a CRD (which may not have a
// schema). Thus, if we are directed to check resources even if they have unknown
// types, we fail here.
return nil, fmt.Errorf("Unable to fetch schema: %v", err)
}
}
}
autonamedInputs, err := plugin.MarshalProperties(
resource.NewPropertyMapFromMap(newInputs.Object), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.autonamedInputs", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
// Return new, possibly-autonamed inputs.
return &pulumirpc.CheckResponse{Inputs: autonamedInputs, Failures: failures}, nil
}
// Diff checks what impacts a hypothetical update will have on the resource's properties.
func (k *kubeProvider) Diff(
ctx context.Context, req *pulumirpc.DiffRequest,
) (*pulumirpc.DiffResponse, error) {
//
// Behavior as of v0.12.x: We take 2 inputs:
//
// 1. req.News, the new resource inputs, i.e., the property bag coming from a custom resource like
// k8s.core.v1.Service
// 2. req.Olds, the old _state_ returned by a `Create` or an `Update`. The old state has the form
// {inputs: {...}, live: {...}}, and is a struct that contains the old inputs as well as the
// last computed value obtained from the Kubernetes API server.
//
// The list of properties that would cause replacement is then computed between the old and new
// _inputs_, as in Kubernetes this captures changes the user made that result in replacement
// (which is not true of the old computed values).
//
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Diff(%s)", k.label(), urn)
glog.V(9).Infof("%s executing", label)
// Get old state. This is an object of the form {inputs: {...}, live: {...}} where `inputs` is the
// previous resource inputs supplied by the user, and `live` is the computed state of that inputs
// we received back from the API server.
oldState, err := plugin.UnmarshalProperties(req.GetOlds(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.olds", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
oldInputs, _ := parseCheckpointObject(oldState)
// Get new resouce inputs. The user is submitting these as an update.
newResInputs, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.news", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
newInputs := propMapToUnstructured(newResInputs)
gvk, err := k.gvkFromURN(urn)
if err != nil {
return nil, err
}
// Decide whether to replace the resource.
replaces, err := forceNewProperties(oldInputs.Object, newInputs.Object, gvk)
if err != nil {
return nil, err
}
// Pack up PB, ship response back.
hasChanges := pulumirpc.DiffResponse_DIFF_NONE
diff := gojsondiff.New().CompareObjects(oldInputs.Object, newInputs.Object)
if len(diff.Deltas()) > 0 {
hasChanges = pulumirpc.DiffResponse_DIFF_SOME
}
// Delete before replacement if we are forced to replace the old object, and the new version of
// that object MUST have the same name.
deleteBeforeReplace :=
// 1. We know resource must be replaced.
(len(replaces) > 0 &&
// 2. Object is NOT autonamed (i.e., user manually named it, and therefore we can't
// auto-generate the name).
!isAutonamed(newInputs) &&
// 3. The new, user-specified name is the same as the old name.
newInputs.GetName() == oldInputs.GetName() &&
// 4. The resource is being deployed to the same namespace (i.e., we aren't creating the
// object in a new namespace and then deleting the old one).
canonicalNamespace(newInputs.GetNamespace()) == canonicalNamespace(oldInputs.GetNamespace()))
return &pulumirpc.DiffResponse{
Changes: hasChanges,
Replaces: replaces,
Stables: []string{},
DeleteBeforeReplace: deleteBeforeReplace,
}, nil
}
// Create allocates a new instance of the provided resource and returns its unique ID afterwards.
// (The input ID must be blank.) If this call fails, the resource must not have been created (i.e.,
// it is "transacational").
func (k *kubeProvider) Create(
ctx context.Context, req *pulumirpc.CreateRequest,
) (*pulumirpc.CreateResponse, error) {
//
// Behavior as of v0.12.x: We take 1 input:
//
// 1. `req.Properties`, the new resource inputs submitted by the user, after having been returned
// by `Check`.
//
// This is used to create a new resource, and the computed values are returned. Importantly:
//
// * The return is formatted as a "checkpoint object", i.e., an object of the form
// {inputs: {...}, live: {...}}. This is important both for `Diff` and for `Update`. See
// comments in those methods for details.
//
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Create(%s)", k.label(), urn)
glog.V(9).Infof("%s executing", label)
// Obtain client from pool for the resource we're creating.
newResInputs, err := plugin.UnmarshalProperties(req.GetProperties(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.properties", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
newInputs := propMapToUnstructured(newResInputs)
initialized, awaitErr := await.Creation(k.canceler.context, k.host, k.pool, k.client,
resource.URN(req.GetUrn()), newInputs)
if awaitErr != nil {
initErr, isInitErr := awaitErr.(await.InitializationError)
if !isInitErr {
// Object creation failed.
return nil, awaitErr
}
// Resource was created, but failed to become fully initialized.
initialized = initErr.Object()
}
inputsAndComputed, err := plugin.MarshalProperties(
checkpointObject(newInputs, initialized), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.inputsAndComputed", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
if awaitErr != nil {
// Resource was created but failed to initialize. Return live version of object so it can be
// checkpointed.
return nil, initializationError(client.FqObjName(initialized), awaitErr, inputsAndComputed)
}
return &pulumirpc.CreateResponse{
Id: client.FqObjName(initialized), Properties: inputsAndComputed,
}, nil
}
// Read the current live state associated with a resource. Enough state must be include in the
// inputs to uniquely identify the resource; this is typically just the resource ID, but may also
// include some properties.
func (k *kubeProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*pulumirpc.ReadResponse, error) {
//
// Behavior as of v0.12.x: We take 1 input:
//
// 1. `req.Properties`, the new resource inputs submitted by the user, after having been persisted
// (e.g., by `Create` or `Update`).
//
// We use this information to read the live version of a Kubernetes resource. This is sometimes
// then checkpointed (e.g., in the case of `refresh`). Specifically:
//
// * The return is formatted as a "checkpoint object", i.e., an object of the form
// {inputs: {...}, live: {...}}. This is important both for `Diff` and for `Update`. See
// comments in those methods for details.
//
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Read(%s)", k.label(), urn)
glog.V(9).Infof("%s executing", label)
// Obtain new properties, create a Kubernetes `unstructured.Unstructured` that we can pass to the
// validation routines.
oldState, err := plugin.UnmarshalProperties(req.GetProperties(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.olds", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
// Ignore old state; we'll get it from Kubernetes later.
oldInputs, _ := parseCheckpointObject(oldState)
gvk, err := k.gvkFromURN(urn)
if err != nil {
return nil, err
}
gvk.Group = schemaGroupName(gvk.Group)
namespace, name := client.ParseFqName(req.GetId())
liveObj, readErr := await.Read(k.canceler.context, k.host, k.pool, k.client,
resource.URN(req.GetUrn()), gvk, canonicalNamespace(namespace), name, oldInputs)
if readErr != nil {
glog.V(3).Infof("%v", readErr)
statusErr, ok := readErr.(*errors.StatusError)
if ok && statusErr.ErrStatus.Code == 404 {
// If it's a 404 error, this resource was probably deleted.
return &pulumirpc.ReadResponse{Id: "", Properties: nil}, nil
}
if initErr, ok := readErr.(await.InitializationError); ok {
glog.V(3).Infof("is init err")
liveObj = initErr.Object()
}
// If `liveObj == nil` at this point, it means we've encountered an error that is neither a
// 404, nor an `await.InitializationError`. For example, the master could be unreachable. We
// should fail in this case.
if liveObj == nil {
return nil, readErr
}
// If we get here, resource successfully registered with the API server, but failed to
// initialize.
}
id := client.FqObjName(liveObj)
if reqID := req.GetId(); len(reqID) > 0 {
id = reqID
}
// Return a new "checkpoint object".
inputsAndComputed, err := plugin.MarshalProperties(
checkpointObject(oldInputs, liveObj), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.inputsAndComputed", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
if readErr != nil {
// Resource was created but failed to initialize. Return live version of object so it can be
// checkpointed.
glog.V(3).Infof("%v", initializationError(id, readErr, inputsAndComputed))
return nil, initializationError(id, readErr, inputsAndComputed)
}
return &pulumirpc.ReadResponse{Id: id, Properties: inputsAndComputed}, nil
}
// Update updates an existing resource with new values. Currently this client supports the
// Kubernetes-standard three-way JSON patch. See references here[1] and here[2].
//
// nolint
// [1]: https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment
// nolint
// [2]: https://kubernetes.io/docs/concepts/overview/object-management-kubectl/declarative-config/#how-apply-calculates-differences-and-merges-changes
func (k *kubeProvider) Update(
ctx context.Context, req *pulumirpc.UpdateRequest,
) (*pulumirpc.UpdateResponse, error) {
//
// Behavior as of v0.12.x: We take 2 inputs:
//
// 1. req.News, the new resource inputs, i.e., the property bag coming from a custom resource like
// k8s.core.v1.Service
// 2. req.Olds, the old _state_ returned by a `Create` or an `Update`. The old state has the form
// {inputs: {...}, live: {...}}, and is a struct that contains the old inputs as well as the
// last computed value obtained from the Kubernetes API server.
//
// Unlike other providers, the update is computed as a three way merge between: (1) the new
// inputs, (2) the computed state returned by the API server, and (3) the old inputs. This is the
// main reason why the old state is an object with both the old inputs and the live version of the
// object.
//
//
// TREAD CAREFULLY. The semantics of a Kubernetes update are subtle and you should proceed to
// change them only if you understand them deeply.
//
// Briefly: when a user updates an existing resource definition (e.g., by modifying YAML), the API
// server must decide how to apply the changes inside it, to the version of the resource that it
// has stored in etcd. In Kubernetes this decision is turns out to be quite complex. `kubectl`
// currently uses the three-way "strategic merge" and falls back to the three-way JSON merge. We
// currently support the second, but eventually we'll have to support the first, too.
//
// (NOTE: This comment is scoped to the question of how to patch an existing resource, rather than
// how to recognize when a resource needs to be re-created from scratch.)
//
// There are several reasons for this complexity:
//
// * It's important not to clobber fields set or default-set by the server (e.g., NodePort,
// namespace, service type, etc.), or by out-of-band tooling like admission controllers
// (which, e.g., might do something like add a sidecar to a container list).
// * For example, consider a scenario where a user renames a container. It is a reasonable
// expectation the old version of the container gets destroyed when the update is applied. And
// if the update strategy is set to three-way JSON merge patching, it is.
// * But, consider if their administrator has set up (say) the Istio admission controller, which
// embeds a sidecar container in pods submitted to the API. This container would not be present
// in the YAML file representing that pod, but when an update is applied by the user, they
// not want it to get destroyed. And, so, when the strategy is set to three-way strategic
// merge, the container is not destroyed. (With this strategy, fields can have "merge keys" as
// part of their schema, which tells the API server how to merge each particular field.)
//
// What's worse is, currently nearly all of this logic exists on the client rather than the
// server, though there is work moving forward to move this to the server.
//
// So the roadmap is:
//
// - [x] Implement `Update` using the three-way JSON merge strategy.
// - [ ] Cause `Update` to default to the three-way JSON merge patch strategy. (This will require
// plumbing, because it expects nominal types representing the API schema, but the
// discovery client is completely dynamic.)
// - [ ] Support server-side apply, when it comes out.
//
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Update(%s)", k.label(), urn)
glog.V(9).Infof("%s executing", label)
// Obtain new properties, create a Kubernetes `unstructured.Unstructured` that we can pass to the
// validation routines.
oldState, err := plugin.UnmarshalProperties(req.GetOlds(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.olds", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
// Ignore old state; we'll get it from Kubernetes later.
oldInputs, _ := parseCheckpointObject(oldState)
// Obtain new properties, create a Kubernetes `unstructured.Unstructured` that we can pass to the
// validation routines.
newResInputs, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.news", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
newInputs := propMapToUnstructured(newResInputs)
// Apply update.
initialized, awaitErr := await.Update(k.canceler.context, k.host, k.pool, k.client,
resource.URN(req.GetUrn()), oldInputs, newInputs)
if awaitErr != nil {
var getErr error
initialized, getErr = k.readLiveObject(newInputs)
if getErr != nil {
// Object update/creation failed.
return nil, awaitErr
}
// If we get here, resource successfully registered with the API server, but failed to
// initialize.
}
// Return a new "checkpoint object".
inputsAndComputed, err := plugin.MarshalProperties(
checkpointObject(newInputs, initialized), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.inputsAndComputed", label), KeepUnknowns: true, SkipNulls: true,
})
if err != nil {
return nil, err
}
if awaitErr != nil {
// Resource was updated/created but failed to initialize. Return live version of object so it
// can be checkpointed.
return nil, initializationError(client.FqObjName(initialized), awaitErr, inputsAndComputed)
}
return &pulumirpc.UpdateResponse{Properties: inputsAndComputed}, nil
}
// Delete tears down an existing resource with the given ID. If it fails, the resource is assumed
// to still exist.
func (k *kubeProvider) Delete(
ctx context.Context, req *pulumirpc.DeleteRequest,
) (*pbempty.Empty, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Delete(%s)", k.label(), urn)
glog.V(9).Infof("%s executing", label)
// TODO(hausdorff): Propagate other options, like grace period through flags.
gvk, err := k.gvkFromURN(resource.URN(req.GetUrn()))
if err != nil {
return nil, err
}
gvk.Group = schemaGroupName(gvk.Group)
namespace, name := client.ParseFqName(req.GetId())
err = await.Deletion(k.canceler.context, k.host, k.pool, k.client, urn, gvk, namespace, name)
if err != nil {
return nil, err
}
return &pbempty.Empty{}, nil
}
// GetPluginInfo returns generic information about this plugin, like its version.
func (k *kubeProvider) GetPluginInfo(context.Context, *pbempty.Empty) (*pulumirpc.PluginInfo, error) {
return &pulumirpc.PluginInfo{
Version: k.version,
}, nil
}
// Cancel signals the provider to gracefully shut down and abort any ongoing resource operations.
// Operations aborted in this way will return an error (e.g., `Update` and `Create` will either a
// creation error or an initialization error). Since Cancel is advisory and non-blocking, it is up
// to the host to decide how long to wait after Cancel is called before (e.g.)
// hard-closing any gRPC connection.
func (k *kubeProvider) Cancel(context.Context, *pbempty.Empty) (*pbempty.Empty, error) {
k.canceler.cancel()
return &pbempty.Empty{}, nil
}
// --------------------------------------------------------------------------
// Private helpers.
// --------------------------------------------------------------------------
func (k *kubeProvider) label() string {
return fmt.Sprintf("Provider[%s]", k.name)
}
func (k *kubeProvider) gvkFromURN(urn resource.URN) (schema.GroupVersionKind, error) {
// Strip prefix.
s := string(urn.Type())
contract.Assertf(strings.HasPrefix(s, k.providerPrefix), "Kubernetes GVK is: '%s'", string(urn))
s = s[len(k.providerPrefix):]
// Emit GVK.
gvk := strings.Split(s, gvkDelimiter)
gv := strings.Split(gvk[0], "/")
if len(gvk) < 2 {
return schema.GroupVersionKind{},
fmt.Errorf("GVK must have both an apiVersion and a Kind: '%s'", s)
} else if len(gv) != 2 {
return schema.GroupVersionKind{},
fmt.Errorf("apiVersion does not have both a group and a version: '%s'", s)
}
return schema.GroupVersionKind{
Group: gv[0],
Version: gv[1],
Kind: gvk[1],
}, nil
}
func (k *kubeProvider) readLiveObject(
obj *unstructured.Unstructured,
) (*unstructured.Unstructured, error) {
// Retrieve live version of last submitted version of object.
clientForResource, err := client.FromResource(k.pool, k.client, obj)
if err != nil {
return nil, err
}
// Get the "live" version of the last submitted object. This is necessary because the server may
// have populated some fields automatically, updated status fields, and so on.
return clientForResource.Get(obj.GetName(), metav1.GetOptions{})
}
func schemaGroupName(group string) string {
switch group {
case "core":
return ""
default:
return group
}
}
func propMapToUnstructured(pm resource.PropertyMap) *unstructured.Unstructured {
return &unstructured.Unstructured{Object: pm.Mappable()}
}
func checkpointObject(inputs, live *unstructured.Unstructured) resource.PropertyMap {
object := resource.NewPropertyMapFromMap(live.Object)
object["__inputs"] = resource.NewObjectProperty(resource.NewPropertyMapFromMap(inputs.Object))
return object
}
func parseCheckpointObject(obj resource.PropertyMap) (oldInputs, live *unstructured.Unstructured) {
pm := obj.Mappable()
inputs, hasInputs := pm["inputs"]
liveMap, hasLive := pm["live"]
if !hasInputs || !hasLive {
liveMap = pm
inputs, hasInputs = pm["__inputs"]
if hasInputs {
delete(liveMap.(map[string]interface{}), "__inputs")
} else {
inputs = map[string]interface{}{}
}
}
oldInputs = &unstructured.Unstructured{Object: inputs.(map[string]interface{})}
live = &unstructured.Unstructured{Object: liveMap.(map[string]interface{})}
return
}
func initializationError(id string, err error, inputsAndComputed *structpb.Struct) error {
reasons := []string{err.Error()}
if aggregate, isAggregate := err.(await.AggregatedError); isAggregate {
reasons = append(reasons, aggregate.SubErrors()...)
}
detail := pulumirpc.ErrorResourceInitFailed{
Id: id,
Properties: inputsAndComputed,
Reasons: reasons,
}
return rpcerror.WithDetails(rpcerror.New(codes.Unknown, err.Error()), &detail)
}
// canonicalNamespace will provides the canonical name for a namespace. Specifically, if the
// namespace is "", the empty string, we report this as its canonical name, "default".
func canonicalNamespace(ns string) string {
if ns == "" {
return "default"
}
return ns
}