Skip to content

Commit 40582ad

Browse files
authored
Merge pull request #245 from Zippo-Wang/feat_ds
feat: supports running as a DaemonSet
2 parents 52aa6a9 + dd28993 commit 40582ad

File tree

6 files changed

+94
-198
lines changed

6 files changed

+94
-198
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
apiVersion: apps/v1
2+
kind: DaemonSet
3+
metadata:
4+
name: huawei-cloud-controller-manager
5+
namespace: kube-system
6+
labels:
7+
k8s-app: huawei-cloud-controller-manager
8+
spec:
9+
selector:
10+
matchLabels:
11+
k8s-app: huawei-cloud-controller-manager
12+
template:
13+
metadata:
14+
labels:
15+
k8s-app: huawei-cloud-controller-manager
16+
spec:
17+
nodeSelector:
18+
kubernetes.io/os: linux
19+
securityContext:
20+
runAsUser: 1001
21+
tolerations:
22+
- key: node.cloudprovider.kubernetes.io/uninitialized
23+
value: "true"
24+
effect: NoSchedule
25+
- key: node-role.kubernetes.io/master
26+
effect: NoSchedule
27+
- key: node-role.kubernetes.io/control-plane
28+
effect: NoSchedule
29+
serviceAccountName: cloud-controller-manager
30+
containers:
31+
- name: huawei-cloud-controller-manager
32+
image: swr.cn-north-4.myhuaweicloud.com/k8s-cloudprovider/huawei-cloud-controller-manager:v0.26.7
33+
args:
34+
- /bin/huawei-cloud-controller-manager
35+
- --v=5
36+
- --cloud-config=/etc/config/cloud-config
37+
- --cloud-provider=huaweicloud
38+
- --use-service-account-credentials=true
39+
- --node-status-update-frequency=5s
40+
- --node-monitor-period=5s
41+
- --leader-elect-lease-duration=30s
42+
- --leader-elect-renew-deadline=20s
43+
- --leader-elect-retry-period=2s
44+
volumeMounts:
45+
- mountPath: /etc/kubernetes
46+
name: k8s-certs
47+
readOnly: true
48+
- mountPath: /etc/ssl/certs
49+
name: ca-certs
50+
readOnly: true
51+
- mountPath: /etc/config
52+
name: cloud-config-volume
53+
readOnly: true
54+
- mountPath: /usr/libexec/kubernetes/kubelet-plugins/volume/exec
55+
name: flexvolume-dir
56+
resources:
57+
requests:
58+
cpu: 200m
59+
memory: 100Mi
60+
limits:
61+
cpu: 2
62+
memory: 2Gi
63+
hostNetwork: true
64+
volumes:
65+
- hostPath:
66+
path: /usr/libexec/kubernetes/kubelet-plugins/volume/exec
67+
type: DirectoryOrCreate
68+
name: flexvolume-dir
69+
- hostPath:
70+
path: /etc/kubernetes
71+
type: DirectoryOrCreate
72+
name: k8s-certs
73+
- hostPath:
74+
path: /etc/ssl/certs
75+
type: DirectoryOrCreate
76+
name: ca-certs
77+
- name: cloud-config-volume
78+
secret:
79+
secretName: cloud-config
File renamed without changes.

pkg/cloudprovider/huaweicloud/dedicatedloadbalancer.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -720,13 +720,6 @@ func (d *DedicatedLoadBalancer) ensureHealthCheck(loadbalancerID string, pool *e
720720
monitorID := pool.HealthmonitorId
721721
klog.Infof("add or update or remove health check: %s : %#v", monitorID, healthCheckOpts)
722722

723-
if healthCheckOpts.Enable {
724-
err := d.allowHealthCheckRule(node)
725-
if err != nil {
726-
return err
727-
}
728-
}
729-
730723
// create health monitor
731724
if monitorID == "" && healthCheckOpts.Enable {
732725
_, err := d.createHealthMonitor(loadbalancerID, pool.Id, pool.Protocol, healthCheckOpts)

pkg/cloudprovider/huaweicloud/huaweicloud.go

Lines changed: 13 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
"io"
2323
"os"
2424
"strings"
25-
"sync"
2625
"time"
2726

27+
ecsmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model"
2828
gocache "github.com/patrickmn/go-cache"
2929
"google.golang.org/grpc/codes"
3030
"google.golang.org/grpc/status"
@@ -46,11 +46,6 @@ import (
4646
"k8s.io/cloud-provider/options"
4747
servicehelper "k8s.io/cloud-provider/service/helpers"
4848
"k8s.io/klog/v2"
49-
"k8s.io/utils/pointer"
50-
51-
ecsmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model"
52-
vpcmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v2/model"
53-
5449
"sigs.k8s.io/cloud-provider-huaweicloud/pkg/cloudprovider/huaweicloud/wrapper"
5550
"sigs.k8s.io/cloud-provider-huaweicloud/pkg/common"
5651
"sigs.k8s.io/cloud-provider-huaweicloud/pkg/config"
@@ -108,17 +103,15 @@ const (
108103
ProtocolHTTPS = "HTTPS"
109104
ProtocolTerminatedHTTPS = "TERMINATED_HTTPS"
110105

111-
healthCheckCidr = "100.125.0.0/16"
112-
113106
endpointAdded = "endpointAdded"
114107
endpointUpdate = "endpointUpdate"
108+
109+
kubeSystemNamespace = "kube-system"
115110
)
116111

117112
type ELBProtocol string
118113
type ELBAlgorithm string
119114

120-
var healthCheckCidrOptLock = &sync.Mutex{}
121-
122115
type Basic struct {
123116
cloudControllerManagerOpts *options.CloudControllerManagerOptions
124117
cloudConfig *config.CloudConfig
@@ -217,103 +210,6 @@ func (b Basic) getNodeSubnetID(node *v1.Node) (string, error) {
217210
return "", fmt.Errorf("failed to get node subnet ID")
218211
}
219212

220-
func (b Basic) allowHealthCheckRule(node *v1.Node) error {
221-
if b.loadbalancerOpts.DisableCreateSecurityGroup {
222-
klog.Infof("automatic creation of security groups has been disabled")
223-
return nil
224-
}
225-
// Avoid adding security group rules in parallel.
226-
healthCheckCidrOptLock.Lock()
227-
defer func() {
228-
healthCheckCidrOptLock.Unlock()
229-
}()
230-
231-
instance, err := b.ecsClient.GetByNodeName(node.Name)
232-
if err != nil {
233-
return err
234-
}
235-
236-
secGroups, err := b.ecsClient.ListSecurityGroups(instance.Id)
237-
if err != nil {
238-
return err
239-
}
240-
if len(secGroups) == 0 {
241-
klog.Warningf("not found any security groups on %s", node.Name)
242-
return nil
243-
}
244-
245-
for _, sg := range secGroups {
246-
rules, err := b.vpcClient.ListSecurityGroupRules(sg.Id)
247-
if err != nil {
248-
return fmt.Errorf("failed to list security group[%s] rules: %s", sg.Id, err)
249-
}
250-
251-
for _, r := range rules {
252-
if r.Direction == "ingress" && r.RemoteIpPrefix == healthCheckCidr &&
253-
r.Ethertype == "IPv4" && r.PortRangeMin == 0 && r.PortRangeMax == 0 {
254-
klog.Infof("the health check IP is already in the security group, no need to add rules")
255-
return nil
256-
}
257-
}
258-
}
259-
260-
desc := fmt.Sprintf("DO NOT EDIT. %s are internal IP addresses used by ELB to check the health of backend"+
261-
" servers. Created by K8s CCM.", healthCheckCidr)
262-
263-
securityGroupID := secGroups[0].Id
264-
_, err = b.vpcClient.CreateSecurityGroupRule(&vpcmodel.CreateSecurityGroupRuleOption{
265-
SecurityGroupId: securityGroupID,
266-
Description: &desc,
267-
Direction: "ingress",
268-
Ethertype: pointer.String("IPv4"),
269-
RemoteIpPrefix: pointer.String(healthCheckCidr),
270-
})
271-
272-
if err != nil {
273-
return fmt.Errorf("failed to create security group[%s] rules: %s", securityGroupID, err)
274-
}
275-
276-
return err
277-
}
278-
279-
func (b Basic) removeHealthCheckRules(node *v1.Node) error {
280-
instance, err := b.ecsClient.GetByNodeName(node.Name)
281-
if err != nil {
282-
return err
283-
}
284-
285-
secGroups, err := b.ecsClient.ListSecurityGroups(instance.Id)
286-
if err != nil {
287-
return err
288-
}
289-
if len(secGroups) == 0 {
290-
klog.Warningf("not found any security groups on %s", node.Name)
291-
return nil
292-
}
293-
294-
desc := fmt.Sprintf("DO NOT EDIT. %s are internal IP addresses used by ELB to check the health of backend"+
295-
" servers. Created by K8s CCM.", healthCheckCidr)
296-
297-
for _, sg := range secGroups {
298-
rules, err := b.vpcClient.ListSecurityGroupRules(sg.Id)
299-
if err != nil {
300-
return fmt.Errorf("failed to list security group[%s] rules: %s", sg.Id, err)
301-
}
302-
303-
for _, r := range rules {
304-
if r.Direction == "ingress" && r.RemoteIpPrefix == healthCheckCidr &&
305-
r.Ethertype == "IPv4" && r.PortRangeMin == 0 && r.PortRangeMax == 0 && r.Description == desc {
306-
err := b.vpcClient.DeleteSecurityGroupRule(r.Id)
307-
if err != nil {
308-
klog.Errorf("failed to delete security group[%s] rule[%s]", sg.Id, r.Id)
309-
}
310-
}
311-
}
312-
}
313-
314-
return nil
315-
}
316-
317213
func (b Basic) updateService(service *v1.Service, lbStatus *v1.LoadBalancerStatus) {
318214
if service.Spec.LoadBalancerClass == nil || *service.Spec.LoadBalancerClass != LoadBalancerClass {
319215
return
@@ -702,7 +598,6 @@ type LoadBalancerServiceListener struct {
702598
func (e *LoadBalancerServiceListener) stopListenerSlice() {
703599
klog.Warningf("Stop listening to Endpoints")
704600
e.stopChannel <- struct{}{}
705-
close(e.stopChannel)
706601
}
707602

708603
func (e *LoadBalancerServiceListener) startEndpointListener(handle func(*v1.Service, bool)) {
@@ -861,73 +756,6 @@ func (e *LoadBalancerServiceListener) dispatcher(namespace, name, eType string,
861756
handle(svc, false)
862757
}
863758

864-
func (e *LoadBalancerServiceListener) autoRemoveHealthCheckRule(handle func(node *v1.Node) error) {
865-
informer := cache.NewSharedIndexInformer(
866-
&cache.ListWatch{
867-
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
868-
return e.kubeClient.Services(metav1.NamespaceAll).List(context.Background(), options)
869-
},
870-
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
871-
return e.kubeClient.Services(metav1.NamespaceAll).Watch(context.Background(), options)
872-
},
873-
},
874-
&v1.Service{},
875-
0,
876-
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
877-
)
878-
879-
_, err := informer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
880-
AddFunc: func(obj interface{}) {
881-
service := obj.(*v1.Service)
882-
if service.Spec.Type != v1.ServiceTypeLoadBalancer {
883-
return
884-
}
885-
886-
key := fmt.Sprintf("%s/%s", service.Namespace, service.Name)
887-
e.serviceCache[key] = service
888-
klog.V(6).Infof("new LoadBalancer service %s/%s added, cache size: %v",
889-
service.Namespace, service.Name, len(e.serviceCache))
890-
},
891-
UpdateFunc: func(oldObj, newObj interface{}) {},
892-
DeleteFunc: func(obj interface{}) {
893-
service := obj.(*v1.Service)
894-
if service.Spec.Type != v1.ServiceTypeLoadBalancer {
895-
return
896-
}
897-
key := fmt.Sprintf("%s/%s", service.Namespace, service.Name)
898-
delete(e.serviceCache, key)
899-
klog.V(6).Infof("found LoadBalancer service %s/%s deleted, cache size: %v",
900-
service.Namespace, service.Name, len(e.serviceCache))
901-
902-
if len(e.serviceCache) > 0 {
903-
klog.V(6).Infof("found %v LoadBalancer service(s), "+
904-
"skip clearing the security group rules for ELB health check", len(e.serviceCache))
905-
return
906-
}
907-
908-
nodes, err := e.kubeClient.Nodes().List(context.TODO(), metav1.ListOptions{
909-
Limit: 1,
910-
})
911-
if err != nil {
912-
klog.Errorf("failed to query a list of nodes in autoRemoveHealthCheckRule: %s", err)
913-
}
914-
915-
if len(nodes.Items) <= 0 {
916-
klog.Warningf("not found any nodes, skip clearing the security group rules for ELB health check")
917-
return
918-
}
919-
klog.Infof("all LoadBalancer services has been deleted, start to clean health check rules")
920-
n := nodes.Items[0]
921-
handle(&n) //nolint:errcheck
922-
},
923-
}, 5*time.Second)
924-
if err != nil {
925-
klog.Errorf("failed to watch service: %s", err)
926-
}
927-
928-
informer.Run(e.stopChannel)
929-
}
930-
931759
func (h *CloudProvider) listenerDeploy() error {
932760
listener := LoadBalancerServiceListener{
933761
Basic: h.Basic,
@@ -946,12 +774,6 @@ func (h *CloudProvider) listenerDeploy() error {
946774
}
947775

948776
go leaderElection(id, h.restConfig, h.eventRecorder, func(ctx context.Context) {
949-
if !h.loadbalancerOpts.DisableCreateSecurityGroup {
950-
go listener.autoRemoveHealthCheckRule(h.removeHealthCheckRules)
951-
} else {
952-
klog.Infof("automatic creation of security groups has been disabled")
953-
}
954-
955777
listener.startEndpointListener(func(service *v1.Service, isDelete bool) {
956778
klog.Infof("Got service %s/%s using loadbalancer class %s",
957779
service.Namespace, service.Name, utils.ToString(service.Spec.LoadBalancerClass))
@@ -1008,9 +830,10 @@ func (h *CloudProvider) listenerDeploy() error {
1008830
service.Namespace, service.Name, err)
1009831
})
1010832
}, func() {
1011-
listener.stopListenerSlice()
1012833
listener.goroutinePool.Stop()
834+
listener.stopListenerSlice()
1013835
})
836+
1014837
return nil
1015838
}
1016839

@@ -1021,7 +844,7 @@ func leaderElection(id string, restConfig *rest.Config, recorder record.EventRec
1021844
retryPeriod := 30 * time.Second
1022845

1023846
configmapLock, err := resourcelock.NewFromKubeconfig(resourcelock.ConfigMapsLeasesResourceLock,
1024-
"kube-system",
847+
kubeSystemNamespace,
1025848
leaseName,
1026849
resourcelock.ResourceLockConfig{
1027850
Identity: fmt.Sprintf("%s_%s", id, string(uuid.NewUUID())),
@@ -1047,6 +870,13 @@ func leaderElection(id string, restConfig *rest.Config, recorder record.EventRec
1047870
klog.Infof("[Listener EndpointSlices] leader election lost: %s", id)
1048871
onStop()
1049872
},
873+
OnNewLeader: func(identity string) {
874+
klog.Infof("[Listener EndpointSlices] leader chenged to %s", identity)
875+
if strings.Contains(identity, id) {
876+
return
877+
}
878+
onStop()
879+
},
1050880
},
1051881
Name: leaseName,
1052882
})

pkg/cloudprovider/huaweicloud/sharedloadbalancer.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,6 @@ func (l *SharedLoadBalancer) ensureHealthCheck(loadbalancerID string, pool *elbm
338338
monitorID := pool.HealthmonitorId
339339
klog.Infof("add or update or remove health check: %s : %#v", monitorID, healthCheckOpts)
340340

341-
if healthCheckOpts.Enable {
342-
err := l.allowHealthCheckRule(node)
343-
if err != nil {
344-
return err
345-
}
346-
}
347-
348341
protocolStr := parseProtocol(service, port)
349342
// create health monitor
350343
if monitorID == "" && healthCheckOpts.Enable {

pkg/common/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ limitations under the License.
1717
package common
1818

1919
import (
20-
"k8s.io/klog/v2"
2120
"os"
2221
"os/signal"
2322
"syscall"
2423
"time"
2524

25+
"k8s.io/klog/v2"
26+
2627
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/sdkerr"
2728
"google.golang.org/grpc/codes"
2829
"google.golang.org/grpc/status"

0 commit comments

Comments
 (0)