-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get Secrets for Service Manager (#655)
* Add interface for cluster object provider * Interface segregation * Add context param to interfaces * Add NamespaceProvider * Add SecretProvider * Add ServiceInstanceProvider * tweaks - use pointers, name changes * Include ServiceInstanceProvider in SecretProvider * Get secrets from secret ref in SI * Fix provider interface * Fix constructor * Add NamespaceProvider unit tests * Fix ServiceInstanceProvider constructor * Fix secret reference filter * Add unit tests for service instance provider * Fix secrets fetching * Log warning when SecretProvider does not find btp operator secrets * Add SecretProvider unit tests * Apply suggestions from code review
- Loading branch information
Showing
13 changed files
with
772 additions
and
24 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
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
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,44 @@ | ||
package clusterobject | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log/slog" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const namespaceProviderName = "NamespaceProvider" | ||
|
||
type NamespaceProvider struct { | ||
client.Reader | ||
logger *slog.Logger | ||
} | ||
|
||
func NewNamespaceProvider(reader client.Reader, logger *slog.Logger) *NamespaceProvider { | ||
logger = logger.With(logComponentNameKey, namespaceProviderName) | ||
|
||
return &NamespaceProvider{ | ||
Reader: reader, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (p *NamespaceProvider) All(ctx context.Context) (*v1.NamespaceList, error) { | ||
p.logger.Info("fetching all namespaces") | ||
|
||
namespaces := &v1.NamespaceList{} | ||
if err := p.Reader.List(ctx, namespaces); err != nil { | ||
p.logger.Error("failed to fetch all namespaces", "error", err) | ||
return nil, err | ||
} | ||
|
||
if len(namespaces.Items) == 0 { | ||
err := errors.New("no namespaces found") | ||
p.logger.Error(err.Error()) | ||
return nil, err | ||
} | ||
|
||
return namespaces, nil | ||
} |
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,69 @@ | ||
package clusterobject | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
func TestNamespaceProvider(t *testing.T) { | ||
// given | ||
logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) | ||
|
||
t.Run("should fetch all namespaces", func(t *testing.T) { | ||
// given | ||
namespaces := initNamespaces() | ||
k8sClient := fake.NewClientBuilder().WithLists(namespaces).Build() | ||
nsProvider := NewNamespaceProvider(k8sClient, logger) | ||
|
||
// when | ||
nsList, err := nsProvider.All(context.TODO()) | ||
|
||
// then | ||
if err != nil { | ||
t.Errorf("Error while fetching namespaces: %s", err) | ||
} | ||
assert.Len(t, nsList.Items, 3) | ||
}) | ||
|
||
t.Run("should return error when no namespaces found", func(t *testing.T) { | ||
// given | ||
k8sClient := fake.NewClientBuilder().Build() | ||
nsProvider := NewNamespaceProvider(k8sClient, logger) | ||
|
||
// when | ||
_, err := nsProvider.All(context.TODO()) | ||
|
||
// then | ||
require.Error(t, err) | ||
}) | ||
} | ||
|
||
func initNamespaces() *corev1.NamespaceList { | ||
return &corev1.NamespaceList{ | ||
Items: []corev1.Namespace{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kube-system", | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
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,17 @@ | ||
package clusterobject | ||
|
||
import ( | ||
"context" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const logComponentNameKey = "component" | ||
|
||
type ClusterScopedProvider[T client.ObjectList] interface { | ||
All(ctx context.Context) (T, error) | ||
} | ||
|
||
type NamespacedProvider[T client.Object] interface { | ||
GetByNameAndNamespace(ctx context.Context, name, namespace string) (T, error) | ||
} |
Oops, something went wrong.