Skip to content

Commit

Permalink
Remove warnings when running "antrea-controller --version" outside of…
Browse files Browse the repository at this point in the history
… K8s (#5993)

We stop calling `env.GetAntreaNamespace()` unconditionally to initialize a global var.
This avoids the warning when we just want to check the version for a container image.

Fixes #5990

Signed-off-by: Griffin <prakritimandal611@gmail.com>
  • Loading branch information
prakrit55 committed Feb 22, 2024
1 parent cb52631 commit d7355e8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ func NewCSRApprovingController(client clientset.Interface, csrInformer cache.Sha
csrListerSynced: csrInformer.HasSynced,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.NewItemExponentialFailureRateLimiter(minRetryDelay, maxRetryDelay), "certificateSigningRequest"),
approvers: []approver{
&ipsecCSRApprover{
client: client,
},
newIPsecCSRApprover(client),
},
}
csrInformer.AddEventHandlerWithResyncPeriod(
Expand Down
24 changes: 16 additions & 8 deletions pkg/controller/certificatesigningrequest/ipsec_csr_approver.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,9 @@ const (
ipsecCSRApproverName = "AntreaIPsecCSRApprover"
)

var (
antreaAgentServiceAccountName = strings.Join([]string{
"system", "serviceaccount", env.GetAntreaNamespace(), "antrea-agent",
}, ":")
)

type ipsecCSRApprover struct {
client clientset.Interface
client clientset.Interface
antreaAgentServiceAccountName string
}

var ipsecTunnelUsages = sets.New[string](
Expand All @@ -54,6 +49,19 @@ var ipsecTunnelUsages = sets.New[string](

var _ approver = (*ipsecCSRApprover)(nil)

func getAntreaAgentServiceAccount() string {
return strings.Join([]string{
"system", "serviceaccount", env.GetAntreaNamespace(), "antrea-agent",
}, ":")
}

func newIPsecCSRApprover(client clientset.Interface) *ipsecCSRApprover {
return &ipsecCSRApprover{
client: client,
antreaAgentServiceAccountName: getAntreaAgentServiceAccount(),
}
}

func (ic *ipsecCSRApprover) recognize(csr *certificatesv1.CertificateSigningRequest) bool {
return csr.Spec.SignerName == antreaapis.AntreaIPsecCSRSignerName
}
Expand Down Expand Up @@ -123,7 +131,7 @@ func (ic *ipsecCSRApprover) verifyCertificateRequest(req *x509.CertificateReques
}

func (ic *ipsecCSRApprover) verifyIdentity(nodeName string, csr *certificatesv1.CertificateSigningRequest) error {
if csr.Spec.Username != antreaAgentServiceAccountName {
if csr.Spec.Username != ic.antreaAgentServiceAccountName {
return errUserUnauthorized
}
podNameValues, podUIDValues := csr.Spec.Extra[sautil.PodNameKey], csr.Spec.Extra[sautil.PodUIDKey]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ func Test_validIPSecCSR(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewSimpleClientset(tt.objects...)
ic := &ipsecCSRApprover{
client: client,
}
ic := newIPsecCSRApprover(client)
err := ic.verifyCertificateRequest(tt.cr, tt.keyUsages)
if tt.expectedErr == nil {
assert.NoError(t, err, "validIPSecCSR should not return an error")
Expand Down Expand Up @@ -373,9 +371,7 @@ func Test_verifyIdentity(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewSimpleClientset(tt.objects...)
ic := &ipsecCSRApprover{
client: client,
}
ic := newIPsecCSRApprover(client)
err := ic.verifyIdentity(tt.nodeName, tt.csr)
if tt.expectedErr == nil {
assert.NoError(t, err, "verifyPodOnNode should not return an error")
Expand Down Expand Up @@ -435,9 +431,7 @@ func Test_ipsecCertificateApprover_recognize(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewSimpleClientset(tt.objects...)
ic := &ipsecCSRApprover{
client: client,
}
ic := newIPsecCSRApprover(client)
recognized := ic.recognize(tt.csr)
assert.Equal(t, tt.expectedResult, recognized)
})
Expand Down Expand Up @@ -590,9 +584,7 @@ func Test_ipsecCertificateApprover_verify(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
objs := append(tt.objects, tt.csr)
client := fake.NewSimpleClientset(objs...)
ic := &ipsecCSRApprover{
client: client,
}
ic := newIPsecCSRApprover(client)
approved, err := ic.verify(tt.csr)
if tt.expectedError != nil {
assert.EqualError(t, err, tt.expectedError.Error())
Expand Down

0 comments on commit d7355e8

Please sign in to comment.