|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "github.com/aws/amazon-network-policy-controller-k8s/pkg/k8s" |
| 8 | + "github.com/go-logr/logr" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/fields" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "k8s.io/apimachinery/pkg/watch" |
| 15 | + "k8s.io/client-go/kubernetes" |
| 16 | + "k8s.io/client-go/tools/cache" |
| 17 | +) |
| 18 | + |
| 19 | +// +kubebuilder:rbac:groups="",resources=configmaps,namespace="system",resourceNames=amazon-vpc-cni,verbs=get;list;watch |
| 20 | + |
| 21 | +type ConfigmapManager interface { |
| 22 | + MonitorConfigMap(ctx context.Context) error |
| 23 | + IsControllerEnabled() bool |
| 24 | +} |
| 25 | + |
| 26 | +var _ ConfigmapManager = (*defaultConfigmapManager)(nil) |
| 27 | + |
| 28 | +type defaultConfigmapManager struct { |
| 29 | + initialState bool |
| 30 | + resourceRef types.NamespacedName |
| 31 | + store cache.Store |
| 32 | + rt *cache.Reflector |
| 33 | + clientSet *kubernetes.Clientset |
| 34 | + logger logr.Logger |
| 35 | + cancelFn context.CancelFunc |
| 36 | + monitorStopChan chan struct{} |
| 37 | + storeNotifyChan chan struct{} |
| 38 | + configMapCheckFunction func(*corev1.ConfigMap) bool |
| 39 | +} |
| 40 | + |
| 41 | +func NewConfigmapManager(resourceRef types.NamespacedName, clientSet *kubernetes.Clientset, |
| 42 | + cancelFn context.CancelFunc, configmapCheckFunction func(configMap *corev1.ConfigMap) bool, logger logr.Logger) *defaultConfigmapManager { |
| 43 | + storeNotifyChan := make(chan struct{}) |
| 44 | + cmStore := k8s.NewConfigMapStore(storeNotifyChan) |
| 45 | + return &defaultConfigmapManager{ |
| 46 | + clientSet: clientSet, |
| 47 | + resourceRef: resourceRef, |
| 48 | + store: cmStore, |
| 49 | + logger: logger, |
| 50 | + cancelFn: cancelFn, |
| 51 | + monitorStopChan: make(chan struct{}), |
| 52 | + storeNotifyChan: storeNotifyChan, |
| 53 | + configMapCheckFunction: configmapCheckFunction, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// IsControllerEnabled returns the initial state of the policy controller. |
| 58 | +func (m *defaultConfigmapManager) IsControllerEnabled() bool { |
| 59 | + m.logger.V(1).Info("IsControllerEnabled() returning", "value", m.initialState) |
| 60 | + return m.initialState |
| 61 | +} |
| 62 | + |
| 63 | +// MonitorConfigMap starts cache reflector and watches for configmap updates. |
| 64 | +func (m *defaultConfigmapManager) MonitorConfigMap(ctx context.Context) error { |
| 65 | + fieldSelector := fields.Set{"metadata.name": m.resourceRef.Name}.AsSelector().String() |
| 66 | + listFunc := func(options metav1.ListOptions) (runtime.Object, error) { |
| 67 | + options.FieldSelector = fieldSelector |
| 68 | + return m.clientSet.CoreV1().ConfigMaps(m.resourceRef.Namespace).List(ctx, options) |
| 69 | + } |
| 70 | + watchFunc := func(options metav1.ListOptions) (watch.Interface, error) { |
| 71 | + options.FieldSelector = fieldSelector |
| 72 | + return m.clientSet.CoreV1().ConfigMaps(m.resourceRef.Namespace).Watch(ctx, options) |
| 73 | + } |
| 74 | + m.rt = cache.NewReflector(&cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}, |
| 75 | + &corev1.ConfigMap{}, |
| 76 | + m.store, |
| 77 | + 0, |
| 78 | + ) |
| 79 | + go m.rt.Run(m.monitorStopChan) |
| 80 | + go m.listenForConfigMapUpdates() |
| 81 | + |
| 82 | + if _, err := m.setInitialControllerState(); err != nil { |
| 83 | + m.logger.Info("Failed to set initial state", "err", err) |
| 84 | + return err |
| 85 | + } |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// listen for the messages in the storeNotifyChan in a loop and update the state of the policy controller accordingly. |
| 90 | +func (m *defaultConfigmapManager) listenForConfigMapUpdates() { |
| 91 | + defer func() { |
| 92 | + m.logger.Info("Controller detected changes to the configmap, cancelling manager context") |
| 93 | + close(m.monitorStopChan) |
| 94 | + m.cancelFn() |
| 95 | + }() |
| 96 | + |
| 97 | + for { |
| 98 | + select { |
| 99 | + case <-m.storeNotifyChan: |
| 100 | + enabled, err := m.getCurrentEnabledConfig() |
| 101 | + if err != nil { |
| 102 | + m.logger.Error(err, "Failed to get controller state from configmap") |
| 103 | + return |
| 104 | + } |
| 105 | + m.logger.V(1).Info("Received configmap notification", "initial", m.initialState, |
| 106 | + "new", enabled) |
| 107 | + if m.initialState != enabled { |
| 108 | + m.logger.Info("Controller state changed", "initial", m.initialState, |
| 109 | + "new", enabled) |
| 110 | + return |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// getCurrentEnabledConfig gets the current state of the policy controller from the configmap |
| 117 | +func (m *defaultConfigmapManager) getCurrentEnabledConfig() (bool, error) { |
| 118 | + cm, exists, err := m.store.GetByKey(m.resourceRef.String()) |
| 119 | + if err != nil { |
| 120 | + return false, err |
| 121 | + } |
| 122 | + if !exists { |
| 123 | + return false, nil |
| 124 | + } |
| 125 | + return m.configMapCheckFunction(cm.(*corev1.ConfigMap)), nil |
| 126 | +} |
| 127 | + |
| 128 | +// setInitialControllerState sets the initial state of the policy controller based on the configmap |
| 129 | +func (m *defaultConfigmapManager) setInitialControllerState() (retVal bool, err error) { |
| 130 | + defer func() { |
| 131 | + m.logger.V(1).Info("setInitialControllerState", "retVal", retVal, "err", err) |
| 132 | + m.initialState = retVal |
| 133 | + }() |
| 134 | + // Wait for cache sync |
| 135 | + if !cache.WaitForCacheSync(m.monitorStopChan, func() bool { |
| 136 | + return m.rt.LastSyncResourceVersion() != "" |
| 137 | + }) { |
| 138 | + return false, errors.New("failed to sync configmap cache") |
| 139 | + } |
| 140 | + return m.getCurrentEnabledConfig() |
| 141 | +} |
0 commit comments