# nginx-operator **Repository Path**: wilds/nginx-operator ## Basic Information - **Project Name**: nginx-operator - **Description**: No description available - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-03-30 - **Last Updated**: 2024-04-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # install Operator SDK包括生成一个Operator项目的选项,它利用现有Helm chart将Kubernetes资源部署为统一应用程序,而无需编写任何Go代码。 如需演示使用Operator SDK提供的工具和库来设置并运行基于Helm的Operator的基本知识,Operator开发人员可以为Nginx构建一个基于Helm的Operator示例,并将它部署到集群中。 ## Operator Lifecycle Manager (OLM) Installation, upgrade, and role-based access control (RBAC) of Operators on a cluster. ## Understanding the Operator logic For this example, the nginx-operator project executes the following reconciliation logic for each Nginx custom resource (CR): - Create an Nginx deployment if it does not exist. - Create an Nginx service if it does not exist. - Create an Nginx ingress if it is enabled and does not exist. - Ensure that the deployment, service, and optional ingress match the desired configuration as specified by the Nginx CR, for example the replica count, image, and service type. By default, the nginx-operator project watches Nginx resource events as shown in the watches.yaml file and executes Helm releases using the specified chart: ``` # Use the 'create api' subcommand to add watches to this file. - group: demo version: v1 kind: Nginx chart: helm-charts/nginx # +kubebuilder:scaffold:watch ``` ## operator-sdk init 使用helm插件运行operator-sdk init命令以初始化项目 ``` [root@openfuyao-0004 nginx-operator]# operator-sdk init --domain wasmdoors --plugins helm Writing kustomize manifests for you to edit... Next: define a resource with: $ operator-sdk create api ``` ## [Create a simple nginx API using Helm’s built-in chart boilerplate (from helm create)](https://access.redhat.com/documentation/en-us/openshift_container_platform/4.7/html/operators/developing-operators#helm-based-operators) 创建简单的Nginx API,This API uses the built-in Helm chart boilerplate from the helm create command. ``` [root@openfuyao-0004 nginx-operator]# operator-sdk create api --group demo --version v1alpha1 --kind Nginx Writing kustomize manifests for you to edit... Created helm-charts/nginx Generating RBAC rules WARN[0000] The RBAC rules generated in config/rbac/role.yaml are based on the chart's default manifest. Some rules may be missing for resources that are only enabled with custom values, and some existing rules may be overly broad. Double check the rules generated in config/rbac/role.yaml to ensure they meet the operator's permission requirements. ``` The init command creates the nginx-operator project specifically for watching a resource with API version wasmdoors/v1 and kind Nginx. For Helm-based projects, the init command generates the RBAC rules in the config/rbac/role.yaml file based on the resources that would be deployed by the default manifest for the chart. Verify that the rules generated in this file meet the permission requirements of the Operator ## Build and push your operator’s image ``` [root@openfuyao-0004 nginx-operator]# make docker-build docker-push IMG="wasmdoors/nginx-operator:v1.0.0" docker build -t wasmdoors/nginx-operator:v1.0.0 . Sending build context to Docker daemon 297kB Step 1/5 : FROM quay.io/operator-framework/helm-operator:v1.33.0 v1.33.0: Pulling from operator-framework/helm-operator f4a3c904e556: Pull complete ... Digest: sha256:0556040e2de6bd7f6bb4c9447fb3ced7978eff45c25164cf7b54c1a0b5bd4f11 Status: Downloaded newer image for quay.io/operator-framework/helm-operator:v1.33.0 ---> c17f18145930 Step 2/5 : ENV HOME=/opt/helm ---> Running in d98cc2ff8d93 Removing intermediate container d98cc2ff8d93 ---> 50c4b25f5fbb Step 3/5 : COPY watches.yaml ${HOME}/watches.yaml ---> 31e4cc42180b Step 4/5 : COPY helm-charts ${HOME}/helm-charts ---> b8c80eeaa7b3 Step 5/5 : WORKDIR ${HOME} ---> Running in ae503c4be36c Removing intermediate container ae503c4be36c ---> e43edfd765a1 Successfully built e43edfd765a1 Successfully tagged wasmdoors/nginx-operator:v1.0.0 docker push wasmdoors/nginx-operator:v1.0.0 The push refers to repository [docker.io/wasmdoors/nginx-operator] e91752143802: Pushed ... v1.0.0: digest: sha256:1ba7917a64062f168c802e3bac646f71bc8ad33f37c4b9872cc8a3b95d4d1d0c size: 1571 ``` ## OLM deployment ``` operator-sdk olm install make bundle IMG="wasmdoors/nginx-operator:v1.0.0" make bundle-build bundle-push IMG="wasmdoors/nginx-operator:v1.0.0" operator-sdk run bundle -n nginx-operator wasmdoors/nginx-operator-bundle:v1.0.0 kubectl apply -f config/samples/demo_v1alpha1_nginx.yaml operator-sdk cleanup -n nginx-operator nginx-operator ``` ## Direct deployment ``` make deploy IMG="wasmdoors/nginx-operator:v1.0.0" ``` Create a sample Nginx custom resource: ``` $ kubectl apply -f config/samples/demo_v1alpha1_nginx.yaml nginx.demo.wasmdoors/nginx-sample created ``` Uninstall the operator: ``` make undeploy ``` ## ref - [helm](https://helm.sh/docs/) - [developing_charts](https://v2.helm.sh/docs/developing_charts/)