Skip to content

Commit

Permalink
No need to list all secret in the namespace to find just one (#5669)
Browse files Browse the repository at this point in the history
Signed-off-by: Jirka Kremser <jiri.kremser@gmail.com>
  • Loading branch information
jkremser committed Apr 16, 2024
1 parent bcaf5c0 commit ee4ab1b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ New deprecation(s):
- **General**: Introduce ENABLE_OPENTELEMETRY in deploying/testing process ([#5375](https://github.com/kedacore/keda/issues/5375)|[#5578](https://github.com/kedacore/keda/issues/5578))
- **General**: Migrate away from unmaintained golang/mock and use uber/gomock ([#5440](https://github.com/kedacore/keda/issues/5440))
- **General**: Minor refactor to reduce copy/paste code in ScaledObject webhook ([#5397](https://github.com/kedacore/keda/issues/5397))
- **General**: No need to list all secret in the namespace to find just one ([#5669](https://github.com/kedacore/keda/pull/5669))
- **Kafka**: Expose GSSAPI service name ([#5474](https://github.com/kedacore/keda/issues/5474))

## v2.13.1
Expand Down
27 changes: 13 additions & 14 deletions pkg/certificates/certificate_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/go-logr/logr"
"github.com/open-policy-agent/cert-controller/pkg/rotator"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -109,26 +110,24 @@ func getDNSNames(service, k8sClusterDomain string) []string {

// ensureSecret ensures that the secret used for storing TLS certificates exists
func (cm CertManager) ensureSecret(ctx context.Context, mgr manager.Manager, secretName string) error {
secrets := &corev1.SecretList{}
secret := &corev1.Secret{}
kedaNamespace := kedautil.GetPodNamespace()
opt := &client.ListOptions{
objKey := client.ObjectKey{
Namespace: kedaNamespace,
Name: secretName,
}

err := mgr.GetAPIReader().List(ctx, secrets, opt)
create := false
err := mgr.GetAPIReader().Get(ctx, objKey, secret)
if err != nil {
cm.Logger.Error(err, "unable to check secrets")
return err
}

exists := false
for _, secret := range secrets.Items {
if secret.Name == secretName {
exists = true
break
if errors.IsNotFound(err) {
create = true
} else {
cm.Logger.Error(err, "unable to check secret")
return err
}
}
if !exists {

if create {
secret := &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: secretName,
Expand Down

0 comments on commit ee4ab1b

Please sign in to comment.