Skip to content

Commit

Permalink
feat: Add Resource Configuration Options to K8sGPT CRD
Browse files Browse the repository at this point in the history
Signed-off-by: Kay Yan <[email protected]>
  • Loading branch information
yankay committed Dec 26, 2024
1 parent ce604d9 commit 3d5c572
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 12 deletions.
26 changes: 14 additions & 12 deletions api/v1alpha1/k8sgpt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

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

Expand Down Expand Up @@ -136,18 +137,19 @@ type ImagePullSecrets struct {
type K8sGPTSpec struct {
Version string `json:"version,omitempty"`
// +kubebuilder:default:=ghcr.io/k8sgpt-ai/k8sgpt
Repository string `json:"repository,omitempty"`
ImagePullSecrets []ImagePullSecrets `json:"imagePullSecrets,omitempty"`
NoCache bool `json:"noCache,omitempty"`
CustomAnalyzers []CustomAnalyzer `json:"customAnalyzers,omitempty"`
Filters []string `json:"filters,omitempty"`
ExtraOptions *ExtraOptionsRef `json:"extraOptions,omitempty"`
Sink *WebhookRef `json:"sink,omitempty"`
AI *AISpec `json:"ai,omitempty"`
RemoteCache *RemoteCacheRef `json:"remoteCache,omitempty"`
Integrations *Integrations `json:"integrations,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
TargetNamespace string `json:"targetNamespace,omitempty"`
Repository string `json:"repository,omitempty"`
ImagePullSecrets []ImagePullSecrets `json:"imagePullSecrets,omitempty"`
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
NoCache bool `json:"noCache,omitempty"`
CustomAnalyzers []CustomAnalyzer `json:"customAnalyzers,omitempty"`
Filters []string `json:"filters,omitempty"`
ExtraOptions *ExtraOptionsRef `json:"extraOptions,omitempty"`
Sink *WebhookRef `json:"sink,omitempty"`
AI *AISpec `json:"ai,omitempty"`
RemoteCache *RemoteCacheRef `json:"remoteCache,omitempty"`
Integrations *Integrations `json:"integrations,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
TargetNamespace string `json:"targetNamespace,omitempty"`
// Define the kubeconfig the Deployment must use.
// If empty, the Deployment will use the ServiceAccount provided by Kubernetes itself.
Kubeconfig *SecretRef `json:"kubeconfig,omitempty"`
Expand Down
13 changes: 13 additions & 0 deletions api/v1alpha1/k8sgpt_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
Expand All @@ -44,6 +46,16 @@ var _ = Describe("The test cases for the K8sGPT CRDs", func() {
version = "v1alpha1"
language = "english"
anonymize = true
resource = corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("1"),
corev1.ResourceMemory: resource.MustParse("512Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0.2"),
corev1.ResourceMemory: resource.MustParse("156Mi"),
},
}

Namespace = "k8sGPT"

Expand Down Expand Up @@ -72,6 +84,7 @@ var _ = Describe("The test cases for the K8sGPT CRDs", func() {
NodeSelector: map[string]string{
"nodepool": "management",
},
Resources: &resource,
},
}

Expand Down
6 changes: 6 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

55 changes: 55 additions & 0 deletions config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,61 @@ spec:
repository:
default: ghcr.io/k8sgpt-ai/k8sgpt
type: string
resources:
description: ResourceRequirements describes the compute resource requirements.
properties:
claims:
description: |-
Claims lists the names of resources, defined in spec.resourceClaims,
that are used by this container.
This is an alpha field and requires enabling the
DynamicResourceAllocation feature gate.
This field is immutable. It can only be set for containers.
items:
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
properties:
name:
description: |-
Name must match the name of one entry in pod.spec.resourceClaims of
the Pod where this field is used. It makes that resource available
inside a container.
type: string
required:
- name
type: object
type: array
x-kubernetes-list-map-keys:
- name
x-kubernetes-list-type: map
limits:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: |-
Limits describes the maximum amount of compute resources allowed.
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
type: object
requests:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: |-
Requests describes the minimum amount of compute resources required.
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
otherwise to an implementation-defined value. Requests cannot exceed Limits.
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
type: object
type: object
sink:
properties:
channel:
Expand Down
7 changes: 7 additions & 0 deletions config/samples/custom_analzyer_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ spec:
# enabled: false
# namespace: trivy-system
# skipInstall: false
# resources:
# limits:
# cpu: 1
# memory: 512Mi
# requests:
# cpu: 200m
# memory: 156Mi
5 changes: 5 additions & 0 deletions pkg/resources/k8sgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ func GetDeployment(config v1alpha1.K8sGPT, outOfClusterMode bool, c client.Clien
deployment.Spec.Template.Spec.Containers[0].Env, baseUrl,
)
}

// Configure the k8sgpt deployment resource if required
if config.Spec.Resources != nil {
deployment.Spec.Template.Spec.Containers[0].Resources = *config.Spec.Resources
}
// Engine is required only when azureopenai is the ai backend
if config.Spec.AI.Engine != "" && config.Spec.AI.Backend == v1alpha1.AzureOpenAI {
engine := corev1.EnvVar{
Expand Down

0 comments on commit 3d5c572

Please sign in to comment.