Skip to content

Commit

Permalink
Revert "Stop crashing when losing connection" (#16)
Browse files Browse the repository at this point in the history
Reverts #14
  • Loading branch information
iblackman authored Aug 11, 2023
1 parent 71fc06b commit 7f3fda0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 65 deletions.
2 changes: 1 addition & 1 deletion application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func New() *cli.App {
app := cli.NewApp()
app.Name = "Keess"
app.Version = "v0.1.9"
app.Version = "v0.1.8"
app.Usage = "Keep stuff synchronized."
app.Description = "Keep secrets and configmaps synchronized."
app.Suggest = true
Expand Down
4 changes: 2 additions & 2 deletions chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ 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.1.9
version: 0.1.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.
# It is recommended to use it with quotes.
appVersion: "v0.1.9"
appVersion: "v0.1.8"
3 changes: 0 additions & 3 deletions kube_syncer/abstractions/abstractions.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ var EntitiesToAllNamespaces map[string]map[string]runtime.Object = make(map[stri
// A map containing the ConfigMaps that sould be present in every Namespace that matches with the configured label
var EntitiesToLabeledNamespaces map[string]map[string]runtime.Object = make(map[string]map[string]runtime.Object)

// Slice containing the connected clusters.
var ConnectedClusters []string = []string{}

// === Functions === //

// Check if exists a valid annotation in an annotation map.
Expand Down
23 changes: 1 addition & 22 deletions kube_syncer/abstractions/configmap_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,13 @@ func (c ConfigMapEvent) Sync(sourceContext string, kubeClients *map[string]*kube

if namespace.Labels[label] == strings.Trim(value, "\"") {
namespaces = append(namespaces, namespaceName)
Logger.Debugf("The namespace '%s' contains the synchronization label '%s'. The configMap '%s' will be synchronized.", namespaceName, namespaceLabelAnnotation, configMap.Name)
Logger.Debugf("The namespace '%s' contains the synchronization label '%s'. The configmap '%s' will be synchronized.", namespaceName, namespaceLabelAnnotation, configMap.Name)
}

}
EntitiesToLabeledNamespaces["ConfigMaps"][configMap.Name] = configMap
}
}

if c.Type == Deleted {
delete(EntitiesToAllNamespaces["ConfigMaps"], configMap.Name)
}

for _, destinationNamespace := range namespaces {
if configMap.Namespace == destinationNamespace {
continue
Expand All @@ -92,17 +87,6 @@ func (c ConfigMapEvent) Sync(sourceContext string, kubeClients *map[string]*kube
annotation := configMap.Annotations[ClusterAnnotation]
clusters := StringToSlice(annotation)

var removedClusters []string = []string{}
for _, destinationContext := range ConnectedClusters {
contains := false
for _, cluster := range clusters {
contains = contains || cluster == destinationContext
}
if !contains {
removedClusters = append(removedClusters, destinationContext)
}
}

for _, destinationContext := range clusters {
if sourceContext == destinationContext {
continue
Expand All @@ -119,11 +103,6 @@ func (c ConfigMapEvent) Sync(sourceContext string, kubeClients *map[string]*kube
kubeEntity.Delete()
}
}

for _, removedCluster := range removedClusters {
kubeEntity := NewKubernetesEntity(*kubeClients, configMap, ConfigMapEntity, sourceNamespace, sourceNamespace, sourceContext, removedCluster)
kubeEntity.Delete()
}
}

if c.Type == Modified {
Expand Down
16 changes: 0 additions & 16 deletions kube_syncer/abstractions/secret_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,6 @@ func (c SecretEvent) Sync(sourceContext string, kubeClients *map[string]*kuberne
annotation := secret.Annotations[ClusterAnnotation]
clusters := StringToSlice(annotation)

var removedClusters []string = []string{}
for _, destinationContext := range ConnectedClusters {
contains := false
for _, cluster := range clusters {
contains = contains || cluster == destinationContext
}
if !contains {
removedClusters = append(removedClusters, destinationContext)
}
}

for _, destinationContext := range clusters {
if sourceContext == destinationContext {
continue
Expand All @@ -119,11 +108,6 @@ func (c SecretEvent) Sync(sourceContext string, kubeClients *map[string]*kuberne
kubeEntity.Delete()
}
}

for _, removedCluster := range removedClusters {
kubeEntity := NewKubernetesEntity(*kubeClients, secret, SecretEntity, sourceNamespace, sourceNamespace, sourceContext, removedCluster)
kubeEntity.Delete()
}
}

if c.Type == Modified {
Expand Down
38 changes: 17 additions & 21 deletions kube_syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
"k8s.io/client-go/util/homedir"
)

// Slice containing the connected clusters.
var ConnectedClusters []string = []string{}

// Represents a base structure for any syncer.
type Syncer struct {
kubeClients map[string]*kubernetes.Clientset
Expand Down Expand Up @@ -59,7 +56,7 @@ func (s *Syncer) Start(kubeConfigPath string, developmentMode bool, sourceContex

zapLogger, err := loggerConfig.Build()
if err != nil {
s.logger.Error(err)
return err
}
abstractions.Logger = zapLogger.Sugar()
s.logger = abstractions.Logger
Expand Down Expand Up @@ -90,7 +87,7 @@ func (s *Syncer) Start(kubeConfigPath string, developmentMode bool, sourceContex
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
s.logger.Error(err)
panic(err.Error())
}

inClusterConfig, err := rest.InClusterConfig()
Expand All @@ -100,32 +97,31 @@ func (s *Syncer) Start(kubeConfigPath string, developmentMode bool, sourceContex
// create the clientset
client, err = kubernetes.NewForConfig(inClusterConfig)
if err != nil {
s.logger.Error(err)
return err
}
s.logger.Info("Config loaded from service account.")
} else {
// create the clientset
client, err = kubernetes.NewForConfig(config)
if err != nil {
s.logger.Error(err)
return err
}
s.logger.Info("Config loaded from kube config.")
}

s.kubeClients = map[string]*kubernetes.Clientset{}
s.kubeClients[s.sourceContext] = client
abstractions.ConnectedClusters = destinationContexts

for _, context := range destinationContexts {
config, err := buildConfigWithContextFromFlags(context, *kubeconfig)
if err != nil {
s.logger.Error(err)
panic(err)
}

// create the clientset
client, err := kubernetes.NewForConfig(config)
if err != nil {
s.logger.Error(err)
return err
}

s.kubeClients[context] = client
Expand Down Expand Up @@ -161,7 +157,7 @@ func (s *Syncer) Run() error {
// First of all we need to load all namespaces.
namespaceList, err := kubeClient.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
s.logger.Error(err)
return err
}

for _, namespace := range namespaceList.Items {
Expand All @@ -173,7 +169,7 @@ func (s *Syncer) Run() error {
LabelSelector: abstractions.LabelSelector,
})
if err != nil {
s.logger.Error(err)
return err
}

for _, configMap := range configMapList.Items {
Expand All @@ -191,7 +187,7 @@ func (s *Syncer) Run() error {
LabelSelector: abstractions.LabelSelector,
})
if err != nil {
s.logger.Error(err)
return err
}

for _, secret := range secretList.Items {
Expand All @@ -211,7 +207,7 @@ func (s *Syncer) Run() error {
LabelSelector: abstractions.ManagedLabelSelector,
})
if err != nil {
s.logger.Error(err)
return err
}

for _, configMap := range managedConfigMapList.Items {
Expand All @@ -230,7 +226,7 @@ func (s *Syncer) Run() error {
sourceConfigMap, err := sourceKubeClient.CoreV1().ConfigMaps(sourceNamespace).Get(context.TODO(), configMap.Name, metav1.GetOptions{})

if err != nil && !errorsTypes.IsNotFound(err) {
s.logger.Error(err)
return err
}

// Check if source configmap was deleted.
Expand All @@ -239,7 +235,7 @@ func (s *Syncer) Run() error {

err := entity.Delete()
if err != nil && !errorsTypes.IsNotFound(err) {
s.logger.Error(err)
return err
} else {
s.logger.Infof("The ConfigMap '%s' was deleted in namespace '%s' on context '%s' because it was deleted in the source namespace '%s' on the source context '%s'.", configMap.Name, configMap.Namespace, currentContext, sourceNamespace, sourceContext)
}
Expand All @@ -251,7 +247,7 @@ func (s *Syncer) Run() error {
entity = abstractions.NewKubernetesEntity(s.kubeClients, sourceConfigMap, abstractions.ConfigMapEntity, sourceNamespace, configMap.Namespace, sourceContext, currentContext)
err := entity.Update()
if err != nil {
s.logger.Error(err)
return err
} else {
s.logger.Infof("The ConfigMap '%s' was updated in namespace '%s' on context '%s' because It was updated in the source namespace '%s' on the source context '%s'.", configMap.Name, configMap.Namespace, currentContext, sourceNamespace, sourceContext)
}
Expand All @@ -264,7 +260,7 @@ func (s *Syncer) Run() error {
LabelSelector: abstractions.ManagedLabelSelector,
})
if err != nil {
s.logger.Error(err)
return err
}

for _, secret := range managedSecretList.Items {
Expand All @@ -283,7 +279,7 @@ func (s *Syncer) Run() error {
sourceSecret, err := sourceKubeClient.CoreV1().Secrets(sourceNamespace).Get(context.TODO(), secret.Name, metav1.GetOptions{})

if err != nil && !errorsTypes.IsNotFound(err) {
s.logger.Error(err)
return err
}

// Check if source secret was deleted.
Expand All @@ -292,7 +288,7 @@ func (s *Syncer) Run() error {

err := entity.Delete()
if err != nil && !errorsTypes.IsNotFound(err) {
s.logger.Error(err)
return err
} else {
s.logger.Infof("The Secret '%s' was deleted in namespace '%s' on context '%s' because It was deleted in the source namespace '%s' on the source context '%s'.", secret.Name, secret.Namespace, currentContext, sourceNamespace, sourceContext)
}
Expand All @@ -304,7 +300,7 @@ func (s *Syncer) Run() error {
entity = abstractions.NewKubernetesEntity(s.kubeClients, sourceSecret, abstractions.SecretEntity, sourceNamespace, secret.Namespace, sourceContext, currentContext)
err := entity.Update()
if err != nil {
s.logger.Error(err)
return err
} else {
s.logger.Infof("The Secret '%s' was updated in namespace '%s' on context '%s' because It was updated in the source namespace '%s' on the source context '%s'.", secret.Name, secret.Namespace, currentContext, sourceNamespace, sourceContext)
}
Expand Down

0 comments on commit 7f3fda0

Please sign in to comment.