|
| 1 | +package k8s |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + "github.com/golang/glog" |
| 8 | + "github.com/nginxinc/kubernetes-ingress/internal/configs" |
| 9 | + conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1" |
| 10 | + api_v1 "k8s.io/api/core/v1" |
| 11 | + "k8s.io/apimachinery/pkg/fields" |
| 12 | + "k8s.io/client-go/tools/cache" |
| 13 | +) |
| 14 | + |
| 15 | +func createGlobalConfigurationHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs { |
| 16 | + return cache.ResourceEventHandlerFuncs{ |
| 17 | + AddFunc: func(obj interface{}) { |
| 18 | + gc := obj.(*conf_v1.GlobalConfiguration) |
| 19 | + glog.V(3).Infof("Adding GlobalConfiguration: %v", gc.Name) |
| 20 | + lbc.AddSyncQueue(gc) |
| 21 | + }, |
| 22 | + DeleteFunc: func(obj interface{}) { |
| 23 | + gc, isGc := obj.(*conf_v1.GlobalConfiguration) |
| 24 | + if !isGc { |
| 25 | + deletedState, ok := obj.(cache.DeletedFinalStateUnknown) |
| 26 | + if !ok { |
| 27 | + glog.V(3).Infof("Error received unexpected object: %v", obj) |
| 28 | + return |
| 29 | + } |
| 30 | + gc, ok = deletedState.Obj.(*conf_v1.GlobalConfiguration) |
| 31 | + if !ok { |
| 32 | + glog.V(3).Infof("Error DeletedFinalStateUnknown contained non-GlobalConfiguration object: %v", deletedState.Obj) |
| 33 | + return |
| 34 | + } |
| 35 | + } |
| 36 | + glog.V(3).Infof("Removing GlobalConfiguration: %v", gc.Name) |
| 37 | + lbc.AddSyncQueue(gc) |
| 38 | + }, |
| 39 | + UpdateFunc: func(old, cur interface{}) { |
| 40 | + curGc := cur.(*conf_v1.GlobalConfiguration) |
| 41 | + if !reflect.DeepEqual(old, cur) { |
| 42 | + glog.V(3).Infof("GlobalConfiguration %v changed, syncing", curGc.Name) |
| 43 | + lbc.AddSyncQueue(curGc) |
| 44 | + } |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (lbc *LoadBalancerController) addGlobalConfigurationHandler(handlers cache.ResourceEventHandlerFuncs, namespace string, name string) { |
| 50 | + options := cache.InformerOptions{ |
| 51 | + ListerWatcher: cache.NewListWatchFromClient( |
| 52 | + lbc.confClient.K8sV1().RESTClient(), |
| 53 | + "globalconfigurations", |
| 54 | + namespace, |
| 55 | + fields.Set{"metadata.name": name}.AsSelector()), |
| 56 | + ObjectType: &conf_v1.GlobalConfiguration{}, |
| 57 | + ResyncPeriod: lbc.resync, |
| 58 | + Handler: handlers, |
| 59 | + } |
| 60 | + lbc.globalConfigurationLister, lbc.globalConfigurationController = cache.NewInformerWithOptions(options) |
| 61 | + lbc.cacheSyncs = append(lbc.cacheSyncs, lbc.globalConfigurationController.HasSynced) |
| 62 | +} |
| 63 | + |
| 64 | +func (lbc *LoadBalancerController) syncGlobalConfiguration(task task) { |
| 65 | + key := task.Key |
| 66 | + obj, gcExists, err := lbc.globalConfigurationLister.GetByKey(key) |
| 67 | + if err != nil { |
| 68 | + lbc.syncQueue.Requeue(task, err) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + var changes []ResourceChange |
| 73 | + var problems []ConfigurationProblem |
| 74 | + var validationErr error |
| 75 | + |
| 76 | + if !gcExists { |
| 77 | + glog.V(2).Infof("Deleting GlobalConfiguration: %v\n", key) |
| 78 | + |
| 79 | + changes, problems = lbc.configuration.DeleteGlobalConfiguration() |
| 80 | + } else { |
| 81 | + glog.V(2).Infof("Adding or Updating GlobalConfiguration: %v\n", key) |
| 82 | + |
| 83 | + gc := obj.(*conf_v1.GlobalConfiguration) |
| 84 | + changes, problems, validationErr = lbc.configuration.AddOrUpdateGlobalConfiguration(gc) |
| 85 | + } |
| 86 | + |
| 87 | + updateErr := lbc.processChangesFromGlobalConfiguration(changes) |
| 88 | + |
| 89 | + if gcExists { |
| 90 | + eventTitle := "Updated" |
| 91 | + eventType := api_v1.EventTypeNormal |
| 92 | + eventMessage := fmt.Sprintf("GlobalConfiguration %s was added or updated", key) |
| 93 | + |
| 94 | + if validationErr != nil { |
| 95 | + eventTitle = "AddedOrUpdatedWithError" |
| 96 | + eventType = api_v1.EventTypeWarning |
| 97 | + eventMessage = fmt.Sprintf("GlobalConfiguration %s is updated with errors: %v", key, validationErr) |
| 98 | + } |
| 99 | + |
| 100 | + if updateErr != nil { |
| 101 | + eventTitle += "WithError" |
| 102 | + eventType = api_v1.EventTypeWarning |
| 103 | + eventMessage = fmt.Sprintf("%s; with reload error: %v", eventMessage, updateErr) |
| 104 | + } |
| 105 | + |
| 106 | + gc := obj.(*conf_v1.GlobalConfiguration) |
| 107 | + lbc.recorder.Eventf(gc, eventType, eventTitle, eventMessage) |
| 108 | + } |
| 109 | + |
| 110 | + lbc.processProblems(problems) |
| 111 | +} |
| 112 | + |
| 113 | +// processChangesFromGlobalConfiguration processes changes that come from updates to the GlobalConfiguration resource. |
| 114 | +// Such changes need to be processed at once to prevent any inconsistencies in the generated NGINX config. |
| 115 | +func (lbc *LoadBalancerController) processChangesFromGlobalConfiguration(changes []ResourceChange) error { |
| 116 | + var updatedTSExes []*configs.TransportServerEx |
| 117 | + var updatedVSExes []*configs.VirtualServerEx |
| 118 | + var deletedTSKeys []string |
| 119 | + var deletedVSKeys []string |
| 120 | + |
| 121 | + var updatedResources []Resource |
| 122 | + |
| 123 | + for _, c := range changes { |
| 124 | + switch impl := c.Resource.(type) { |
| 125 | + case *VirtualServerConfiguration: |
| 126 | + if c.Op == AddOrUpdate { |
| 127 | + vsEx := lbc.createVirtualServerEx(impl.VirtualServer, impl.VirtualServerRoutes) |
| 128 | + |
| 129 | + updatedVSExes = append(updatedVSExes, vsEx) |
| 130 | + updatedResources = append(updatedResources, impl) |
| 131 | + } else if c.Op == Delete { |
| 132 | + key := getResourceKey(&impl.VirtualServer.ObjectMeta) |
| 133 | + |
| 134 | + deletedVSKeys = append(deletedVSKeys, key) |
| 135 | + } |
| 136 | + case *TransportServerConfiguration: |
| 137 | + if c.Op == AddOrUpdate { |
| 138 | + tsEx := lbc.createTransportServerEx(impl.TransportServer, impl.ListenerPort) |
| 139 | + |
| 140 | + updatedTSExes = append(updatedTSExes, tsEx) |
| 141 | + updatedResources = append(updatedResources, impl) |
| 142 | + } else if c.Op == Delete { |
| 143 | + key := getResourceKey(&impl.TransportServer.ObjectMeta) |
| 144 | + |
| 145 | + deletedTSKeys = append(deletedTSKeys, key) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + var updateErr error |
| 151 | + |
| 152 | + if len(updatedTSExes) > 0 || len(deletedTSKeys) > 0 { |
| 153 | + tsUpdateErrs := lbc.configurator.UpdateTransportServers(updatedTSExes, deletedTSKeys) |
| 154 | + |
| 155 | + if len(tsUpdateErrs) > 0 { |
| 156 | + updateErr = fmt.Errorf("errors received from updating TransportServers after GlobalConfiguration change: %v", tsUpdateErrs) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if len(updatedVSExes) > 0 || len(deletedVSKeys) > 0 { |
| 161 | + vsUpdateErrs := lbc.configurator.UpdateVirtualServers(updatedVSExes, deletedVSKeys) |
| 162 | + |
| 163 | + if len(vsUpdateErrs) > 0 { |
| 164 | + updateErr = fmt.Errorf("errors received from updating VirtualSrvers after GlobalConfiguration change: %v", vsUpdateErrs) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + lbc.updateResourcesStatusAndEvents(updatedResources, configs.Warnings{}, updateErr) |
| 169 | + |
| 170 | + return updateErr |
| 171 | +} |
0 commit comments