Skip to content

Commit

Permalink
Fix conflicts with kubed (#7)
Browse files Browse the repository at this point in the history
This PR fixes possible conflict scenarios when the cluster is already
running kubed.
  • Loading branch information
mvleandro authored Aug 2, 2023
1 parent 5fc44bb commit 9e2aa77
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
36 changes: 20 additions & 16 deletions kube_syncer/abstractions/kubernetes_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ func (e *KubernetesEntity) Create() error {
_, error := client.Create(context.TODO(), entity, v1.CreateOptions{})

if error == nil {
Logger.Infof("The configmap '%s' was added in the namespace '%s' on context '%s'.", entity.Name, entity.Namespace, e.DestinationContext)
Logger.Infof("The configMap '%s' was added in the namespace '%s' on context '%s'.", entity.Name, entity.Namespace, e.DestinationContext)
} else {
if !errorsTypes.IsAlreadyExists(error) {
Logger.Error(error)
} else {
Logger.Debugf("The configmap '%s' already exists in namespace '%s' on context '%s'.", entity.Name, entity.Namespace, e.DestinationContext)
// If alredy exists it need to be updated.
_, error := client.Update(context.TODO(), entity, v1.UpdateOptions{})
if error == nil {
Logger.Infof("The configMap '%s' was updated in the namespace '%s' on context '%s'.", entity.Name, entity.Namespace, e.DestinationContext)
} else {
Logger.Error(error)
}
}
}

Expand All @@ -71,7 +77,13 @@ func (e *KubernetesEntity) Create() error {
if !errorsTypes.IsAlreadyExists(error) {
Logger.Error(error)
} else {
Logger.Debugf("The secret '%s' already exists in namespace '%s' on context '%s'.", entity.Name, entity.Namespace, e.DestinationContext)
// If alredy exists it need to be updated.
_, error := client.Update(context.TODO(), entity, v1.UpdateOptions{})
if error == nil {
Logger.Infof("The secret '%s' was updated in the namespace '%s' on context '%s'.", entity.Name, entity.Namespace, e.DestinationContext)
} else {
Logger.Error(error)
}
}
}

Expand Down Expand Up @@ -151,7 +163,7 @@ func (e *KubernetesEntity) Delete() error {
error := client.Delete(context.TODO(), entity.Name, v1.DeleteOptions{})

if error == nil {
Logger.Infof("The secret '%s' was delete from namespace '%s' on context '%s'.", entity.Name, entity.Namespace, e.DestinationContext)
Logger.Infof("The secret '%s' was deleted from namespace '%s' on context '%s'.", entity.Name, entity.Namespace, e.DestinationContext)
} else {
if !errorsTypes.IsNotFound(error) {
Logger.Error(error)
Expand All @@ -170,17 +182,13 @@ func getNewConfigMap(sourceConfigMap *corev1.ConfigMap, namespace, sourceContext
destinationConfigMap := sourceConfigMap.DeepCopy()

destinationConfigMap.UID = ""
destinationConfigMap.Labels = map[string]string{}
destinationConfigMap.Labels[ManagedLabelSelector] = "true"
destinationConfigMap.Annotations = map[string]string{}
destinationConfigMap.Annotations[SourceClusterAnnotation] = sourceContext
destinationConfigMap.Annotations[SourceNamespaceAnnotation] = sourceConfigMap.Namespace
destinationConfigMap.Annotations[SourceResourceVersionAnnotation] = sourceConfigMap.ResourceVersion
destinationConfigMap.Namespace = namespace

delete(destinationConfigMap.Labels, LabelSelector)
delete(destinationConfigMap.Annotations, NamespaceNameAnnotation)
delete(destinationConfigMap.Annotations, ClusterAnnotation)
delete(destinationConfigMap.Annotations, "creationTimestamp")
delete(destinationConfigMap.Annotations, KubectlApplyAnnotation)
destinationConfigMap.ResourceVersion = ""

return destinationConfigMap
Expand All @@ -190,17 +198,13 @@ func getNewSecret(sourceSecret *corev1.Secret, namespace, sourceContext string)
destinationSecret := sourceSecret.DeepCopy()

destinationSecret.UID = ""
destinationSecret.Labels = map[string]string{}
destinationSecret.Labels[ManagedLabelSelector] = "true"
destinationSecret.Annotations = map[string]string{}
destinationSecret.Annotations[SourceClusterAnnotation] = sourceContext
destinationSecret.Annotations[SourceNamespaceAnnotation] = sourceSecret.Namespace
destinationSecret.Annotations[SourceResourceVersionAnnotation] = sourceSecret.ResourceVersion
destinationSecret.Namespace = namespace

delete(destinationSecret.Labels, LabelSelector)
delete(destinationSecret.Annotations, NamespaceNameAnnotation)
delete(destinationSecret.Annotations, ClusterAnnotation)
delete(destinationSecret.Annotations, "creationTimestamp")
delete(destinationSecret.Annotations, KubectlApplyAnnotation)
destinationSecret.ResourceVersion = ""

return destinationSecret
Expand Down
7 changes: 4 additions & 3 deletions kube_syncer/configmap_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/client-go/kubernetes"
)

// The configmap watcher.
// The configMap watcher.
type ConfigMapWatcher struct {
// The kubeapi client.
kubeClient *kubernetes.Clientset
Expand All @@ -24,10 +24,11 @@ func (w ConfigMapWatcher) Watch() <-chan abstractions.ISynchronizable {
configMapsChan := make(chan abstractions.ISynchronizable)

go func() {
watcher, _ := w.kubeClient.CoreV1().ConfigMaps(metav1.NamespaceAll).Watch(context.Background(), metav1.ListOptions{
watcher, _ := w.kubeClient.CoreV1().ConfigMaps(metav1.NamespaceAll).Watch(context.TODO(), metav1.ListOptions{
LabelSelector: abstractions.LabelSelector,
})
w.logger.Info("Watching configmaps events.")

w.logger.Info("Watching configMaps events.")

for event := range watcher.ResultChan() {

Expand Down
2 changes: 1 addition & 1 deletion kube_syncer/secret_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (w SecretWatcher) Watch() <-chan abstractions.ISynchronizable {
secretsChan := make(chan abstractions.ISynchronizable)

go func() {
watcher, _ := w.kubeClient.CoreV1().Secrets(metav1.NamespaceAll).Watch(context.Background(), metav1.ListOptions{
watcher, _ := w.kubeClient.CoreV1().Secrets(metav1.NamespaceAll).Watch(context.TODO(), metav1.ListOptions{
LabelSelector: abstractions.LabelSelector,
})

Expand Down

0 comments on commit 9e2aa77

Please sign in to comment.