Skip to content

Commit 627239c

Browse files
authored
Reduce permission of antrea-agent service account (#3691)
Remove the update permission for services/status of antrea-agent service account. Remove the optimization for ExternalTrafficPolicy setting to Local cases in ServiceExternalIP feature accordingly. Introduce "antctl get serviceexternalip" command for the agent to make checking the assigned Node of external IPs easier. Signed-off-by: Xu Liu <xliu2@vmware.com>
1 parent 92dded2 commit 627239c

16 files changed

Lines changed: 334 additions & 223 deletions

File tree

build/charts/antrea/templates/agent/clusterrole.yaml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,7 @@ rules:
4242
verbs:
4343
- get
4444
- watch
45-
- list
46-
- apiGroups:
47-
- ""
48-
resources:
49-
- services/status
50-
verbs:
51-
- update
45+
- list
5246
- apiGroups:
5347
- discovery.k8s.io
5448
resources:

build/charts/antrea/templates/antctl/clusterrole.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ rules:
5353
- /ovstracing
5454
- /podinterfaces
5555
- /featuregates
56+
- /serviceexternalip
5657
verbs:
5758
- get

build/yamls/antrea-aks.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,13 +2848,7 @@ rules:
28482848
verbs:
28492849
- get
28502850
- watch
2851-
- list
2852-
- apiGroups:
2853-
- ""
2854-
resources:
2855-
- services/status
2856-
verbs:
2857-
- update
2851+
- list
28582852
- apiGroups:
28592853
- discovery.k8s.io
28602854
resources:
@@ -3049,6 +3043,7 @@ rules:
30493043
- /ovstracing
30503044
- /podinterfaces
30513045
- /featuregates
3046+
- /serviceexternalip
30523047
verbs:
30533048
- get
30543049
---

build/yamls/antrea-eks.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,13 +2848,7 @@ rules:
28482848
verbs:
28492849
- get
28502850
- watch
2851-
- list
2852-
- apiGroups:
2853-
- ""
2854-
resources:
2855-
- services/status
2856-
verbs:
2857-
- update
2851+
- list
28582852
- apiGroups:
28592853
- discovery.k8s.io
28602854
resources:
@@ -3049,6 +3043,7 @@ rules:
30493043
- /ovstracing
30503044
- /podinterfaces
30513045
- /featuregates
3046+
- /serviceexternalip
30523047
verbs:
30533048
- get
30543049
---

build/yamls/antrea-gke.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,13 +2848,7 @@ rules:
28482848
verbs:
28492849
- get
28502850
- watch
2851-
- list
2852-
- apiGroups:
2853-
- ""
2854-
resources:
2855-
- services/status
2856-
verbs:
2857-
- update
2851+
- list
28582852
- apiGroups:
28592853
- discovery.k8s.io
28602854
resources:
@@ -3049,6 +3043,7 @@ rules:
30493043
- /ovstracing
30503044
- /podinterfaces
30513045
- /featuregates
3046+
- /serviceexternalip
30523047
verbs:
30533048
- get
30543049
---

build/yamls/antrea-ipsec.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,13 +2861,7 @@ rules:
28612861
verbs:
28622862
- get
28632863
- watch
2864-
- list
2865-
- apiGroups:
2866-
- ""
2867-
resources:
2868-
- services/status
2869-
verbs:
2870-
- update
2864+
- list
28712865
- apiGroups:
28722866
- discovery.k8s.io
28732867
resources:
@@ -3062,6 +3056,7 @@ rules:
30623056
- /ovstracing
30633057
- /podinterfaces
30643058
- /featuregates
3059+
- /serviceexternalip
30653060
verbs:
30663061
- get
30673062
---

build/yamls/antrea.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,13 +2848,7 @@ rules:
28482848
verbs:
28492849
- get
28502850
- watch
2851-
- list
2852-
- apiGroups:
2853-
- ""
2854-
resources:
2855-
- services/status
2856-
verbs:
2857-
- update
2851+
- list
28582852
- apiGroups:
28592853
- discovery.k8s.io
28602854
resources:
@@ -3049,6 +3043,7 @@ rules:
30493043
- /ovstracing
30503044
- /podinterfaces
30513045
- /featuregates
3046+
- /serviceexternalip
30523047
verbs:
30533048
- get
30543049
---

cmd/antrea-agent/agent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ func run(o *Options) error {
614614
apiServer, err := apiserver.New(
615615
agentQuerier,
616616
networkPolicyController,
617+
externalIPController,
617618
o.config.APIPort,
618619
*o.config.EnablePrometheusMetrics,
619620
o.config.ClientConnection.Kubeconfig,

pkg/agent/apiserver/apiserver.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"antrea.io/antrea/pkg/agent/apiserver/handlers/ovsflows"
4141
"antrea.io/antrea/pkg/agent/apiserver/handlers/ovstracing"
4242
"antrea.io/antrea/pkg/agent/apiserver/handlers/podinterface"
43+
"antrea.io/antrea/pkg/agent/apiserver/handlers/serviceexternalip"
4344
agentquerier "antrea.io/antrea/pkg/agent/querier"
4445
systeminstall "antrea.io/antrea/pkg/apis/system/install"
4546
systemv1beta1 "antrea.io/antrea/pkg/apis/system/v1beta1"
@@ -72,7 +73,7 @@ func (s *agentAPIServer) Run(stopCh <-chan struct{}) error {
7273
return s.GenericAPIServer.PrepareRun().Run(stopCh)
7374
}
7475

75-
func installHandlers(aq agentquerier.AgentQuerier, npq querier.AgentNetworkPolicyInfoQuerier, s *genericapiserver.GenericAPIServer) {
76+
func installHandlers(aq agentquerier.AgentQuerier, npq querier.AgentNetworkPolicyInfoQuerier, seipq querier.ServiceExternalIPStatusQuerier, s *genericapiserver.GenericAPIServer) {
7677
s.Handler.NonGoRestfulMux.HandleFunc("/loglevel", loglevel.HandleFunc())
7778
s.Handler.NonGoRestfulMux.HandleFunc("/featuregates", featuregates.HandleFunc())
7879
s.Handler.NonGoRestfulMux.HandleFunc("/agentinfo", agentinfo.HandleFunc(aq))
@@ -82,6 +83,7 @@ func installHandlers(aq agentquerier.AgentQuerier, npq querier.AgentNetworkPolic
8283
s.Handler.NonGoRestfulMux.HandleFunc("/addressgroups", addressgroup.HandleFunc(npq))
8384
s.Handler.NonGoRestfulMux.HandleFunc("/ovsflows", ovsflows.HandleFunc(aq))
8485
s.Handler.NonGoRestfulMux.HandleFunc("/ovstracing", ovstracing.HandleFunc(aq))
86+
s.Handler.NonGoRestfulMux.HandleFunc("/serviceexternalip", serviceexternalip.HandleFunc(seipq))
8587
}
8688

8789
func installAPIGroup(s *genericapiserver.GenericAPIServer, aq agentquerier.AgentQuerier, npq querier.AgentNetworkPolicyInfoQuerier, v4Enabled, v6Enabled bool) error {
@@ -95,8 +97,8 @@ func installAPIGroup(s *genericapiserver.GenericAPIServer, aq agentquerier.Agent
9597
}
9698

9799
// New creates an APIServer for running in antrea agent.
98-
func New(aq agentquerier.AgentQuerier, npq querier.AgentNetworkPolicyInfoQuerier, bindPort int,
99-
enableMetrics bool, kubeconfig string, cipherSuites []uint16, tlsMinVersion uint16, v4Enabled, v6Enabled bool) (*agentAPIServer, error) {
100+
func New(aq agentquerier.AgentQuerier, npq querier.AgentNetworkPolicyInfoQuerier, seipq querier.ServiceExternalIPStatusQuerier,
101+
bindPort int, enableMetrics bool, kubeconfig string, cipherSuites []uint16, tlsMinVersion uint16, v4Enabled, v6Enabled bool) (*agentAPIServer, error) {
100102
cfg, err := newConfig(npq, bindPort, enableMetrics, kubeconfig)
101103
if err != nil {
102104
return nil, err
@@ -110,7 +112,7 @@ func New(aq agentquerier.AgentQuerier, npq querier.AgentNetworkPolicyInfoQuerier
110112
if err := installAPIGroup(s, aq, npq, v4Enabled, v6Enabled); err != nil {
111113
return nil, err
112114
}
113-
installHandlers(aq, npq, s)
115+
installHandlers(aq, npq, seipq, s)
114116
return &agentAPIServer{GenericAPIServer: s}, nil
115117
}
116118

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2022 Antrea Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package serviceexternalip
16+
17+
import (
18+
"encoding/json"
19+
"net/http"
20+
21+
"antrea.io/antrea/pkg/antctl/transform/common"
22+
"antrea.io/antrea/pkg/features"
23+
"antrea.io/antrea/pkg/querier"
24+
)
25+
26+
// HandleFunc creates a http.HandlerFunc which uses an ServiceExternalIPStatusQuerier
27+
// to query Service external IP status.
28+
func HandleFunc(sq querier.ServiceExternalIPStatusQuerier) http.HandlerFunc {
29+
return func(w http.ResponseWriter, r *http.Request) {
30+
name := r.URL.Query().Get("name")
31+
ns := r.URL.Query().Get("namespace")
32+
if !features.DefaultFeatureGate.Enabled(features.ServiceExternalIP) {
33+
http.Error(w, "ServiceExternalIP is not enabled", http.StatusServiceUnavailable)
34+
return
35+
}
36+
result := sq.GetServiceExternalIPStatus()
37+
var response []Response
38+
for _, r := range result {
39+
if (len(name) == 0 || name == r.ServiceName) && (len(ns) == 0 || ns == r.Namespace) {
40+
response = append(response, Response{r})
41+
}
42+
}
43+
if len(name) > 0 && len(response) == 0 {
44+
w.WriteHeader(http.StatusNotFound)
45+
return
46+
}
47+
if err := json.NewEncoder(w).Encode(response); err != nil {
48+
http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
49+
}
50+
}
51+
}
52+
53+
// Response describes the response struct of serviceexternalip command.
54+
type Response struct {
55+
querier.ServiceExternalIPInfo
56+
}
57+
58+
var _ common.TableOutput = (*Response)(nil)
59+
60+
func (r Response) GetTableHeader() []string {
61+
return []string{"NAMESPACE", "NAME", "EXTERNAL-IP-POOL", "EXTERNAL-IP", "ASSIGNED-NODE"}
62+
}
63+
64+
func (r Response) GetTableRow(_ int) []string {
65+
return []string{r.Namespace, r.ServiceName, r.ExternalIPPool, r.ExternalIP, r.AssignedNode}
66+
}
67+
68+
func (r Response) SortRows() bool {
69+
return true
70+
}

0 commit comments

Comments
 (0)