|
| 1 | +/* |
| 2 | +Copyright © 2018 inwinSTACK.inc |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package namespace |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/golang/glog" |
| 24 | + clientset "github.com/inwinstack/blended/client/clientset/versioned" |
| 25 | + opkit "github.com/inwinstack/operator-kit" |
| 26 | + "github.com/inwinstack/pa-svc-syncker/pkg/config" |
| 27 | + "github.com/inwinstack/pa-svc-syncker/pkg/constants" |
| 28 | + "github.com/inwinstack/pa-svc-syncker/pkg/k8sutil" |
| 29 | + slice "github.com/thoas/go-funk" |
| 30 | + v1 "k8s.io/api/core/v1" |
| 31 | + "k8s.io/client-go/tools/cache" |
| 32 | +) |
| 33 | + |
| 34 | +var Resource = opkit.CustomResource{ |
| 35 | + Name: "namespace", |
| 36 | + Plural: "namespaces", |
| 37 | + Version: "v1", |
| 38 | + Kind: reflect.TypeOf(v1.Namespace{}).Name(), |
| 39 | +} |
| 40 | + |
| 41 | +type NamespaceController struct { |
| 42 | + ctx *opkit.Context |
| 43 | + client clientset.Interface |
| 44 | + cfg *config.OperatorConfig |
| 45 | +} |
| 46 | + |
| 47 | +func NewController(ctx *opkit.Context, client clientset.Interface, cfg *config.OperatorConfig) *NamespaceController { |
| 48 | + return &NamespaceController{ctx: ctx, client: client, cfg: cfg} |
| 49 | +} |
| 50 | + |
| 51 | +func (c *NamespaceController) StartWatch(namespace string, stopCh chan struct{}) error { |
| 52 | + resourceHandlerFuncs := cache.ResourceEventHandlerFuncs{ |
| 53 | + AddFunc: c.onAdd, |
| 54 | + UpdateFunc: c.onUpdate, |
| 55 | + } |
| 56 | + |
| 57 | + glog.Info("Start watching service resources.") |
| 58 | + watcher := opkit.NewWatcher(Resource, namespace, resourceHandlerFuncs, c.ctx.Clientset.CoreV1().RESTClient()) |
| 59 | + go watcher.Watch(&v1.Namespace{}, stopCh) |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func (c *NamespaceController) onAdd(obj interface{}) { |
| 64 | + ns := obj.(*v1.Namespace).DeepCopy() |
| 65 | + glog.V(2).Infof("Received add on %s namespace.", ns.Name) |
| 66 | + |
| 67 | + if ns.Status.Phase == v1.NamespaceActive { |
| 68 | + if err := c.updateSecurityPolicies(ns); err != nil { |
| 69 | + glog.Errorf("Failed to add sources addresss to all policies on %s namespace.: %+v.", ns.Name, err) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func (c *NamespaceController) onUpdate(oldObj, newObj interface{}) { |
| 75 | + ns := newObj.(*v1.Namespace).DeepCopy() |
| 76 | + glog.V(2).Infof("Received update on %s namespace.", ns.Name) |
| 77 | + |
| 78 | + if ns.Status.Phase == v1.NamespaceActive { |
| 79 | + if err := c.updateSecurityPolicies(ns); err != nil { |
| 80 | + glog.Errorf("Failed to add sources addresss to all policies on %s namespace.: %+v.", ns.Name, err) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func (c *NamespaceController) updateSecurityPolicies(ns *v1.Namespace) error { |
| 86 | + if slice.Contains(c.cfg.IgnoreNamespaces, ns.Name) { |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + addrs := []string{"any"} |
| 91 | + if value, ok := ns.Annotations[constants.AnnKeyWhiteListAddresses]; ok { |
| 92 | + addrString := strings.TrimSpace(value) |
| 93 | + if len(addrString) > 0 { |
| 94 | + addrs = strings.Split(addrString, ",") |
| 95 | + } |
| 96 | + } |
| 97 | + if err := k8sutil.UpdateSecuritiesSourceIPs(c.client, ns.Name, addrs); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
0 commit comments