Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
fix tests and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
nacx committed Feb 28, 2024
1 parent e7c1e60 commit 30cbcc7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 112 deletions.
43 changes: 0 additions & 43 deletions internal/k8s/client.go

This file was deleted.

41 changes: 0 additions & 41 deletions internal/k8s/client_test.go

This file was deleted.

47 changes: 28 additions & 19 deletions internal/k8s/secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
_ run.PreRunner = (*SecretController)(nil)
_ run.ServiceContext = (*SecretController)(nil)

ErrLoadingConfig = errors.New("error loading kube config")
ErrCrossNamespaceSecretRef = errors.New("cross-namespace secret reference is not allowed")
)

Expand Down Expand Up @@ -104,25 +105,8 @@ func (s *SecretController) PreRun() error {
}

// Collect the k8s secrets that are used in the configuration
s.secrets = make(map[string][]*oidcv1.OIDCConfig)
for _, c := range s.config.Chains {
for _, f := range c.Filters {
oidcCfg, isOIDCConf := f.Type.(*configv1.Filter_Oidc)
if !isOIDCConf ||
oidcCfg.Oidc.GetClientSecretRef() == nil ||
oidcCfg.Oidc.GetClientSecretRef().GetName() == "" {
continue
}

ref := oidcCfg.Oidc.GetClientSecretRef()
if ref.Namespace != "" && ref.Namespace != s.namespace {
return fmt.Errorf("%w: secret reference namespace %s does not match the current namespace %s",
ErrCrossNamespaceSecretRef, ref.Namespace, s.namespace)
}

key := secretNamespacedName(ref, s.namespace).String()
s.secrets[key] = append(s.secrets[key], oidcCfg.Oidc)
}
if err = s.loadSecrets(); err != nil {
return err
}

// Load the k8s configuration from in-cluster environment
Expand Down Expand Up @@ -173,6 +157,31 @@ func (s *SecretController) ServeContext(ctx context.Context) error {
return nil
}

// loadSecrets loads the secrets from the configuration and stores them in the secrets map.
func (s *SecretController) loadSecrets() error {
s.secrets = make(map[string][]*oidcv1.OIDCConfig)
for _, c := range s.config.Chains {
for _, f := range c.Filters {
oidcCfg, isOIDCConf := f.Type.(*configv1.Filter_Oidc)
if !isOIDCConf ||
oidcCfg.Oidc.GetClientSecretRef() == nil ||
oidcCfg.Oidc.GetClientSecretRef().GetName() == "" {
continue
}

ref := oidcCfg.Oidc.GetClientSecretRef()
if ref.Namespace != "" && ref.Namespace != s.namespace {
return fmt.Errorf("%w: secret reference namespace %s does not match the current namespace %s",
ErrCrossNamespaceSecretRef, ref.Namespace, s.namespace)
}

key := secretNamespacedName(ref, s.namespace).String()
s.secrets[key] = append(s.secrets[key], oidcCfg.Oidc)
}
}
return nil
}

func secretNamespacedName(secretRef *oidcv1.OIDCConfig_SecretReference, currentNamespace string) types.NamespacedName {
return types.NamespacedName{
Namespace: currentNamespace,
Expand Down
9 changes: 9 additions & 0 deletions internal/k8s/secret_controller_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ const (
defaultTick = time.Millisecond * 20
)

func TestErrorLoadingConfig(t *testing.T) {
t.Setenv("KUBECONFIG", "non-existent-file")
sc := NewSecretController(loadTestConf(t, "testdata/oidc-with-secret-ref.json"))
sc.namespace = "default"

Check failure on line 42 in internal/k8s/secret_controller_lifecycle_test.go

View workflow job for this annotation

GitHub Actions / lint

string `default` has 4 occurrences, make it a constant (goconst)

require.ErrorIs(t, sc.PreRun(), ErrLoadingConfig)
}

func TestManagerStarts(t *testing.T) {
var (
g = run.Group{Logger: telemetry.NoopLogger()}
Expand Down Expand Up @@ -116,6 +124,7 @@ func startEnv(t *testing.T) *rest.Config {
env := &envtest.Environment{}
cfg, err := env.Start()
require.NoError(t, err)

t.Cleanup(func() {
require.NoError(t, env.Stop())
})
Expand Down
13 changes: 4 additions & 9 deletions internal/k8s/secret_controller_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,20 @@ func TestOIDCProcessWithKubernetesSecret(t *testing.T) {
kubeClient := fake.NewClientBuilder().WithLists(secrets).Build()
controller := NewSecretController(originalConf)
controller.namespace = "default"

// pre-run the controller
err := controller.PreRun()
require.ErrorIs(t, err, tt.err)

// replace the k8s client with the fake client for testing
controller.k8sClient = kubeClient
controller.k8sClient = kubeClient // set the k8s client with the fake client for testing
require.ErrorIs(t, controller.loadSecrets(), tt.err)

// reconcile the secrets
for _, secret := range secrets.Items {
_, err = controller.Reconcile(context.Background(), ctrl.Request{
_, err := controller.Reconcile(context.Background(), ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: secret.Namespace,
Name: secret.Name,
},
})
require.NoError(t, err)
}
_, err = controller.Reconcile(context.Background(), ctrl.Request{
_, err := controller.Reconcile(context.Background(), ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: "default",
Name: "non-existing-secret",
Expand Down

0 comments on commit 30cbcc7

Please sign in to comment.