2019-02-12 16:55:14 +02:00
|
|
|
package controller
|
2019-02-11 19:49:27 +02:00
|
|
|
|
|
|
|
import (
|
2019-03-04 20:40:02 +02:00
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-03-07 17:42:37 +02:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-03-04 20:40:02 +02:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
|
|
|
|
types "github.com/nirmata/kube-policy/pkg/apis/policy/v1alpha1"
|
|
|
|
clientset "github.com/nirmata/kube-policy/pkg/client/clientset/versioned"
|
2019-03-07 17:42:37 +02:00
|
|
|
policies "github.com/nirmata/kube-policy/pkg/client/clientset/versioned/typed/policy/v1alpha1"
|
2019-03-04 20:40:02 +02:00
|
|
|
informers "github.com/nirmata/kube-policy/pkg/client/informers/externalversions"
|
|
|
|
lister "github.com/nirmata/kube-policy/pkg/client/listers/policy/v1alpha1"
|
2019-02-11 19:49:27 +02:00
|
|
|
)
|
|
|
|
|
2019-02-21 20:31:18 +02:00
|
|
|
// PolicyController for CRD
|
|
|
|
type PolicyController struct {
|
2019-03-04 20:40:02 +02:00
|
|
|
policyInformerFactory informers.SharedInformerFactory
|
|
|
|
policyLister lister.PolicyLister
|
2019-03-07 17:42:37 +02:00
|
|
|
policiesInterface policies.PolicyInterface
|
2019-03-04 20:40:02 +02:00
|
|
|
logger *log.Logger
|
2019-02-11 19:49:27 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:31:18 +02:00
|
|
|
// NewPolicyController from cmd args
|
2019-03-04 20:40:02 +02:00
|
|
|
func NewPolicyController(config *rest.Config, logger *log.Logger) (*PolicyController, error) {
|
|
|
|
if logger == nil {
|
|
|
|
logger = log.New(os.Stdout, "Policy Controller: ", log.LstdFlags|log.Lshortfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("Client Config should be set for controller")
|
|
|
|
}
|
|
|
|
|
|
|
|
policyClientset, err := clientset.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
policyInformerFactory := informers.NewSharedInformerFactory(policyClientset, time.Second*30)
|
|
|
|
policyInformer := policyInformerFactory.Nirmata().V1alpha1().Policies()
|
|
|
|
|
|
|
|
controller := &PolicyController{
|
|
|
|
policyInformerFactory: policyInformerFactory,
|
|
|
|
policyLister: policyInformer.Lister(),
|
2019-03-07 17:42:37 +02:00
|
|
|
policiesInterface: policyClientset.NirmataV1alpha1().Policies("default"),
|
2019-03-04 20:40:02 +02:00
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
policyInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: controller.createPolicyHandler,
|
|
|
|
UpdateFunc: controller.updatePolicyHandler,
|
|
|
|
DeleteFunc: controller.deletePolicyHandler,
|
|
|
|
})
|
|
|
|
|
|
|
|
return controller, nil
|
2019-02-11 19:49:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run is main controller thread
|
2019-02-21 20:31:18 +02:00
|
|
|
func (c *PolicyController) Run(stopCh <-chan struct{}) {
|
2019-03-04 20:40:02 +02:00
|
|
|
c.policyInformerFactory.Start(stopCh)
|
2019-02-13 20:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPolicies retrieves all policy resources
|
|
|
|
// from cache. Cache is refreshed by informer
|
2019-02-21 20:31:18 +02:00
|
|
|
func (c *PolicyController) GetPolicies() []types.Policy {
|
2019-03-04 20:40:02 +02:00
|
|
|
// Create nil Selector to grab all the policies
|
|
|
|
selector := labels.NewSelector()
|
|
|
|
cachedPolicies, err := c.policyLister.List(selector)
|
2019-02-21 20:31:18 +02:00
|
|
|
|
2019-03-04 20:40:02 +02:00
|
|
|
if err != nil {
|
|
|
|
c.logger.Printf("Error: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-21 18:13:21 +02:00
|
|
|
|
2019-03-04 20:40:02 +02:00
|
|
|
var policies []types.Policy
|
|
|
|
for _, elem := range cachedPolicies {
|
|
|
|
policies = append(policies, *elem.DeepCopy())
|
|
|
|
}
|
2019-02-21 18:13:21 +02:00
|
|
|
|
2019-03-04 20:40:02 +02:00
|
|
|
return policies
|
2019-02-11 19:49:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 17:57:43 +02:00
|
|
|
// Writes error message to the policy logs in status section
|
2019-03-07 17:42:37 +02:00
|
|
|
func (c *PolicyController) LogPolicyError(name, text string) {
|
|
|
|
c.addPolicyLog(name, "[ERROR] "+text)
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:57:43 +02:00
|
|
|
// Writes info message to the policy logs in status section
|
2019-03-07 17:42:37 +02:00
|
|
|
func (c *PolicyController) LogPolicyInfo(name, text string) {
|
|
|
|
c.addPolicyLog(name, "[ INFO] "+text)
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:57:43 +02:00
|
|
|
// This is the maximum number of records that can be written to the log object of the policy.
|
|
|
|
// If this number is exceeded, the older entries will be deleted.
|
|
|
|
const policyLogMaxRecords int = 50
|
2019-03-07 17:42:37 +02:00
|
|
|
|
|
|
|
// Appends given log text to the status/logs array.
|
|
|
|
func (c *PolicyController) addPolicyLog(name, text string) {
|
|
|
|
getOptions := metav1.GetOptions{
|
|
|
|
ResourceVersion: "1",
|
|
|
|
IncludeUninitialized: true,
|
|
|
|
}
|
|
|
|
policy, err := c.policiesInterface.Get(name, getOptions)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Printf("Unable to get policy %s: %s", name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new log record
|
|
|
|
text = time.Now().Format("2006 Jan 02 15:04:05.999 ") + text
|
|
|
|
policy.Status.Logs = append(policy.Status.Logs, text)
|
|
|
|
// Pop front extra log records
|
|
|
|
logsCount := len(policy.Status.Logs)
|
|
|
|
if logsCount > policyLogMaxRecords {
|
|
|
|
policy.Status.Logs = policy.Status.Logs[logsCount-policyLogMaxRecords:]
|
|
|
|
}
|
|
|
|
// Save logs to policy object
|
|
|
|
_, err = c.policiesInterface.UpdateStatus(policy)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Printf("Unable to update logs for policy %s: %s", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:31:18 +02:00
|
|
|
func (c *PolicyController) createPolicyHandler(resource interface{}) {
|
2019-03-04 20:40:02 +02:00
|
|
|
key := c.getResourceKey(resource)
|
2019-03-07 17:42:37 +02:00
|
|
|
c.logger.Printf("Policy created: %s", key)
|
2019-02-13 20:44:43 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:31:18 +02:00
|
|
|
func (c *PolicyController) updatePolicyHandler(oldResource, newResource interface{}) {
|
2019-03-04 20:40:02 +02:00
|
|
|
oldKey := c.getResourceKey(oldResource)
|
|
|
|
newKey := c.getResourceKey(newResource)
|
2019-02-13 20:44:43 +02:00
|
|
|
|
2019-03-07 17:42:37 +02:00
|
|
|
c.logger.Printf("Policy %s updated to %s", oldKey, newKey)
|
2019-02-11 19:49:27 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:31:18 +02:00
|
|
|
func (c *PolicyController) deletePolicyHandler(resource interface{}) {
|
2019-03-04 20:40:02 +02:00
|
|
|
key := c.getResourceKey(resource)
|
2019-03-07 17:42:37 +02:00
|
|
|
c.logger.Printf("Policy deleted: %s", key)
|
2019-02-11 19:49:27 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:31:18 +02:00
|
|
|
func (c *PolicyController) getResourceKey(resource interface{}) string {
|
2019-03-04 20:40:02 +02:00
|
|
|
if key, err := cache.MetaNamespaceKeyFunc(resource); err != nil {
|
2019-03-07 17:42:37 +02:00
|
|
|
c.logger.Fatalf("Error retrieving policy key: %v", err)
|
2019-03-04 20:40:02 +02:00
|
|
|
} else {
|
|
|
|
return key
|
|
|
|
}
|
2019-02-26 20:05:07 +02:00
|
|
|
|
2019-03-04 20:40:02 +02:00
|
|
|
return ""
|
2019-02-21 18:13:21 +02:00
|
|
|
}
|