<commit_before>package elasticsearch

import (
	"fmt"

	"github.com/golang/glog"
	apiv1 "k8s.io/api/core/v1"
	k8sErrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	utilerrors "k8s.io/apimachinery/pkg/util/errors"
	"k8s.io/client-go/kubernetes"
	appslisters "k8s.io/client-go/listers/apps/v1beta1"
	corelisters "k8s.io/client-go/listers/core/v1"
	"k8s.io/client-go/tools/record"

	"github.com/jetstack/navigator/pkg/apis/navigator/v1alpha1"
	clientset "github.com/jetstack/navigator/pkg/client/clientset/versioned"
	listers "github.com/jetstack/navigator/pkg/client/listers/navigator/v1alpha1"
	"github.com/jetstack/navigator/pkg/controllers"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/actions"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/configmap"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/role"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/rolebinding"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/service"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/serviceaccount"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/util"
)

const (
	errorSync = "ErrSync"

	messageErrorSyncServiceAccount = "Error syncing service account: %s"
	messageErrorSyncConfigMap      = "Error syncing config map: %s"
	messageErrorSyncService        = "Error syncing service: %s"
	messageErrorSyncNodePools      = "Error syncing node pools: %s"
	messageErrorSyncRoles          = "Error syncing RBAC roles: %s"
	messageErrorSyncRoleBindings   = "Error syncing RBAC role bindings: %s"
	messageSuccessExecuteAction    = "Successfully executed action"
)

type ControlInterface interface {
	Sync(*v1alpha1.ElasticsearchCluster) (v1alpha1.ElasticsearchClusterStatus, error)
}

var _ ControlInterface = &defaultElasticsearchClusterControl{}

type defaultElasticsearchClusterControl struct {
	kubeClient      kubernetes.Interface
	navigatorClient clientset.Interface

	statefulSetLister    appslisters.StatefulSetLister
	serviceAccountLister corelisters.ServiceAccountLister
	serviceLister        corelisters.ServiceLister
	configMapLister      corelisters.ConfigMapLister
	pilotLister          listers.PilotLister
	podLister            corelisters.PodLister

	configMapControl      configmap.Interface
	serviceAccountControl serviceaccount.Interface
	serviceControl        service.Interface
	roleControl           role.Interface
	roleBindingControl    rolebinding.Interface

	recorder record.EventRecorder
}

var _ ControlInterface = &defaultElasticsearchClusterControl{}

func NewController(
	kubeClient kubernetes.Interface,
	navigatorClient clientset.Interface,
	statefulSetLister appslisters.StatefulSetLister,
	serviceAccountLister corelisters.ServiceAccountLister,
	serviceLister corelisters.ServiceLister,
	configMapLister corelisters.ConfigMapLister,
	pilotLister listers.PilotLister,
	podLister corelisters.PodLister,
	configMapControl configmap.Interface,
	serviceAccountControl serviceaccount.Interface,
	serviceControl service.Interface,
	roleControl role.Interface,
	roleBindingControl rolebinding.Interface,
	recorder record.EventRecorder,
) ControlInterface {
	return &defaultElasticsearchClusterControl{
		kubeClient:            kubeClient,
		navigatorClient:       navigatorClient,
		statefulSetLister:     statefulSetLister,
		serviceAccountLister:  serviceAccountLister,
		serviceLister:         serviceLister,
		configMapLister:       configMapLister,
		pilotLister:           pilotLister,
		podLister:             podLister,
		configMapControl:      configMapControl,
		serviceAccountControl: serviceAccountControl,
		serviceControl:        serviceControl,
		roleControl:           roleControl,
		roleBindingControl:    roleBindingControl,
		recorder:              recorder,
	}
}

// checkPausedConditions checks if the given cluster is paused or not and adds an appropriate condition.
func (e *defaultElasticsearchClusterControl) checkPausedConditions(c *v1alpha1.ElasticsearchCluster) error {
	cond := c.Status.GetStatusCondition(v1alpha1.ClusterConditionProgressing)
	pausedCondExists := cond != nil && cond.Reason == v1alpha1.PausedClusterReason

	needsUpdate := false
	if c.Spec.Paused && !pausedCondExists {
		c.Status.UpdateStatusCondition(
			v1alpha1.ClusterConditionProgressing,
			v1alpha1.ConditionUnknown,
			v1alpha1.PausedClusterReason,
			"Cluster is paused",
		)
		needsUpdate = true
	} else if !c.Spec.Paused && pausedCondExists {
		c.Status.UpdateStatusCondition(
			v1alpha1.ClusterConditionProgressing,
			v1alpha1.ConditionUnknown,
			v1alpha1.ResumedClusterReason,
			"Cluster is resumed",
		)
		needsUpdate = true
	}

	if !needsUpdate {
		return nil
	}

	var err error
	c, err = e.navigatorClient.NavigatorV1alpha1().ElasticsearchClusters(c.Namespace).UpdateStatus(c)
	return err
}

func (e *defaultElasticsearchClusterControl) Sync(c *v1alpha1.ElasticsearchCluster) (v1alpha1.ElasticsearchClusterStatus, error) {
	c = c.DeepCopy()
	var err error

	err = e.checkPausedConditions(c)<commit_msg>Set conditions to true and false<commit_after>package elasticsearch

import (
	"fmt"

	"github.com/golang/glog"
	apiv1 "k8s.io/api/core/v1"
	k8sErrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	utilerrors "k8s.io/apimachinery/pkg/util/errors"
	"k8s.io/client-go/kubernetes"
	appslisters "k8s.io/client-go/listers/apps/v1beta1"
	corelisters "k8s.io/client-go/listers/core/v1"
	"k8s.io/client-go/tools/record"

	"github.com/jetstack/navigator/pkg/apis/navigator/v1alpha1"
	clientset "github.com/jetstack/navigator/pkg/client/clientset/versioned"
	listers "github.com/jetstack/navigator/pkg/client/listers/navigator/v1alpha1"
	"github.com/jetstack/navigator/pkg/controllers"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/actions"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/configmap"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/role"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/rolebinding"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/service"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/serviceaccount"
	"github.com/jetstack/navigator/pkg/controllers/elasticsearch/util"
)

const (
	errorSync = "ErrSync"

	messageErrorSyncServiceAccount = "Error syncing service account: %s"
	messageErrorSyncConfigMap      = "Error syncing config map: %s"
	messageErrorSyncService        = "Error syncing service: %s"
	messageErrorSyncNodePools      = "Error syncing node pools: %s"
	messageErrorSyncRoles          = "Error syncing RBAC roles: %s"
	messageErrorSyncRoleBindings   = "Error syncing RBAC role bindings: %s"
	messageSuccessExecuteAction    = "Successfully executed action"
)

type ControlInterface interface {
	Sync(*v1alpha1.ElasticsearchCluster) (v1alpha1.ElasticsearchClusterStatus, error)
}

var _ ControlInterface = &defaultElasticsearchClusterControl{}

type defaultElasticsearchClusterControl struct {
	kubeClient      kubernetes.Interface
	navigatorClient clientset.Interface

	statefulSetLister    appslisters.StatefulSetLister
	serviceAccountLister corelisters.ServiceAccountLister
	serviceLister        corelisters.ServiceLister
	configMapLister      corelisters.ConfigMapLister
	pilotLister          listers.PilotLister
	podLister            corelisters.PodLister

	configMapControl      configmap.Interface
	serviceAccountControl serviceaccount.Interface
	serviceControl        service.Interface
	roleControl           role.Interface
	roleBindingControl    rolebinding.Interface

	recorder record.EventRecorder
}

var _ ControlInterface = &defaultElasticsearchClusterControl{}

func NewController(
	kubeClient kubernetes.Interface,
	navigatorClient clientset.Interface,
	statefulSetLister appslisters.StatefulSetLister,
	serviceAccountLister corelisters.ServiceAccountLister,
	serviceLister corelisters.ServiceLister,
	configMapLister corelisters.ConfigMapLister,
	pilotLister listers.PilotLister,
	podLister corelisters.PodLister,
	configMapControl configmap.Interface,
	serviceAccountControl serviceaccount.Interface,
	serviceControl service.Interface,
	roleControl role.Interface,
	roleBindingControl rolebinding.Interface,
	recorder record.EventRecorder,
) ControlInterface {
	return &defaultElasticsearchClusterControl{
		kubeClient:            kubeClient,
		navigatorClient:       navigatorClient,
		statefulSetLister:     statefulSetLister,
		serviceAccountLister:  serviceAccountLister,
		serviceLister:         serviceLister,
		configMapLister:       configMapLister,
		pilotLister:           pilotLister,
		podLister:             podLister,
		configMapControl:      configMapControl,
		serviceAccountControl: serviceAccountControl,
		serviceControl:        serviceControl,
		roleControl:           roleControl,
		roleBindingControl:    roleBindingControl,
		recorder:              recorder,
	}
}

// checkPausedConditions checks if the given cluster is paused or not and adds an appropriate condition.
func (e *defaultElasticsearchClusterControl) checkPausedConditions(c *v1alpha1.ElasticsearchCluster) error {
	cond := c.Status.GetStatusCondition(v1alpha1.ClusterConditionProgressing)
	pausedCondExists := cond != nil && cond.Reason == v1alpha1.PausedClusterReason

	needsUpdate := false
	if c.Spec.Paused && !pausedCondExists {
		c.Status.UpdateStatusCondition(
			v1alpha1.ClusterConditionProgressing,
			v1alpha1.ConditionFalse,
			v1alpha1.PausedClusterReason,
			"Cluster is paused",
		)
		needsUpdate = true
	} else if !c.Spec.Paused && pausedCondExists {
		c.Status.UpdateStatusCondition(
			v1alpha1.ClusterConditionProgressing,
			v1alpha1.ConditionTrue,
			v1alpha1.ResumedClusterReason,
			"Cluster is resumed",
		)
		needsUpdate = true
	}

	if !needsUpdate {
		return nil
	}

	var err error
	_, err = e.navigatorClient.NavigatorV1alpha1().ElasticsearchClusters(c.Namespace).UpdateStatus(c)
	return err
}

func (e *defaultElasticsearchClusterControl) Sync(c *v1alpha1.ElasticsearchCluster) (v1alpha1.ElasticsearchClusterStatus, error) {
	c = c.DeepCopy()
	var err error

	err = e.checkPausedConditions(c)
{'fraction_non_alphanumeric': 0.08095331888910774, 'fraction_numerical': 0.009454402206027182, 'mean_word_length': 5.015995260663507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2267178', 'n_tokens_mistral': 3333, 'n_tokens_neox': 3191, 'n_words': 523}