This project is designed to provide a Kubernetes operator framework that automates the deployment, management, and scaling of cloud cost optimization resources in a Kubernetes cluster.
Kubewise-operator helps users save costs by dynamically managing cloud resources related to Kubernetes workloads. This operator implements custom resource definitions (CRDs) to extend Kubernetes capabilities, making it easy to optimize workloads on various cloud providers.
- Automatic resource optimization based on historical usage metrics
- Configurable analysis intervals and cost-saving thresholds
- Support for ignoring specific resources (Deployments, StatefulSets, DaemonSets) from optimization
- Integration with Prometheus for metric collection
- Discord notifications for optimization recommendations
- Basic CloudCostOptimizer CRD definition
- Resource analysis based on historical metrics
- Configurable analysis intervals
- Ignore specific resources (Deployments, StatefulSets, DaemonSets)
- Prometheus integration for metric collection
- Discord notifications for optimization recommendations
- Cost-saving threshold configuration
- Machine learning-based predictive optimization
- Integration with cloud-specific cost management APIs
- User interface for visualization and manual control
- Multi-cluster support
- Custom metric support beyond Prometheus
- Automated rollback of optimizations if performance degrades
- Integration with other notification platforms (Slack, Email, etc.)
- Cost allocation and chargeback features
- Support for optimizing storage resources
The project follows a standard Kubernetes operator structure:
api/v1alpha1/
: Contains the API definitions for the CloudCostOptimizer CRDcontrollers/
: Contains the main logic for the operatorconfig/
: Contains Kubernetes manifests for deploying the operatorDockerfile
: Defines the container image for the operatormain.go
: The entry point of the operator
Key files:
api/v1alpha1/cloudcostoptimizer_types.go
: Defines the CloudCostOptimizer CRDcontrollers/cloudcostoptimizer_controller.go
: Main reconciliation loopcontrollers/cloudcostoptimizer_analyze.go
: Resource analysis and optimization logicconfig/samples/optimization_v1alpha1_cloudcostoptimizer.yaml
: Sample CloudCostOptimizer resource
- Go version v1.21.0+ (https://golang.org/doc/install)
- Docker version 17.03+ (https://docs.docker.com/get-docker/)
- kubectl version v1.11.3+ (https://kubernetes.io/docs/tasks/tools/)
- Access to a Kubernetes v1.11.3+ cluster
- Kubernetes cluster with Prometheus installed (for metric collection)
- kustomize v3.8.7+ (https://kustomize.io/)
- Operator SDK v1.28.0+ (https://sdk.operatorframework.io/docs/installation/)
- A container registry account (e.g., Docker Hub) to store the operator image
Optional:
- Discord webhook URL and bot token (for notifications)
- Install Go, Docker, kubectl, and kustomize using the links provided above.
- Install the Operator SDK:
export ARCH=$(case $(uname -m) in x86_64) echo -n amd64 ;; aarch64) echo -n arm64 ;; *) echo -n $(uname -m) ;; esac) export OS=$(uname | awk '{print tolower($0)}') export OPERATOR_SDK_DL_URL=https://github.com/operator-framework/operator-sdk/releases/download/v1.28.0 curl -LO ${OPERATOR_SDK_DL_URL}/operator-sdk_${OS}_${ARCH} chmod +x operator-sdk_${OS}_${ARCH} && sudo mv operator-sdk_${OS}_${ARCH} /usr/local/bin/operator-sdk
- Clone the kubewise-operator repository:
git clone https://github.com/your-org/kubewise-operator.git cd kubewise-operator
Build and push your image to the location specified by IMG
:
make docker-build docker-push IMG=<some-registry>/kubewise-operator:tag
NOTE: This image ought to be published in the personal registry you specified. And it is required to have access to pull the image from the working environment. Make sure you have the proper permission to the registry if the above commands don't work.
Install the CRDs into the cluster:
make install
Deploy the Manager to the cluster with the image specified by IMG
:
make deploy IMG=<some-registry>/kubewise-operator:tag
NOTE: If you encounter RBAC errors, you may need to grant yourself cluster-admin privileges or be logged in as admin.
Create instances of your solution You can apply the samples (examples) from the config/sample:
kubectl apply -k config/samples/
NOTE: Ensure that the samples has default values to test it out.
The CloudCostOptimizer custom resource allows you to configure various aspects of the optimization process. Here's an example configuration:
apiVersion: optimization.dwarvesf.com/v1alpha1
kind: CloudCostOptimizer
metadata:
name: cloudcostoptimizer-sample
spec:
analysisInterval: "1h"
targets:
- resources: ["pods"]
namespaces: ["default", "kube-system"]
automateOptimization: false
ignoreResources:
deployment: ["important-deployment", "critical-app"]
statefulSet: ["database"]
daemonSet: ["monitoring-agent"]
costSavingThreshold: 10
prometheusConfig:
serverAddress: "http://prometheus-server.monitoring"
historicalMetricDuration: 6h
communication:
discord:
webhookURL: "https://discord.com/api/webhooks/your-webhook-url"
botToken: "your-discord-bot-token"
This configuration sets up the CloudCostOptimizer to:
- Analyze resources every hour
- Target pods in the "default" and "kube-system" namespaces
- Ignore specific deployments, statefulsets, and daemonsets
- Set a cost-saving threshold of 10%
- Use Prometheus for historical metrics
- Send notifications to Discord using both a webhook URL and a bot token
When using the automateOptimization
feature with ArgoCD, you need to configure ArgoCD to ignore the changes made by the CloudCostOptimizer. This is because the operator will automatically adjust resource configurations, which may conflict with ArgoCD's sync process.
To configure ArgoCD to ignore these changes, add the following ignoreDifferences
section to your Application or ApplicationSet:
spec:
ignoreDifferences:
- group: apps
kind: Deployment
jsonPointers:
- /spec/template/spec/containers/0/resources
This configuration tells ArgoCD to ignore differences in the container resources specification, which is where the CloudCostOptimizer makes its optimizations. You can adjust the jsonPointers
as needed to match the specific resources you want ArgoCD to ignore.
By applying this configuration, you can ensure that ArgoCD and the CloudCostOptimizer work together harmoniously, allowing the operator to make resource optimizations without triggering unnecessary syncs or conflicts in ArgoCD.
Delete the instances (CRs) from the cluster:
kubectl delete -k config/samples/
Delete the APIs(CRDs) from the cluster:
make uninstall
UnDeploy the controller from the cluster:
make undeploy
Following are the steps to build the installer and distribute this project to users.
- Build the installer for the image built and published in the registry:
make build-installer IMG=<some-registry>/kubewise-operator:tag
NOTE: The makefile target mentioned above generates an 'install.yaml' file in the dist directory. This file contains all the resources built with Kustomize, which are necessary to install this project without its dependencies.
- Using the installer
Users can just run kubectl apply -f to install the project, i.e.:
kubectl apply -f https://raw.githubusercontent.com/<org>/kubewise-operator/<tag or branch>/dist/install.yaml
To contribute to this project, please follow these guidelines:
- Fork the repository.
- Create a feature branch.
- Make your changes and commit them.
- Push your branch to GitHub.
- Create a pull request.
For detailed information on contribution processes and coding standards, please refer to the CONTRIBUTING.md file (to be created).
NOTE: Run make help
for more information on all potential make
targets
More information can be found via the Kubebuilder Documentation
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.