|
| 1 | +// Copyright 2020 Authors of Cilium |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package watchers |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/cilium/cilium/pkg/k8s" |
| 19 | + cilium_v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2" |
| 20 | + "github.com/cilium/cilium/pkg/k8s/informer" |
| 21 | + "github.com/cilium/cilium/pkg/logging/logfields" |
| 22 | + "github.com/cilium/cilium/pkg/redirectpolicy" |
| 23 | + |
| 24 | + "github.com/sirupsen/logrus" |
| 25 | + v1 "k8s.io/api/core/v1" |
| 26 | + "k8s.io/apimachinery/pkg/fields" |
| 27 | + "k8s.io/apimachinery/pkg/util/wait" |
| 28 | + "k8s.io/client-go/tools/cache" |
| 29 | +) |
| 30 | + |
| 31 | +func (k *K8sWatcher) ciliumLocalRedirectPolicyInit(ciliumLRPClient *k8s.K8sCiliumClient) { |
| 32 | + |
| 33 | + _, lrpController := informer.NewInformer( |
| 34 | + cache.NewListWatchFromClient(ciliumLRPClient.CiliumV2().RESTClient(), |
| 35 | + "ciliumlocalredirectpolicies", v1.NamespaceAll, fields.Everything()), |
| 36 | + &cilium_v2.CiliumLocalRedirectPolicy{}, |
| 37 | + 0, |
| 38 | + cache.ResourceEventHandlerFuncs{ |
| 39 | + AddFunc: func(obj interface{}) { |
| 40 | + var valid, equal bool |
| 41 | + defer func() { k.K8sEventReceived(metricCLRP, metricCreate, valid, equal) }() |
| 42 | + if cLRP := k8s.ObjToCLRP(obj); cLRP != nil { |
| 43 | + valid = true |
| 44 | + err := k.addCiliumLocalRedirectPolicy(cLRP) |
| 45 | + k.K8sEventProcessed(metricCLRP, metricCreate, err == nil) |
| 46 | + } |
| 47 | + |
| 48 | + }, |
| 49 | + UpdateFunc: func(oldObj, newObj interface{}) { |
| 50 | + log.Info("Local Redirect Policy updates are not handled") |
| 51 | + |
| 52 | + }, |
| 53 | + DeleteFunc: func(obj interface{}) { |
| 54 | + var valid, equal bool |
| 55 | + defer func() { k.K8sEventReceived(metricCLRP, metricDelete, valid, equal) }() |
| 56 | + cLRP := k8s.ObjToCLRP(obj) |
| 57 | + if cLRP == nil { |
| 58 | + return |
| 59 | + } |
| 60 | + valid = true |
| 61 | + err := k.deleteCiliumLocalRedirectPolicy(cLRP) |
| 62 | + k.K8sEventProcessed(metricCLRP, metricDelete, err == nil) |
| 63 | + }, |
| 64 | + }, |
| 65 | + k8s.ConvertToCiliumLocalRedirectPolicy, |
| 66 | + ) |
| 67 | + k.blockWaitGroupToSyncResources( |
| 68 | + wait.NeverStop, |
| 69 | + nil, |
| 70 | + lrpController, |
| 71 | + k8sAPIGroupCiliumLocalRedirectPolicyV2, |
| 72 | + ) |
| 73 | + |
| 74 | + go lrpController.Run(wait.NeverStop) |
| 75 | + k.k8sAPIGroups.addAPI(k8sAPIGroupCiliumClusterwideNetworkPolicyV2) |
| 76 | +} |
| 77 | + |
| 78 | +func (k *K8sWatcher) addCiliumLocalRedirectPolicy(clrp *cilium_v2.CiliumLocalRedirectPolicy) error { |
| 79 | + scopedLog := log.WithFields(logrus.Fields{ |
| 80 | + logfields.CiliumLocalRedirectName: clrp.ObjectMeta.Name, |
| 81 | + logfields.K8sUID: clrp.ObjectMeta.UID, |
| 82 | + logfields.K8sAPIVersion: clrp.TypeMeta.APIVersion, |
| 83 | + logfields.K8sNamespace: clrp.ObjectMeta.Namespace, |
| 84 | + }) |
| 85 | + |
| 86 | + scopedLog.Debug("Add CiliumLocalRedirectPolicy") |
| 87 | + |
| 88 | + rp, policyAddErr := redirectpolicy.Parse(clrp, true) |
| 89 | + if policyAddErr == nil { |
| 90 | + _, policyAddErr = k.redirectPolicyManager.AddRedirectPolicy(*rp, &k.K8sSvcCache, |
| 91 | + k.podStore) |
| 92 | + } |
| 93 | + |
| 94 | + if policyAddErr != nil { |
| 95 | + scopedLog.WithError(policyAddErr).Warn("Failed to add CiliumLocalRedirectPolicy") |
| 96 | + } else { |
| 97 | + scopedLog.Info("Added CiliumLocalRedirectPolicy") |
| 98 | + } |
| 99 | + |
| 100 | + //TODO update status |
| 101 | + |
| 102 | + return policyAddErr |
| 103 | +} |
| 104 | + |
| 105 | +func (k *K8sWatcher) deleteCiliumLocalRedirectPolicy(clrp *cilium_v2.CiliumLocalRedirectPolicy) error { |
| 106 | + scopedLog := log.WithFields(logrus.Fields{ |
| 107 | + logfields.CiliumLocalRedirectName: clrp.ObjectMeta.Name, |
| 108 | + logfields.K8sUID: clrp.ObjectMeta.UID, |
| 109 | + logfields.K8sAPIVersion: clrp.TypeMeta.APIVersion, |
| 110 | + logfields.K8sNamespace: clrp.ObjectMeta.Namespace, |
| 111 | + }) |
| 112 | + |
| 113 | + scopedLog.Debug("Delete CiliumLocalRedirectPolicy") |
| 114 | + |
| 115 | + rp, policyDelErr := redirectpolicy.Parse(clrp, false) |
| 116 | + if policyDelErr == nil { |
| 117 | + policyDelErr = k.redirectPolicyManager.DeleteRedirectPolicy(*rp) |
| 118 | + } |
| 119 | + |
| 120 | + if policyDelErr != nil { |
| 121 | + scopedLog.WithError(policyDelErr).Warn("Failed to delete CiliumLocalRedirectPolicy") |
| 122 | + } else { |
| 123 | + scopedLog.Info("Deleted CiliumLocalRedirectPolicy") |
| 124 | + } |
| 125 | + |
| 126 | + return policyDelErr |
| 127 | +} |
0 commit comments