Skip to content

Commit

Permalink
spoke token controller unit test fix along with app addon ns customiz…
Browse files Browse the repository at this point in the history
…ation (#398)

Signed-off-by: Xiangjing Li <xiangli@redhat.com>
  • Loading branch information
xiangjingli authored May 10, 2024
1 parent f9dc1e6 commit 6c99bb5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
29 changes: 20 additions & 9 deletions pkg/controller/spoketoken/spoke_toke_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"open-cluster-management.io/multicloud-operators-subscription/pkg/utils"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -37,7 +38,7 @@ const (
var (
sakey = types.NamespacedName{
Name: "application-manager",
Namespace: "open-cluster-management-agent-addon",
Namespace: utils.GetComponentNamespace(),
}

secretkey = types.NamespacedName{
Expand All @@ -53,14 +54,14 @@ var (

agentNamespace = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "open-cluster-management-agent-addon",
Name: utils.GetComponentNamespace(),
},
}

sa1 = &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "application-manager",
Namespace: "open-cluster-management-agent-addon",
Namespace: utils.GetComponentNamespace(),
},
Secrets: []corev1.ObjectReference{
{
Expand All @@ -72,7 +73,7 @@ var (
saToBeIgnored = &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "sa-to-be-ignored",
Namespace: "open-cluster-management-agent-addon",
Namespace: utils.GetComponentNamespace(),
},
Secrets: []corev1.ObjectReference{
{
Expand All @@ -81,13 +82,18 @@ var (
},
}

dockerSecretKey = types.NamespacedName{
Name: "application-manager-dockercfg-1",
Namespace: utils.GetComponentNamespace(),
}

dockerSecret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "application-manager-dockercfg-1",
Namespace: "open-cluster-management-agent-addon",
Namespace: utils.GetComponentNamespace(),
Annotations: map[string]string{
"kubernetes.io/service-account.name": "application-manager",
"openshift.io/token-secret.name": "application-manager-token-1",
"openshift.io/token-secret.name": "application-manager-dockercfg-1",
"openshift.io/token-secret.value": "dummy-1",
},
},
Expand All @@ -96,7 +102,7 @@ var (
secret1 = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "application-manager-token-1",
Namespace: "open-cluster-management-agent-addon",
Namespace: utils.GetComponentNamespace(),
Annotations: map[string]string{"kubernetes.io/service-account.name": "application-manager"},
},
Type: corev1.SecretTypeServiceAccountToken,
Expand All @@ -105,14 +111,14 @@ var (
sa2 = &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "application-manager",
Namespace: "open-cluster-management-agent-addon",
Namespace: utils.GetComponentNamespace(),
},
}
)

var expectedRequest = reconcile.Request{NamespacedName: sakey}

const timeout = time.Second * 5
const timeout = time.Second * 10

func TestReconcile(t *testing.T) {
g := gomega.NewGomegaWithT(t)
Expand Down Expand Up @@ -159,6 +165,11 @@ func TestReconcile(t *testing.T) {
g.Expect(c.Create(context.TODO(), dockerSecret)).NotTo(gomega.HaveOccurred())
defer c.Delete(context.TODO(), dockerSecret)

time.Sleep(time.Second * 5)

newDockerSecret := &corev1.Secret{}
g.Expect(c.Get(context.TODO(), dockerSecretKey, newDockerSecret)).NotTo(gomega.HaveOccurred())

// Create the addon agent service account token secret and reconcile.
g.Expect(c.Create(context.TODO(), secret1)).NotTo(gomega.HaveOccurred())
defer c.Delete(context.TODO(), secret1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/spoketoken/spoke_token_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (r *ReconcileAgentToken) getServiceAccountTokenSecret() string {

err = r.Client.Get(context.TODO(), types.NamespacedName{Name: secret.Name, Namespace: appAddonNS}, dockerSecret)
if err != nil {
klog.Error(err.Error())
klog.Errorf("secret not found: %v/%v, err: %v", appAddonNS, secret.Name, err.Error())
return ""
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func GetComponentNamespace() string {
nsBytes, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")

if err != nil || len(nsBytes) == 0 {
klog.Errorf("failed to get app addon pod namespace use. error: %v", err)
klog.Errorf("failed to get app addon pod namespace. error: %v", err)

addonNameSpace = "open-cluster-management-agent-addon"
} else {
Expand Down

0 comments on commit 6c99bb5

Please sign in to comment.