forked from Azure/aad-pod-identity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mic.go
1458 lines (1288 loc) · 56.5 KB
/
mic.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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mic
import (
"context"
"fmt"
"os"
"reflect"
"strings"
"sync"
"sync/atomic"
"time"
aadpodid "github.com/Azure/aad-pod-identity/pkg/apis/aadpodidentity"
"github.com/Azure/aad-pod-identity/pkg/cloudprovider"
"github.com/Azure/aad-pod-identity/pkg/crd"
"github.com/Azure/aad-pod-identity/pkg/filewatcher"
"github.com/Azure/aad-pod-identity/pkg/metrics"
"github.com/Azure/aad-pod-identity/pkg/pod"
"github.com/Azure/aad-pod-identity/pkg/stats"
"github.com/Azure/aad-pod-identity/pkg/utils"
"github.com/Azure/aad-pod-identity/version"
"github.com/fsnotify/fsnotify"
"golang.org/x/sync/semaphore"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
)
const (
stopped = int32(0)
running = int32(1)
)
// NodeGetter is an abstraction used to get Kubernetes node info.
type NodeGetter interface {
Get(name string) (*corev1.Node, error)
Start(<-chan struct{})
}
// TypeUpgradeConfig - configuration aspects of type related changes required for client-go upgrade.
type TypeUpgradeConfig struct {
// Key in the config map which indicates if a type upgrade has been performed.
TypeUpgradeStatusKey string
EnableTypeUpgrade bool
}
// CMConfig - config map for aad-pod-identity
type CMConfig struct {
Namespace string
Name string
}
// LeaderElectionConfig - used to keep track of leader election config.
type LeaderElectionConfig struct {
Namespace string
Name string
Duration time.Duration
Instance string
}
// UpdateUserMSIConfig - parameters for retrying cloudprovider's UpdateUserMSI function
type UpdateUserMSIConfig struct {
MaxRetry int
RetryInterval time.Duration
}
// Client has the required pointers to talk to the api server
// and interact with the CRD related data structure.
type Client struct {
CRDClient crd.ClientInt
CloudClient cloudprovider.ClientInt
PodClient pod.ClientInt
CloudConfigWatcher filewatcher.ClientInt
EventRecorder record.EventRecorder
EventChannel chan aadpodid.EventType
NodeClient NodeGetter
IsNamespaced bool
SyncLoopStarted bool
syncRetryInterval time.Duration
enableScaleFeatures bool
createDeleteBatch int64
ImmutableUserMSIsMap map[string]bool
identityAssignmentReconcileInterval time.Duration
syncing int32 // protect against concurrent sync's
leaderElector *leaderelection.LeaderElector
*LeaderElectionConfig
Reporter *metrics.Reporter
TypeUpgradeCfg *TypeUpgradeConfig
CMCfg *CMConfig
CMClient typedcorev1.ConfigMapInterface
}
// Config - MIC Config
type Config struct {
CloudCfgPath string
RestConfig *rest.Config
IsNamespaced bool
SyncRetryInterval time.Duration
LeaderElectionCfg *LeaderElectionConfig
EnableScaleFeatures bool
CreateDeleteBatch int64
ImmutableUserMSIsList []string
CMcfg *CMConfig
TypeUpgradeCfg *TypeUpgradeConfig
UpdateUserMSICfg *UpdateUserMSIConfig
IdentityAssignmentReconcileInterval time.Duration
}
// ClientInt is an abstraction used to perform an MIC sync cycle.
type ClientInt interface {
Start(exit <-chan struct{})
Sync(exit <-chan struct{})
}
type trackUserAssignedMSIIds struct {
addUserAssignedMSIIDs []string
removeUserAssignedMSIIDs []string
assignedIDsToCreate []aadpodid.AzureAssignedIdentity
assignedIDsToDelete []aadpodid.AzureAssignedIdentity
assignedIDsToUpdate []aadpodid.AzureAssignedIdentity
isvmss bool
}
// NewMICClient returns new mic client
func NewMICClient(cfg *Config) (*Client, error) {
klog.Infof("starting to create the pod identity client. Version: %v. Build date: %v", version.MICVersion, version.BuildDate)
clientSet := kubernetes.NewForConfigOrDie(cfg.RestConfig)
k8sVersion, err := clientSet.ServerVersion()
if err == nil {
klog.Infof("Kubernetes server version: %s", k8sVersion.String())
}
informer := informers.NewSharedInformerFactory(clientSet, 30*time.Second)
cloudClient, err := cloudprovider.NewCloudProvider(cfg.CloudCfgPath, cfg.UpdateUserMSICfg.MaxRetry, cfg.UpdateUserMSICfg.RetryInterval)
if err != nil {
return nil, err
}
klog.V(1).Infof("cloud provider initialized")
eventCh := make(chan aadpodid.EventType, 100)
crdClient, err := crd.NewCRDClient(cfg.RestConfig, eventCh)
if err != nil {
return nil, err
}
klog.V(1).Infof("CRD client initialized")
podClient := pod.NewPodClient(informer, eventCh)
klog.V(1).Infof("pod Client initialized")
cloudConfigWatcher, err := filewatcher.NewFileWatcher(
func(event fsnotify.Event) {
if event.Op&fsnotify.Write == fsnotify.Write {
if err := cloudClient.Init(); err != nil {
return
}
klog.V(1).Infof("cloud provider re-initialized")
}
}, func(err error) {
klog.Errorf("failed to handle fsnotify event, error: %+v", err)
})
if err != nil {
return nil, err
}
if err := cloudConfigWatcher.Add(cfg.CloudCfgPath); err != nil {
return nil, err
}
klog.V(1).Infof("cloud config watcher initialized")
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: clientSet.CoreV1().Events("")})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: aadpodid.CRDGroup})
immutableUserMSIsMap := make(map[string]bool)
if len(cfg.ImmutableUserMSIsList) > 0 {
for _, item := range cfg.ImmutableUserMSIsList {
immutableUserMSIsMap[strings.ToLower(item)] = true
}
}
// Cluster identity used for cloud provider operations is also immutable.
// For clusters created with managed identity, the cluster identity is used for all
// cloud provider operations and is also used by MIC. If the user configures the cluster
// identity to be used by pod, we should not delete it when all pods are deleted.
clusterIdentity := cloudClient.GetClusterIdentity()
if clusterIdentity != "" {
immutableUserMSIsMap[clusterIdentity] = true
}
var cmClient typedcorev1.ConfigMapInterface
if cfg.TypeUpgradeCfg.EnableTypeUpgrade {
cmClient = clientSet.CoreV1().ConfigMaps(cfg.CMcfg.Namespace)
}
c := &Client{
CRDClient: crdClient,
CloudClient: cloudClient,
PodClient: podClient,
CloudConfigWatcher: cloudConfigWatcher,
EventRecorder: recorder,
EventChannel: eventCh,
NodeClient: &NodeClient{informer.Core().V1().Nodes()},
IsNamespaced: cfg.IsNamespaced,
syncRetryInterval: cfg.SyncRetryInterval,
enableScaleFeatures: cfg.EnableScaleFeatures,
createDeleteBatch: cfg.CreateDeleteBatch,
ImmutableUserMSIsMap: immutableUserMSIsMap,
TypeUpgradeCfg: cfg.TypeUpgradeCfg,
CMCfg: cfg.CMcfg,
CMClient: cmClient,
identityAssignmentReconcileInterval: cfg.IdentityAssignmentReconcileInterval,
}
leaderElector, err := c.NewLeaderElector(clientSet, recorder, cfg.LeaderElectionCfg)
if err != nil {
return nil, fmt.Errorf("failed to create new leader elector, error: %+v", err)
}
c.leaderElector = leaderElector
reporter, err := metrics.NewReporter()
if err != nil {
return nil, fmt.Errorf("failed to create reporter for metrics, error: %+v", err)
}
c.Reporter = reporter
return c, nil
}
// Run - Initiates the leader election run call to find if its leader and run it
func (c *Client) Run() {
klog.Info("initiating MIC Leader election")
// counter to track number of mic election
c.Reporter.Report(metrics.MICNewLeaderElectionCountM.M(1))
c.leaderElector.Run(context.Background())
}
// NewLeaderElector - does the required leader election initialization
func (c *Client) NewLeaderElector(clientSet *kubernetes.Clientset, recorder record.EventRecorder, leaderElectionConfig *LeaderElectionConfig) (leaderElector *leaderelection.LeaderElector, err error) {
c.LeaderElectionConfig = leaderElectionConfig
resourceLock, err := resourcelock.New(resourcelock.EndpointsResourceLock,
c.Namespace,
c.Name,
clientSet.CoreV1(),
clientSet.CoordinationV1(),
resourcelock.ResourceLockConfig{
Identity: c.Instance,
EventRecorder: recorder})
if err != nil {
return nil, fmt.Errorf("failed to create resource lock for leader election, error: %+v", err)
}
config := leaderelection.LeaderElectionConfig{
LeaseDuration: c.Duration,
RenewDeadline: c.Duration / 2,
RetryPeriod: c.Duration / 4,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(ctx context.Context) {
c.Start(ctx.Done())
},
OnStoppedLeading: func() {
klog.Error("lost leader lease")
klog.Flush()
os.Exit(1)
},
},
Lock: resourceLock,
}
leaderElector, err = leaderelection.NewLeaderElector(config)
if err != nil {
return nil, err
}
return leaderElector, nil
}
// UpgradeTypeIfRequired performs type upgrade for all aad-pod-identity CRDs if required.
func (c *Client) UpgradeTypeIfRequired() error {
if c.TypeUpgradeCfg.EnableTypeUpgrade {
cm, err := c.CMClient.Get(context.TODO(), c.CMCfg.Name, v1.GetOptions{})
// If we get an error and its not NotFound then return, because we cannot proceed.
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to get ConfigMap %s/%s, error: %+v", c.CMCfg.Namespace, c.CMCfg.Name, err)
}
// Now either the configmap is not there or we successfully got the configmap
// Handle the case where the configmap is not found.
if err != nil && apierrors.IsNotFound(err) {
// Create the configmap
newCfgMap := &corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Namespace: c.CMCfg.Namespace,
Name: c.CMCfg.Name,
},
}
if cm, err = c.CMClient.Create(context.TODO(), newCfgMap, metav1.CreateOptions{}); err != nil {
return fmt.Errorf("failed to create ConfigMap %s/%s, error: %+v", c.CMCfg.Namespace, c.CMCfg.Name, err)
}
}
// We reach here only if the configmap is present or we created new one.
// Check if the key for type upgrade is present. If the key is present,
// then the upgrade is already performed. If not then go through the type upgrade
// process.
if v, ok := cm.Data[c.TypeUpgradeCfg.TypeUpgradeStatusKey]; !ok {
klog.Infof("upgrading the types to work with case sensitive go-client")
if err := c.CRDClient.UpgradeAll(); err != nil {
return fmt.Errorf("failed to upgrade type, error: %+v", err)
}
klog.Infof("type upgrade completed !!")
// Upgrade completed so update the data with the upgrade key.
if cm.Data == nil {
cm.Data = make(map[string]string)
}
cm.Data[c.TypeUpgradeCfg.TypeUpgradeStatusKey] = version.MICVersion
_, err = c.CMClient.Update(context.TODO(), cm, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("failed to update ConfigMap key %s failed, error: %+v", c.TypeUpgradeCfg.TypeUpgradeStatusKey, err)
}
} else {
klog.Infof("type upgrade status configmap found from version: %s. Skipping type upgrade!", v)
}
}
return nil
}
// Start starts various go routines to watch for any relevant changes that would trigger a MIC sync.
func (c *Client) Start(exit <-chan struct{}) {
klog.V(6).Infof("MIC client starting..")
if err := c.UpgradeTypeIfRequired(); err != nil {
klog.Fatalf("type upgrade failed with error: %+v", err)
return
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
c.PodClient.Start(exit)
klog.V(6).Infof("pod client started")
wg.Done()
}()
wg.Add(1)
go func() {
c.CRDClient.Start(exit)
klog.V(6).Infof("CRD client started")
wg.Done()
}()
wg.Add(1)
go func() {
c.NodeClient.Start(exit)
klog.V(6).Infof("node client started")
wg.Done()
}()
wg.Add(1)
go func() {
c.CloudConfigWatcher.Start(exit)
klog.V(6).Infof("cloud config watcher started")
wg.Done()
}()
wg.Wait()
go c.Sync(exit)
}
func (c *Client) canSync() bool {
return atomic.CompareAndSwapInt32(&c.syncing, stopped, running)
}
func (c *Client) setStopped() {
atomic.StoreInt32(&c.syncing, stopped)
}
// Sync perform a sync cycle.
func (c *Client) Sync(exit <-chan struct{}) {
if !c.canSync() {
panic("concurrent syncs")
}
defer c.setStopped()
ticker := time.NewTicker(c.syncRetryInterval)
defer ticker.Stop()
identityAssignmentReconcileTicker := time.NewTicker(c.identityAssignmentReconcileInterval)
defer identityAssignmentReconcileTicker.Stop()
klog.Info("sync thread started.")
c.SyncLoopStarted = true
var event aadpodid.EventType
totalWorkDoneCycles := 0
totalSyncCycles := 0
for {
select {
case <-exit:
return
case event = <-c.EventChannel:
klog.V(6).Infof("received event: %v", event)
case <-ticker.C:
klog.V(6).Infof("running periodic sync loop")
case <-identityAssignmentReconcileTicker.C:
klog.V(6).Infof("reconciling identity assignment on Azure")
c.reconcileIdentityAssignment()
continue
}
totalSyncCycles++
stats.Init()
// This is the only place where the AzureAssignedIdentity creation is initiated.
begin := time.Now()
workDone := false
cacheTime := time.Now()
// There is a delay in data propagation to cache. It's possible that the creates performed in the previous sync cycle
// are not propagated before this sync cycle began. In order to avoid redoing the cycle, we sync cache again.
c.CRDClient.SyncCacheAll(exit, false)
stats.Put(stats.CacheSync, time.Since(cacheTime))
// List all pods in all namespaces
systemTime := time.Now()
listPods, err := c.PodClient.GetPods()
if err != nil {
klog.Errorf("failed to list pods, error: %+v", err)
continue
}
listBindings, err := c.CRDClient.ListBindings()
if err != nil {
continue
}
klog.V(6).Infof("number of bindings: %d", len(*listBindings))
listIDs, err := c.CRDClient.ListIds()
if err != nil {
continue
}
klog.V(6).Infof("number of identities: %d", len(*listIDs))
idMap, err := c.convertIDListToMap(*listIDs)
if err != nil {
klog.Errorf("failed to convert ID list to map, error: %+v", err)
continue
}
currentAssignedIDs, err := c.CRDClient.ListAssignedIDsInMap()
if err != nil {
continue
}
klog.V(6).Infof("number of assigned identities: %d", len(currentAssignedIDs))
stats.Put(stats.System, time.Since(systemTime))
beginNewListTime := time.Now()
newAssignedIDs, nodeRefs, err := c.createDesiredAssignedIdentityList(listPods, listBindings, idMap)
if err != nil {
klog.Errorf("failed to create a list of desired AzureAssignedIdentity, error: %+v", err)
continue
}
stats.Put(stats.CurrentState, time.Since(beginNewListTime))
// Extract add list and delete list based on existing assigned ids in the system (currentAssignedIDs).
// and the ones we have arrived at in the volatile list (newAssignedIDs).
addList, err := c.getAzureAssignedIDsToCreate(currentAssignedIDs, newAssignedIDs)
if err != nil {
klog.Errorf("failed to get a list of AzureAssignedIdentities to create, error: %+v", err)
continue
}
deleteList, err := c.getAzureAssignedIDsToDelete(currentAssignedIDs, newAssignedIDs)
if err != nil {
klog.Errorf("failed to get a list of AzureAssignedIdentities to delete, error: %+v", err)
continue
}
beforeUpdateList, afterUpdateList := c.getAzureAssignedIdentitiesToUpdate(addList, deleteList)
klog.V(5).Infof("del: %v, add: %v, update: %v", deleteList, addList, afterUpdateList)
// the node map is used to track assigned ids to create/delete, identities to assign/remove
// for each node or vmss
nodeMap := make(map[string]trackUserAssignedMSIIds)
// separate the add, delete and update list per node
c.convertAssignedIDListToMap(addList, deleteList, afterUpdateList, nodeMap)
// process the delete and add list
// determine the list of identities that need to updated, create a node to identity list mapping for add and delete
if len(deleteList) > 0 || len(beforeUpdateList) > 0 {
workDone = true
c.getListOfIdsToDelete(deleteList, beforeUpdateList, afterUpdateList, newAssignedIDs, nodeMap, nodeRefs)
}
if len(addList) > 0 || len(afterUpdateList) > 0 {
workDone = true
c.getListOfIdsToAssign(addList, afterUpdateList, nodeMap)
}
var wg sync.WaitGroup
// check if vmss and consolidate vmss nodes into vmss if necessary
c.consolidateVMSSNodes(nodeMap, &wg)
// one final createorupdate to each node or vmss in the map
c.updateNodeAndDeps(newAssignedIDs, nodeMap, nodeRefs, &wg)
wg.Wait()
if workDone || ((totalSyncCycles % 1000) == 0) {
if workDone {
totalWorkDoneCycles++
}
idsFound := 0
bindingsFound := 0
if listIDs != nil {
idsFound = len(*listIDs)
}
if listBindings != nil {
bindingsFound = len(*listBindings)
}
klog.Infof("work done: %v. Found %d pods, %d ids, %d bindings", workDone, len(listPods), idsFound, bindingsFound)
klog.Infof("total work cycles: %d, out of which work was done in: %d", totalSyncCycles, totalWorkDoneCycles)
stats.Put(stats.Total, time.Since(begin))
c.Reporter.Report(
metrics.MICCycleCountM.M(1),
metrics.MICCycleDurationM.M(metrics.SinceInSeconds(begin)))
stats.PrintSync()
if workDone {
// We need to synchronize the cache inorder to get the latest updates.
// Even though we sync at the beginning of every cycle, we are still seeing
// conflicts indicating the assigned identities are not reflecting in
// the cache. Continue to use the sleep workaround.
time.Sleep(time.Millisecond * 200)
}
}
}
}
func (c *Client) convertAssignedIDListToMap(addList, deleteList, updateList map[string]aadpodid.AzureAssignedIdentity, nodeMap map[string]trackUserAssignedMSIIds) {
for _, createID := range addList {
if trackList, ok := nodeMap[createID.Spec.NodeName]; ok {
trackList.assignedIDsToCreate = append(trackList.assignedIDsToCreate, createID)
nodeMap[createID.Spec.NodeName] = trackList
continue
}
nodeMap[createID.Spec.NodeName] = trackUserAssignedMSIIds{assignedIDsToCreate: []aadpodid.AzureAssignedIdentity{createID}}
}
for _, delID := range deleteList {
if trackList, ok := nodeMap[delID.Spec.NodeName]; ok {
trackList.assignedIDsToDelete = append(trackList.assignedIDsToDelete, delID)
nodeMap[delID.Spec.NodeName] = trackList
continue
}
nodeMap[delID.Spec.NodeName] = trackUserAssignedMSIIds{assignedIDsToDelete: []aadpodid.AzureAssignedIdentity{delID}}
}
for _, updateID := range updateList {
if trackList, ok := nodeMap[updateID.Spec.NodeName]; ok {
trackList.assignedIDsToUpdate = append(trackList.assignedIDsToUpdate, updateID)
nodeMap[updateID.Spec.NodeName] = trackList
continue
}
nodeMap[updateID.Spec.NodeName] = trackUserAssignedMSIIds{assignedIDsToUpdate: []aadpodid.AzureAssignedIdentity{updateID}}
}
}
func (c *Client) createDesiredAssignedIdentityList(
listPods []*corev1.Pod, listBindings *[]aadpodid.AzureIdentityBinding, idMap map[string]aadpodid.AzureIdentity) (map[string]aadpodid.AzureAssignedIdentity, map[string]bool, error) {
// For each pod, check what bindings are matching. For each binding create volatile azure assigned identity.
// Compare this list with the current list of azure assigned identities.
// For any new assigned identities found in this volatile list, create assigned identity and assign user assigned msis.
// For any assigned ids not present the volatile list, proceed with the deletion.
nodeRefs := make(map[string]bool)
newAssignedIDs := make(map[string]aadpodid.AzureAssignedIdentity)
for _, pod := range listPods {
klog.V(6).Infof("checking pod %s/%s", pod.Namespace, pod.Name)
if pod.Spec.NodeName == "" {
// Node is not yet allocated. In that case skip the pod
klog.Infof("pod %s/%s has no assigned node yet. it will be ignored", pod.Namespace, pod.Name)
continue
}
crdPodLabelVal := pod.Labels[aadpodid.CRDLabelKey]
klog.V(6).Infof("pod: %s/%s. Label value: %v", pod.Namespace, pod.Name, crdPodLabelVal)
if crdPodLabelVal == "" {
// No binding mentioned in the label. Just continue to the next pod
klog.Infof("pod %s/%s doesn't contain %s label field. it will be ignored", pod.Namespace, pod.Name, aadpodid.CRDLabelKey)
continue
}
var matchedBindings []aadpodid.AzureIdentityBinding
for _, allBinding := range *listBindings {
klog.V(6).Infof("check the binding (pod - %s/%s): %s", pod.Namespace, pod.Name, allBinding.Spec.Selector)
if allBinding.Spec.Selector == crdPodLabelVal {
klog.V(5).Infof("found binding match for pod %s/%s with binding %s/%s", pod.Namespace, pod.Name, allBinding.Namespace, allBinding.Name)
matchedBindings = append(matchedBindings, allBinding)
nodeRefs[pod.Spec.NodeName] = true
}
}
if len(matchedBindings) == 0 {
klog.Infof("No AzureIdentityBinding found for pod %s/%s that matches selector: %s. it will be ignored", pod.Namespace, pod.Name, crdPodLabelVal)
continue
}
for _, binding := range matchedBindings {
klog.V(5).Infof("looking up id map: %s/%s", binding.Namespace, binding.Spec.AzureIdentity)
if azureID, idPresent := idMap[getIDKey(binding.Namespace, binding.Spec.AzureIdentity)]; idPresent {
// working in Namespaced mode or this specific identity is namespaced
if c.IsNamespaced || aadpodid.IsNamespacedIdentity(&azureID) {
// They have to match all
if !(azureID.Namespace == binding.Namespace && binding.Namespace == pod.Namespace) {
klog.V(5).Infof("identity %s/%s was matched via binding %s/%s to %s/%s but namespaced identity is enforced, so it will be ignored",
azureID.Namespace, azureID.Name, binding.Namespace, binding.Name, pod.Namespace, pod.Name)
continue
}
}
klog.V(5).Infof("identity %s/%s assigned to %s/%s via %s/%s", azureID.Namespace, azureID.Name, pod.Namespace, pod.Name, binding.Namespace, binding.Name)
assignedID, err := c.makeAssignedIDs(azureID, binding, pod.Name, pod.Namespace, pod.Spec.NodeName)
if err != nil {
klog.Errorf("failed to create an AzureAssignedIdentity between pod %s/%s and AzureIdentity %s/%s, error: %+v", pod.Namespace, pod.Name, azureID.Namespace, azureID.Name, err)
continue
}
newAssignedIDs[assignedID.Name] = *assignedID
} else {
// This is the case where the identity has been deleted.
// In such a case, we will skip it from matching binding.
// This will ensure that the new assigned ids created will not have the
// one associated with this azure identity.
klog.Infof("%s identity not found when using %s/%s binding", binding.Spec.AzureIdentity, binding.Namespace, binding.Name)
}
}
}
return newAssignedIDs, nodeRefs, nil
}
// getListOfIdsToDelete will go over the delete list to determine if the id is required to be deleted
// only user assigned identity not in use are added to the remove list for the node
func (c *Client) getListOfIdsToDelete(deleteList, beforeUpdateList, afterUpdateList, newAssignedIDs map[string]aadpodid.AzureAssignedIdentity,
nodeMap map[string]trackUserAssignedMSIIds,
nodeRefs map[string]bool) {
vmssGroups, err := getVMSSGroups(c.NodeClient, nodeRefs)
if err != nil {
klog.Errorf("failed to get VMSS groups, error: %+v", err)
return
}
consolidatedMapToCheck := make(map[string]aadpodid.AzureAssignedIdentity)
for name, id := range newAssignedIDs {
consolidatedMapToCheck[name] = id
}
for name, id := range afterUpdateList {
consolidatedMapToCheck[name] = id
}
for _, delID := range deleteList {
err := c.shouldRemoveID(delID, consolidatedMapToCheck, nodeMap, vmssGroups)
if err != nil {
klog.Errorf("failed to check if identity should be removed, error: %+v", err)
}
}
// this loop checks the azure identity before it was updated and cleans up
// the old identity
for _, oldUpdateID := range beforeUpdateList {
err := c.shouldRemoveID(oldUpdateID, consolidatedMapToCheck, nodeMap, vmssGroups)
if err != nil {
klog.Errorf("failed to check if identity should be removed, error: %+v", err)
}
}
}
// getListOfIdsToAssign will add the id to the append list for node if it's user assigned identity
func (c *Client) getListOfIdsToAssign(addList, updateList map[string]aadpodid.AzureAssignedIdentity, nodeMap map[string]trackUserAssignedMSIIds) {
for _, createID := range addList {
c.shouldAssignID(createID, nodeMap)
}
for _, updateID := range updateList {
c.shouldAssignID(updateID, nodeMap)
}
}
func (c *Client) shouldAssignID(assignedID aadpodid.AzureAssignedIdentity, nodeMap map[string]trackUserAssignedMSIIds) {
id := assignedID.Spec.AzureIdentityRef
isUserAssignedMSI := c.checkIfUserAssignedMSI(*id)
if assignedID.Status.Status == "" || assignedID.Status.Status == aadpodid.AssignedIDCreated {
if isUserAssignedMSI {
c.appendToAddListForNode(id.Spec.ResourceID, assignedID.Spec.NodeName, nodeMap)
}
}
klog.V(5).Infof("binding applied: %+v", assignedID.Spec.AzureBindingRef)
}
func (c *Client) shouldRemoveID(assignedID aadpodid.AzureAssignedIdentity,
newAssignedIDs map[string]aadpodid.AzureAssignedIdentity,
nodeMap map[string]trackUserAssignedMSIIds, vmssGroups *vmssGroupList) error {
klog.V(5).Infof("deletion of id: %s", assignedID.Name)
inUse, err := c.checkIfInUse(assignedID, newAssignedIDs, vmssGroups)
if err != nil {
return err
}
id := assignedID.Spec.AzureIdentityRef
isUserAssignedMSI := c.checkIfUserAssignedMSI(*id)
isImmutableIdentity := c.checkIfIdentityImmutable(id.Spec.ClientID)
// this case includes Assigned state and empty state to ensure backward compatibility
if assignedID.Status.Status == aadpodid.AssignedIDAssigned || assignedID.Status.Status == "" {
// only user assigned identities that are not in use and are not defined as
// immutable will be removed from underlying node/vmss
if !inUse && isUserAssignedMSI && !isImmutableIdentity {
c.appendToRemoveListForNode(id.Spec.ResourceID, assignedID.Spec.NodeName, nodeMap)
}
}
klog.V(5).Infof("binding removed: %+v", assignedID.Spec.AzureBindingRef)
return nil
}
func (c *Client) matchAssignedID(x aadpodid.AzureAssignedIdentity, y aadpodid.AzureAssignedIdentity) (ret bool) {
bindingX := x.Spec.AzureBindingRef
bindingY := y.Spec.AzureBindingRef
idX := x.Spec.AzureIdentityRef
idY := y.Spec.AzureIdentityRef
klog.V(7).Infof("assignedidX - %+v\n", x)
klog.V(7).Infof("assignedidY - %+v\n", y)
klog.V(7).Infof("bindingX - %+v\n", bindingX)
klog.V(7).Infof("bindingY - %+v\n", bindingY)
klog.V(7).Infof("idX - %+v\n", idX)
klog.V(7).Infof("idY - %+v\n", idY)
return bindingX.Name == bindingY.Name &&
bindingX.ResourceVersion == bindingY.ResourceVersion &&
idX.Name == idY.Name &&
idX.ResourceVersion == idY.ResourceVersion &&
x.Spec.Pod == y.Spec.Pod &&
x.Spec.PodNamespace == y.Spec.PodNamespace &&
x.Spec.NodeName == y.Spec.NodeName
}
func (c *Client) getAzureAssignedIDsToCreate(old, new map[string]aadpodid.AzureAssignedIdentity) (map[string]aadpodid.AzureAssignedIdentity, error) {
// everything in new needs to be created
if len(old) == 0 {
return new, nil
}
create := make(map[string]aadpodid.AzureAssignedIdentity)
begin := time.Now()
for assignedIDName, newAssignedID := range new {
oldAssignedID, exists := old[assignedIDName]
idMatch := false
if exists {
idMatch = c.matchAssignedID(oldAssignedID, newAssignedID)
}
if idMatch && oldAssignedID.Status.Status == aadpodid.AssignedIDCreated {
// if the old assigned id is in created state, then the identity assignment to the node
// is not done. Adding to the list will ensure we retry identity assignment to node for
// this assigned identity.
klog.V(5).Infof("ok: %v, Create added: %s as assignedID in CREATED state", idMatch, assignedIDName)
create[assignedIDName] = oldAssignedID
}
if !idMatch {
// We are done checking that this new id is not present in the old
// list. So we will add it to the create list.
klog.V(5).Infof("ok: %v, Create added: %s", idMatch, assignedIDName)
create[assignedIDName] = newAssignedID
}
}
stats.Put(stats.FindAzureAssignedIdentitiesToCreate, time.Since(begin))
return create, nil
}
func (c *Client) getAzureAssignedIDsToDelete(old, new map[string]aadpodid.AzureAssignedIdentity) (map[string]aadpodid.AzureAssignedIdentity, error) {
delete := make(map[string]aadpodid.AzureAssignedIdentity)
// nothing to delete
if len(old) == 0 {
return delete, nil
}
// delete everything as nothing in new
if len(new) == 0 {
return old, nil
}
begin := time.Now()
for assignedIDName, oldAssignedID := range old {
newAssignedID, exists := new[assignedIDName]
idMatch := false
if exists {
idMatch = c.matchAssignedID(oldAssignedID, newAssignedID)
}
// assigned identity exists in the desired list too which means
// it should not be deleted
if exists && idMatch {
continue
}
// We are done checking that this old id is not present in the new
// list. So we will add it to the delete list.
delete[assignedIDName] = oldAssignedID
}
stats.Put(stats.FindAzureAssignedIdentitiesToDelete, time.Since(begin))
return delete, nil
}
// getAzureAssignedIdentitiesToUpdate returns a list of assignedIDs that need to be updated
// because of change in azureIdentity or azurerIdentityBinding
// returns 2 maps, first the assigned IDs currently on cluster, second the assignedID value to update with
func (c *Client) getAzureAssignedIdentitiesToUpdate(add, del map[string]aadpodid.AzureAssignedIdentity) (map[string]aadpodid.AzureAssignedIdentity, map[string]aadpodid.AzureAssignedIdentity) {
beforeUpdate := make(map[string]aadpodid.AzureAssignedIdentity)
afterUpdate := make(map[string]aadpodid.AzureAssignedIdentity)
// no updates required as assigned identities will not be in both lists
if len(add) == 0 || len(del) == 0 {
return beforeUpdate, afterUpdate
}
for assignedIDName, addAssignedID := range add {
if delAssignedID, exists := del[assignedIDName]; exists {
// assigned identity exists in add and del list
// update the assigned identity to the latest
addAssignedID.ObjectMeta = delAssignedID.ObjectMeta
beforeUpdate[assignedIDName] = delAssignedID
afterUpdate[assignedIDName] = addAssignedID
// since this is part of update, remove the assignedID from the add and del list
delete(add, assignedIDName)
delete(del, assignedIDName)
}
}
return beforeUpdate, afterUpdate
}
func (c *Client) makeAssignedIDs(azID aadpodid.AzureIdentity, azBinding aadpodid.AzureIdentityBinding, podName, podNameSpace, nodeName string) (res *aadpodid.AzureAssignedIdentity, err error) {
binding := azBinding
id := azID
labels := make(map[string]string)
labels["nodename"] = nodeName
oMeta := v1.ObjectMeta{
Name: c.getAssignedIDName(podName, podNameSpace, azID.Name),
Labels: labels,
}
assignedID := &aadpodid.AzureAssignedIdentity{
ObjectMeta: oMeta,
Spec: aadpodid.AzureAssignedIdentitySpec{
AzureIdentityRef: &id,
AzureBindingRef: &binding,
Pod: podName,
PodNamespace: podNameSpace,
NodeName: nodeName,
},
Status: aadpodid.AzureAssignedIdentityStatus{
AvailableReplicas: 1,
},
}
// if we are in namespaced mode (or az identity is namespaced)
if c.IsNamespaced || aadpodid.IsNamespacedIdentity(&id) {
assignedID.Namespace = azID.Namespace
} else {
// eventually this should be identity namespace
// but to maintain back compat we will use existing
// behavior
assignedID.Namespace = "default"
}
klog.V(6).Infof("binding - %+v identity - %+v", azBinding, azID)
klog.V(5).Infof("making assigned ID: %+v", assignedID)
return assignedID, nil
}
func (c *Client) createAssignedIdentity(assignedID *aadpodid.AzureAssignedIdentity) error {
err := c.CRDClient.CreateAssignedIdentity(assignedID)
if err != nil {
return err
}
return nil
}
func (c *Client) removeAssignedIdentity(assignedID *aadpodid.AzureAssignedIdentity) error {
err := c.CRDClient.RemoveAssignedIdentity(assignedID)
if err != nil {
return err
}
return nil
}
func (c *Client) updateAssignedIdentity(assignedID *aadpodid.AzureAssignedIdentity) error {
return c.CRDClient.UpdateAssignedIdentity(assignedID)
}
func (c *Client) appendToRemoveListForNode(resourceID, nodeName string, nodeMap map[string]trackUserAssignedMSIIds) {
if trackList, ok := nodeMap[nodeName]; ok {
trackList.removeUserAssignedMSIIDs = append(trackList.removeUserAssignedMSIIDs, resourceID)
nodeMap[nodeName] = trackList
return
}
nodeMap[nodeName] = trackUserAssignedMSIIds{removeUserAssignedMSIIDs: []string{resourceID}}
}
func (c *Client) appendToAddListForNode(resourceID, nodeName string, nodeMap map[string]trackUserAssignedMSIIds) {
if trackList, ok := nodeMap[nodeName]; ok {
trackList.addUserAssignedMSIIDs = append(trackList.addUserAssignedMSIIDs, resourceID)
nodeMap[nodeName] = trackList
return
}
nodeMap[nodeName] = trackUserAssignedMSIIds{addUserAssignedMSIIDs: []string{resourceID}}
}
func (c *Client) checkIfUserAssignedMSI(id aadpodid.AzureIdentity) bool {
return id.Spec.Type == aadpodid.UserAssignedMSI
}
func (c *Client) getAssignedIDName(podName, podNameSpace, idName string) string {
return podName + "-" + podNameSpace + "-" + idName
}
func (c *Client) checkIfMSIExistsOnNode(id *aadpodid.AzureIdentity, nodeName string, nodeMSIList []string) bool {
for _, userAssignedMSI := range nodeMSIList {
if strings.EqualFold(userAssignedMSI, id.Spec.ResourceID) {
return true
}
}
return false
}
func (c *Client) getUserMSIListForNode(nodeOrVMSSName string, isvmss bool) ([]string, error) {
return c.CloudClient.GetUserMSIs(nodeOrVMSSName, isvmss)
}
func getIDKey(ns, name string) string {
return strings.Join([]string{ns, name}, "/")
}
func (c *Client) convertIDListToMap(azureIdentities []aadpodid.AzureIdentity) (m map[string]aadpodid.AzureIdentity, err error) {
m = make(map[string]aadpodid.AzureIdentity, len(azureIdentities))
for _, azureIdentity := range azureIdentities {
// validate the resourceID in azure identity for type 0 (UserAssignedMSI) to ensure format is as expected
if c.checkIfUserAssignedMSI(azureIdentity) {
err := utils.ValidateResourceID(azureIdentity.Spec.ResourceID)
if err != nil {
klog.Errorf("ignoring azure identity %s/%s, error: %+v", azureIdentity.Namespace, azureIdentity.Name, err)
continue
}
}
m[getIDKey(azureIdentity.Namespace, azureIdentity.Name)] = azureIdentity
}
return m, nil
}
func (c *Client) checkIfInUse(checkAssignedID aadpodid.AzureAssignedIdentity, assignedIDMap map[string]aadpodid.AzureAssignedIdentity, vmssGroups *vmssGroupList) (bool, error) {
for _, assignedID := range assignedIDMap {
checkID := checkAssignedID.Spec.AzureIdentityRef
id := assignedID.Spec.AzureIdentityRef
// If they have the same client id, reside on the same node but the pod name is different, then the
// assigned id is in use.
// This is applicable only for user assigned MSI since that is node specific. Ignore other cases.
if checkID.Spec.Type != aadpodid.UserAssignedMSI {
continue
}
if checkAssignedID.Spec.Pod == assignedID.Spec.Pod {
// No need to do the rest of the checks in this case, since it's the same assignment
// The same identity won't be assigned to a pod twice, so it's the same reference.
continue
}
if checkID.Spec.ClientID != id.Spec.ClientID {
continue
}
if checkAssignedID.Spec.NodeName == assignedID.Spec.NodeName {
return true, nil
}
vmss, err := getVMSSGroupFromPossiblyUnreferencedNode(c.NodeClient, vmssGroups, checkAssignedID.Spec.NodeName)
if err != nil {
return false, err
}
// check if this identity is used on another node in the same vmss
// This check is needed because vmss identities currently operate on all nodes
// in the vmss not just a single node.
if vmss != nil && vmss.hasNode(assignedID.Spec.NodeName) {
return true, nil
}
}
return false, nil
}
func (c *Client) getUniqueIDs(idList []string) []string {