Add Prometheus/KEDA as addon (#1915)

* add keda addon

* add promethus addon

* rebase

* add generate namespace.
add prometheus as addon
add keda as addon

* rebase
This commit is contained in:
chival
2021-07-14 16:48:11 +08:00
committed by GitHub
parent 40171f08bb
commit e91deaa4cc
19 changed files with 442 additions and 21 deletions
@@ -4486,3 +4486,4 @@ metadata:
labels:
addons.oam.dev/type: fluxcd
name: fluxcd
namespace: {{.Values.systemDefinitionNamespace}}
@@ -0,0 +1,70 @@
apiVersion: v1
data:
initializer: |
apiVersion: core.oam.dev/v1beta1
kind: Initializer
metadata:
annotations:
addons.oam.dev/description: KEDA is a Kubernetes-based Event Driven Autoscaler.
name: keda
namespace: keda
spec:
appTemplate:
spec:
components:
- name: keda
properties:
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: keda
namespace: keda
spec:
chart:
spec:
chart: keda
interval: 1m
sourceRef:
kind: HelmRepository
name: kedacore
namespace: keda
version: 2.3.2
interval: 5m
values:
replicaCount: 1
type: raw
- name: kedacore
properties:
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: HelmRepository
metadata:
name: kedacore
namespace: keda
spec:
interval: 1m
url: https://kedacore.github.io/charts
type: raw
status:
rollout:
batchRollingState: ""
currentBatch: 0
lastTargetAppRevision: ""
rollingState: ""
upgradedReadyReplicas: 0
upgradedReplicas: 0
dependsOn:
- ref:
apiVersion: core.oam.dev/v1beta1
kind: Initializer
name: fluxcd
namespace: flux-system
status:
observedGeneration: 0
kind: ConfigMap
metadata:
annotations:
addons.oam.dev/description: KEDA is a Kubernetes-based Event Driven Autoscaler.
labels:
addons.oam.dev/type: keda
name: keda
namespace: {{.Values.systemDefinitionNamespace}}
@@ -67,3 +67,4 @@ metadata:
labels:
addons.oam.dev/type: kruise
name: kruise
namespace: {{.Values.systemDefinitionNamespace}}
@@ -434,3 +434,4 @@ metadata:
labels:
addons.oam.dev/type: ocm-cluster-manager
name: ocm-cluster-manager
namespace: {{.Values.systemDefinitionNamespace}}
@@ -0,0 +1,67 @@
apiVersion: v1
data:
initializer: |
apiVersion: core.oam.dev/v1beta1
kind: Initializer
metadata:
annotations:
addons.oam.dev/description: Prometheus is an open-source systems monitoring and alerting toolkit
name: prometheus
namespace: prometheus
spec:
appTemplate:
spec:
components:
- name: prometheus
properties:
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: prometheus
namespace: prometheus
spec:
chart:
spec:
chart: premetheus
interval: 1pm
sourceRef:
kind: HelmRepository
name: prometheus-community
namespace: premetheus
interval: 5m
type: raw
- name: prometheus-community
properties:
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: HelmRepository
metadata:
name: prometheus-community
namespace: prometheus
spec:
interval: 1m
url: https://prometheus-community.github.io/helm-charts
type: raw
status:
rollout:
batchRollingState: ""
currentBatch: 0
lastTargetAppRevision: ""
rollingState: ""
upgradedReadyReplicas: 0
upgradedReplicas: 0
dependsOn:
- ref:
apiVersion: core.oam.dev/v1beta1
kind: Initializer
name: fluxcd
namespace: flux-system
status:
observedGeneration: 0
kind: ConfigMap
metadata:
annotations:
addons.oam.dev/description: Prometheus is an open-source systems monitoring and alerting toolkit
labels:
addons.oam.dev/type: prometheus
name: prometheus
namespace: {{.Values.systemDefinitionNamespace}}
@@ -68,3 +68,4 @@ metadata:
labels:
addons.oam.dev/type: terraform
name: terraform
namespace: {{.Values.systemDefinitionNamespace}}
+22 -15
View File
@@ -1,19 +1,8 @@
apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
apiVersion: v1
kind: Namespace
metadata:
annotations:
definition.oam.dev/description: kustomize can fetching, building, updating and applying Kustomize manifests from git repo.
name: kustomize
namespace: vela-system
spec:
schematic:
cue:
template: "output: {\n\tapiVersion: \"source.toolkit.fluxcd.io/v1beta1\"\n\tkind: \"GitRepository\"\n\tmetadata: {\n\t\tname: context.name\n\t}\n\tspec: {\n\t\tinterval: parameter.pull_interval\n\t\turl: parameter.repo_url\n\t\tref: branch: parameter.branch\n\t}\n}\noutputs: kustomize: {\n\tapiVersion: \"kustomize.toolkit.fluxcd.io/v1beta1\"\n\tkind: \"Kustomization\"\n\tmetadata: {\n\t\tname: context.name\n\t}\n\tspec: {\n\t\tinterval: parameter.pull_interval\n\t\tsourceRef: {\n\t\t\tkind: \"GitRepository\"\n\t\t\tname: context.name\n\t\t}\n\t\tpath: parameter.path\n\t\tprune: true\n\t\tvalidation: \"client\"\n\t}\n}\nparameter: {\n\t//+usage=The repository URL, can be a HTTP/S or SSH address.\n\trepo_url: string\n\t//+usage=The interval at which to check for repository updates.\n\tpull_interval: *\"1m\" | string\n\t//+usage=The Git reference to checkout and monitor for changes, defaults to master branch.\n\tbranch: *\"master\" | string\n\t//+usage=Path to the directory containing the kustomization.yaml file, or the set of plain YAMLs a kustomization.yaml should be generated for.\n\tpath: string\n}\n"
workload:
definition:
apiVersion: ""
kind: ""
type: autodetects.core.oam.dev
name: flux-system
spec: {}
status: {}
---
apiVersion: core.oam.dev/v1beta1
@@ -4494,3 +4483,21 @@ spec:
upgradedReplicas: 0
status:
observedGeneration: 0
---
apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
annotations:
definition.oam.dev/description: kustomize can fetching, building, updating and applying Kustomize manifests from git repo.
name: kustomize
namespace: vela-system
spec:
schematic:
cue:
template: "output: {\n\tapiVersion: \"source.toolkit.fluxcd.io/v1beta1\"\n\tkind: \"GitRepository\"\n\tmetadata: {\n\t\tname: context.name\n\t}\n\tspec: {\n\t\tinterval: parameter.pull_interval\n\t\turl: parameter.repo_url\n\t\tref: branch: parameter.branch\n\t}\n}\noutputs: kustomize: {\n\tapiVersion: \"kustomize.toolkit.fluxcd.io/v1beta1\"\n\tkind: \"Kustomization\"\n\tmetadata: {\n\t\tname: context.name\n\t}\n\tspec: {\n\t\tinterval: parameter.pull_interval\n\t\tsourceRef: {\n\t\t\tkind: \"GitRepository\"\n\t\t\tname: context.name\n\t\t}\n\t\tpath: parameter.path\n\t\tprune: true\n\t\tvalidation: \"client\"\n\t}\n}\nparameter: {\n\t//+usage=The repository URL, can be a HTTP/S or SSH address.\n\trepo_url: string\n\t//+usage=The interval at which to check for repository updates.\n\tpull_interval: *\"1m\" | string\n\t//+usage=The Git reference to checkout and monitor for changes, defaults to master branch.\n\tbranch: *\"master\" | string\n\t//+usage=Path to the directory containing the kustomization.yaml file, or the set of plain YAMLs a kustomization.yaml should be generated for.\n\tpath: string\n}\n"
workload:
definition:
apiVersion: ""
kind: ""
type: autodetects.core.oam.dev
status: {}
+66
View File
@@ -0,0 +1,66 @@
apiVersion: v1
kind: Namespace
metadata:
name: keda
spec: {}
status: {}
---
apiVersion: core.oam.dev/v1beta1
kind: Initializer
metadata:
annotations:
addons.oam.dev/description: KEDA is a Kubernetes-based Event Driven Autoscaler.
name: keda
namespace: keda
spec:
appTemplate:
spec:
components:
- name: keda
properties:
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: keda
namespace: keda
spec:
chart:
spec:
chart: keda
interval: 1m
sourceRef:
kind: HelmRepository
name: kedacore
namespace: keda
version: 2.3.2
interval: 5m
values:
replicaCount: 1
type: raw
- name: kedacore
properties:
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: HelmRepository
metadata:
name: kedacore
namespace: keda
spec:
interval: 1m
url: https://kedacore.github.io/charts
type: raw
status:
rollout:
batchRollingState: ""
currentBatch: 0
lastTargetAppRevision: ""
rollingState: ""
upgradedReadyReplicas: 0
upgradedReplicas: 0
dependsOn:
- ref:
apiVersion: core.oam.dev/v1beta1
kind: Initializer
name: fluxcd
namespace: flux-system
status:
observedGeneration: 0
@@ -1,3 +1,10 @@
apiVersion: v1
kind: Namespace
metadata:
name: kruise-system
spec: {}
status: {}
---
apiVersion: core.oam.dev/v1beta1
kind: Initializer
metadata:
@@ -1,3 +1,10 @@
apiVersion: v1
kind: Namespace
metadata:
name: open-cluster-management
spec: {}
status: {}
---
apiVersion: core.oam.dev/v1beta1
kind: Initializer
metadata:
@@ -0,0 +1,63 @@
apiVersion: v1
kind: Namespace
metadata:
name: prometheus
spec: {}
status: {}
---
apiVersion: core.oam.dev/v1beta1
kind: Initializer
metadata:
annotations:
addons.oam.dev/description: Prometheus is an open-source systems monitoring and alerting toolkit
name: prometheus
namespace: prometheus
spec:
appTemplate:
spec:
components:
- name: prometheus
properties:
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: prometheus
namespace: prometheus
spec:
chart:
spec:
chart: premetheus
interval: 1pm
sourceRef:
kind: HelmRepository
name: prometheus-community
namespace: premetheus
interval: 5m
type: raw
- name: prometheus-community
properties:
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: HelmRepository
metadata:
name: prometheus-community
namespace: prometheus
spec:
interval: 1m
url: https://prometheus-community.github.io/helm-charts
type: raw
status:
rollout:
batchRollingState: ""
currentBatch: 0
lastTargetAppRevision: ""
rollingState: ""
upgradedReadyReplicas: 0
upgradedReplicas: 0
dependsOn:
- ref:
apiVersion: core.oam.dev/v1beta1
kind: Initializer
name: fluxcd
namespace: flux-system
status:
observedGeneration: 0
@@ -1,3 +1,10 @@
apiVersion: v1
kind: Namespace
metadata:
name: tc-system
spec: {}
status: {}
---
apiVersion: core.oam.dev/v1beta1
kind: Initializer
metadata:
@@ -0,0 +1,18 @@
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: keda
namespace: keda
spec:
interval: 5m
chart:
spec:
chart: keda
sourceRef:
kind: HelmRepository
name: kedacore
namespace: keda
version: 2.3.2
interval: 1m
values:
replicaCount: 1
@@ -0,0 +1,8 @@
kind: HelmRepository
apiVersion: source.toolkit.fluxcd.io/v1beta1
metadata:
name: kedacore
namespace: keda
spec:
interval: 1m
url: https://kedacore.github.io/charts
+21
View File
@@ -0,0 +1,21 @@
apiVersion: core.oam.dev/v1beta1
kind: Initializer
metadata:
annotations:
addons.oam.dev/description: "KEDA is a Kubernetes-based Event Driven Autoscaler."
name: keda
namespace: keda
spec:
dependsOn:
- ref:
apiVersion: core.oam.dev/v1beta1
kind: Initializer
name: fluxcd
namespace: flux-system
appTemplate:
spec:
components: {{ range .ResourceFiles }}
- name: {{ .Name }}
type: raw
properties:
{{ .Content | indent 12 }} {{ end }}
@@ -0,0 +1,15 @@
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: prometheus
namespace: prometheus
spec:
interval: 5m
chart:
spec:
chart: premetheus
sourceRef:
kind: HelmRepository
name: prometheus-community
namespace: premetheus
interval: 1pm
@@ -0,0 +1,8 @@
kind: HelmRepository
apiVersion: source.toolkit.fluxcd.io/v1beta1
metadata:
name: prometheus-community
namespace: prometheus
spec:
interval: 1m
url: https://prometheus-community.github.io/helm-charts
@@ -0,0 +1,21 @@
apiVersion: core.oam.dev/v1beta1
kind: Initializer
metadata:
annotations:
addons.oam.dev/description: "Prometheus is an open-source systems monitoring and alerting toolkit"
name: prometheus
namespace: prometheus
spec:
dependsOn:
- ref:
apiVersion: core.oam.dev/v1beta1
kind: Initializer
name: fluxcd
namespace: flux-system
appTemplate:
spec:
components: {{ range .ResourceFiles }}
- name: {{ .Name }}
type: raw
properties:
{{ .Content | indent 12 }} {{ end }}
+38 -6
View File
@@ -57,6 +57,9 @@ const (
// MarkLabel is annotation key marks configMap as an addon
MarkLabel = "addons.oam.dev/type"
// ChartTemplateNamespace is placeholder for helm chart
ChartTemplateNamespace = "{{.Values.systemDefinitionNamespace}}"
)
type velaFile struct {
@@ -199,11 +202,16 @@ func setConfigMapAnnotations(addonInfo *AddonInfo) map[string]string {
}
}
func removeTimestampInplace(s *string) {
clearStr := "(\n.*?metadata:.*?)?\n.*?creationTimestamp:.*?null"
var re = regexp.MustCompile(clearStr)
timeStampwithApptemplate := "appTemplate:\n(.*metadata:)?\n[ ]*creationTimestamp: null"
re := regexp.MustCompile(timeStampwithApptemplate)
*s = re.ReplaceAllString(*s, "appTemplate:")
pureTimeStamp := "\n[ ]*creationTimestamp: null"
re = regexp.MustCompile(pureTimeStamp)
*s = re.ReplaceAllString(*s, "")
}
// storeConfigMap store configMap in helm chart
func storeConfigMap(addonInfo *AddonInfo, initializer *v1beta1.Initializer, storePath string) error {
configMap := &corev1.ConfigMap{
TypeMeta: v1.TypeMeta{
@@ -213,7 +221,7 @@ func storeConfigMap(addonInfo *AddonInfo, initializer *v1beta1.Initializer, stor
}
addonInfo.Description = initializer.GetAnnotations()[DescAnnotation]
configMap.SetName(addonInfo.Name)
configMap.SetNamespace(addonInfo.Namespace)
configMap.SetNamespace(ChartTemplateNamespace)
configMap.SetAnnotations(setConfigMapAnnotations(addonInfo))
configMap.SetLabels(setConfigMapLabels(addonInfo))
@@ -230,15 +238,22 @@ func storeConfigMap(addonInfo *AddonInfo, initializer *v1beta1.Initializer, stor
}
raw := string(content)
removeTimestampInplace(&raw)
raw = strings.ReplaceAll(raw, fmt.Sprintf("'%s'", ChartTemplateNamespace), ChartTemplateNamespace)
filename := storePath + "/" + addonInfo.Name + ".yaml"
return WriteToFile(filename, raw)
}
func storeInitAndDef(init *v1beta1.Initializer, cds []*v1beta1.ComponentDefinition, addonPath string, addonName string) error {
// storeAutoGenResources store init/componentdefs/namespace in one file
func storeAutoGenResources(init *v1beta1.Initializer, cds []*v1beta1.ComponentDefinition, ns *corev1.Namespace, addonPath string, addonName string) error {
initContent, err := yaml.Marshal(init)
if err != nil {
return err
}
nsContent, err := yaml.Marshal(ns)
if err != nil {
return err
}
filename := path.Join(addonPath, InitializerFileDir, addonName+".yaml")
splitter := "---\n"
cdContents := make([]string, 0, len(cds))
@@ -249,7 +264,10 @@ func storeInitAndDef(init *v1beta1.Initializer, cds []*v1beta1.ComponentDefiniti
}
cdContents = append(cdContents, string(cdContent))
}
fileContent := strings.Join(append(cdContents, string(initContent)), splitter)
contents := []string{string(nsContent)}
contents = append(contents, string(initContent))
contents = append(contents, cdContents...)
fileContent := strings.Join(contents, splitter)
removeTimestampInplace(&fileContent)
return WriteToFile(filename, fileContent)
}
@@ -270,6 +288,18 @@ func getComponentDefs(info *AddonInfo) ([]*v1beta1.ComponentDefinition, error) {
return cds, nil
}
func getNamespace(info *AddonInfo) *corev1.Namespace {
ns := corev1.Namespace{
TypeMeta: v1.TypeMeta{APIVersion: "v1", Kind: "Namespace"},
ObjectMeta: v1.ObjectMeta{Name: info.Namespace},
}
return &ns
}
func setAddonNamespace(info *AddonInfo, init *v1beta1.Initializer) {
info.Namespace = init.Namespace
}
func main() {
var addonsPath string
var storePath string
@@ -296,7 +326,9 @@ func main() {
dealErr(err)
cds, err := getComponentDefs(addInfo)
dealErr(err)
err = storeInitAndDef(init, cds, addonsPath, addInfo.Name)
setAddonNamespace(addInfo, init)
ns := getNamespace(addInfo)
err = storeAutoGenResources(init, cds, ns, addonsPath, addInfo.Name)
dealErr(err)
err = storeConfigMap(addInfo, init, storePath)
dealErr(err)