-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
certrotationcontroller: ensure that secrets/configmaps are unique
Collect a list of secrets/configmaps tracked in controllers to make sure any secret/configmap is not being declared twice to avoid races when updating it
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 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
63 changes: 63 additions & 0 deletions
63
pkg/operator/certrotationcontroller/certrotationcontroller_test.go
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,63 @@ | ||
package certrotationcontroller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"k8s.io/client-go/kubernetes/fake" | ||
|
||
operatorv1 "github.com/openshift/api/operator/v1" | ||
"github.com/openshift/cluster-kube-controller-manager-operator/pkg/operator/operatorclient" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/openshift/library-go/pkg/operator/certrotation" | ||
"github.com/openshift/library-go/pkg/operator/events" | ||
"github.com/openshift/library-go/pkg/operator/v1helpers" | ||
) | ||
|
||
func TestNewCertRotationControllerHasUniqueSigners(t *testing.T) { | ||
ctx := context.Background() | ||
kubeClient := fake.NewSimpleClientset() | ||
|
||
operatorClient := v1helpers.NewFakeStaticPodOperatorClient(&operatorv1.StaticPodOperatorSpec{OperatorSpec: operatorv1.OperatorSpec{ManagementState: operatorv1.Managed}}, &operatorv1.StaticPodOperatorStatus{}, nil, nil) | ||
certRotationScale, err := certrotation.GetCertRotationScale(ctx, kubeClient, operatorclient.GlobalUserSpecifiedConfigNamespace) | ||
require.NoError(t, err) | ||
|
||
kubeInformersForNamespaces := v1helpers.NewKubeInformersForNamespaces( | ||
kubeClient, | ||
operatorclient.GlobalMachineSpecifiedConfigNamespace, | ||
operatorclient.GlobalUserSpecifiedConfigNamespace, | ||
operatorclient.OperatorNamespace, | ||
operatorclient.TargetNamespace, | ||
) | ||
|
||
eventRecorder := events.NewInMemoryRecorder("") | ||
c, err := NewCertRotationController( | ||
v1helpers.CachedSecretGetter(kubeClient.CoreV1(), kubeInformersForNamespaces), | ||
v1helpers.CachedConfigMapGetter(kubeClient.CoreV1(), kubeInformersForNamespaces), | ||
operatorClient, | ||
kubeInformersForNamespaces, | ||
eventRecorder, | ||
certRotationScale) | ||
require.NoError(t, err) | ||
|
||
require.NotEmpty(t, c.certRotators) | ||
|
||
sets := map[string][]metav1.ObjectMeta{ | ||
"Secret": c.controlledSecrets, | ||
"ConfigMap": c.controlledConfigMaps, | ||
} | ||
for objType, set := range sets { | ||
slice := make(map[string]bool) | ||
for _, objMeta := range set { | ||
objKey := fmt.Sprintf("%s/%s", objMeta.Name, objMeta.Namespace) | ||
if _, found := slice[objKey]; !found { | ||
slice[objKey] = true | ||
} else { | ||
t.Fatalf("%s %s is being managed by two controllers", objType, objKey) | ||
} | ||
} | ||
} | ||
} |