├── .github └── workflows │ ├── e2e-workflow.yml │ └── maven.yml ├── .gitignore ├── README.md ├── license.txt ├── pom.xml └── src ├── main ├── java │ └── io │ │ └── fabric8 │ │ └── podset │ │ └── operator │ │ ├── PodSetOperatorMain.java │ │ ├── controller │ │ └── PodSetController.java │ │ └── model │ │ └── v1alpha1 │ │ ├── PodSet.java │ │ ├── PodSetSpec.java │ │ └── PodSetStatus.java └── resources │ ├── cr.yaml │ ├── crd.yaml │ └── second-cr.yml └── test └── java └── io └── fabric8 └── podset └── operator └── controller ├── PodSetControllerIT.java └── PodSetControllerTest.java /.github/workflows/e2e-workflow.yml: -------------------------------------------------------------------------------- 1 | name: E2E Tests 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: 7 | - master 8 | pull_request: 9 | paths-ignore: 10 | - 'doc/**' 11 | - 'ide-config/**' 12 | - '**.md' 13 | schedule: 14 | - cron: '0 1 * * *' # Every day at 1 15 | 16 | concurrency: 17 | # Only run once for latest commit per ref and cancel other (previous) runs. 18 | group: ci-e2e-podsetoperatorinjava-${{ github.ref }} 19 | cancel-in-progress: true 20 | 21 | env: 22 | MAVEN_ARGS: -B -C -V -ntp -Dhttp.keepAlive=false -e 23 | 24 | jobs: 25 | minikube_baremetal: 26 | name: Baremetal K8S 27 | runs-on: ubuntu-latest 28 | strategy: 29 | fail-fast: false 30 | matrix: 31 | kubernetes: [v1.25.0, v1.24.0, v1.23.3, v1.22.6, v1.20.15, v1.19.16] 32 | steps: 33 | - name: Checkout 34 | uses: actions/checkout@v3 35 | - name: Setup Minikube-Kubernetes 36 | uses: manusa/actions-setup-minikube@v2.7.2 37 | with: 38 | minikube version: v1.28.0 39 | kubernetes version: ${{ matrix.kubernetes }} 40 | github token: ${{ secrets.GITHUB_TOKEN }} 41 | start args: '--force' 42 | - name: Setup Java 11 43 | uses: actions/setup-java@v2 44 | with: 45 | java-version: '11' 46 | distribution: 'adopt' 47 | - name: Install and Run Integration Tests 48 | run: | 49 | kubectl create -f src/main/resources/crd.yaml 50 | kubectl create clusterrolebinding default-pod --clusterrole cluster-admin --serviceaccount=default:default 51 | mvn clean install -Pe2e-kubernetes 52 | 53 | minikube_docker: 54 | name: Docker K8S 55 | runs-on: ubuntu-latest 56 | strategy: 57 | fail-fast: false 58 | matrix: 59 | kubernetes: [v1.27.3, v1.26.0] 60 | steps: 61 | - name: Checkout 62 | uses: actions/checkout@v3 63 | - name: Setup Minikube-Kubernetes 64 | uses: manusa/actions-setup-minikube@v2.7.2 65 | with: 66 | minikube version: v1.30.1 67 | driver: docker 68 | container runtime: containerd 69 | kubernetes version: ${{ matrix.kubernetes }} 70 | github token: ${{ secrets.GITHUB_TOKEN }} 71 | start args: '--force' 72 | - name: Cache .m2 registry 73 | uses: actions/cache@v3 74 | with: 75 | path: ~/.m2/repository 76 | key: cache-e2e-${{ github.sha }}-${{ github.run_id }} 77 | - name: Setup Java 8 78 | uses: actions/setup-java@v3 79 | with: 80 | java-version: '8' 81 | distribution: 'temurin' 82 | - name: Install and Run Integration Tests 83 | run: | 84 | kubectl create -f src/main/resources/crd.yaml 85 | kubectl create clusterrolebinding default-pod --clusterrole cluster-admin --serviceaccount=default:default 86 | mvn clean install -Pe2e-kubernetes 87 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Set up JDK 1.8 20 | uses: actions/setup-java@v1 21 | with: 22 | java-version: 1.8 23 | - name: Build with Maven 24 | run: mvn -B clean install 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Maven template 3 | target/ 4 | pom.xml.tag 5 | pom.xml.releaseBackup 6 | pom.xml.versionsBackup 7 | pom.xml.next 8 | release.properties 9 | dependency-reduced-pom.xml 10 | buildNumber.properties 11 | .mvn/timing.properties 12 | 13 | # Eclipse 14 | .project 15 | .settings/ 16 | .classpath 17 | 18 | ### Java template 19 | *.class 20 | 21 | # Mobile Tools for Java (J2ME) 22 | .mtj.tmp/ 23 | 24 | # Package Files # 25 | *.jar 26 | *.war 27 | *.ear 28 | 29 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 30 | hs_err_pid* 31 | 32 | 33 | ### JetBrains template 34 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion 35 | 36 | *.iml 37 | 38 | ## Directory-based project format: 39 | .idea/ 40 | # if you remove the above rule, at least ignore the following: 41 | 42 | # User-specific stuff: 43 | # .idea/workspace.xml 44 | # .idea/tasks.xml 45 | # .idea/dictionaries 46 | 47 | # Sensitive or high-churn files: 48 | # .idea/dataSources.ids 49 | # .idea/dataSources.xml 50 | # .idea/sqlDataSources.xml 51 | # .idea/dynamic.xml 52 | # .idea/uiDesigner.xml 53 | 54 | # Gradle: 55 | # .idea/gradle.xml 56 | # .idea/libraries 57 | 58 | # Mongo Explorer plugin: 59 | # .idea/mongoSettings.xml 60 | 61 | ## File-based project format: 62 | *.ipr 63 | *.iws 64 | 65 | ## Plugin-specific files: 66 | 67 | # IntelliJ 68 | /out/ 69 | 70 | # mpeltonen/sbt-idea plugin 71 | .idea_modules/ 72 | 73 | # JIRA plugin 74 | atlassian-ide-plugin.xml 75 | 76 | # Crashlytics plugin (for Android Studio and IntelliJ) 77 | com_crashlytics_export_strings.xml 78 | crashlytics.properties 79 | crashlytics-build.properties 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PodSet Operator in Java Using Fabric8 Kubernetes Client 2 | 3 | ![Build](https://github.com/rohanKanojia/podsetoperatorinjava/workflows/Java%20CI%20with%20Maven/badge.svg?branch=master) 4 | ![License](https://img.shields.io/github/license/rohanKanojia/podsetoperatorinjava) 5 | [![Twitter](https://img.shields.io/twitter/follow/fabric8io?style=social)](https://twitter.com/fabric8io) 6 | 7 | ## Note 8 | > This project is a demo project for blog for writing a simple Kubernetes operation in Java using [Fabric8 Kubernetes Client](https://github.com/fabric8io/kubernetes-client). You can find full blog here: [Writing a simple Kubernetes Operation in Java](https://developers.redhat.com/blog/2019/10/07/write-a-simple-kubernetes-operator-in-java-using-the-fabric8-kubernetes-client/) 9 | 10 | This is a demo operator which implements a simple operator for a custom resource called PodSet which is somewhat equal to ReplicaSet. Here 11 | is what this resource looks like: 12 | ``` 13 | apiVersion: demo.fabric8.io/v1alpha1 14 | kind: PodSet 15 | metadata: 16 | name: example-podset 17 | spec: 18 | replicas: 5 19 | ``` 20 | 21 | Each PodSet object would have 'x' number of replicas, so this operator just tries to maintain x number of replicas checking whether that number 22 | of pods are running in cluster or not. 23 | 24 | ## How to Build 25 | ``` 26 | mvn clean install 27 | ``` 28 | 29 | ## How to Run 30 | ``` 31 | mvn exec:java -Dexec.mainClass=io.fabric8.podset.operator.PodSetOperatorMain 32 | ``` 33 | 34 | Make Sure that PodSet Custom Resource Definition is already applied onto the cluster. If not, just apply it using this command: 35 | ``` 36 | kubectl apply -f src/main/resources/crd.yaml 37 | ``` 38 | 39 | Once everything is set, you can see that operator creating pods in your cluster for PodSet resource: 40 | ![Demo Screenshot](https://i.imgur.com/ECNKBjG.png) 41 | 42 | ## Deploying onto Kubernetes(minikube) using [Eclipse JKube](https://github.com/eclipse/jkube) 43 | - Make Sure that PodSet Custom Resource Definition is already applied onto the cluster. If not, just apply it using this command: 44 | ``` 45 | kubectl apply -f src/main/resources/crd.yaml 46 | ``` 47 | - Make sure that you have given correct privileges to `ServiceAccount` that would be used by the `Pod`, it's `default` in our case. Otherwise you might get 403 from Kubernetes API server. 48 | ``` 49 | kubectl create clusterrolebinding default-pod --clusterrole cluster-admin --serviceaccount=default:default 50 | 51 | # In case of some other namespace: 52 | kubectl create clusterrolebinding default-pod --clusterrole cluster-admin --serviceaccount=:default 53 | ``` 54 | - Build Docker Image 55 | ``` 56 | mvn k8s:build 57 | ``` 58 | ![JKube Build Docker Image](https://i.imgur.com/IXVlZ8e.png) 59 | 60 | - Generate Kubernetes Manifests 61 | ``` 62 | mvn k8s:resource 63 | ``` 64 | ![JKube Generate Kubernetes Manifests](https://i.imgur.com/slDdq3X.png) 65 | - Apply generated Kubernetes Manifests onto Kubernetes 66 | ``` 67 | mvn k8s:apply 68 | ``` 69 | Once generated resources are applied, try creating one of `PodSet` objects in `src/main/resources` 70 | ``` 71 | kubectl apply -f src/main/resources/cr.yaml 72 | ``` 73 | ![JKube Apply Generated Kubernetes Manifests](https://i.imgur.com/dgp8lX5.png) 74 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.fabric8 8 | podset-operator-in-java 9 | 1.0-SNAPSHOT 10 | jar 11 | 12 | 6.13.3 13 | 1.8 14 | 1.8 15 | 3.8.1 16 | 3.3.0 17 | UTF-8 18 | UTF-8 19 | 5.7.2 20 | 3.0.0-M4 21 | 3.0.0-M5 22 | 3.0.0 23 | rohankanojia 24 | quay.io/${image.user}/${project.artifactId}:%t 25 | 1.17.0 26 | 2.0.1 27 | 28 | 29 | 30 | 31 | io.fabric8 32 | kubernetes-client 33 | ${fabric8.version} 34 | 35 | 36 | org.slf4j 37 | slf4j-api 38 | ${slf4j.version} 39 | 40 | 41 | org.slf4j 42 | slf4j-simple 43 | ${slf4j.version} 44 | 45 | 46 | 47 | io.fabric8 48 | kubernetes-server-mock 49 | ${fabric8.version} 50 | test 51 | 52 | 53 | io.fabric8 54 | kubernetes-junit-jupiter 55 | ${fabric8.version} 56 | test 57 | 58 | 59 | org.junit.jupiter 60 | junit-jupiter-api 61 | ${junit-jupiter-engine.version} 62 | test 63 | 64 | 65 | org.junit.jupiter 66 | junit-jupiter-engine 67 | ${junit-jupiter-engine.version} 68 | test 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-assembly-plugin 78 | ${maven-assembly-plugin.version} 79 | 80 | 81 | package 82 | 83 | single 84 | 85 | 86 | 87 | 88 | 89 | io.fabric8.podset.operator.PodSetOperatorMain 90 | 91 | 92 | 93 | 94 | jar-with-dependencies 95 | 96 | 97 | 98 | 99 | 100 | 101 | org.eclipse.jkube 102 | kubernetes-maven-plugin 103 | ${jkube.version} 104 | 105 | 106 | org.codehaus.mojo 107 | exec-maven-plugin 108 | ${exec-maven-plugin.version} 109 | 110 | 111 | org.apache.maven.plugins 112 | maven-surefire-plugin 113 | 114 | ${maven-surefire-plugin.version} 115 | 116 | 117 | org.apache.maven.plugins 118 | maven-compiler-plugin 119 | ${maven-compiler-plugin.version} 120 | 121 | true 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | e2e-kubernetes 130 | 131 | ttl.sh/%a-%t:5m 132 | 133 | 134 | 135 | 136 | org.eclipse.jkube 137 | kubernetes-maven-plugin 138 | ${jkube.version} 139 | 140 | 141 | jkube-it-preparation 142 | pre-integration-test 143 | 144 | resource 145 | build 146 | push 147 | apply 148 | 149 | 150 | 151 | jkube-it-cleanup 152 | post-integration-test 153 | 154 | undeploy 155 | 156 | 157 | 158 | 159 | 160 | org.apache.maven.plugins 161 | maven-failsafe-plugin 162 | ${maven-failsafe-plugin.version} 163 | 164 | 165 | integration-tests 166 | 167 | integration-test 168 | verify 169 | 170 | 171 | 172 | **/PodSetControllerIT.java 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/podset/operator/PodSetOperatorMain.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.podset.operator; 2 | 3 | import io.fabric8.kubernetes.api.model.KubernetesResourceList; 4 | import io.fabric8.kubernetes.api.model.Pod; 5 | import io.fabric8.kubernetes.client.KubernetesClient; 6 | import io.fabric8.kubernetes.client.KubernetesClientBuilder; 7 | import io.fabric8.kubernetes.client.KubernetesClientException; 8 | import io.fabric8.kubernetes.client.dsl.MixedOperation; 9 | import io.fabric8.kubernetes.client.dsl.Resource; 10 | import io.fabric8.kubernetes.client.informers.SharedIndexInformer; 11 | import io.fabric8.kubernetes.client.informers.SharedInformerFactory; 12 | import io.fabric8.podset.operator.controller.PodSetController; 13 | import io.fabric8.podset.operator.model.v1alpha1.PodSet; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | 17 | import java.util.concurrent.Future; 18 | import java.util.concurrent.ExecutionException; 19 | 20 | /** 21 | * Main Class for Operator, you can run this sample using this command: 22 | *

23 | * mvn exec:java -Dexec.mainClass=io.fabric8.podset.operator.PodSetOperatorMain 24 | */ 25 | public class PodSetOperatorMain { 26 | public static final Logger logger = LoggerFactory.getLogger(PodSetOperatorMain.class.getSimpleName()); 27 | 28 | public static void main(String[] args) { 29 | try (KubernetesClient client = new KubernetesClientBuilder().build()) { 30 | String namespace = client.getNamespace(); 31 | if (namespace == null) { 32 | logger.info("No namespace found via config, assuming default."); 33 | namespace = "default"; 34 | } 35 | 36 | logger.info("Using namespace : {}", namespace); 37 | 38 | SharedInformerFactory informerFactory = client.informers(); 39 | 40 | MixedOperation, Resource> podSetClient = client.resources(PodSet.class); 41 | SharedIndexInformer podSharedIndexInformer = informerFactory.sharedIndexInformerFor(Pod.class, 10 * 60 * 1000L); 42 | SharedIndexInformer podSetSharedIndexInformer = informerFactory.sharedIndexInformerFor(PodSet.class, 10 * 60 * 1000L); 43 | PodSetController podSetController = new PodSetController(client, podSetClient, podSharedIndexInformer, podSetSharedIndexInformer, namespace); 44 | Future startedInformersFuture = informerFactory.startAllRegisteredInformers(); 45 | startedInformersFuture.get(); 46 | 47 | podSetController.run(); 48 | } catch (KubernetesClientException | ExecutionException exception) { 49 | logger.error("Kubernetes Client Exception : ", exception); 50 | } catch (InterruptedException interruptedException) { 51 | logger.error("Interrupted: ", interruptedException); 52 | Thread.currentThread().interrupt(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/podset/operator/controller/PodSetController.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.podset.operator.controller; 2 | 3 | import io.fabric8.kubernetes.api.model.KubernetesResourceList; 4 | import io.fabric8.kubernetes.api.model.OwnerReference; 5 | import io.fabric8.kubernetes.api.model.Pod; 6 | import io.fabric8.kubernetes.api.model.PodBuilder; 7 | import io.fabric8.kubernetes.client.KubernetesClient; 8 | import io.fabric8.kubernetes.client.dsl.MixedOperation; 9 | import io.fabric8.kubernetes.client.dsl.Resource; 10 | import io.fabric8.kubernetes.client.informers.ResourceEventHandler; 11 | import io.fabric8.kubernetes.client.informers.SharedIndexInformer; 12 | import io.fabric8.kubernetes.client.informers.cache.Cache; 13 | import io.fabric8.kubernetes.client.informers.cache.Lister; 14 | import io.fabric8.podset.operator.model.v1alpha1.PodSet; 15 | import io.fabric8.podset.operator.model.v1alpha1.PodSetStatus; 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | import java.util.AbstractMap; 20 | import java.util.ArrayList; 21 | import java.util.Collections; 22 | import java.util.List; 23 | import java.util.Objects; 24 | import java.util.concurrent.ArrayBlockingQueue; 25 | import java.util.concurrent.BlockingQueue; 26 | import java.util.concurrent.TimeUnit; 27 | 28 | public class PodSetController { 29 | private final BlockingQueue workqueue; 30 | private final SharedIndexInformer podSetInformer; 31 | private final SharedIndexInformer podInformer; 32 | private final Lister podSetLister; 33 | private final Lister podLister; 34 | private final KubernetesClient kubernetesClient; 35 | private final MixedOperation, Resource> podSetClient; 36 | public static final Logger logger = LoggerFactory.getLogger(PodSetController.class.getSimpleName()); 37 | public static final String APP_LABEL = "app"; 38 | 39 | public PodSetController(KubernetesClient kubernetesClient, MixedOperation, Resource> podSetClient, SharedIndexInformer podInformer, SharedIndexInformer podSetInformer, String namespace) { 40 | this.kubernetesClient = kubernetesClient; 41 | this.podSetClient = podSetClient; 42 | this.podSetLister = new Lister<>(podSetInformer.getIndexer(), namespace); 43 | this.podSetInformer = podSetInformer; 44 | this.podLister = new Lister<>(podInformer.getIndexer(), namespace); 45 | this.podInformer = podInformer; 46 | this.workqueue = new ArrayBlockingQueue<>(1024); 47 | addEventHandlersToSharedIndexInformers(); 48 | } 49 | 50 | public void run() { 51 | logger.info("Starting PodSet controller"); 52 | while (!Thread.currentThread().isInterrupted()) { 53 | if (podInformer.hasSynced() && podSetInformer.hasSynced()) { 54 | break; 55 | } 56 | } 57 | 58 | while (true) { 59 | try { 60 | logger.info("trying to fetch item from workqueue..."); 61 | if (workqueue.isEmpty()) { 62 | logger.info("Work Queue is empty"); 63 | } 64 | String key = workqueue.take(); 65 | Objects.requireNonNull(key, "key can't be null"); 66 | logger.info("Got {}", key); 67 | if ((!key.contains("/"))) { 68 | logger.warn("invalid resource key: {}", key); 69 | } 70 | 71 | // Get the PodSet resource's name from key which is in format namespace/name 72 | String name = key.split("/")[1]; 73 | PodSet podSet = podSetLister.get(key.split("/")[1]); 74 | if (podSet == null) { 75 | logger.error("PodSet {} in workqueue no longer exists", name); 76 | return; 77 | } 78 | reconcile(podSet); 79 | 80 | } catch (InterruptedException interruptedException) { 81 | Thread.currentThread().interrupt(); 82 | logger.error("controller interrupted.."); 83 | } 84 | } 85 | } 86 | 87 | /** 88 | * Tries to achieve the desired state for podset. 89 | * 90 | * @param podSet specified podset 91 | */ 92 | protected void reconcile(PodSet podSet) { 93 | List pods = podCountByLabel(APP_LABEL, podSet.getMetadata().getName()); 94 | logger.info("reconcile() : Found {} number of Pods owned by PodSet {}", pods.size(), podSet.getMetadata().getName()); 95 | if (pods.isEmpty()) { 96 | createPods(podSet.getSpec().getReplicas(), podSet); 97 | return; 98 | } 99 | int existingPods = pods.size(); 100 | 101 | // Compare it with desired state i.e spec.replicas 102 | // if less then spin up pods 103 | if (existingPods < podSet.getSpec().getReplicas()) { 104 | createPods(podSet.getSpec().getReplicas() - existingPods, podSet); 105 | } 106 | 107 | // If more pods then delete the pods 108 | int diff = existingPods - podSet.getSpec().getReplicas(); 109 | for (; diff > 0; diff--) { 110 | String podName = pods.remove(0); 111 | kubernetesClient.pods().inNamespace(podSet.getMetadata().getNamespace()).withName(podName).delete(); 112 | } 113 | 114 | // Update PodSet status 115 | updateAvailableReplicasInPodSetStatus(podSet, podSet.getSpec().getReplicas()); 116 | } 117 | 118 | private void addEventHandlersToSharedIndexInformers() { 119 | podSetInformer.addEventHandler(new ResourceEventHandler() { 120 | @Override 121 | public void onAdd(PodSet podSet) { 122 | logger.info("PodSet {} ADDED", podSet.getMetadata().getName()); 123 | enqueuePodSet(podSet); 124 | } 125 | 126 | @Override 127 | public void onUpdate(PodSet podSet, PodSet newPodSet) { 128 | logger.info("PodSet {} MODIFIED", podSet.getMetadata().getName()); 129 | enqueuePodSet(newPodSet); 130 | } 131 | 132 | @Override 133 | public void onDelete(PodSet podSet, boolean b) { 134 | // Do nothing 135 | } 136 | }); 137 | 138 | podInformer.addEventHandler(new ResourceEventHandler() { 139 | @Override 140 | public void onAdd(Pod pod) { 141 | handlePodObject(pod); 142 | } 143 | 144 | @Override 145 | public void onUpdate(Pod oldPod, Pod newPod) { 146 | if (oldPod.getMetadata().getResourceVersion().equals(newPod.getMetadata().getResourceVersion())) { 147 | return; 148 | } 149 | handlePodObject(newPod); 150 | } 151 | 152 | @Override 153 | public void onDelete(Pod pod, boolean b) { 154 | // Do nothing 155 | } 156 | }); 157 | } 158 | 159 | private void createPods(int numberOfPods, PodSet podSet) { 160 | for (int index = 0; index < numberOfPods; index++) { 161 | Pod pod = createNewPod(podSet); 162 | pod = kubernetesClient.pods().inNamespace(podSet.getMetadata().getNamespace()).resource(pod).create(); 163 | kubernetesClient.pods().inNamespace(podSet.getMetadata().getNamespace()) 164 | .withName(pod.getMetadata().getName()) 165 | .waitUntilCondition(Objects::nonNull, 3, TimeUnit.SECONDS); 166 | } 167 | logger.info("Created {} pods for {} PodSet", numberOfPods, podSet.getMetadata().getName()); 168 | } 169 | 170 | private List podCountByLabel(String label, String podSetName) { 171 | List podNames = new ArrayList<>(); 172 | List pods = podLister.list(); 173 | 174 | for (Pod pod : pods) { 175 | if (pod.getMetadata().getLabels().entrySet().contains(new AbstractMap.SimpleEntry<>(label, podSetName))) { 176 | if (pod.getStatus().getPhase().equals("Running") || pod.getStatus().getPhase().equals("Pending")) { 177 | podNames.add(pod.getMetadata().getName()); 178 | } 179 | } 180 | } 181 | 182 | logger.info("count: {}", podNames.size()); 183 | return podNames; 184 | } 185 | 186 | private void enqueuePodSet(PodSet podSet) { 187 | logger.info("enqueuePodSet({})", podSet.getMetadata().getName()); 188 | String key = Cache.metaNamespaceKeyFunc(podSet); 189 | logger.info("Going to enqueue key {}", key); 190 | if (key != null && !key.isEmpty()) { 191 | logger.info("Adding item to workqueue"); 192 | workqueue.add(key); 193 | } 194 | } 195 | 196 | private void handlePodObject(Pod pod) { 197 | logger.info("handlePodObject({})", pod.getMetadata().getName()); 198 | OwnerReference ownerReference = getControllerOf(pod); 199 | if (ownerReference == null || !ownerReference.getKind().equalsIgnoreCase("PodSet")) { 200 | return; 201 | } 202 | PodSet podSet = podSetLister.get(ownerReference.getName()); 203 | logger.info("PodSetLister returned {} for PodSet", podSet); 204 | if (podSet != null) { 205 | enqueuePodSet(podSet); 206 | } 207 | } 208 | 209 | private void updateAvailableReplicasInPodSetStatus(PodSet podSet, int replicas) { 210 | PodSetStatus podSetStatus = new PodSetStatus(); 211 | podSetStatus.setAvailableReplicas(replicas); 212 | podSet.setStatus(podSetStatus); 213 | podSetClient.inNamespace(podSet.getMetadata().getNamespace()).resource(podSet).replaceStatus(); 214 | } 215 | 216 | private Pod createNewPod(PodSet podSet) { 217 | return new PodBuilder() 218 | .withNewMetadata() 219 | .withGenerateName(podSet.getMetadata().getName() + "-pod") 220 | .withNamespace(podSet.getMetadata().getNamespace()) 221 | .withLabels(Collections.singletonMap(APP_LABEL, podSet.getMetadata().getName())) 222 | .addNewOwnerReference().withController(true).withKind("PodSet").withApiVersion("demo.k8s.io/v1alpha1").withName(podSet.getMetadata().getName()).withUid(podSet.getMetadata().getUid()).endOwnerReference() 223 | .endMetadata() 224 | .withNewSpec() 225 | .addNewContainer().withName("busybox").withImage("busybox").withCommand("sleep", "3600").endContainer() 226 | .endSpec() 227 | .build(); 228 | } 229 | 230 | private OwnerReference getControllerOf(Pod pod) { 231 | List ownerReferences = pod.getMetadata().getOwnerReferences(); 232 | for (OwnerReference ownerReference : ownerReferences) { 233 | if (ownerReference.getController().equals(Boolean.TRUE)) { 234 | return ownerReference; 235 | } 236 | } 237 | return null; 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/podset/operator/model/v1alpha1/PodSet.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.podset.operator.model.v1alpha1; 2 | 3 | import io.fabric8.kubernetes.api.model.Namespaced; 4 | import io.fabric8.kubernetes.client.CustomResource; 5 | import io.fabric8.kubernetes.model.annotation.Group; 6 | import io.fabric8.kubernetes.model.annotation.Version; 7 | 8 | @Version("v1alpha1") 9 | @Group("demo.fabric8.io") 10 | public class PodSet extends CustomResource implements Namespaced { } 11 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/podset/operator/model/v1alpha1/PodSetSpec.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.podset.operator.model.v1alpha1; 2 | 3 | public class PodSetSpec { 4 | public int getReplicas() { 5 | return replicas; 6 | } 7 | 8 | @Override 9 | public String toString() { 10 | return "PodSetSpec{replicas=" + replicas + "}"; 11 | } 12 | 13 | public void setReplicas(int replicas) { 14 | this.replicas = replicas; 15 | } 16 | 17 | private int replicas; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/podset/operator/model/v1alpha1/PodSetStatus.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.podset.operator.model.v1alpha1; 2 | 3 | public class PodSetStatus { 4 | public int getAvailableReplicas() { 5 | return availableReplicas; 6 | } 7 | 8 | public void setAvailableReplicas(int availableReplicas) { 9 | this.availableReplicas = availableReplicas; 10 | } 11 | 12 | @Override 13 | public String toString() { 14 | return "PodSetStatus{ availableReplicas=" + availableReplicas + "}"; 15 | } 16 | 17 | private int availableReplicas; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/resources/cr.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: demo.fabric8.io/v1alpha1 2 | kind: PodSet 3 | metadata: 4 | name: example-podset 5 | spec: 6 | replicas: 5 7 | -------------------------------------------------------------------------------- /src/main/resources/crd.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apiextensions.k8s.io/v1 2 | kind: CustomResourceDefinition 3 | metadata: 4 | name: podsets.demo.fabric8.io 5 | spec: 6 | group: demo.fabric8.io 7 | versions: 8 | - name: v1alpha1 9 | served: true 10 | storage: true 11 | schema: 12 | openAPIV3Schema: 13 | type: object 14 | properties: 15 | spec: 16 | type: object 17 | properties: 18 | replicas: 19 | type: integer 20 | status: 21 | type: object 22 | properties: 23 | availableReplicas: 24 | type: integer 25 | subresources: 26 | status: {} 27 | names: 28 | kind: PodSet 29 | plural: podsets 30 | singular: podset 31 | shortNames: 32 | - ps 33 | scope: Namespaced 34 | -------------------------------------------------------------------------------- /src/main/resources/second-cr.yml: -------------------------------------------------------------------------------- 1 | apiVersion: demo.fabric8.io/v1alpha1 2 | kind: PodSet 3 | metadata: 4 | name: dummy-podset 5 | spec: 6 | replicas: 2 7 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/podset/operator/controller/PodSetControllerIT.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.podset.operator.controller; 2 | 3 | import io.fabric8.kubernetes.api.model.DeletionPropagation; 4 | import io.fabric8.kubernetes.api.model.KubernetesResourceList; 5 | import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; 6 | import io.fabric8.kubernetes.api.model.Pod; 7 | import io.fabric8.kubernetes.api.model.PodList; 8 | import io.fabric8.kubernetes.client.KubernetesClient; 9 | import io.fabric8.kubernetes.client.KubernetesClientBuilder; 10 | import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable; 11 | import io.fabric8.kubernetes.client.dsl.MixedOperation; 12 | import io.fabric8.kubernetes.client.dsl.PodResource; 13 | import io.fabric8.kubernetes.client.dsl.Resource; 14 | import io.fabric8.kubernetes.client.readiness.Readiness; 15 | import io.fabric8.kubernetes.client.utils.PodStatusUtil; 16 | import io.fabric8.podset.operator.model.v1alpha1.PodSet; 17 | import io.fabric8.podset.operator.model.v1alpha1.PodSetSpec; 18 | import io.fabric8.junit.jupiter.api.RequireK8sVersionAtLeast; 19 | import io.fabric8.junit.jupiter.api.RequireK8sSupport; 20 | import org.junit.jupiter.api.AfterEach; 21 | import org.junit.jupiter.api.BeforeEach; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import java.util.Collections; 25 | import java.util.concurrent.CompletableFuture; 26 | import java.util.concurrent.ExecutionException; 27 | import java.util.concurrent.TimeUnit; 28 | import java.util.concurrent.TimeoutException; 29 | import java.util.function.Function; 30 | import java.util.function.Predicate; 31 | import java.util.function.Supplier; 32 | 33 | import static org.junit.jupiter.api.Assertions.assertEquals; 34 | import static org.junit.jupiter.api.Assertions.assertNotNull; 35 | import static org.junit.jupiter.api.Assertions.assertTrue; 36 | 37 | /* 38 | * This Test requires Operator to be already running in the cluster. 39 | */ 40 | @RequireK8sSupport(PodSet.class) 41 | @RequireK8sVersionAtLeast(majorVersion = 1, minorVersion = 16) 42 | class PodSetControllerIT { 43 | private KubernetesClient kubernetesClient; 44 | private MixedOperation, Resource> podSetClient; 45 | private static final String TEST_NAMESPACE = "default"; 46 | 47 | @BeforeEach 48 | void initPodSetClient() { 49 | kubernetesClient = new KubernetesClientBuilder().build(); 50 | podSetClient = kubernetesClient.resources(PodSet.class); 51 | } 52 | 53 | @Test 54 | void testPodsWithDesiredReplicasCreatedOnPodSetCreate() throws InterruptedException, ExecutionException, TimeoutException { 55 | // Given 56 | Pod operatorPod = getOperatorPod(); 57 | waitUntilOperatorIsRunning(operatorPod); 58 | PodSet podSet = createNewPodSet("test-podset1", 2); 59 | 60 | // When 61 | podSet = podSetClient.inNamespace(TEST_NAMESPACE).resource(podSet).create(); 62 | waitUntilOperatorLogsPodCreation(operatorPod); 63 | 64 | // Then 65 | assertTrue(getPodSetDependentPodsCount(podSet) >= 2); 66 | PodSet podSetFromServer = podSetClient.inNamespace(TEST_NAMESPACE).withName(podSet.getMetadata().getName()).get(); 67 | assertNotNull(podSetFromServer.getStatus()); 68 | assertEquals(2, podSetFromServer.getStatus().getAvailableReplicas()); 69 | podSetClient.inNamespace(TEST_NAMESPACE).withName("test-podset1").withPropagationPolicy(DeletionPropagation.FOREGROUND).delete(); 70 | kubernetesClient.pods().inNamespace(TEST_NAMESPACE).withLabel("app", "test-podset1").delete(); 71 | } 72 | 73 | @AfterEach 74 | void cleanup() { 75 | podSetClient.inNamespace(TEST_NAMESPACE) 76 | .withLabel("app", "PodSetControllerIT") 77 | .withPropagationPolicy(DeletionPropagation.BACKGROUND) 78 | .delete(); 79 | } 80 | 81 | private int getPodSetDependentPodsCount(PodSet podSet) { 82 | FilterWatchListDeletable podWithLabels = kubernetesClient.pods().inNamespace(TEST_NAMESPACE) 83 | .withLabel("app", podSet.getMetadata().getName()); 84 | 85 | podWithLabels.waitUntilCondition(Readiness::isPodReady, 2, TimeUnit.MINUTES); 86 | PodList podList = podWithLabels.list(); 87 | 88 | int nDependents = 0; 89 | for (Pod pod : podList.getItems()) { 90 | boolean isOwnedByPodSet = pod.getMetadata().getOwnerReferences().stream() 91 | .anyMatch(o -> o.getController() && o.getUid().equals(podSet.getMetadata().getUid())); 92 | if (isOwnedByPodSet && Readiness.isPodReady(pod)) { 93 | System.out.println(pod.getMetadata().getName() + PodStatusUtil.isRunning(pod)); 94 | nDependents++; 95 | } 96 | } 97 | return nDependents; 98 | } 99 | 100 | private Pod getOperatorPod() { 101 | PodList operatorPodList = kubernetesClient.pods() 102 | .inNamespace(TEST_NAMESPACE) 103 | .withLabel("app", "podset-operator-in-java") 104 | .list(); 105 | assertEquals(1, operatorPodList.getItems().size()); 106 | return operatorPodList.getItems().get(0); 107 | } 108 | 109 | private void waitUntilOperatorIsRunning(Pod operatorPod) { 110 | kubernetesClient.pods() 111 | .inNamespace(TEST_NAMESPACE) 112 | .withName(operatorPod.getMetadata().getName()) 113 | .waitUntilReady(2, TimeUnit.MINUTES); 114 | } 115 | 116 | private void waitUntilOperatorLogsPodCreation(Pod operatorPod) throws ExecutionException, InterruptedException, TimeoutException { 117 | PodResource podResource = kubernetesClient.pods().inNamespace(TEST_NAMESPACE) 118 | .resource(operatorPod); 119 | await(podResource::getLog) 120 | .apply(l -> { 121 | System.out.println(l); 122 | return l.contains("Created 2 pods for test-podset1 PodSet"); 123 | }) 124 | .get(3, TimeUnit.MINUTES); 125 | } 126 | 127 | private PodSet createNewPodSet(String name, int replicas) { 128 | PodSetSpec podSetSpec = new PodSetSpec(); 129 | podSetSpec.setReplicas(replicas); 130 | 131 | PodSet podSet = new PodSet(); 132 | podSet.setMetadata(new ObjectMetaBuilder().withName(name) 133 | .withLabels(Collections.singletonMap("app", "PodSetControllerIT")) 134 | .build()); 135 | podSet.setSpec(podSetSpec); 136 | return podSet; 137 | } 138 | 139 | private Function, CompletableFuture> await(Supplier supplier) { 140 | return condition -> CompletableFuture.supplyAsync(() -> { 141 | String result = null; 142 | while (!Thread.currentThread().isInterrupted() && 143 | !condition.test(result = supplier.get())) { 144 | try { 145 | Thread.sleep(100L); 146 | } catch (InterruptedException interruptedException) { 147 | Thread.currentThread().interrupt(); 148 | } 149 | } 150 | return result; 151 | }); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/podset/operator/controller/PodSetControllerTest.java: -------------------------------------------------------------------------------- 1 | package io.fabric8.podset.operator.controller; 2 | 3 | import io.fabric8.kubernetes.api.model.KubernetesResourceList; 4 | import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; 5 | import io.fabric8.kubernetes.api.model.Pod; 6 | import io.fabric8.kubernetes.api.model.PodBuilder; 7 | import io.fabric8.kubernetes.api.model.WatchEvent; 8 | import io.fabric8.kubernetes.client.KubernetesClient; 9 | import io.fabric8.kubernetes.client.dsl.MixedOperation; 10 | import io.fabric8.kubernetes.client.dsl.Resource; 11 | import io.fabric8.kubernetes.client.informers.SharedIndexInformer; 12 | import io.fabric8.kubernetes.client.informers.SharedInformerFactory; 13 | import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; 14 | import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; 15 | import io.fabric8.kubernetes.client.utils.Utils; 16 | import io.fabric8.podset.operator.model.v1alpha1.PodSet; 17 | import io.fabric8.podset.operator.model.v1alpha1.PodSetSpec; 18 | import okhttp3.mockwebserver.RecordedRequest; 19 | import org.junit.jupiter.api.DisplayName; 20 | import org.junit.jupiter.api.Test; 21 | 22 | import java.net.HttpURLConnection; 23 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; 25 | import static org.junit.jupiter.api.Assertions.assertTrue; 26 | 27 | @EnableKubernetesMockClient 28 | class PodSetControllerTest { 29 | private KubernetesMockServer server; 30 | private KubernetesClient client; 31 | 32 | private static final long RESYNC_PERIOD_MILLIS = 10 * 60 * 1000L; 33 | 34 | @Test 35 | @DisplayName("Should create pods for with respect to a specified PodSet") 36 | void testReconcile() throws InterruptedException { 37 | // Given 38 | String testNamespace = "ns1"; 39 | PodSet testPodSet = getPodSet("example-podset", testNamespace, "0800cff3-9d80-11ea-8973-0e13a02d8ebd"); 40 | setupMockExpectations(testNamespace); 41 | 42 | SharedInformerFactory informerFactory = client.informers(); 43 | MixedOperation, Resource> podSetClient = client.resources(PodSet.class); 44 | SharedIndexInformer podSharedIndexInformer = informerFactory.sharedIndexInformerFor(Pod.class, RESYNC_PERIOD_MILLIS); 45 | SharedIndexInformer podSetSharedIndexInformer = informerFactory.sharedIndexInformerFor(PodSet.class, RESYNC_PERIOD_MILLIS); 46 | PodSetController podSetController = new PodSetController(client, podSetClient, podSharedIndexInformer, podSetSharedIndexInformer, testNamespace); 47 | 48 | // When 49 | podSetController.reconcile(testPodSet); 50 | 51 | // Then 52 | RecordedRequest recordedRequest = server.takeRequest(); 53 | assertEquals("POST", recordedRequest.getMethod()); 54 | assertTrue(recordedRequest.getBody().readUtf8().contains(testPodSet.getMetadata().getName())); 55 | } 56 | 57 | private Pod createPodWithName(String name) { 58 | return new PodBuilder().withNewMetadata().withName(name).endMetadata() 59 | .withNewStatus() 60 | .addNewCondition() 61 | .withType("Ready") 62 | .endCondition() 63 | .endStatus() 64 | .build(); 65 | } 66 | 67 | void setupMockExpectations(String testNamespace) { 68 | setupMockExpectationsForPod(createPodWithName("pod1-clone"), testNamespace); 69 | setupMockExpectationsForPod(createPodWithName("pod2-clone"), testNamespace); 70 | setupMockExpectationsForPod(createPodWithName("pod3-clone"), testNamespace); 71 | } 72 | 73 | void setupMockExpectationsForPod(Pod clonePod, String testNamespace) { 74 | server.expect().post().withPath("/api/v1/namespaces/" + testNamespace + "/pods") 75 | .andReturn(HttpURLConnection.HTTP_CREATED, clonePod) 76 | .always(); 77 | server.expect().get().withPath("/api/v1/namespaces/" + testNamespace + "/pods?fieldSelector=" + Utils.toUrlEncoded("metadata.name=" + clonePod.getMetadata().getName())) 78 | .andReturn(HttpURLConnection.HTTP_OK, clonePod) 79 | .always(); 80 | server.expect().get().withPath("/api/v1/namespaces/" + testNamespace + "/pods?allowWatchBookmarks=true&fieldSelector=" + Utils.toUrlEncoded("metadata.name=" + clonePod.getMetadata().getName()) + "&timeoutSeconds=600&watch=true") 81 | .andUpgradeToWebSocket().open() 82 | .waitFor(100).andEmit(new WatchEvent(clonePod, "ADDED")) 83 | .done() 84 | .always(); 85 | } 86 | 87 | private PodSet getPodSet(String name, String testNamespace, String uid) { 88 | PodSet podSet = new PodSet(); 89 | PodSetSpec podSetSpec = new PodSetSpec(); 90 | podSetSpec.setReplicas(3); 91 | 92 | podSet.setSpec(podSetSpec); 93 | podSet.setMetadata(new ObjectMetaBuilder().withName(name).withNamespace(testNamespace).withUid(uid).build()); 94 | return podSet; 95 | } 96 | } 97 | --------------------------------------------------------------------------------