Skip to content

Commit

Permalink
feat: [TKC-1570] disable secret var (#235)
Browse files Browse the repository at this point in the history
* feat: disable secret var

* fix: change method parameters
  • Loading branch information
vsukhin authored Mar 21, 2024
1 parent 03a6d2d commit fdc6429
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 80 deletions.
32 changes: 16 additions & 16 deletions pkg/client/tests/v3/mock_tests.go

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

54 changes: 29 additions & 25 deletions pkg/client/tests/v3/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ type Interface interface {
List(selector string) (*testsv3.TestList, error)
ListLabels() (map[string][]string, error)
Get(name string) (*testsv3.Test, error)
Create(test *testsv3.Test, options ...Option) (*testsv3.Test, error)
Update(test *testsv3.Test, options ...Option) (*testsv3.Test, error)
Create(test *testsv3.Test, disableSecretCreation bool, options ...Option) (*testsv3.Test, error)
Update(test *testsv3.Test, disableSecretCreation bool, options ...Option) (*testsv3.Test, error)
Delete(name string) error
DeleteAll() error
CreateTestSecrets(test *testsv3.Test) error
UpdateTestSecrets(test *testsv3.Test) error
CreateTestSecrets(test *testsv3.Test, disableSecretCreation bool) error
UpdateTestSecrets(test *testsv3.Test, disableSecretCreation bool) error
LoadTestVariablesSecret(test *testsv3.Test) (*corev1.Secret, error)
GetCurrentSecretUUID(testName string) (string, error)
GetSecretTestVars(testName, secretUUID string) (map[string]string, error)
Expand All @@ -67,7 +67,7 @@ func NewDeleteDependenciesError(testName string, allErrors []error) error {
return &DeleteDependenciesError{testName: testName, allErrors: allErrors}
}

// Option contain test source options
// Option contain test options
type Option struct {
Secrets map[string]string
}
Expand Down Expand Up @@ -161,8 +161,8 @@ func (s TestsClient) Get(name string) (*testsv3.Test, error) {
}

// Create creates new Test and coupled resources
func (s TestsClient) Create(test *testsv3.Test, options ...Option) (*testsv3.Test, error) {
err := s.CreateTestSecrets(test)
func (s TestsClient) Create(test *testsv3.Test, disableSecretCreation bool, options ...Option) (*testsv3.Test, error) {
err := s.CreateTestSecrets(test, disableSecretCreation)
if err != nil {
return nil, err
}
Expand All @@ -175,8 +175,8 @@ func (s TestsClient) Create(test *testsv3.Test, options ...Option) (*testsv3.Tes
}
}

secretName := secret.GetMetadataName(test.Name, secretKind)
if len(secrets) != 0 {
secretName := secret.GetMetadataName(test.Name, secretKind)
if err := s.secretClient.Create(secretName, test.Labels, secrets); err != nil {
return nil, err
}
Expand All @@ -190,8 +190,8 @@ func (s TestsClient) Create(test *testsv3.Test, options ...Option) (*testsv3.Tes
}

// Update updates existing Test and coupled resources
func (s TestsClient) Update(test *testsv3.Test, options ...Option) (*testsv3.Test, error) {
err := s.UpdateTestSecrets(test)
func (s TestsClient) Update(test *testsv3.Test, disableSecretCreation bool, options ...Option) (*testsv3.Test, error) {
err := s.UpdateTestSecrets(test, disableSecretCreation)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -285,7 +285,7 @@ func (s TestsClient) DeleteAll() error {
}

// CreateTestSecrets creates corresponding test vars secrets
func (s TestsClient) CreateTestSecrets(test *testsv3.Test) error {
func (s TestsClient) CreateTestSecrets(test *testsv3.Test, disableSecretCreation bool) error {
secretName := secretName(test.Name)
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -301,7 +301,7 @@ func (s TestsClient) CreateTestSecrets(test *testsv3.Test) error {
secret.Labels[key] = value
}

if err := testVarsToSecret(test, secret); err != nil {
if err := testVarsToSecret(test, secret, disableSecretCreation); err != nil {
return err
}

Expand All @@ -315,7 +315,7 @@ func (s TestsClient) CreateTestSecrets(test *testsv3.Test) error {
return nil
}

func (s TestsClient) UpdateTestSecrets(test *testsv3.Test) error {
func (s TestsClient) UpdateTestSecrets(test *testsv3.Test, disableSecretCreation bool) error {
secret, err := s.LoadTestVariablesSecret(test)
secretExists := !errors.IsNotFound(err)
if err != nil && secretExists {
Expand All @@ -337,7 +337,7 @@ func (s TestsClient) UpdateTestSecrets(test *testsv3.Test) error {
secret.Labels[key] = value
}

if err = testVarsToSecret(test, secret); err != nil {
if err = testVarsToSecret(test, secret, disableSecretCreation); err != nil {
return err
}

Expand Down Expand Up @@ -440,7 +440,7 @@ func (s TestsClient) UpdateStatus(test *testsv3.Test) error {
}

// testVarsToSecret loads secrets data passed into Test CRD and remove plain text data
func testVarsToSecret(test *testsv3.Test, secret *corev1.Secret) error {
func testVarsToSecret(test *testsv3.Test, secret *corev1.Secret, disablesecretCreation bool) error {
if secret.StringData == nil {
secret.StringData = map[string]string{}
}
Expand All @@ -463,18 +463,22 @@ func testVarsToSecret(test *testsv3.Test, secret *corev1.Secret) error {
},
}
} else {
secret.StringData[v.Name] = v.Value
secretMap[v.Name] = v.Value
// clear passed test variable secret value and save as reference o secret
v.Value = ""
v.ValueFrom = corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: v.Name,
LocalObjectReference: corev1.LocalObjectReference{
Name: secret.Name,
if !disablesecretCreation {
// save as reference to secret
secret.StringData[v.Name] = v.Value
secretMap[v.Name] = v.Value
v.ValueFrom = corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: v.Name,
LocalObjectReference: corev1.LocalObjectReference{
Name: secret.Name,
},
},
},
}
}

// clear passed test variable secret value
v.Value = ""
}

test.Spec.ExecutionRequest.Variables[k] = v
Expand Down
32 changes: 16 additions & 16 deletions pkg/client/testsuites/v3/mock_testsuites.go

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

Loading

0 comments on commit fdc6429

Please sign in to comment.