Skip to content

Commit

Permalink
Merge pull request #1 in CLOUD/mk8s-marketplace-products from feature…
Browse files Browse the repository at this point in the history
…/CLOUD-83572/1 to master

* commit 'da298d56e8d05eead939ef333aedd9d8ed47bb30':
  cloud-93570: Added missing tests
  CLOUD-83572: updated vendor
  CLOUD-83572: moved Dockerfiles, got rid of compiled dlv, fixed Dockerfiles and Makefile
  CLOUD-83572: moved hpa to a separate file
  CLOUD-83572: removed outdated scenario from Makefile
  CLOUD-83572: updated token usage
  CLOUD-83572: moved helm chart to another repository.
  CLOUD-83572: updated helm chart.
  CLOUD-83572: implemented helm chart.
  CLOUD-83572: updated and moved k8s configs.
  CLOUD-83572: replaced the configuration form from yaml files to environment variables.
  CLOUD-83572: refactored the project.
  CLOUD-83572: updated k8s config files.
  CLOUD-83572: updated k8s config files and made some refactor.
  CLOUD-83572: implemented getting an IAM token via JWT, made a small refactoring.
  CLOUD-83572: Makefile refactoring and provider-debug.yaml update.
  CLOUD-83572: Updated Makefile and k8s config files.
  CLOUD-83572: Added copyright head
  CLOUD-83572: Project developments
  • Loading branch information
Edgar Zhivaev committed Mar 14, 2022
2 parents c5dd11c + da298d5 commit 051ea85
Show file tree
Hide file tree
Showing 4,509 changed files with 1,255,435 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/_output/
/apiserver.local.config/
/config.yaml
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
OUT_DIR?=./_output
REGISTRY?=cr.yandex
REGISTRY_ID?=
VENDOR_NAME?=yandex-cloud
PRODUCT_NAME?=marketplace
COMPONENT_NAME?=metric-provider
ARCH?=amd64
OS?=linux
VERSION?=0.1.2
DEBUG?=false


TEMP_DIR:=$(shell mktemp -d)

.PHONY: all build vendor tidy container push

all: build

tidy:
go mod tidy

vendor: tidy
go mod vendor

build: vendor
if [ $(DEBUG) = "true" ] ; then \
echo "Building in debug mode" ; \
GOOS=$(OS) GOARCH=$(ARCH) go build -gcflags "-N -l" -o $(OUT_DIR)/metric_provider ./cmd/metric_provider ; \
else \
GOOS=$(OS) GOARCH=$(ARCH) go build -o $(OUT_DIR)/metric_provider ./cmd/metric_provider ; \
fi

container: build
cp $(OUT_DIR)/metric_provider $(TEMP_DIR)/metric_provider
if [ $(DEBUG) = "true" ] ; then \
cp ./deploy/test/Dockerfile $(TEMP_DIR) ; \
cp ./dlv $(TEMP_DIR)/dlv ; \
else \
cp ./deploy/production/Dockerfile $(TEMP_DIR) ; \
fi
docker build -t $(REGISTRY)/$(REGISTRY_ID)/$(VENDOR_NAME)/$(PRODUCT_NAME)/$(COMPONENT_NAME):$(VERSION) $(TEMP_DIR)

push: container
docker push $(REGISTRY)/$(REGISTRY_ID)/$(VENDOR_NAME)/$(PRODUCT_NAME)/$(COMPONENT_NAME):$(VERSION)
188 changes: 188 additions & 0 deletions cmd/metric_provider/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Copyright (c) 2021 Yandex LLC. All rights reserved.
// Author: Edgar Zhivaev <[email protected]>

package main

import (
"encoding/json"
"flag"
"fmt"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/util/wait"
"metric_provider/pkg/metric_provider"
"metric_provider/pkg/yandex_monitoring"
"metric_provider/pkg/yandex_token"
"os"
basecmd "sigs.k8s.io/custom-metrics-apiserver/pkg/cmd"
"strings"
"time"
)

const (
successResult = 0
envError = -1
serverError = -2
)

type appConfig struct {
loggingDebug bool
yandexMonitoringConfig *yandex_monitoring.Config
token *yandex_token.TokenizerConfig
}

func main() {
code, err := run(os.Args[1:])
if err != nil {
fmt.Println("Error!", err.Error())
}
os.Exit(code)
}

func run(args []string) (int, error) {
var err error

config, err := parseEnvVariables()
if err != nil {
return envError, err
}

logger, err := initLogger(config.loggingDebug)
if err != nil {
return serverError, err
}
logger = logger.Named("main")
defer logger.Sync()

cmd := &basecmd.AdapterBase{Name: "yandex-metrics-apiserver"}

cmd.Flags().AddGoFlagSet(flag.CommandLine)
cmd.Flags().Parse(os.Args)
var tokenizer *yandex_token.Tokenizer
if config.token.Url != "" {
tokenizer, err = yandex_token.NewTokenizerWithURL(config.token.KeyID, config.token.ServiceAccountID, config.token.PrivateKey, config.token.Url)
} else {
tokenizer, err = yandex_token.NewTokenizer(config.token.KeyID, config.token.ServiceAccountID, config.token.PrivateKey)
}
if err != nil {
return serverError, err
}

helper := yandex_monitoring.NewYandexMonitoringService(config.yandexMonitoringConfig, tokenizer, logger)
metricProvider := metric_provider.NewMetricProvider(helper, logger)
cmd.WithExternalMetrics(metricProvider)

logger.Info("Application initialized")

err = cmd.Run(wait.NeverStop)
if err != nil {
return serverError, err
}
return successResult, nil
}

func parseEnvVariables() (*appConfig, error) {
loggingDebug := false
loggingDebugStr := os.Getenv("LOGGING_DEBUG")
if strings.ToLower(loggingDebugStr) == "true" {
loggingDebug = true
}

ymUrl, err := getEnvValue("YM_URL")
if err != nil {
return nil, err
}

ymFolderId, err := getEnvValue("YM_FOLDER_ID")
if err != nil {
return nil, err
}

ymWindowStr, err := getEnvValue("YM_WINDOW")
if err != nil {
return nil, err
}

ymWindow, err := time.ParseDuration(ymWindowStr)
if err != nil {
return nil, err
}

ymDsGridAggregation, _ := getEnvValue("YM_DS_GRIDAGGREGATION")

ymDsGapFilling, _ := getEnvValue("YM_DS_GAPFILLING")

ymDsMaxPoints, _ := getEnvValue("YM_DS_MAXPOINTS")

ymDsGridInterval, _ := getEnvValue("YM_DS_GRIDINTERVAL")

ymDsDisabled := true
ymDsDisabledStr, _ := getEnvValue("YM_DS_DISABLED")
if strings.ToLower(ymDsDisabledStr) == "false" {
ymDsDisabled = false
}

ytTokenStr, err := getEnvValue("YM_TOKEN")
if err != nil {
return nil, err
}

var tyToken yandex_token.TokenizerConfig
err = json.Unmarshal([]byte(ytTokenStr), &tyToken)
if err != nil {
return nil, err
}

ytTokenUrl, err := getEnvValue("YM_TOKEN_URL")
if err != nil {
return nil, err
}
tyToken.Url = ytTokenUrl

return &appConfig{
loggingDebug: loggingDebug,
yandexMonitoringConfig: &yandex_monitoring.Config{
Url: ymUrl,
FolderId: ymFolderId,
Window: ymWindow,
Downsampling: &yandex_monitoring.Downsampling{
GridAggregation: changeEmptyStrToNil(&ymDsGridAggregation),
GapFilling: changeEmptyStrToNil(&ymDsGapFilling),
MaxPoints: changeEmptyStrToNil(&ymDsMaxPoints),
GridInterval: changeEmptyStrToNil(&ymDsGridInterval),
Disabled: &ymDsDisabled,
},
},
token: &tyToken,
}, nil
}

func changeEmptyStrToNil(str *string) *string {
if *str == "" {
return nil
}
return str
}

func getEnvValue(key string) (string, error) {
value := os.Getenv(key)
if value == "" {
return "", fmt.Errorf("environment variable %s is not specified or is empty", key)
}
return value, nil
}

func initLogger(debug bool) (*zap.Logger, error) {
if debug == true {
logger, err := zap.NewProduction()
if err != nil {
return nil, err
}
return logger, nil
}

logger, err := zap.NewDevelopment()
if err != nil {
return nil, err
}
return logger, nil
}
2 changes: 2 additions & 0 deletions deploy/production/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM alpine:3.15
COPY metric_provider /
Loading

0 comments on commit 051ea85

Please sign in to comment.