Skip to content

Commit

Permalink
feat: Add KubecostExtractors controller (#320)
Browse files Browse the repository at this point in the history
* add kubecostextractors controller

* fetch cluster data

* add recommendations

* add comment

* bump docker image

* add container

* set one mont window

* refactor to use svc dns

---------

Co-authored-by: michaeljguarino <mjg@plural.sh>
  • Loading branch information
zreigz and michaeljguarino authored Dec 18, 2024
1 parent c673038 commit 5fc94fd
Show file tree
Hide file tree
Showing 18 changed files with 1,052 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22.6-alpine3.20 AS builder
FROM golang:1.22.7-alpine3.20 AS builder

ARG TARGETARCH

Expand Down
5 changes: 5 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ resources:
kind: CustomHealth
path: github.com/pluralsh/deployment-operator/api/v1alpha1
version: v1alpha1
- controller: true
domain: plural.sh
group: deployments
kind: KubecostExtractor
version: v1alpha1
version: "3"
77 changes: 77 additions & 0 deletions api/v1alpha1/kubecostextractor_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package v1alpha1

import (
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
defaultKubecostExtractorInterval = 2 * time.Hour
defaultKubecostExtractorPort = "9090"
)

func init() {
SchemeBuilder.Register(&KubecostExtractor{}, &KubecostExtractorList{})
}

type KubecostExtractorSpec struct {
// +kubebuilder:default="1h"
// +kubebuilder:validation:Optional
Interval *string `json:"interval,omitempty"`
KubecostServiceRef corev1.ObjectReference `json:"kubecostServiceRef"`
// +kubebuilder:validation:Optional
KubecostPort *int32 `json:"kubecostPort,omitempty"`
// RecommendationThreshold float value for example: `1.2 or 0.001`
RecommendationThreshold string `json:"recommendationThreshold"`
}

// KubecostExtractorList contains a list of [KubecostExtractor]
// +kubebuilder:object:root=true
type KubecostExtractorList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []KubecostExtractor `json:"items"`
}

// KubecostExtractor
// +kubebuilder:object:root=true
// +kubebuilder:resource:scope=Cluster
// +kubebuilder:subresource:status
type KubecostExtractor struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec KubecostExtractorSpec `json:"spec"`

// Status of the MetricsAggregate
Status Status `json:"status,omitempty"`
}

func (in *KubecostExtractorSpec) GetInterval() time.Duration {
if in.Interval == nil {
return defaultKubecostExtractorInterval
}

interval, err := time.ParseDuration(*in.Interval)
if err != nil {
return defaultKubecostExtractorInterval
}

return interval
}

func (in *KubecostExtractorSpec) GetPort() string {
if in.KubecostPort == nil {
return defaultKubecostExtractorPort
}

return fmt.Sprintf("%d", *in.KubecostPort)
}

func (in *KubecostExtractor) SetCondition(condition metav1.Condition) {
meta.SetStatusCondition(&in.Status.Conditions, condition)
}
98 changes: 96 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.3
name: kubecostextractors.deployments.plural.sh
spec:
group: deployments.plural.sh
names:
kind: KubecostExtractor
listKind: KubecostExtractorList
plural: kubecostextractors
singular: kubecostextractor
scope: Cluster
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: KubecostExtractor
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:
properties:
interval:
default: 1h
type: string
kubecostPort:
format: int32
type: integer
kubecostServiceRef:
description: ObjectReference contains enough information to let you
inspect or modify the referred object.
properties:
apiVersion:
description: API version of the referent.
type: string
fieldPath:
description: |-
If referring to a piece of an object instead of an entire object, this string
should contain a valid JSON/Go field access statement, such as desiredState.manifest.containers[2].
For example, if the object reference is to a container within a pod, this would take on a value like:
"spec.containers{name}" (where "name" refers to the name of the container that triggered
the event) or if no container name is specified "spec.containers[2]" (container with
index 2 in this pod). This syntax is chosen only to have some well-defined way of
referencing a part of an object.
type: string
kind:
description: |-
Kind of the referent.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
name:
description: |-
Name of the referent.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
type: string
namespace:
description: |-
Namespace of the referent.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
type: string
resourceVersion:
description: |-
Specific resourceVersion to which this reference is made, if any.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency
type: string
uid:
description: |-
UID of the referent.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids
type: string
type: object
x-kubernetes-map-type: atomic
recommendationThreshold:
description: 'RecommendationThreshold float value for example: `1.2
or 0.001`'
type: string
required:
- kubecostServiceRef
- recommendationThreshold
type: object
status:
description: Status of the MetricsAggregate
properties:
conditions:
description: Represents the observations of a PrAutomation's current
state.
items:
description: Condition contains details for one aspect of the current
state of this API Resource.
properties:
lastTransitionTime:
description: |-
lastTransitionTime is the last time the condition transitioned from one status to another.
This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: |-
message is a human readable message indicating details about the transition.
This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: |-
observedGeneration represents the .metadata.generation that the condition was set based upon.
For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date
with respect to the current state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: |-
reason contains a programmatic identifier indicating the reason for the condition's last transition.
Producers of specific condition types may define expected values and meanings for this field,
and whether the values are considered a guaranteed API.
The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
id:
description: ID of the resource in the Console API.
type: string
sha:
description: SHA of last applied configuration.
type: string
type: object
required:
- spec
type: object
served: true
storage: true
subresources:
status: {}
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ spec:
type: object
x-kubernetes-map-type: atomic
required:
- accessKeyID
- region
- secretAccessKeyRef
type: object
type: object
distro:
Expand Down
Loading

0 comments on commit 5fc94fd

Please sign in to comment.