Created
August 29, 2017 10:14
-
-
Save treacher/1c7a45bc709ca7dbe6889f4fd0c7fdd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NamespaceController watches the kubernetes api for changes to namespaces and | |
// creates a RoleBinding for that particular namespace. | |
type NamespaceController struct { | |
namespaceInformer cache.SharedIndexInformer | |
kclient *kubernetes.Clientset | |
} | |
// Run starts the process for listening for namespace changes and acting upon those changes. | |
func (c *NamespaceController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) { | |
// When this function completes, mark the go function as done | |
defer wg.Done() | |
// Increment wait group as we're about to execute a go function | |
wg.Add(1) | |
// Execute go function | |
go c.namespaceInformer.Run(stopCh) | |
// Wait till we receive a stop signal | |
<-stopCh | |
} | |
// NewNamespaceController creates a new NewNamespaceController | |
func NewNamespaceController(kclient *kubernetes.Clientset) *NamespaceController { | |
namespaceWatcher := &NamespaceController{} | |
// Create informer for watching Namespaces | |
namespaceInformer := cache.NewSharedIndexInformer( | |
&cache.ListWatch{ | |
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { | |
return kclient.Core().Namespaces().List(options) | |
}, | |
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { | |
return kclient.Core().Namespaces().Watch(options) | |
}, | |
}, | |
&v1.Namespace{}, | |
3*time.Minute, | |
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, | |
) | |
namespaceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ | |
AddFunc: namespaceWatcher.createRoleBinding, | |
}) | |
namespaceWatcher.kclient = kclient | |
namespaceWatcher.namespaceInformer = namespaceInformer | |
return namespaceWatcher | |
} | |
func (c *NamespaceController) createRoleBinding(obj interface{}) { | |
namespaceObj := obj.(*v1.Namespace) | |
namespaceName := namespaceObj.Name | |
roleBinding := &v1beta1.RoleBinding{ | |
TypeMeta: metav1.TypeMeta{ | |
Kind: "RoleBinding", | |
APIVersion: "rbac.authorization.k8s.io/v1beta1", | |
}, | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: fmt.Sprintf("ad-kubernetes-%s", namespaceName), | |
Namespace: namespaceName, | |
}, | |
Subjects: []v1beta1.Subject{ | |
v1beta1.Subject{ | |
Kind: "Group", | |
Name: fmt.Sprintf("ad-kubernetes-%s", namespaceName), | |
}, | |
}, | |
RoleRef: v1beta1.RoleRef{ | |
APIGroup: "rbac.authorization.k8s.io", | |
Kind: "ClusterRole", | |
Name: "edit", | |
}, | |
} | |
_, err := c.kclient.Rbac().RoleBindings(namespaceName).Create(roleBinding) | |
if err != nil { | |
log.Println(fmt.Sprintf("Failed to create Role Binding: %s", err.Error())) | |
} else { | |
log.Println(fmt.Sprintf("Created AD RoleBinding for Namespace: %s", roleBinding.Name)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment