1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-14 11:57:59 +00:00
external-secrets/e2e/framework/eso.go
Alexander Chernov 280964f84e
fix: dependent kind=secret are not recreated in case of deletion. (#349)
* chore: whitespace, typos, superflous aliases

* fix: deleted child secret is not recreated straight away.

* fix: e2e run
2021-09-09 11:14:17 +02:00

73 lines
2.1 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package framework
import (
"bytes"
"context"
"encoding/json"
"time"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
)
// WaitForSecretValue waits until a secret comes into existence and compares the secret.Data
// with the provided values.
func (f *Framework) WaitForSecretValue(namespace, name string, expected *v1.Secret) (*v1.Secret, error) {
secret := &v1.Secret{}
err := wait.PollImmediate(time.Second*2, time.Minute*2, func() (bool, error) {
err := f.CRClient.Get(context.Background(), types.NamespacedName{
Namespace: namespace,
Name: name,
}, secret)
if apierrors.IsNotFound(err) {
return false, nil
}
return equalSecrets(expected, secret), nil
})
return secret, err
}
func equalSecrets(exp, ts *v1.Secret) bool {
if exp.Type != ts.Type {
return false
}
expLabels, _ := json.Marshal(exp.ObjectMeta.Labels)
tsLabels, _ := json.Marshal(ts.ObjectMeta.Labels)
if !bytes.Equal(expLabels, tsLabels) {
return false
}
// secret contains data hash property which must be ignored
delete(ts.ObjectMeta.Annotations, esv1alpha1.AnnotationDataHash)
if len(ts.ObjectMeta.Annotations) == 0 {
ts.ObjectMeta.Annotations = nil
}
expAnnotations, _ := json.Marshal(exp.ObjectMeta.Annotations)
tsAnnotations, _ := json.Marshal(ts.ObjectMeta.Annotations)
if !bytes.Equal(expAnnotations, tsAnnotations) {
return false
}
expData, _ := json.Marshal(exp.Data)
tsData, _ := json.Marshal(ts.Data)
return bytes.Equal(expData, tsData)
}