Skip to content

Commit a435581

Browse files
Merge pull request #280 from devtron-labs/rename-secret-to-cm-cluster
chore: when a cluster event occurs, create config map instead of secret
2 parents 3c18cce + 2650af5 commit a435581

26 files changed

Lines changed: 3396 additions & 2031 deletions

File tree

common-lib/informer/bean.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package informer
1818

1919
const (
20-
ClusterModifyEventSecretType = "cluster.request/modify"
21-
ClusterActionAdd = "add"
22-
ClusterActionUpdate = "update"
23-
ClusterActionDelete = "delete"
24-
SecretFieldAction = "action"
25-
SecretFieldClusterId = "cluster_id"
20+
ClusterModifyEventSecretType = "cluster.request/modify"
21+
ClusterModifyEventSecretTypeKey = "type"
22+
ClusterActionAdd = "add"
23+
ClusterActionUpdate = "update"
24+
ClusterActionDelete = "delete"
25+
CmFieldAction = "action"
26+
CmFieldClusterId = "cluster_id"
27+
ClusterModifyEventCmLabelKeyValue = "type=devtron.ai-cluster-request-modify"
28+
ClusterModifyEventCmLabelValue = "devtron.ai-cluster-request-modify"
2629
)
2730

2831
const (

common-lib/utils/k8s/K8sService.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package k8s
1919
import (
2020
"context"
2121
"flag"
22+
"github.com/devtron-labs/common-lib/utils/k8s/configMap"
2223
"go.uber.org/zap"
2324
"io"
2425
batchV1 "k8s.io/api/batch/v1"
@@ -76,7 +77,9 @@ type K8sService interface {
7677
PatchConfigMap(namespace string, clusterConfig *ClusterConfig, name string, data map[string]interface{}) (*v1.ConfigMap, error)
7778
UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error)
7879
CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error)
80+
CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.ConfigMapOption) (*v1.ConfigMap, error)
7981
GetConfigMap(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error)
82+
DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error
8083
GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error)
8184
GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error)
8285
CreateNsIfNotExists(namespace string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error)

common-lib/utils/k8s/K8sUtil.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/devtron-labs/common-lib/utils"
2525
http2 "github.com/devtron-labs/common-lib/utils/http"
2626
"github.com/devtron-labs/common-lib/utils/k8s/commonBean"
27+
"github.com/devtron-labs/common-lib/utils/k8s/configMap"
2728
"io"
2829
"k8s.io/client-go/dynamic"
2930
"k8s.io/client-go/kubernetes/scheme"
@@ -237,6 +238,27 @@ func (impl *K8sServiceImpl) CreateConfigMap(namespace string, cm *v1.ConfigMap,
237238
}
238239
}
239240

241+
func (impl *K8sServiceImpl) CreateConfigMapObject(name, namespace string, client *v12.CoreV1Client, opts ...configMap.ConfigMapOption) (*v1.ConfigMap, error) {
242+
configMap := &v1.ConfigMap{
243+
ObjectMeta: metav1.ObjectMeta{
244+
Name: name,
245+
},
246+
}
247+
for _, option := range opts {
248+
option(configMap)
249+
}
250+
return impl.CreateConfigMap(namespace, configMap, client)
251+
}
252+
253+
func (impl *K8sServiceImpl) DeleteConfigMap(namespace string, name string, client *v12.CoreV1Client) error {
254+
err := client.ConfigMaps(namespace).Delete(context.Background(), name, metav1.DeleteOptions{})
255+
if err != nil {
256+
impl.logger.Errorw("error in deleting cm", "namespace", namespace, "err", err)
257+
return err
258+
}
259+
return nil
260+
}
261+
240262
func (impl *K8sServiceImpl) UpdateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) {
241263
cm, err := client.ConfigMaps(namespace).Update(context.Background(), cm, metav1.UpdateOptions{})
242264
if err != nil {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package configMap
2+
3+
import (
4+
v1 "k8s.io/api/core/v1"
5+
)
6+
7+
type ConfigMapOption func(*v1.ConfigMap)
8+
9+
// WithLabels adds labels to a ConfigMap
10+
func WithLabels(labels map[string]string) ConfigMapOption {
11+
return func(cm *v1.ConfigMap) {
12+
if labels != nil && len(labels) > 0 {
13+
cm.ObjectMeta.Labels = labels
14+
}
15+
}
16+
}
17+
18+
// WithAnnotations adds annotations to a ConfigMap
19+
func WithAnnotations(annotations map[string]string) ConfigMapOption {
20+
return func(cm *v1.ConfigMap) {
21+
if annotations != nil && len(annotations) > 0 {
22+
cm.ObjectMeta.Annotations = annotations
23+
}
24+
}
25+
}
26+
27+
// WithData adds string data to a ConfigMap
28+
func WithData(data map[string]string) ConfigMapOption {
29+
return func(cm *v1.ConfigMap) {
30+
if data != nil && len(data) > 0 {
31+
cm.Data = data
32+
}
33+
}
34+
}
35+
36+
// WithBinaryData adds binary data to a ConfigMap
37+
func WithBinaryData(binaryData map[string][]byte) ConfigMapOption {
38+
return func(cm *v1.ConfigMap) {
39+
if binaryData != nil && len(binaryData) > 0 {
40+
cm.BinaryData = binaryData
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)