Skip to content

Commit

Permalink
LBwacher minor fixes (#39)
Browse files Browse the repository at this point in the history
* fix: lb-watcher: use only pods with same seletors and in the same namespace with service

* fix: lb-watcher: patch IP in CR after creating or updating the l4lb in automatic mode

* deploy: remove default 'resources' in manifest

* Preparation for new release

Co-authored-by: Artashes Balabekyan <[email protected]>
  • Loading branch information
pogossian and artashesbalabekyan authored Aug 26, 2021
1 parent 920acbc commit 49c0c66
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 48 deletions.
7 changes: 0 additions & 7 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,4 @@ spec:
image: controller:latest
imagePullPolicy: "Always"
name: manager
resources:
limits:
cpu: 100m
memory: 30Mi
requests:
cpu: 100m
memory: 20Mi
terminationGracePeriodSeconds: 10
7 changes: 3 additions & 4 deletions controllers/l4lbmeta_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,12 @@ func (u *uniReconciler) updateL4LBIfNeccesarry(l4lbCR *v1alpha1.L4LB, l4lbMeta v
l4lbCR.Spec.Frontend.IP = l4lbMeta.Spec.IP
shouldUpdateCR = true
}
if l4lbCR.Spec.OwnerTenant == "" || l4lbCR.Spec.Site == "" {
if l4lbCR.Spec.OwnerTenant == "" || l4lbCR.Spec.Site == "" || l4lbCR.Spec.Frontend.IP == "" {
_ = u.NStorage.L4LBStorage.Download()
if updatedL4LB, ok := u.NStorage.L4LBStorage.FindByID(l4lbMeta.Spec.ID); ok {
l4lbCR.Spec.OwnerTenant = updatedL4LB.TenantName
l4lbCR.Spec.Site = updatedL4LB.SiteName
if l4lbCR.Spec.Frontend.IP == "" {
l4lbCR.Spec.Frontend.IP = updatedL4LB.IP
}
l4lbCR.Spec.Frontend.IP = updatedL4LB.IP
shouldUpdateCR = true
}
}
Expand Down
4 changes: 2 additions & 2 deletions deploy/charts/netris-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.3.7
version: 0.3.8

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
appVersion: v0.4.5
appVersion: v0.4.6
home: https://github.com/netrisai/netris-operator
icon: https://www.netris.ai/wp-content/uploads/2020/05/logo-600.png # [todo] Change url to permalink
keywords:
Expand Down
23 changes: 7 additions & 16 deletions lbwatcher/lb_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
k8sv1alpha1 "github.com/netrisai/netris-operator/api/v1alpha1"
"github.com/netrisai/netris-operator/netrisstorage"
"go.uber.org/zap/zapcore"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -417,12 +416,6 @@ func (w *Watcher) generateLoadBalancers(clientset *kubernetes.Clientset, lbTimeo
return lbList, fmt.Errorf("{generateLoadBalancers} %s", err)
}

debugLogger.Info("Getting k8s pods...")
podList, err := getPods(clientset, "")
if err != nil {
return lbList, fmt.Errorf("{generateLoadBalancers} %s", err)
}

timeout, err := strconv.Atoi(lbTimeout)
if err != nil {
return lbList, fmt.Errorf("{generateLoadBalancers} %s", err)
Expand All @@ -435,21 +428,19 @@ func (w *Watcher) generateLoadBalancers(clientset *kubernetes.Clientset, lbTimeo

for _, svc := range serviceList.Items {
if svc.Spec.Type == "LoadBalancer" {
selectors := []selector{}
selectors := []string{}
hostIPs := map[string]int{}
for key, value := range svc.Spec.Selector {
selectors = append(selectors, selector{
Key: key,
Value: value,
})
selectors = append(selectors, fmt.Sprintf("%s=%s", key, value))
}

pods := []v1.Pod{}
for _, sel := range selectors {
pods = append(pods, filterPodsBySelector(podList, sel.Key, sel.Value)...)
debugLogger.Info("Getting k8s pods...", "service", svc.Name, "namespace", svc.Namespace)
podList, err := getPodsByLabelSeector(clientset, svc.Namespace, strings.Join(selectors, ","))
if err != nil {
return lbList, fmt.Errorf("{generateLoadBalancers} %s", err)
}

for _, pod := range pods {
for _, pod := range podList.Items {
hostIPs[pod.Status.HostIP] = 1
}

Expand Down
19 changes: 5 additions & 14 deletions lbwatcher/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,15 @@ import (
"k8s.io/client-go/kubernetes"
)

func getPods(clientset *kubernetes.Clientset, namespace string) (*v1.PodList, error) {
func getPodsByLabelSeector(clientset *kubernetes.Clientset, namespace, selectors string) (*v1.PodList, error) {
ctx, cancel := context.WithTimeout(cntxt, contextTimeout)
defer cancel()
pods, err := clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
listOptions := metav1.ListOptions{
LabelSelector: selectors,
}
pods, err := clientset.CoreV1().Pods(namespace).List(ctx, listOptions)
if err != nil {
return pods, fmt.Errorf("{getPods} %s", err)
}
return pods, nil
}

func filterPodsBySelector(pods *v1.PodList, selectorKey, selectorValue string) []v1.Pod {
filteredPods := []v1.Pod{}
for _, pod := range pods.Items {
for labelKey, labelValue := range pod.Labels {
if labelKey == selectorKey && labelValue == selectorValue {
filteredPods = append(filteredPods, pod)
}
}
}
return filteredPods
}
5 changes: 0 additions & 5 deletions lbwatcher/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ type Watcher struct {
MGR manager.Manager
}

type selector struct {
Key string
Value string
}

type lbIP struct {
Name string
IP string
Expand Down

0 comments on commit 49c0c66

Please sign in to comment.