Skip to content

Commit

Permalink
fix: export config of flow
Browse files Browse the repository at this point in the history
  • Loading branch information
KingsonKai committed Jan 8, 2024
1 parent 2300ed4 commit 15a53e6
Show file tree
Hide file tree
Showing 14 changed files with 751 additions and 13 deletions.
20 changes: 19 additions & 1 deletion chaosmeta-deploy/templates/chaosmeta-flow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ spec:
- --leader-elect
command:
- /manager
image: registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-flow-controller:v0.0.3
image: registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-flow-controller:v0.0.5
env:
- name: DEFAULTNAMESPACE
valueFrom:
Expand Down Expand Up @@ -342,3 +342,21 @@ webhooks:
resources:
- loadtests
sideEffects: None

---
apiVersion: v1
kind: ConfigMap
metadata:
name: chaosmeta-flow-config
namespace: chaosmeta
data:
chaosmeta-flow.json: |-
{
"executor": {
"image": "registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-jmeter:v1.0.0",
"resource": {
"cpu": "0",
"memory": "1Gi"
}
}
}
2 changes: 2 additions & 0 deletions chaosmeta-flow-operator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/
COPY config/executor config/executor
COPY config/chaosmeta-flow.json config/chaosmeta-flow.json

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand All @@ -30,6 +31,7 @@ FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /workspace/config/executor /config/executor
COPY --from=builder /workspace/config/chaosmeta-flow.json /workspace/config/chaosmeta-flow.json
USER 65532:65532

ENTRYPOINT ["/manager"]
2 changes: 1 addition & 1 deletion chaosmeta-flow-operator/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Image URL to use all building/pushing image targets
IMG ?= registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-flow-controller:v0.0.2
IMG ?= registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-flow-controller:v0.0.5
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.25.0

Expand Down
4 changes: 2 additions & 2 deletions chaosmeta-flow-operator/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ domain: chaosmeta.io
layout:
- go.kubebuilder.io/v3
projectName: chaosmeta-flow-operator
repo: self/chaosmeta/chaosmeta-flow-operator
repo: github.com/traas-stack/chaosmeta/chaosmeta-flow-operator
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: chaosmeta.io
kind: LoadTest
path: self/chaosmeta/chaosmeta-flow-operator/api/v1alpha1
path: github.com/traas-stack/chaosmeta/chaosmeta-flow-operator/api/v1alpha1
version: v1alpha1
webhooks:
defaulting: true
Expand Down
2 changes: 1 addition & 1 deletion chaosmeta-flow-operator/build/yamls/chaosmeta-flow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ spec:
- --leader-elect
command:
- /manager
image: registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-flow-controller:v0.0.2
image: registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-flow-controller:v0.0.5
livenessProbe:
httpGet:
path: /healthz
Expand Down
9 changes: 9 additions & 0 deletions chaosmeta-flow-operator/config/chaosmeta-flow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"executor": {
"image": "registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-jmeter:v1.0.0",
"resource": {
"cpu": "0",
"memory": "1Gi"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ spec:
resources:
requests:
cpu: "@CPU_REQ@"
memory: 1Gi
memory: "@MEM_REQ@"
limits:
cpu: "@CPU_REQ@"
memory: 1Gi
memory: "@MEM_REQ@"
env:
- name: JEMETER_CONFIG
value: |
Expand Down
2 changes: 1 addition & 1 deletion chaosmeta-flow-operator/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ kind: Kustomization
images:
- name: controller
newName: registry.cn-hangzhou.aliyuncs.com/chaosmeta/chaosmeta-flow-controller
newTag: v0.0.2
newTag: v0.0.5
14 changes: 13 additions & 1 deletion chaosmeta-flow-operator/controllers/loadtest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/traas-stack/chaosmeta/chaosmeta-flow-operator/pkg/config"
"io"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -37,7 +38,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"self/chaosmeta/chaosmeta-flow-operator/api/v1alpha1"
"github.com/traas-stack/chaosmeta/chaosmeta-flow-operator/api/v1alpha1"
)

// LoadTestReconciler reconciles a LoadTest object
Expand Down Expand Up @@ -294,19 +295,30 @@ func loadConfig(ctx context.Context, ins *v1alpha1.LoadTest) string {
}

func loadJob(ctx context.Context, ins *v1alpha1.LoadTest, configFileStr string) (*batchv1.Job, error) {
mainConfig := config.GetGlobalConfig()
logger := log.FromContext(ctx)
logger.Info(fmt.Sprintf("read config: image: %s, cpu: %s, mem: %s", mainConfig.Executor.Image, mainConfig.Executor.Resource.CPU, mainConfig.Executor.Resource.Memory))

yamlStr := strings.ReplaceAll(v1alpha1.JobYamlStr, "@INITIAL_CONFIG@", configFileStr)

cpuCore := strconv.Itoa(ins.Spec.Parallelism / ins.Spec.Source / 2)
if cpuCore == "0" {
cpuCore = "0.5"
}

if mainConfig.Executor.Resource.CPU != "" && mainConfig.Executor.Resource.CPU != "0" {
cpuCore = mainConfig.Executor.Resource.CPU
}

yamlStr = strings.ReplaceAll(yamlStr, "@CPU_REQ@", cpuCore)
yamlStr = strings.ReplaceAll(yamlStr, "@MEM_REQ@", mainConfig.Executor.Resource.Memory)

job := &batchv1.Job{}
if err := yaml.Unmarshal([]byte(yamlStr), job); err != nil {
return nil, fmt.Errorf("convert yaml to job instance error: %s", err.Error())
}

job.Spec.Template.Spec.Containers[0].Image = mainConfig.Executor.Image
job.Name = ins.Name
job.Spec.Template.Name = ins.Name
job.Namespace = ins.Namespace
Expand Down
2 changes: 1 addition & 1 deletion chaosmeta-flow-operator/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

chaosmetaiov1alpha1 "self/chaosmeta/chaosmeta-flow-operator/api/v1alpha1"
chaosmetaiov1alpha1 "github.com/traas-stack/chaosmeta/chaosmeta-flow-operator/api/v1alpha1"
//+kubebuilder:scaffold:imports
)

Expand Down
2 changes: 1 addition & 1 deletion chaosmeta-flow-operator/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module self/chaosmeta/chaosmeta-flow-operator
module github.com/traas-stack/chaosmeta/chaosmeta-flow-operator

go 1.19

Expand Down
5 changes: 3 additions & 2 deletions chaosmeta-flow-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

chaosmetaiov1alpha1 "self/chaosmeta/chaosmeta-flow-operator/api/v1alpha1"
"self/chaosmeta/chaosmeta-flow-operator/controllers"
chaosmetaiov1alpha1 "github.com/traas-stack/chaosmeta/chaosmeta-flow-operator/api/v1alpha1"
"github.com/traas-stack/chaosmeta/chaosmeta-flow-operator/controllers"
_ "github.com/traas-stack/chaosmeta/chaosmeta-flow-operator/pkg/config"
//+kubebuilder:scaffold:imports
)

Expand Down
68 changes: 68 additions & 0 deletions chaosmeta-flow-operator/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2022-2023 Chaos Meta Authors.
*
* 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.
*/

package config

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
)

var globalConfig *MainConfig

func init() {
_, filename, _, _ := runtime.Caller(0)
absPath, _ := filepath.Abs(filepath.Dir(filename))
if err := LoadConfig(fmt.Sprintf("%s/config/chaosmeta-flow.json", filepath.Dir(filepath.Dir(absPath)))); err != nil {
panic(any(fmt.Sprintf("load config error: %s", err.Error())))
}
}

func GetGlobalConfig() *MainConfig {
return globalConfig
}

func LoadConfig(path string) error {
configBytes, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("load config file error: %s", err.Error())
}
var mainConfig MainConfig
if err := json.Unmarshal(configBytes, &mainConfig); err != nil {
return fmt.Errorf("convert config file error: %s", err.Error())
}

globalConfig = &mainConfig
fmt.Println(string(configBytes))
return nil
}

type MainConfig struct {
Executor ExecutorConfig `json:"executor"`
}

type ExecutorConfig struct {
Image string `json:"image"`
Resource ResourceConfig `json:"resource"`
}

type ResourceConfig struct {
CPU string `json:"image"`
Memory string `json:"memory"`
}
Loading

0 comments on commit 15a53e6

Please sign in to comment.