├── .gitignore ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── cmd └── main.go ├── docker-compose.yml ├── glide.lock ├── glide.yaml ├── operator.yaml └── pkg └── controller └── controller.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | bin/ 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 15 | .glide/ 16 | vendor/ 17 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.9 2 | 3 | WORKDIR /go/src/github.com/treacher/namespace-rolebinding-operator 4 | 5 | RUN useradd -u 10001 kube-operator 6 | 7 | RUN go get github.com/Masterminds/glide 8 | 9 | COPY glide.yaml . 10 | COPY glide.lock . 11 | 12 | RUN glide install 13 | 14 | COPY . . 15 | 16 | RUN GOOS=linux GOARCH=amd64 go build -v -i -o bin/linux/namespace-rolebinding-operator ./cmd 17 | 18 | FROM scratch 19 | MAINTAINER Michael Treacher 20 | 21 | COPY --from=0 /etc/passwd /etc/passwd 22 | 23 | USER kube-operator 24 | 25 | COPY --from=0 /go/src/github.com/treacher/namespace-rolebinding-operator/bin/linux/namespace-rolebinding-operator . 26 | 27 | ENTRYPOINT ["./namespace-rolebinding-operator"] 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | OPERATOR_NAME := namespace-rolebinding-operator 2 | VERSION := $(shell date +%Y%m%d%H%M) 3 | IMAGE := treacher/$(OPERATOR_NAME) 4 | 5 | .PHONY: install_deps build build-image 6 | 7 | install_deps: 8 | glide install 9 | 10 | build: 11 | rm -rf bin/%/$(OPERATOR_NAME) 12 | go build -v -i -o bin/$(OPERATOR_NAME) ./cmd 13 | 14 | bin/%/$(OPERATOR_NAME): 15 | rm -rf bin/%/$(OPERATOR_NAME) 16 | GOOS=$* GOARCH=amd64 go build -v -i -o bin/$*/$(OPERATOR_NAME) ./cmd 17 | 18 | build-image: bin/linux/$(OPERATOR_NAME) 19 | docker build . -t $(IMAGE):$(VERSION) 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Namespace Rolebinding Operator 2 | 3 | This repository contains an example of a Kubernetes operator that 4 | listens for changes on namespaces and creates a rolebinding with 5 | cluster edit access within that namespace. This example would be 6 | useful for when using OIDC in a Kubernetes cluster. 7 | 8 | For example: You might have a group in your AD with the name: 9 | `ad-kubernetes-kube-system` when the `kube-system` namespace is created, 10 | this operator would create the required RoleBinding so that when a user 11 | with the group `ad-kubernetes-kube-system` logs in via OIDC they'll have 12 | access to edit things in the `kube-system` namespace 13 | 14 | ## Usage 15 | `--run-outside-cluster # Uses ~/.kube/config rather than in cluster configuration` 16 | 17 | ## Development 18 | 19 | ### Build from source 20 | 1. `make install_deps` 21 | 2. `make build` 22 | 3. `./bin/namespace-rolebinding-operator --run-outside-cluster 1` 23 | -------------------------------------------------------------------------------- /cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "log" 6 | "os" 7 | "os/signal" 8 | "path/filepath" 9 | "sync" 10 | "syscall" 11 | 12 | "github.com/treacher/namespace-rolebinding-operator/pkg/controller" 13 | "k8s.io/client-go/kubernetes" 14 | "k8s.io/client-go/tools/clientcmd" 15 | ) 16 | 17 | func main() { 18 | // Set logging output to standard console out 19 | log.SetOutput(os.Stdout) 20 | 21 | sigs := make(chan os.Signal, 1) // Create channel to receive OS signals 22 | stop := make(chan struct{}) // Create channel to receive stop signal 23 | 24 | signal.Notify(sigs, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) // Register the sigs channel to receieve SIGTERM 25 | 26 | wg := &sync.WaitGroup{} // Goroutines can add themselves to this to be waited on so that they finish 27 | 28 | runOutsideCluster := flag.Bool("run-outside-cluster", false, "Set this flag when running outside of the cluster.") 29 | flag.Parse() 30 | // Create clientset for interacting with the kubernetes cluster 31 | clientset, err := newClientSet(*runOutsideCluster) 32 | 33 | if err != nil { 34 | panic(err.Error()) 35 | } 36 | 37 | controller.NewNamespaceController(clientset).Run(stop, wg) 38 | 39 | <-sigs // Wait for signals (this hangs until a signal arrives) 40 | log.Printf("Shutting down...") 41 | 42 | close(stop) // Tell goroutines to stop themselves 43 | wg.Wait() // Wait for all to be stopped 44 | } 45 | 46 | func newClientSet(runOutsideCluster bool) (*kubernetes.Clientset, error) { 47 | kubeConfigLocation := "" 48 | 49 | if runOutsideCluster == true { 50 | homeDir := os.Getenv("HOME") 51 | kubeConfigLocation = filepath.Join(homeDir, ".kube", "config") 52 | } 53 | 54 | // use the current context in kubeconfig 55 | config, err := clientcmd.BuildConfigFromFlags("", kubeConfigLocation) 56 | 57 | if err != nil { 58 | return nil, err 59 | } 60 | 61 | return kubernetes.NewForConfig(config) 62 | } 63 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | operator: 4 | build: . -------------------------------------------------------------------------------- /glide.lock: -------------------------------------------------------------------------------- 1 | hash: 035b7b418a876f7c9d409a5eab98db2984f63ef42abf6820a17dc385d9a8f669 2 | updated: 2017-08-29T18:11:34.105529336+10:00 3 | imports: 4 | - name: github.com/davecgh/go-spew 5 | version: 782f4967f2dc4564575ca782fe2d04090b5faca8 6 | subpackages: 7 | - spew 8 | - name: github.com/emicklei/go-restful 9 | version: ff4f55a206334ef123e4f79bbf348980da81ca46 10 | subpackages: 11 | - log 12 | - name: github.com/emicklei/go-restful-swagger12 13 | version: dcef7f55730566d41eae5db10e7d6981829720f6 14 | - name: github.com/ghodss/yaml 15 | version: 73d445a93680fa1a78ae23a5839bad48f32ba1ee 16 | - name: github.com/go-openapi/jsonpointer 17 | version: 46af16f9f7b149af66e5d1bd010e3574dc06de98 18 | - name: github.com/go-openapi/jsonreference 19 | version: 13c6e3589ad90f49bd3e3bbe2c2cb3d7a4142272 20 | - name: github.com/go-openapi/spec 21 | version: 6aced65f8501fe1217321abf0749d354824ba2ff 22 | - name: github.com/go-openapi/swag 23 | version: 1d0bd113de87027671077d3c71eb3ac5d7dbba72 24 | - name: github.com/gogo/protobuf 25 | version: c0656edd0d9eab7c66d1eb0c568f9039345796f7 26 | subpackages: 27 | - proto 28 | - sortkeys 29 | - name: github.com/golang/glog 30 | version: 44145f04b68cf362d9c4df2182967c2275eaefed 31 | - name: github.com/golang/protobuf 32 | version: 4bd1920723d7b7c925de087aa32e2187708897f7 33 | subpackages: 34 | - proto 35 | - ptypes 36 | - ptypes/any 37 | - ptypes/duration 38 | - ptypes/timestamp 39 | - name: github.com/google/gofuzz 40 | version: 44d81051d367757e1c7c6a5a86423ece9afcf63c 41 | - name: github.com/googleapis/gnostic 42 | version: 68f4ded48ba9414dab2ae69b3f0d69971da73aa5 43 | subpackages: 44 | - OpenAPIv2 45 | - compiler 46 | - extensions 47 | - name: github.com/hashicorp/golang-lru 48 | version: a0d98a5f288019575c6d1f4bb1573fef2d1fcdc4 49 | subpackages: 50 | - simplelru 51 | - name: github.com/howeyc/gopass 52 | version: bf9dde6d0d2c004a008c27aaee91170c786f6db8 53 | - name: github.com/imdario/mergo 54 | version: 6633656539c1639d9d78127b7d47c622b5d7b6dc 55 | - name: github.com/juju/ratelimit 56 | version: 5b9ff866471762aa2ab2dced63c9fb6f53921342 57 | - name: github.com/mailru/easyjson 58 | version: d5b7844b561a7bc640052f1b935f7b800330d7e0 59 | subpackages: 60 | - buffer 61 | - jlexer 62 | - jwriter 63 | - name: github.com/PuerkitoBio/purell 64 | version: 8a290539e2e8629dbc4e6bad948158f790ec31f4 65 | - name: github.com/PuerkitoBio/urlesc 66 | version: 5bd2802263f21d8788851d5305584c82a5c75d7e 67 | - name: github.com/spf13/pflag 68 | version: 9ff6c6923cfffbcd502984b8e0c80539a94968b7 69 | - name: github.com/ugorji/go 70 | version: ded73eae5db7e7a0ef6f55aace87a2873c5d2b74 71 | subpackages: 72 | - codec 73 | - name: golang.org/x/crypto 74 | version: d172538b2cfce0c13cee31e647d0367aa8cd2486 75 | subpackages: 76 | - ssh/terminal 77 | - name: golang.org/x/net 78 | version: f2499483f923065a842d38eb4c7f1927e6fc6e6d 79 | subpackages: 80 | - context 81 | - context/ctxhttp 82 | - http2 83 | - http2/hpack 84 | - idna 85 | - lex/httplex 86 | - name: golang.org/x/sys 87 | version: 8f0908ab3b2457e2e15403d3697c9ef5cb4b57a9 88 | subpackages: 89 | - unix 90 | - name: golang.org/x/text 91 | version: 2910a502d2bf9e43193af9d68ca516529614eed3 92 | subpackages: 93 | - cases 94 | - internal/tag 95 | - language 96 | - runes 97 | - secure/bidirule 98 | - secure/precis 99 | - transform 100 | - unicode/bidi 101 | - unicode/norm 102 | - width 103 | - name: gopkg.in/inf.v0 104 | version: 3887ee99ecf07df5b447e9b00d9c0b2adaa9f3e4 105 | - name: gopkg.in/yaml.v2 106 | version: 53feefa2559fb8dfa8d81baad31be332c97d6c77 107 | - name: k8s.io/api 108 | version: e012d1a19ed30848135c78dca93b6d6a0f82d9e0 109 | subpackages: 110 | - admissionregistration/v1alpha1 111 | - apps/v1beta1 112 | - apps/v1beta2 113 | - authentication/v1 114 | - authentication/v1beta1 115 | - authorization/v1 116 | - authorization/v1beta1 117 | - autoscaling/v1 118 | - autoscaling/v2alpha1 119 | - batch/v1 120 | - batch/v2alpha1 121 | - certificates/v1beta1 122 | - core/v1 123 | - extensions/v1beta1 124 | - imagepolicy/v1alpha1 125 | - networking/v1 126 | - policy/v1beta1 127 | - rbac/v1alpha1 128 | - rbac/v1beta1 129 | - scheduling/v1alpha1 130 | - settings/v1alpha1 131 | - storage/v1 132 | - storage/v1beta1 133 | - name: k8s.io/apimachinery 134 | version: dc1f89aff9a7509782bde3b68824c8043a3e58cc 135 | subpackages: 136 | - pkg/api/equality 137 | - pkg/api/errors 138 | - pkg/api/meta 139 | - pkg/api/resource 140 | - pkg/apimachinery 141 | - pkg/apimachinery/registered 142 | - pkg/apis/meta/v1 143 | - pkg/apis/meta/v1/unstructured 144 | - pkg/apis/meta/v1alpha1 145 | - pkg/conversion 146 | - pkg/conversion/queryparams 147 | - pkg/conversion/unstructured 148 | - pkg/fields 149 | - pkg/labels 150 | - pkg/openapi 151 | - pkg/runtime 152 | - pkg/runtime/schema 153 | - pkg/runtime/serializer 154 | - pkg/runtime/serializer/json 155 | - pkg/runtime/serializer/protobuf 156 | - pkg/runtime/serializer/recognizer 157 | - pkg/runtime/serializer/streaming 158 | - pkg/runtime/serializer/versioning 159 | - pkg/selection 160 | - pkg/types 161 | - pkg/util/cache 162 | - pkg/util/clock 163 | - pkg/util/diff 164 | - pkg/util/errors 165 | - pkg/util/framer 166 | - pkg/util/httpstream 167 | - pkg/util/httpstream/spdy 168 | - pkg/util/intstr 169 | - pkg/util/json 170 | - pkg/util/mergepatch 171 | - pkg/util/net 172 | - pkg/util/remotecommand 173 | - pkg/util/runtime 174 | - pkg/util/sets 175 | - pkg/util/strategicpatch 176 | - pkg/util/validation 177 | - pkg/util/validation/field 178 | - pkg/util/wait 179 | - pkg/util/yaml 180 | - pkg/version 181 | - pkg/watch 182 | - third_party/forked/golang/json 183 | - third_party/forked/golang/netutil 184 | - third_party/forked/golang/reflect 185 | - name: k8s.io/client-go 186 | version: 9675d048204baf818bb24f7c74d0e64f6254902e 187 | subpackages: 188 | - discovery 189 | - kubernetes 190 | - kubernetes/scheme 191 | - kubernetes/typed/admissionregistration/v1alpha1 192 | - kubernetes/typed/apps/v1beta1 193 | - kubernetes/typed/apps/v1beta2 194 | - kubernetes/typed/authentication/v1 195 | - kubernetes/typed/authentication/v1beta1 196 | - kubernetes/typed/authorization/v1 197 | - kubernetes/typed/authorization/v1beta1 198 | - kubernetes/typed/autoscaling/v1 199 | - kubernetes/typed/autoscaling/v2alpha1 200 | - kubernetes/typed/batch/v1 201 | - kubernetes/typed/batch/v2alpha1 202 | - kubernetes/typed/certificates/v1beta1 203 | - kubernetes/typed/core/v1 204 | - kubernetes/typed/extensions/v1beta1 205 | - kubernetes/typed/networking/v1 206 | - kubernetes/typed/policy/v1beta1 207 | - kubernetes/typed/rbac/v1alpha1 208 | - kubernetes/typed/rbac/v1beta1 209 | - kubernetes/typed/scheduling/v1alpha1 210 | - kubernetes/typed/settings/v1alpha1 211 | - kubernetes/typed/storage/v1 212 | - kubernetes/typed/storage/v1beta1 213 | - pkg/version 214 | - rest 215 | - rest/watch 216 | - tools/auth 217 | - tools/cache 218 | - tools/clientcmd 219 | - tools/clientcmd/api 220 | - tools/clientcmd/api/latest 221 | - tools/clientcmd/api/v1 222 | - tools/metrics 223 | - tools/reference 224 | - transport 225 | - util/cert 226 | - util/flowcontrol 227 | - util/homedir 228 | - util/integer 229 | testImports: [] 230 | -------------------------------------------------------------------------------- /glide.yaml: -------------------------------------------------------------------------------- 1 | package: github.com/treacher/namespace-rolebinding-operator 2 | import: 3 | - package: k8s.io/client-go 4 | -------------------------------------------------------------------------------- /operator.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | kind: ServiceAccount 3 | apiVersion: v1 4 | metadata: 5 | name: namespace-rolebinding-operator 6 | namespace: kube-system 7 | 8 | --- 9 | kind: ClusterRole 10 | apiVersion: rbac.authorization.k8s.io/v1beta1 11 | metadata: 12 | name: namespace-rolebinding-operator 13 | rules: 14 | - apiGroups: [""] 15 | resources: 16 | - namespaces 17 | verbs: ["get", "watch", "list"] 18 | 19 | --- 20 | kind: ClusterRoleBinding 21 | apiVersion: rbac.authorization.k8s.io/v1beta1 22 | metadata: 23 | name: namespace-rolebinding-operator 24 | roleRef: 25 | apiGroup: rbac.authorization.k8s.io 26 | kind: ClusterRole 27 | name: admin 28 | subjects: 29 | - kind: ServiceAccount 30 | name: namespace-rolebinding-operator 31 | namespace: kube-system 32 | 33 | --- 34 | kind: Deployment 35 | apiVersion: extensions/v1beta1 36 | metadata: 37 | name: namespace-rolebinding-operator 38 | namespace: kube-system 39 | labels: 40 | k8s-app: namespace-rolebinding-operator 41 | spec: 42 | replicas: 1 43 | template: 44 | metadata: 45 | labels: 46 | k8s-app: namespace-rolebinding-operator 47 | spec: 48 | serviceAccountName: namespace-rolebinding-operator 49 | containers: 50 | - image: galkon/namespace-rolebinding-operator:201708291842 51 | name: namespace-rolebinding-operator -------------------------------------------------------------------------------- /pkg/controller/controller.go: -------------------------------------------------------------------------------- 1 | package controller 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "sync" 7 | "time" 8 | 9 | "k8s.io/api/core/v1" 10 | "k8s.io/api/rbac/v1beta1" 11 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 | "k8s.io/apimachinery/pkg/runtime" 13 | "k8s.io/apimachinery/pkg/watch" 14 | "k8s.io/client-go/kubernetes" 15 | "k8s.io/client-go/tools/cache" 16 | ) 17 | 18 | // NamespaceController watches the kubernetes api for changes to namespaces and 19 | // creates a RoleBinding for that particular namespace. 20 | type NamespaceController struct { 21 | namespaceInformer cache.SharedIndexInformer 22 | kclient *kubernetes.Clientset 23 | } 24 | 25 | // Run starts the process for listening for namespace changes and acting upon those changes. 26 | func (c *NamespaceController) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) { 27 | // When this function completes, mark the go function as done 28 | defer wg.Done() 29 | 30 | // Increment wait group as we're about to execute a go function 31 | wg.Add(1) 32 | 33 | // Execute go function 34 | go c.namespaceInformer.Run(stopCh) 35 | 36 | // Wait till we receive a stop signal 37 | <-stopCh 38 | } 39 | 40 | // NewNamespaceController creates a new NewNamespaceController 41 | func NewNamespaceController(kclient *kubernetes.Clientset) *NamespaceController { 42 | namespaceWatcher := &NamespaceController{} 43 | 44 | // Create informer for watching Namespaces 45 | namespaceInformer := cache.NewSharedIndexInformer( 46 | &cache.ListWatch{ 47 | ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { 48 | return kclient.Core().Namespaces().List(options) 49 | }, 50 | WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { 51 | return kclient.Core().Namespaces().Watch(options) 52 | }, 53 | }, 54 | &v1.Namespace{}, 55 | 3*time.Minute, 56 | cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, 57 | ) 58 | 59 | namespaceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ 60 | AddFunc: namespaceWatcher.createRoleBinding, 61 | }) 62 | 63 | namespaceWatcher.kclient = kclient 64 | namespaceWatcher.namespaceInformer = namespaceInformer 65 | 66 | return namespaceWatcher 67 | } 68 | 69 | func (c *NamespaceController) createRoleBinding(obj interface{}) { 70 | namespaceObj := obj.(*v1.Namespace) 71 | namespaceName := namespaceObj.Name 72 | 73 | roleBinding := &v1beta1.RoleBinding{ 74 | TypeMeta: metav1.TypeMeta{ 75 | Kind: "RoleBinding", 76 | APIVersion: "rbac.authorization.k8s.io/v1beta1", 77 | }, 78 | ObjectMeta: metav1.ObjectMeta{ 79 | Name: fmt.Sprintf("ad-kubernetes-%s", namespaceName), 80 | Namespace: namespaceName, 81 | }, 82 | Subjects: []v1beta1.Subject{ 83 | v1beta1.Subject{ 84 | Kind: "Group", 85 | Name: fmt.Sprintf("ad-kubernetes-%s", namespaceName), 86 | }, 87 | }, 88 | RoleRef: v1beta1.RoleRef{ 89 | APIGroup: "rbac.authorization.k8s.io", 90 | Kind: "ClusterRole", 91 | Name: "edit", 92 | }, 93 | } 94 | 95 | _, err := c.kclient.Rbac().RoleBindings(namespaceName).Create(roleBinding) 96 | 97 | if err != nil { 98 | log.Println(fmt.Sprintf("Failed to create Role Binding: %s", err.Error())) 99 | } else { 100 | log.Println(fmt.Sprintf("Created AD RoleBinding for Namespace: %s", roleBinding.Name)) 101 | } 102 | } 103 | --------------------------------------------------------------------------------