-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from castai/store-metadata-in-secret
feat: store cluster id in a metadata secret
- Loading branch information
Showing
5 changed files
with
185 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package metadata | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
"castai-agent/internal/config" | ||
) | ||
|
||
type Store interface { | ||
// StoreMetadataConfigMap stores relevant agent runtime metadata in a config map. | ||
StoreMetadataConfigMap(ctx context.Context, metadata *Metadata) error | ||
} | ||
|
||
var _ Store = (*StoreImpl)(nil) | ||
|
||
type StoreImpl struct { | ||
clientset kubernetes.Interface | ||
cfg config.Config | ||
} | ||
|
||
func New(clientset kubernetes.Interface, cfg config.Config) *StoreImpl { | ||
return &StoreImpl{ | ||
clientset: clientset, | ||
cfg: cfg, | ||
} | ||
} | ||
|
||
func (s *StoreImpl) StoreMetadataConfigMap(ctx context.Context, metadata *Metadata) error { | ||
if !s.cfg.MetadataStore.Enabled { | ||
return nil | ||
} | ||
|
||
configMapNamespace := s.cfg.MetadataStore.ConfigMapNamespace | ||
configMap := &corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: s.cfg.MetadataStore.ConfigMapName, | ||
Namespace: configMapNamespace, | ||
}, | ||
Data: map[string]string{ | ||
"CLUSTER_ID": metadata.ClusterID, | ||
}, | ||
} | ||
|
||
_, err := s.clientset.CoreV1().ConfigMaps(configMapNamespace).Update(ctx, configMap, metav1.UpdateOptions{}) | ||
if errors.IsNotFound(err) { | ||
_, err = s.clientset.CoreV1().ConfigMaps(configMapNamespace).Create(ctx, configMap, metav1.CreateOptions{}) | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package metadata | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
fakeclientset "k8s.io/client-go/kubernetes/fake" | ||
|
||
"castai-agent/internal/config" | ||
) | ||
|
||
func TestStoreImpl_StoreMetadataConfigMap(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg config.Config | ||
metadata *Metadata | ||
existingConfigMap *corev1.ConfigMap | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "should store metadata successfully when config map does not exist", | ||
cfg: config.Config{ | ||
MetadataStore: &config.MetadataStoreConfig{ | ||
Enabled: true, | ||
ConfigMapName: "castai-agent-metadata", | ||
ConfigMapNamespace: "default", | ||
}, | ||
}, | ||
metadata: &Metadata{ | ||
ClusterID: "test-cluster-id", | ||
}, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "should store metadata successfully when config map exists", | ||
cfg: config.Config{ | ||
MetadataStore: &config.MetadataStoreConfig{ | ||
Enabled: true, | ||
ConfigMapName: "castai-agent-metadata", | ||
ConfigMapNamespace: "default", | ||
}, | ||
}, | ||
metadata: &Metadata{ | ||
ClusterID: "test-cluster-id", | ||
}, | ||
existingConfigMap: &corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "castai-agent-metadata", | ||
Namespace: "default", | ||
}, | ||
Data: map[string]string{ | ||
"CLUSTER_ID": "original-test-cluster-id", | ||
}, | ||
}, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "should not store metadata when store is disabled", | ||
cfg: config.Config{ | ||
MetadataStore: &config.MetadataStoreConfig{ | ||
Enabled: false, | ||
}, | ||
}, | ||
metadata: &Metadata{ | ||
ClusterID: "test-cluster-id", | ||
}, | ||
expectedError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := require.New(t) | ||
clientset := fakeclientset.NewSimpleClientset() | ||
store := New(clientset, tt.cfg) | ||
|
||
if tt.existingConfigMap != nil { | ||
_, err := clientset.CoreV1().ConfigMaps(tt.cfg.MetadataStore.ConfigMapNamespace).Create(context.Background(), tt.existingConfigMap, metav1.CreateOptions{}) | ||
r.NoError(err) | ||
} | ||
|
||
err := store.StoreMetadataConfigMap(context.Background(), tt.metadata) | ||
if tt.expectedError { | ||
r.Error(err) | ||
} else { | ||
r.NoError(err) | ||
if tt.cfg.MetadataStore.Enabled { | ||
configMap, err := clientset.CoreV1().ConfigMaps(tt.cfg.MetadataStore.ConfigMapNamespace).Get(context.Background(), tt.cfg.MetadataStore.ConfigMapName, metav1.GetOptions{}) | ||
r.NoError(err) | ||
r.Equal(tt.metadata.ClusterID, configMap.Data["CLUSTER_ID"]) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package metadata | ||
|
||
type Metadata struct { | ||
ClusterID string | ||
} |