{"id":15638390,"url":"https://github.com/rohankanojia/sample-controller-java","last_synced_at":"2025-04-30T06:40:45.640Z","repository":{"id":102178578,"uuid":"336612446","full_name":"rohanKanojia/sample-controller-java","owner":"rohanKanojia","description":"Java Implementation of https://github.com/kubernetes/sample-controller using Fabric8 Kubernetes Client","archived":false,"fork":false,"pushed_at":"2024-08-13T14:32:16.000Z","size":56,"stargazers_count":13,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-18T21:31:22.760Z","etag":null,"topics":["controller","fabric8","java","kubernetes","sample"],"latest_commit_sha":null,"homepage":"https://github.com/fabric8io/kubernetes-client","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rohanKanojia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-06T18:54:05.000Z","updated_at":"2025-02-12T05:20:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"661a6f52-3738-45cc-bd91-ea9e1cbafa8a","html_url":"https://github.com/rohanKanojia/sample-controller-java","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohanKanojia%2Fsample-controller-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohanKanojia%2Fsample-controller-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohanKanojia%2Fsample-controller-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rohanKanojia%2Fsample-controller-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rohanKanojia","download_url":"https://codeload.github.com/rohanKanojia/sample-controller-java/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251657412,"owners_count":21622813,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["controller","fabric8","java","kubernetes","sample"],"created_at":"2024-10-03T11:21:26.795Z","updated_at":"2025-04-30T06:40:45.617Z","avatar_url":"https://github.com/rohanKanojia.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sample Controller in Java Using Fabric8 Kubernetes Client\n\n![Build](https://github.com/rohanKanojia/sample-controller-java/workflows/Java%20CI%20with%20Maven/badge.svg?branch=master)\n![License](https://img.shields.io/github/license/rohanKanojia/sample-controller-java)\n[![Twitter](https://img.shields.io/twitter/follow/fabric8io?style=social)](https://twitter.com/fabric8io)\n\n## Note\n\u003e This repository implements a simple controller for watching Foo resources as defined with a CustomResourceDefinition (CRD).\n\nThis is a simple cont which implements a simple operator for a custom resource called `Foo`. Here \nis what this resource looks like:\n```yaml\napiVersion: samplecontroller.k8s.io/v1alpha1\nkind: Foo\nmetadata:\n  name: example-foo\nspec:\n  deploymentName: example-foo-deploy\n  replicas: 1\n```\n\nEach Foo object would have a child `Deployment` which would have a specified number of replicas in `Foo` object, so this controller just tries to maintain `Deployment` for a `Foo` resource checking whether a `Deployment` which is owned by `Foo` resource is running in cluster or not.\n\n## How to Build\n```\n   mvn clean install\n```\n\n## How to Run\n**Prerequisite**: Since the sample-controller uses apps/v1 deployments, the Kubernetes cluster version should be greater than 1.9.\n```bash\n# create a CustomResourceDefinition\nkubectl create -f src/main/resources/crd.yaml\n\n# Run Controller\nmvn exec:java -Dexec.mainClass=io.fabric8.samplecontroller.SampleControllerMain\n\n# create a custom resource of type Foo\nkubectl create -f src/main/resources/example-foo.yaml\n\n# check deployments created through the custom resource\nkubectl get deployments\n```\n\nMake Sure that Foo Custom Resource Definition is already applied onto the cluster. If not, just apply it using this command:\n```\nkubectl apply -f src/main/resources/crd.yaml\n```\n\nOnce everything is set, you can see that controller creating `Deployment` in your cluster for `Foo` resource:\n```\n[main] INFO SampleController - trying to fetch item from workqueue...\n[main] INFO SampleController - Got default/example-foo\n[pool-3-thread-1] INFO SampleController - handleDeploymentObject(example-foo-deploy)\n[main] INFO SampleController - trying to fetch item from workqueue...\n[main] INFO SampleController - Got default/example-foo\n[pool-3-thread-1] INFO SampleController - enqueueFoo(example-foo)\n[pool-3-thread-1] INFO SampleController - Going to enqueue key default/example-foo\n[pool-3-thread-1] INFO SampleController - Adding item to workqueue\n[main] INFO SampleController - trying to fetch item from workqueue...\n[main] INFO SampleController - Got default/example-foo\n[main] INFO SampleController - trying to fetch item from workqueue...\n[main] INFO SampleController - Work Queue is empty\n```\n\n## Deploy to Kubernetes using [Kubernetes Maven Plugin](https://www.eclipse.org/jkube/docs/kubernetes-maven-plugin)\n\nYou can use Kubernetes Maven Plugin to build and push image to a registry like this:\n- Build and Push image to some registry. Before that you'll need to configure `image.user` and `image.registry` properties in `pom.xml`:\n```\nmvn k8s:build k8s:push -Djkube.build.strategy=jib\n```\n![Building and Pushing Image](https://i.imgur.com/uJesL9q.png)\n\n- Generate and Apply Kubernetes manifests to Kubernetes Cluster. First you'll need to configure ServiceAccount with correct roles.  Otherwise, you might get 403 from Kubernetes API server. Since our application would be using `default` ServiceAccount, we can configure it like this:\n\n```\nkubectl create clusterrolebinding default-pod --clusterrole cluster-admin --serviceaccount=default:default\n\n# In case of some other namespace:\nkubectl create clusterrolebinding default-pod --clusterrole cluster-admin --serviceaccount=\u003cnamespace\u003e:default\n```\nThen you can go ahead and run resource and apply goals:\n```\nmvn k8s:resource k8s:apply\n```\n![Apply to Kubernetes](https://i.imgur.com/18hhsYp.png)\n\nCheck pods after deployment:\n```\nsample-controller-java : $ kubectl get pods\nNAME                                      READY   STATUS    RESTARTS   AGE\nsample-controller-java-654d478c4f-qk84r   1/1     Running   0          2m38s\n```\nCreate some Foo resource and check if Deployment gets created:\n```\nsample-controller-java : $ kubectl create -f src/main/resources/cr.yaml \nfoo.samplecontroller.k8s.io/example-foo created\nsample-controller-java : $ kubectl get pods\nNAME                                      READY   STATUS              RESTARTS   AGE\nexample-foo-deploy-9bbb75dc8-6tkf8        0/1     ContainerCreating   0          2s\nsample-controller-java-654d478c4f-qk84r   1/1     Running             0          3m36s\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frohankanojia%2Fsample-controller-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frohankanojia%2Fsample-controller-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frohankanojia%2Fsample-controller-java/lists"}