Skip to content

Commit

Permalink
rename cost-exportor to fadvisor. use secret to store cloud config fi…
Browse files Browse the repository at this point in the history
…le which include cloud provider credentials, rm helm charts and integration yaml files, move to helm chart repo
  • Loading branch information
kitianFresh committed Feb 25, 2022
1 parent e0bc7be commit 110d69d
Show file tree
Hide file tree
Showing 140 changed files with 113 additions and 13,288 deletions.
42 changes: 14 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ REGISTRY_USER_NAME?=""
REGISTRY_PASSWORD?=""

# Image URL to use all building/pushing image targets
COST_EXPORTER_IMG ?= "${REGISTRY}/${REGISTRY_NAMESPACE}/fadvisor-cost-exporter:${GIT_VERSION}"
FADVISOR_IMG ?= "${REGISTRY}/${REGISTRY_NAMESPACE}/fadvisor:${GIT_VERSION}"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -98,32 +98,32 @@ test: fmt vet goimports ## Run tests.


.PHONY: build
build: cost-exporter
build: fadvisor

.PHONY: all
all: test lint vet cost-exporter
all: test lint vet fadvisor

.PHONY: cost-exporter
cost-exporter: ## Build binary with the cost-exporter.
CGO_ENABLED=0 GOOS=$(GOOS) go build -ldflags $(LDFLAGS) -o bin/cost-exporter cmd/cost-exporter/main.go
.PHONY: fadvisor
fadvisor: ## Build binary with the fadvisor.
CGO_ENABLED=0 GOOS=$(GOOS) go build -ldflags $(LDFLAGS) -o bin/fadvisor cmd/fadvisor/main.go

.PHONY: images
images: image-cost-exporter
images: image-fadvisor

.PHONY: image-cost-exporter
image-cost-exporter: test ## Build docker image with the cost-exporter.
docker build --build-arg LDFLAGS=$(LDFLAGS) --build-arg PKGNAME=cost-exporter -t ${COST_EXPORTER_IMG} .
.PHONY: image-fadvisor
image-fadvisor: test ## Build docker image with the fadvisor.
docker build --build-arg LDFLAGS=$(LDFLAGS) --build-arg PKGNAME=fadvisor -t ${FADVISOR_IMG} .


.PHONY: push-images
push-images: push-image-cost-exporter
push-images: push-image-fadvisor

.PHONY: push-image-cost-exporter
push-image-cost-exporter: ## Push images.
.PHONY: push-image-fadvisor
push-image-fadvisor: ## Push images.
ifneq ($(REGISTRY_USER_NAME), "")
docker login -u $(REGISTRY_USER_NAME) -p $(REGISTRY_PASSWORD) ${REGISTRY}
endif
docker push ${COST_EXPORTER_IMG}
docker push ${FADVISOR_IMG}


# go-get-tool will 'go get' any package $2 and install it to $1.
Expand All @@ -140,20 +140,6 @@ rm -rf $$TMP_DIR ;\
}
endef

controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(shell go env GOPATH)/bin/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif

golangci-lint:
ifeq (, $(shell which golangci-lint))
Expand Down
118 changes: 69 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,95 +33,115 @@ Cost model is an implementation of the cost allocation and showback & chargeback
So the containers price in different node type is different.

# Tutorial
Cost-Exporter is a metrics exporter which collects cloud instance price information by calling Cloud Billing API and exports the price information as metrics.
Fadvisor is a metrics exporter which collects cloud instance price information by calling Cloud Billing API and exports the price information as metrics.
Any cloud provider can implement the API and Crane will work for the specific Cloud, Tencent Cloud is supported in current release.

## Deploy all components by one command
## PreRequests
Install Prometheus
```
helm install fadvisor deploy/helm/fadvisor -n crane-system --set cost-exporter.extraArgs.provider={cloud provider, now support qcloud} --set cost-exporter.extraArgs.secretid={{your cloud secret id}} --set cost-exporter.extraArgs.secretkey={{your cloud secret key}}
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus -n crane-system --set pushgateway.enabled=false --set alertmanager.enabled=false --set server.persistentVolume.enabled=false -f https://raw.githubusercontent.com/gocrane/helm-charts/main/integration/prometheus/override_values.yaml --create-namespace prometheus-community/prometheus
```
Except cost-exporter, it will install following components in your system by default.
Install Grafana
```
dependencies:
- name: kube-state-metrics
condition: fadvisor.kube-state-metrics.enabled,kube-state-metrics.enabled
repository: file://./charts/kube-state-metrics
- name: node-exporter
condition: fadvisor.node-exporter.enabled,node-exporter.enabled
repository: file://./charts/node-exporter
- name: prometheus
condition: fadvisor.prometheus.enabled,prometheus.enabled
repository: file://./charts/prometheus
- name: grafana
condition: fadvisor.grafana.enabled,grafana.enabled
repository: file://./charts/grafana
helm repo add grafana https://grafana.github.io/helm-charts
helm install grafana -f https://raw.githubusercontent.com/gocrane/helm-charts/main/integration/grafana/override_values.yaml -n crane-system --create-namespace grafana/grafana
```

Install on local, it will use default config.
## Deploy fadvisor
### Deploy on local
Install on local such as mac desktop, it will use default config. you can also deploy on cloud by this way, but it will use default config.

```
helm install fadvisor deploy/helm/fadvisor -n crane-system
helm repo add crane https://gocrane.github.io/helm-charts
helm install fadvisor -n crane-system --create-namespace crane/fadvisor
```

## Install one by one

To install cost-exporter, you must specify cloud provider and your cloud account credentials as secretid & secretkey.
If you are running Crane in your private cloud, default price will be applied.
### Deploy on cloud
If you deploy fadvisor on cloud, now it support tencent cloud. you need provide a qcloud config file which include cloud credentials as following, make sure you specify your `clusterId`,`secretId`,`secretKey`,`region`

```
helm install cost-exporter deploy/helm/fadvisor/charts/cost-exporter -n crane-system --set extraArgs.provider={cloud provider, now support qcloud} --set extraArgs.secretid={{your cloud secret id}} --set extraArgs.secretkey={{your cloud secret key}}
[credentials]
clusterId={your cluster id}
appId=app1
secretId={your cloud provider credential secret id}
secretKey={your cloud provider credential secret key}
[clientProfile]
defaultLimit=100
defaultLanguage=zh-CN
defaultTimeoutSeconds=10
region={your cluster region, such as ap-beijing、ap-shanghai、ap-guangzhou、ap-shenzhen and so on, you can find region name in your cloud provider console}
domainSuffix=internal.tencentcloudapi.com
scheme=
```

Install other components.
then execute following commands, suppose your config file name is qcloud-config.ini in your current directory:
```
helm install kube-state-metrics deploy/helm/fadvisor/charts/kube-state-metrics -n crane-system
helm install node-exporter deploy/helm/fadvisor/charts/node-exporter -n crane-system
helm install prometheus deploy/helm/fadvisor/charts/prometheus -n crane-system
helm install grafana deploy/helm/fadvisor/charts/grafana -n crane-system
helm repo add crane https://gocrane.github.io/helm-charts
helm install fadvisor --set-file cloudConfigFile=qcloud-config.ini --set extraArgs.provider=qcloud -n crane-system --create-namespace crane/fadvisor
```
Except Fadvisor, it will install following components in your system by default.

- kube-state-metrics
- node-exporter
- prometheus
- grafana


## Integrated with existing monitoring components
If you have Prometheus and Grafana installed, you can just deploy the exporter and change related configuration.
If you have Prometheus and Grafana installed, you can just deploy fadvisor and change related configuration.

You can deploy the cost-exporter to your tke cluster to collect the metric, use prometheus to scrape the metric, and following dashboards can be used;
You can deploy the fadvisor to your tke cluster to collect the metric, use prometheus to scrape the metric, and following dashboards can be used;

### 1. Deploy cost-exporter
### 1. Deploy fadvisor
#### Install by helm
```
helm install cost-exporter deploy/helm/fadvisor/charts/cost-exporter -n crane-system --set extraArgs.provider={cloud provider, now support qcloud} --set extraArgs.secretid={{your cloud secret id}} --set extraArgs.secretkey={{your cloud secret key}}
helm repo add crane https://gocrane.github.io/helm-charts
helm install fadvisor -n crane-system --create-namespace crane/fadvisor
```

#### Install by kubectl
**NOTE** you must specify your k8s secret id and secret key in yaml, this is used to access Tencent Cloud Cvm API.
**NOTE** you must specify your k8s secret id and secret key in `config` file, this is used to access Tencent Cloud Cvm API.

```
[credentials]
clusterId={your cluster id}
appId=app1
secretId={your cloud provider credential secret id}
secretKey={your cloud provider credential secret key}
[clientProfile]
defaultLimit=100
defaultLanguage=zh-CN
defaultTimeoutSeconds=10
region={your cluster region, such as ap-beijing、ap-shanghai、ap-guangzhou、ap-shenzhen and so on, you can find region name in your cloud provider console}
domainSuffix=internal.tencentcloudapi.com
scheme=
```
then execute `cat config | base64`, paste the secret to following secret.yaml's config.
```
kubectl create -f deploy/cost-exporter/ -n crane-system
apiVersion: v1
kind: Secret
metadata:
name: fadvisor
data:
config: W2NyZWRlbnRpYWxzXQpjbHVzdGVySWQ9Y2x1c3RlcjEKYXBwSWQ9YXBwMQpzZWNyZXRJZD1pZDEKc2VjcmV0S2V5PWtleTEKW2NsaWVudFByb2ZpbGVdCmRlYnVnPXRydWUKZGVmYXVsdExpbWl0PTEKZGVmYXVsdExhbmd1YWdlPUNICmRlZmF1bHRUaW1lb3V0U2Vjb25kcz0xMApyZWdpb249c2hhbmdoYWkKZG9tYWluU3VmZml4PWNsb3VkLnRlbmNlbnQuY29tCnNjaGVtZT1odHRwCg==
```
The cost-exporter param has secretId and secretKey, you must provide your cloud provider secret
then deploy by kubectl.
```
containers:
- name: fadvisor-cost-exporter
image: docker.io/gocrane/fadvisor-cost-exporter:6927f01
imagePullPolicy: IfNotPresent
command:
- /cost-exporter
- --v=4
- --provider=qcloud
- --secretId=
- --secretKey=
kubectl create -f deploy/fadvisor/ -n crane-system
```

### 2. Configure the prometheus scrape config and rules
Configure following scrape target to your prometheus.

```
- job_name: "fadvisor-cost-exporter"
- job_name: "fadvisor"
honor_timestamps: true
scrape_interval: 5m
scheme: http
metrics_path: /metrics
static_configs:
- targets: ['cost-exporter.crane-system.svc.cluster.local:8081']
- targets: ['fadvisor.crane-system.svc.cluster.local:8081']
```
**NOTE**, except cost-exporter, your prometheus must have scraped the kubernetes metrics including:
- `kubelet-cadvisor` metrics.
Expand Down
24 changes: 0 additions & 24 deletions cmd/cost-exporter/main.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"k8s.io/client-go/tools/leaderelection"
"k8s.io/klog/v2"

"github.com/gocrane/fadvisor/cmd/cost-exporter/app/options"
"github.com/gocrane/fadvisor/cmd/fadvisor/app/options"
exporter "github.com/gocrane/fadvisor/pkg/cost-exporter"
"github.com/gocrane/fadvisor/pkg/cost-exporter/cache"
"github.com/gocrane/fadvisor/pkg/cost-exporter/cloudcost"
Expand All @@ -30,8 +30,8 @@ func NewExporterCommand(ctx context.Context) *cobra.Command {
opts := options.NewOptions()

cmd := &cobra.Command{
Use: "cost-exporter",
Long: `cost-exporter used to export cost metrics to storage store such as prometheus`,
Use: "fadvisor",
Long: `fadvisor used to export cost metrics to storage store such as prometheus`,
Run: func(cmd *cobra.Command, args []string) {
if err := opts.Complete(); err != nil {
klog.Errorf("opts complete failed, exit: %v", err)
Expand All @@ -55,16 +55,16 @@ func NewExporterCommand(ctx context.Context) *cobra.Command {
return cmd
}

// Run runs the cost-exporter with options. This should never exit.
// Run runs the fadvisor with options. This should never exit.
func Run(ctx context.Context, opts *options.Options) error {

creator, err := util.CreateK8sClient(opts.ClientConfig, opts.MaxIdleConnsPerClient)
if err != nil {
return err
}

kubeClient := creator("cost-exporter")
kubeEventClient := creator("cost-exporter-event")
kubeClient := creator("fadvisor")
kubeEventClient := creator("fadvisor-event")

k8sCache := cache.NewCache(kubeClient)
k8sCache.WaitForCacheSync(ctx.Done())
Expand Down Expand Up @@ -98,8 +98,8 @@ func Run(ctx context.Context, opts *options.Options) error {
<-serverStopedCh
}

eventRecorder := events.NewEventBroadcasterAdapter(kubeEventClient).NewRecorder("cost-exporter-event")
leadElectCfg, err := util.CreateLeaderElectionConfig("cost-exporter", kubeClient, eventRecorder, opts.LeaderElection)
eventRecorder := events.NewEventBroadcasterAdapter(kubeEventClient).NewRecorder("fadvisor-event")
leadElectCfg, err := util.CreateLeaderElectionConfig("fadvisor", kubeClient, eventRecorder, opts.LeaderElection)
if err != nil {
return fmt.Errorf("couldn't create leader elector config: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (o *Options) AddFlags(flags *pflag.FlagSet) {
"The namespace of resource object that is used for locking during "+
"leader election.")

flags.StringVar(&o.CloudConfig.Provider, "provider", "default", "cloud provider the cost-exporter running on, now support default and qcloud only.")
flags.StringVar(&o.CloudConfig.Provider, "provider", "default", "cloud provider the fadvisor running on, now support default and qcloud only.")
flags.StringVar(&o.CloudConfig.CloudConfigFile, "cloudConfigFile", "", "cloudConfigFile specifies path for the cloud configuration.")

flags.StringVar(&o.ClientConfig.Kubeconfig, "kubeconfig",
Expand Down
4 changes: 2 additions & 2 deletions cmd/apiserver/main.go → cmd/fadvisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/component-base/logs"

"github.com/gocrane/fadvisor/cmd/cost-exporter/app"
"github.com/gocrane/fadvisor/cmd/fadvisor/app"
)

// apiserver main.
// fadvisor main.
func main() {
logs.InitLogs()
defer logs.FlushLogs()
Expand Down
Loading

0 comments on commit 110d69d

Please sign in to comment.