diff --git a/docs/content/en/docs/crd-ref/metrics/v1alpha3/_index.md b/docs/content/en/docs/crd-ref/metrics/v1alpha3/_index.md index a6a00d70f0..e9e86a6880 100644 --- a/docs/content/en/docs/crd-ref/metrics/v1alpha3/_index.md +++ b/docs/content/en/docs/crd-ref/metrics/v1alpha3/_index.md @@ -17,6 +17,8 @@ Package v1alpha3 contains API Schema definitions for the metrics v1alpha3 API gr - [AnalysisDefinition](#analysisdefinition) - [AnalysisDefinitionList](#analysisdefinitionlist) - [AnalysisList](#analysislist) +- [AnalysisValueTemplate](#analysisvaluetemplate) +- [AnalysisValueTemplateList](#analysisvaluetemplatelist) - [KeptnMetric](#keptnmetric) - [KeptnMetricList](#keptnmetriclist) - [KeptnMetricsProvider](#keptnmetricsprovider) @@ -123,6 +125,55 @@ _Appears in:_ | `analysisDefinition` _[ObjectReference](#objectreference)_ | AnalysisDefinition refers to the AnalysisDefinition, a CRD that stores the AnalysisValuesTemplates | +#### AnalysisValueTemplate + + + +AnalysisValueTemplate is the Schema for the analysisvaluetemplates API + +_Appears in:_ +- [AnalysisValueTemplateList](#analysisvaluetemplatelist) + +| Field | Description | +| --- | --- | +| `apiVersion` _string_ | `metrics.keptn.sh/v1alpha3` +| `kind` _string_ | `AnalysisValueTemplate` +| `metadata` _[ObjectMeta](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#objectmeta-v1-meta)_ | Refer to Kubernetes API documentation for fields of `metadata`. | +| `spec` _[AnalysisValueTemplateSpec](#analysisvaluetemplatespec)_ | Spec contains the specification for the AnalysisValueTemplate | +| `status` _string_ | unused field | + + +#### AnalysisValueTemplateList + + + +AnalysisValueTemplateList contains a list of AnalysisValueTemplate + + + +| Field | Description | +| --- | --- | +| `apiVersion` _string_ | `metrics.keptn.sh/v1alpha3` +| `kind` _string_ | `AnalysisValueTemplateList` +| `metadata` _[ListMeta](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#listmeta-v1-meta)_ | Refer to Kubernetes API documentation for fields of `metadata`. | +| `items` _[AnalysisValueTemplate](#analysisvaluetemplate) array_ | | + + +#### AnalysisValueTemplateSpec + + + +AnalysisValueTemplateSpec defines the desired state of AnalysisValueTemplate + +_Appears in:_ +- [AnalysisValueTemplate](#analysisvaluetemplate) + +| Field | Description | +| --- | --- | +| `provider` _[ObjectReference](#objectreference)_ | Provider refers to the KeptnMetricsProvider which should be used to retrieve the data | +| `query` _string_ | Query represents the query to be run. It can include placeholders that are defined using the go template syntax. More info on go templating - https://pkg.go.dev/text/template | + + #### KeptnMetric @@ -249,6 +300,7 @@ _Appears in:_ _Appears in:_ - [AnalysisSpec](#analysisspec) +- [AnalysisValueTemplateSpec](#analysisvaluetemplatespec) - [Objective](#objective) | Field | Description | diff --git a/helm/chart/templates/analysisvaluetemplate-crd.yaml b/helm/chart/templates/analysisvaluetemplate-crd.yaml new file mode 100644 index 0000000000..e8533962c5 --- /dev/null +++ b/helm/chart/templates/analysisvaluetemplate-crd.yaml @@ -0,0 +1,77 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: analysisvaluetemplates.metrics.keptn.sh + annotations: + controller-gen.kubebuilder.io/version: v0.12.1 + labels: + app.kubernetes.io/part-of: keptn-lifecycle-toolkit + crdGroup: metrics.keptn.sh + keptn.sh/inject-cert: "true" + {{- include "chart.labels" . | nindent 4 }} +spec: + group: metrics.keptn.sh + names: + kind: AnalysisValueTemplate + listKind: AnalysisValueTemplateList + plural: analysisvaluetemplates + singular: analysisvaluetemplate + scope: Namespaced + versions: + - additionalPrinterColumns: + - jsonPath: .spec.provider.name + name: Provider + type: string + name: v1alpha3 + schema: + openAPIV3Schema: + description: AnalysisValueTemplate is the Schema for the analysisvaluetemplates + API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: Spec contains the specification for the AnalysisValueTemplate + properties: + provider: + description: Provider refers to the KeptnMetricsProvider which should + be used to retrieve the data + properties: + name: + description: Name defines the name of the referenced object + type: string + namespace: + description: Namespace defines the namespace of the referenced + object + type: string + required: + - name + type: object + query: + description: Query represents the query to be run. It can include + placeholders that are defined using the go template syntax. More + info on go templating - https://pkg.go.dev/text/template + type: string + required: + - provider + - query + type: object + status: + description: unused field + type: string + type: object + served: true + storage: true + subresources: + status: {} diff --git a/metrics-operator/PROJECT b/metrics-operator/PROJECT index 0accda4a10..7fadd11475 100644 --- a/metrics-operator/PROJECT +++ b/metrics-operator/PROJECT @@ -72,4 +72,12 @@ resources: kind: Analysis path: github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3 version: v1alpha3 +- api: + crdVersion: v1 + namespaced: true + domain: keptn.sh + group: metrics + kind: AnalysisValueTemplate + path: github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3 + version: v1alpha3 version: "3" diff --git a/metrics-operator/api/v1alpha3/analysisvaluetemplate_types.go b/metrics-operator/api/v1alpha3/analysisvaluetemplate_types.go new file mode 100644 index 0000000000..a8cb943844 --- /dev/null +++ b/metrics-operator/api/v1alpha3/analysisvaluetemplate_types.go @@ -0,0 +1,58 @@ +/* +Copyright 2023. + +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 v1alpha3 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// AnalysisValueTemplateSpec defines the desired state of AnalysisValueTemplate +type AnalysisValueTemplateSpec struct { + // Provider refers to the KeptnMetricsProvider which should be used to retrieve the data + Provider ObjectReference `json:"provider"` + // Query represents the query to be run. It can include placeholders that are defined using the go template + // syntax. More info on go templating - https://pkg.go.dev/text/template + Query string `json:"query"` +} + +//+kubebuilder:object:root=true +//+kubebuilder:subresource:status +// +kubebuilder:printcolumn:name="Provider",type=string,JSONPath=`.spec.provider.name` + +// AnalysisValueTemplate is the Schema for the analysisvaluetemplates API +type AnalysisValueTemplate struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + // Spec contains the specification for the AnalysisValueTemplate + Spec AnalysisValueTemplateSpec `json:"spec,omitempty"` + // unused field + Status string `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// AnalysisValueTemplateList contains a list of AnalysisValueTemplate +type AnalysisValueTemplateList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []AnalysisValueTemplate `json:"items"` +} + +func init() { + SchemeBuilder.Register(&AnalysisValueTemplate{}, &AnalysisValueTemplateList{}) +} diff --git a/metrics-operator/api/v1alpha3/zz_generated.deepcopy.go b/metrics-operator/api/v1alpha3/zz_generated.deepcopy.go index a0bbc15960..214894b69b 100644 --- a/metrics-operator/api/v1alpha3/zz_generated.deepcopy.go +++ b/metrics-operator/api/v1alpha3/zz_generated.deepcopy.go @@ -188,6 +188,80 @@ func (in *AnalysisSpec) DeepCopy() *AnalysisSpec { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *AnalysisValueTemplate) DeepCopyInto(out *AnalysisValueTemplate) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.Spec = in.Spec +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AnalysisValueTemplate. +func (in *AnalysisValueTemplate) DeepCopy() *AnalysisValueTemplate { + if in == nil { + return nil + } + out := new(AnalysisValueTemplate) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *AnalysisValueTemplate) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *AnalysisValueTemplateList) DeepCopyInto(out *AnalysisValueTemplateList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]AnalysisValueTemplate, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AnalysisValueTemplateList. +func (in *AnalysisValueTemplateList) DeepCopy() *AnalysisValueTemplateList { + if in == nil { + return nil + } + out := new(AnalysisValueTemplateList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *AnalysisValueTemplateList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *AnalysisValueTemplateSpec) DeepCopyInto(out *AnalysisValueTemplateSpec) { + *out = *in + out.Provider = in.Provider +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AnalysisValueTemplateSpec. +func (in *AnalysisValueTemplateSpec) DeepCopy() *AnalysisValueTemplateSpec { + if in == nil { + return nil + } + out := new(AnalysisValueTemplateSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *KeptnMetric) DeepCopyInto(out *KeptnMetric) { *out = *in diff --git a/metrics-operator/config/crd/bases/metrics.keptn.sh_analysisvaluetemplates.yaml b/metrics-operator/config/crd/bases/metrics.keptn.sh_analysisvaluetemplates.yaml new file mode 100644 index 0000000000..01288641ae --- /dev/null +++ b/metrics-operator/config/crd/bases/metrics.keptn.sh_analysisvaluetemplates.yaml @@ -0,0 +1,72 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.12.1 + name: analysisvaluetemplates.metrics.keptn.sh +spec: + group: metrics.keptn.sh + names: + kind: AnalysisValueTemplate + listKind: AnalysisValueTemplateList + plural: analysisvaluetemplates + singular: analysisvaluetemplate + scope: Namespaced + versions: + - additionalPrinterColumns: + - jsonPath: .spec.provider.name + name: Provider + type: string + name: v1alpha3 + schema: + openAPIV3Schema: + description: AnalysisValueTemplate is the Schema for the analysisvaluetemplates + API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: Spec contains the specification for the AnalysisValueTemplate + properties: + provider: + description: Provider refers to the KeptnMetricsProvider which should + be used to retrieve the data + properties: + name: + description: Name defines the name of the referenced object + type: string + namespace: + description: Namespace defines the namespace of the referenced + object + type: string + required: + - name + type: object + query: + description: Query represents the query to be run. It can include + placeholders that are defined using the go template syntax. More + info on go templating - https://pkg.go.dev/text/template + type: string + required: + - provider + - query + type: object + status: + description: unused field + type: string + type: object + served: true + storage: true + subresources: + status: {} diff --git a/metrics-operator/config/crd/kustomization.yaml b/metrics-operator/config/crd/kustomization.yaml index 474932803b..f0acf67e5a 100644 --- a/metrics-operator/config/crd/kustomization.yaml +++ b/metrics-operator/config/crd/kustomization.yaml @@ -6,6 +6,7 @@ resources: - bases/metrics.keptn.sh_keptnmetrics.yaml - bases/metrics.keptn.sh_analyses.yaml - bases/metrics.keptn.sh_analysisdefinitions.yaml + - bases/metrics.keptn.sh_analysisvaluetemplates.yaml # +kubebuilder:scaffold:crdkustomizeresource patchesStrategicMerge: @@ -13,16 +14,18 @@ patchesStrategicMerge: # patches here are for enabling the conversion webhook for each CRD # - patches/webhook_in_keptnmetricsproviders.yaml - patches/webhook_in_keptnmetrics.yaml -#- patches/webhook_in_analyses.yaml + # - patches/webhook_in_analyses.yaml # - patches/webhook_in_analysisdefinitions.yaml + # - patches/webhook_in_analysisvaluetemplates.yaml # +kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD # - patches/cainjection_in_keptnmetricsproviders.yaml # - patches/cainjection_in_keptnmetrics.yaml -#- patches/cainjection_in_analyses.yaml +# - patches/cainjection_in_analyses.yaml # - patches/cainjection_in_analysisdefinitions.yaml +# - patches/cainjection_in_analysisvaluetemplates.yaml # +kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/metrics-operator/config/crd/patches/webhook_in_analysisvaluetemplates.yaml b/metrics-operator/config/crd/patches/webhook_in_analysisvaluetemplates.yaml new file mode 100644 index 0000000000..3da1b99178 --- /dev/null +++ b/metrics-operator/config/crd/patches/webhook_in_analysisvaluetemplates.yaml @@ -0,0 +1,16 @@ +# The following patch enables a conversion webhook for the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: analysisvaluetemplates.metrics.keptn.sh +spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + service: + namespace: system + name: webhook-service + path: /convert + conversionReviewVersions: + - v1 diff --git a/metrics-operator/config/rbac/analysisvaluetemplate_editor_role.yaml b/metrics-operator/config/rbac/analysisvaluetemplate_editor_role.yaml new file mode 100644 index 0000000000..600efa2818 --- /dev/null +++ b/metrics-operator/config/rbac/analysisvaluetemplate_editor_role.yaml @@ -0,0 +1,24 @@ +# permissions for end users to edit analysisvaluetemplates. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: analysisvaluetemplate-editor-role +rules: + - apiGroups: + - metrics.keptn.sh + resources: + - analysisvaluetemplates + verbs: + - create + - delete + - get + - list + - patch + - update + - watch + - apiGroups: + - metrics.keptn.sh + resources: + - analysisvaluetemplates/status + verbs: + - get diff --git a/metrics-operator/config/rbac/analysisvaluetemplate_viewer_role.yaml b/metrics-operator/config/rbac/analysisvaluetemplate_viewer_role.yaml new file mode 100644 index 0000000000..01501f43bc --- /dev/null +++ b/metrics-operator/config/rbac/analysisvaluetemplate_viewer_role.yaml @@ -0,0 +1,20 @@ +# permissions for end users to view analysisvaluetemplates. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: analysisvaluetemplate-viewer-role +rules: + - apiGroups: + - metrics.keptn.sh + resources: + - analysisvaluetemplates + verbs: + - get + - list + - watch + - apiGroups: + - metrics.keptn.sh + resources: + - analysisvaluetemplates/status + verbs: + - get diff --git a/metrics-operator/config/samples/metrics_v1alpha3_analysisvaluetemplate.yaml b/metrics-operator/config/samples/metrics_v1alpha3_analysisvaluetemplate.yaml new file mode 100644 index 0000000000..955d36855b --- /dev/null +++ b/metrics-operator/config/samples/metrics_v1alpha3_analysisvaluetemplate.yaml @@ -0,0 +1,14 @@ +apiVersion: metrics.keptn.sh/v1alpha3 +kind: AnalysisValueTemplate +metadata: + labels: + app.kubernetes.io/name: analysisvaluetemplate + app.kubernetes.io/instance: analysisvaluetemplate-sample + app.kubernetes.io/part-of: metrics-operator + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: metrics-operator + name: analysisvaluetemplate-sample +spec: + provider: + name: prometheus + query: "sum(kube_pod_container_resource_limits{resource='{{.Resource}}'}) - sum(kube_node_status_capacity{resource='{{.Resource}}'})" diff --git a/test/integration/analysis-value-template/00-assert.yaml b/test/integration/analysis-value-template/00-assert.yaml new file mode 100644 index 0000000000..0a251927a7 --- /dev/null +++ b/test/integration/analysis-value-template/00-assert.yaml @@ -0,0 +1,8 @@ +apiVersion: metrics.keptn.sh/v1alpha3 +kind: AnalysisValueTemplate +metadata: + name: analysis-value-template-sample +spec: + provider: + name: some-provider + query: "sum(kube_pod_container_resource_limits{resource='{{.Resource}}'}) - sum(kube_node_status_capacity{resource='{{.Resource}}'})" diff --git a/test/integration/analysis-value-template/00-install.yaml b/test/integration/analysis-value-template/00-install.yaml new file mode 100644 index 0000000000..0a251927a7 --- /dev/null +++ b/test/integration/analysis-value-template/00-install.yaml @@ -0,0 +1,8 @@ +apiVersion: metrics.keptn.sh/v1alpha3 +kind: AnalysisValueTemplate +metadata: + name: analysis-value-template-sample +spec: + provider: + name: some-provider + query: "sum(kube_pod_container_resource_limits{resource='{{.Resource}}'}) - sum(kube_node_status_capacity{resource='{{.Resource}}'})"