Skip to content

Commit 9bda00b

Browse files
authored
feat: k8s resource recommendation service integration (#297)
* chore: refactored k8s resource service into common-lib * chore: updated kubelink imports to common-lib module * chore: updated kubelink vendor files * feat: dynamic default parent gvk mapping and support for new method GetChildObjectsV2WithFilter added * chore: updated common lib method signatrues as per new definitions * chore: exported gvr constants * chore: common-lib dependency updated to 'origin/feat/k8s-resource-recommendation' * feat: added gvr const for rollout and cronjob * feat: added new service const for resource optimizer * feat: updated ManifestResponse struct * feat: updated ManifestResponse struct with pointer * fix: import cycle issue * fix: added omitempty to ManifestResponse.RecommendedManifest * chore: removed dead code flow * chore: dependency updated to 'origin/feat/k8s-resource-recommendation' * chore: dependency updated to 'origin/feat/k8s-resource-recommendation' * chore: dependency updated to 'origin/feat/k8s-resource-recommendation'
1 parent 52f0096 commit 9bda00b

472 files changed

Lines changed: 118265 additions & 6100 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

chart-sync/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.24.0
44

55
toolchain go1.24.3
66

7-
replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c
7+
replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15
88

99
require (
1010
github.com/caarlos0/env v3.5.0+incompatible

chart-sync/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
4343
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4444
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
4545
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46-
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c h1:19ve9SuKcmDmEH0Q4s13xgLyK1oMdGZeMcM/B54hYN4=
47-
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c/go.mod h1:/Ciy9tD9OxZOWBDPIasM448H7uvSo4+ZJiExpfwBZpA=
46+
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15 h1:WlUiwAkOdgCMRg1pZLAEC6nx+C7MWtYn5QaJrw64eFY=
47+
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15/go.mod h1:/Ciy9tD9OxZOWBDPIasM448H7uvSo4+ZJiExpfwBZpA=
4848
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
4949
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
5050
github.com/distribution/distribution/v3 v3.0.0 h1:q4R8wemdRQDClzoNNStftB2ZAfqOiN6UX90KJc4HjyM=

chart-sync/vendor/github.com/devtron-labs/common-lib/constants/constants.go

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chart-sync/vendor/modules.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ github.com/containerd/platforms
9595
# github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
9696
## explicit
9797
github.com/davecgh/go-spew/spew
98-
# github.com/devtron-labs/common-lib v0.19.1 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c
98+
# github.com/devtron-labs/common-lib v0.19.1 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15
9999
## explicit; go 1.24.0
100100
github.com/devtron-labs/common-lib/constants
101101
github.com/devtron-labs/common-lib/fetchAllEnv
@@ -967,4 +967,4 @@ sigs.k8s.io/structured-merge-diff/v4/value
967967
sigs.k8s.io/yaml
968968
sigs.k8s.io/yaml/goyaml.v2
969969
sigs.k8s.io/yaml/goyaml.v3
970-
# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c
970+
# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15

ci-runner/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.24.0
44

55
toolchain go1.24.3
66

7-
replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c
7+
replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15
88

99
require (
1010
github.com/Knetic/govaluate v3.0.0+incompatible

ci-runner/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
114114
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
115115
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
116116
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
117-
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c h1:19ve9SuKcmDmEH0Q4s13xgLyK1oMdGZeMcM/B54hYN4=
118-
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c/go.mod h1:/Ciy9tD9OxZOWBDPIasM448H7uvSo4+ZJiExpfwBZpA=
117+
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15 h1:WlUiwAkOdgCMRg1pZLAEC6nx+C7MWtYn5QaJrw64eFY=
118+
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15/go.mod h1:/Ciy9tD9OxZOWBDPIasM448H7uvSo4+ZJiExpfwBZpA=
119119
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
120120
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
121121
github.com/docker/cli v28.1.1+incompatible h1:eyUemzeI45DY7eDPuwUcmDyDj1pM98oD5MdSpiItp8k=

ci-runner/vendor/github.com/devtron-labs/common-lib/constants/constants.go

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ci-runner/vendor/modules.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ github.com/cncf/xds/go/xds/type/v3
298298
# github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
299299
## explicit
300300
github.com/davecgh/go-spew/spew
301-
# github.com/devtron-labs/common-lib v0.19.1 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c
301+
# github.com/devtron-labs/common-lib v0.19.1 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15
302302
## explicit; go 1.24.0
303303
github.com/devtron-labs/common-lib/blob-storage
304304
github.com/devtron-labs/common-lib/constants
@@ -1194,4 +1194,4 @@ sigs.k8s.io/structured-merge-diff/v4/value
11941194
## explicit; go 1.12
11951195
sigs.k8s.io/yaml
11961196
sigs.k8s.io/yaml/goyaml.v2
1197-
# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702064426-05664fd92d8c
1197+
# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250702071043-b85690139d15

common-lib/constants/constants.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ func (m ServiceName) ToString() string {
3030
}
3131

3232
const (
33-
Orchestrator ServiceName = "ORCHESTRATOR"
34-
Kubelink ServiceName = "KUBELINK"
35-
GitSensor ServiceName = "GITSENSOR"
36-
Kubewatch ServiceName = "KUBEWATCH"
37-
ImageScanner ServiceName = "IMAGE_SCANNER"
33+
Orchestrator ServiceName = "ORCHESTRATOR"
34+
Kubelink ServiceName = "KUBELINK"
35+
GitSensor ServiceName = "GITSENSOR"
36+
Kubewatch ServiceName = "KUBEWATCH"
37+
ImageScanner ServiceName = "IMAGE_SCANNER"
38+
ResourceOptimizer ServiceName = "RESOURCE_OPTIMIZER"
3839
)
3940

4041
// metrics name constants

common-lib/k8sResource/bean.go

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package k8sResource
2+
3+
import (
4+
k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean"
5+
"k8s.io/apimachinery/pkg/api/meta"
6+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
7+
"k8s.io/apimachinery/pkg/runtime/schema"
8+
)
9+
10+
type Identifier struct {
11+
gvk schema.GroupVersionKind
12+
name string
13+
namespace string
14+
}
15+
16+
func NewIdentifier(name, namespace string, gvk schema.GroupVersionKind) *Identifier {
17+
return &Identifier{
18+
gvk: gvk,
19+
name: name,
20+
namespace: namespace,
21+
}
22+
}
23+
24+
func (identifier *Identifier) GetGvk() schema.GroupVersionKind {
25+
if identifier == nil {
26+
return schema.GroupVersionKind{}
27+
}
28+
return identifier.gvk
29+
}
30+
31+
func (identifier *Identifier) GetName() string {
32+
if identifier == nil {
33+
return ""
34+
}
35+
return identifier.name
36+
}
37+
38+
func (identifier *Identifier) GetNamespace() string {
39+
if identifier == nil {
40+
return ""
41+
}
42+
return identifier.namespace
43+
}
44+
45+
type PatchRequest struct {
46+
// TODO: Use Identifier instead of Name, Namespace, Gvk
47+
Name string
48+
Namespace string
49+
Gvk *schema.GroupVersionKind
50+
Patch string
51+
PatchType string
52+
}
53+
54+
type ParentChildGvkMapping struct {
55+
Group string `json:"group"`
56+
Version string `json:"version"`
57+
Kind string `json:"kind"`
58+
ChildObjects []ChildObjects `json:"childObjects"`
59+
}
60+
61+
type ChildObjects struct {
62+
Group string `json:"group"`
63+
Version string `json:"version"`
64+
Resource string `json:"resource"`
65+
Scope meta.RESTScopeName `json:"scope"`
66+
}
67+
68+
func (r ChildObjects) GetGvrAndScopeForChildObject() *k8sCommonBean.GvrAndScope {
69+
return &k8sCommonBean.GvrAndScope{
70+
Gvr: schema.GroupVersionResource{
71+
Group: r.Group,
72+
Version: r.Version,
73+
Resource: r.Resource,
74+
},
75+
Scope: r.Scope,
76+
}
77+
}
78+
79+
func (r ParentChildGvkMapping) GetParentGvk() schema.GroupVersionKind {
80+
return schema.GroupVersionKind{
81+
Group: r.Group,
82+
Version: r.Version,
83+
Kind: r.Kind,
84+
}
85+
}
86+
87+
type FilterChildrenObjectsResponse struct {
88+
pvcs []unstructured.Unstructured
89+
manifests []*unstructured.Unstructured
90+
}
91+
92+
func NewFilterChildrenObjectsResponse() *FilterChildrenObjectsResponse {
93+
return &FilterChildrenObjectsResponse{}
94+
}
95+
96+
func (resp *FilterChildrenObjectsResponse) GetPvcs() []unstructured.Unstructured {
97+
return resp.pvcs
98+
}
99+
100+
func (resp *FilterChildrenObjectsResponse) GetManifests() []*unstructured.Unstructured {
101+
return resp.manifests
102+
}
103+
104+
func (resp *FilterChildrenObjectsResponse) WithPVCs(pvcs []unstructured.Unstructured) *FilterChildrenObjectsResponse {
105+
resp.pvcs = append(resp.pvcs, pvcs...)
106+
return resp
107+
}
108+
109+
func (resp *FilterChildrenObjectsResponse) WithManifest(manifest *unstructured.Unstructured) *FilterChildrenObjectsResponse {
110+
if manifest == nil {
111+
return resp
112+
}
113+
resp.manifests = append(resp.manifests, manifest)
114+
return resp
115+
}
116+
117+
type FilterChildrenObjectsRequest struct {
118+
childGvk schema.GroupVersionResource
119+
pvcs []unstructured.Unstructured
120+
listObjects *unstructured.UnstructuredList
121+
// TODO: Use Identifier instead of Name, Namespace, Gvk
122+
namespace string
123+
parentGvk schema.GroupVersionKind
124+
parentName string
125+
}
126+
127+
func (req *FilterChildrenObjectsRequest) IsChildResourceTypePVC() bool {
128+
return req.GetParentGvk().Kind == k8sCommonBean.StatefulSetKind && req.GetChildGvk().Resource == k8sCommonBean.PersistentVolumeClaimsResourceType
129+
}
130+
131+
func (req *FilterChildrenObjectsRequest) GetLoggerMetadata(keysAndValues ...any) []any {
132+
metaData := []any{
133+
"namespace", req.namespace,
134+
"childGvk", req.childGvk,
135+
"parentGvk", req.parentGvk,
136+
"parentName", req.parentName,
137+
}
138+
return append(metaData, keysAndValues...)
139+
}
140+
141+
func (req *FilterChildrenObjectsRequest) GetChildGvk() schema.GroupVersionResource {
142+
return req.childGvk
143+
}
144+
145+
func (req *FilterChildrenObjectsRequest) GetPvcs() []unstructured.Unstructured {
146+
return req.pvcs
147+
}
148+
149+
func (req *FilterChildrenObjectsRequest) GetListObjects() *unstructured.UnstructuredList {
150+
return req.listObjects
151+
}
152+
153+
func (req *FilterChildrenObjectsRequest) GetNamespace() string {
154+
return req.namespace
155+
}
156+
157+
func (req *FilterChildrenObjectsRequest) GetParentGvk() schema.GroupVersionKind {
158+
return req.parentGvk
159+
}
160+
161+
func (req *FilterChildrenObjectsRequest) GetParentName() string {
162+
return req.parentName
163+
}
164+
165+
func NewFilterChildrenObjectsRequest() *FilterChildrenObjectsRequest {
166+
return &FilterChildrenObjectsRequest{}
167+
}
168+
169+
func (req *FilterChildrenObjectsRequest) WithChildGvk(gvr schema.GroupVersionResource) *FilterChildrenObjectsRequest {
170+
req.childGvk = gvr
171+
return req
172+
}
173+
174+
func (req *FilterChildrenObjectsRequest) WithPvcs(pvcs []unstructured.Unstructured) *FilterChildrenObjectsRequest {
175+
req.pvcs = pvcs
176+
return req
177+
}
178+
179+
func (req *FilterChildrenObjectsRequest) WithListObjects(objects *unstructured.UnstructuredList) *FilterChildrenObjectsRequest {
180+
req.listObjects = objects
181+
return req
182+
}
183+
184+
func (req *FilterChildrenObjectsRequest) WithNamespace(namespace string) *FilterChildrenObjectsRequest {
185+
req.namespace = namespace
186+
return req
187+
}
188+
189+
func (req *FilterChildrenObjectsRequest) WithParentGvk(parentGvk schema.GroupVersionKind) *FilterChildrenObjectsRequest {
190+
req.parentGvk = parentGvk
191+
return req
192+
}
193+
194+
func (req *FilterChildrenObjectsRequest) WithParentName(parentName string) *FilterChildrenObjectsRequest {
195+
req.parentName = parentName
196+
return req
197+
}

0 commit comments

Comments
 (0)