Skip to content

Commit

Permalink
Enforce metallb config map reload everytime. (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Jan 14, 2022
1 parent c0d64d8 commit d38924f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
13 changes: 7 additions & 6 deletions pkg/controllers/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ func (l *LoadBalancerController) UpdateLoadBalancer(ctx context.Context, cluster
func (l *LoadBalancerController) EnsureLoadBalancerDeleted(ctx context.Context, clusterName string, service *v1.Service) error {
l.logger.Printf("EnsureLoadBalancerDeleted: clusterName %q, namespace %q, serviceName %q, serviceStatus: %v\n", clusterName, service.Namespace, service.Name, service.Status)

nodes, err := kubernetes.GetNodes(l.K8sClient)
if err != nil {
return err
}

s := *service
serviceTag := tags.BuildClusterServiceFQNTag(l.clusterID, s.GetNamespace(), s.GetName())
ips, err := metal.FindProjectIPsWithTag(l.client, l.projectID, serviceTag)
Expand Down Expand Up @@ -253,7 +248,13 @@ func (l *LoadBalancerController) EnsureLoadBalancerDeleted(ctx context.Context,
return err
}
}
return l.UpdateMetalLBConfig(nodes)

// we do not update the metallb config here because then the metallb controller will report a stale config
// this is because the service gets deleted after updating the metallb config map
//
// therefore, we let the housekeeping update the config map

return nil
}

// removes the service tag and checks whether it is the last service tag.
Expand Down
45 changes: 22 additions & 23 deletions pkg/resources/kubernetes/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@ package kubernetes

import (
"context"
"strconv"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// Apply inserts or updates given config map.
// ApplyConfigMap creates or updates given config map.
func ApplyConfigMap(client kubernetes.Interface, namespace, name string, configMap map[string]string) error {
ctx := context.Background()
cmi := client.CoreV1().ConfigMaps(namespace)
cm, err := cmi.Get(ctx, name, metav1.GetOptions{})
if err == nil {
cm.Data = configMap
var (
ctx = context.Background()
cm = &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{
// we enforce updating the metallb config every time such that
// deleted service ips will be cleaned up from the config regularly.
"cluster.metal-stack.io.metal-ccm/last-update-time": strconv.FormatInt(time.Now().Unix(), 10),
},
},
Data: configMap,
}
)

_, err = cmi.Update(ctx, cm, metav1.UpdateOptions{})
_, err := client.CoreV1().ConfigMaps(namespace).Update(ctx, cm, metav1.UpdateOptions{})
if err != nil && errors.IsNotFound(err) {
_, err = client.CoreV1().ConfigMaps(namespace).Create(ctx, cm, metav1.CreateOptions{})
return err
}

cm = &v1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
DeletionGracePeriodSeconds: nil,
Labels: nil,
Annotations: nil,
},
Data: configMap,
}

_, err = cmi.Create(ctx, cm, metav1.CreateOptions{})
return err
}

0 comments on commit d38924f

Please sign in to comment.