Code improvements
This commit is contained in:
		
							parent
							
								
									a4e115d12d
								
							
						
					
					
						commit
						a275ed62d5
					
				|  | @ -13,7 +13,6 @@ import ( | ||||||
| 	"github.com/VirtusLab/jenkins-operator/pkg/log" | 	"github.com/VirtusLab/jenkins-operator/pkg/log" | ||||||
| 
 | 
 | ||||||
| 	"github.com/go-logr/logr" | 	"github.com/go-logr/logr" | ||||||
| 	"github.com/pkg/errors" |  | ||||||
| 	corev1 "k8s.io/api/core/v1" | 	corev1 "k8s.io/api/core/v1" | ||||||
| 	apierrors "k8s.io/apimachinery/pkg/api/errors" | 	apierrors "k8s.io/apimachinery/pkg/api/errors" | ||||||
| 	"k8s.io/apimachinery/pkg/api/resource" | 	"k8s.io/apimachinery/pkg/api/resource" | ||||||
|  | @ -28,9 +27,12 @@ import ( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const ( | const ( | ||||||
| 	ReasonBaseConfigurationSuccess event.Reason = "BaseConfigurationSuccess" | 	// reasonBaseConfigurationSuccess is the event which informs base configuration has been completed successfully
 | ||||||
| 	ReasonBaseConfigurationFailure event.Reason = "BaseConfigurationFailure" | 	reasonBaseConfigurationSuccess event.Reason = "BaseConfigurationSuccess" | ||||||
| 	ReasonCRValidationFailure      event.Reason = "CRValidationFailure" | 	// reasonUserConfigurationSuccess is the event which informs user configuration has been completed successfully
 | ||||||
|  | 	reasonUserConfigurationSuccess event.Reason = "BaseConfigurationFailure" | ||||||
|  | 	// reasonCRValidationFailure is the event which informs user has provided invalid configuration in Jenkins CR
 | ||||||
|  | 	reasonCRValidationFailure event.Reason = "CRValidationFailure" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // Add creates a new Jenkins Controller and adds it to the Manager. The Manager will set fields on the Controller
 | // Add creates a new Jenkins Controller and adds it to the Manager. The Manager will set fields on the Controller
 | ||||||
|  | @ -138,34 +140,31 @@ func (r *ReconcileJenkins) reconcile(request reconcile.Request, logger logr.Logg | ||||||
| 
 | 
 | ||||||
| 	valid, err := baseConfiguration.Validate(jenkins) | 	valid, err := baseConfiguration.Validate(jenkins) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		r.events.Emitf(jenkins, event.TypeWarning, ReasonBaseConfigurationFailure, "Base configuration failed: %s", err) | 		return reconcile.Result{}, err | ||||||
| 		return reconcile.Result{}, errors.Wrap(err, "Base configuration failed") |  | ||||||
| 	} | 	} | ||||||
| 	if !valid { | 	if !valid { | ||||||
| 		r.events.Emit(jenkins, event.TypeWarning, ReasonCRValidationFailure, "Base CR validation failed") | 		r.events.Emit(jenkins, event.TypeWarning, reasonCRValidationFailure, "Base CR validation failed") | ||||||
| 		logger.V(log.VWarn).Info("Validation of base configuration failed, please correct Jenkins CR") | 		logger.V(log.VWarn).Info("Validation of base configuration failed, please correct Jenkins CR") | ||||||
| 		return reconcile.Result{}, nil // don't requeue
 | 		return reconcile.Result{}, nil // don't requeue
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	result, jenkinsClient, err := baseConfiguration.Reconcile() | 	result, jenkinsClient, err := baseConfiguration.Reconcile() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		r.events.Emitf(jenkins, event.TypeWarning, ReasonBaseConfigurationFailure, "Base configuration failed: %s", err) | 		return reconcile.Result{}, err | ||||||
| 		return reconcile.Result{}, errors.Wrap(err, "Base configuration failed") |  | ||||||
| 	} | 	} | ||||||
| 	if result.Requeue { | 	if result.Requeue { | ||||||
| 		return result, nil | 		return result, nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if jenkins.Status.BaseConfigurationCompletedTime == nil { | 	if jenkins.Status.BaseConfigurationCompletedTime == nil { | ||||||
| 		logger.Info("Base configuration phase is complete") |  | ||||||
| 		now := metav1.Now() | 		now := metav1.Now() | ||||||
| 		jenkins.Status.BaseConfigurationCompletedTime = &now | 		jenkins.Status.BaseConfigurationCompletedTime = &now | ||||||
| 		err = r.client.Update(context.TODO(), jenkins) | 		err = r.client.Update(context.TODO(), jenkins) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return reconcile.Result{}, err | 			return reconcile.Result{}, err | ||||||
| 		} | 		} | ||||||
| 		r.events.Emit(jenkins, event.TypeNormal, ReasonBaseConfigurationSuccess, "Base configuration completed") | 		logger.Info("Base configuration phase is complete") | ||||||
| 		logger.Info("Base configuration completed time has been updated") | 		r.events.Emit(jenkins, event.TypeNormal, reasonBaseConfigurationSuccess, "Base configuration completed") | ||||||
| 	} | 	} | ||||||
| 	// Reconcile user configuration
 | 	// Reconcile user configuration
 | ||||||
| 	userConfiguration := user.New(r.client, jenkinsClient, logger, jenkins) | 	userConfiguration := user.New(r.client, jenkinsClient, logger, jenkins) | ||||||
|  | @ -176,33 +175,33 @@ func (r *ReconcileJenkins) reconcile(request reconcile.Request, logger logr.Logg | ||||||
| 	} | 	} | ||||||
| 	if !valid { | 	if !valid { | ||||||
| 		logger.V(log.VWarn).Info("Validation of user configuration failed, please correct Jenkins CR") | 		logger.V(log.VWarn).Info("Validation of user configuration failed, please correct Jenkins CR") | ||||||
| 		r.events.Emit(jenkins, event.TypeWarning, ReasonCRValidationFailure, "User CR validation failed") | 		r.events.Emit(jenkins, event.TypeWarning, reasonCRValidationFailure, "User CR validation failed") | ||||||
| 		return reconcile.Result{}, nil // don't requeue
 | 		return reconcile.Result{}, nil // don't requeue
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	result, err = userConfiguration.Reconcile() | 	result, err = userConfiguration.Reconcile() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return reconcile.Result{}, errors.Wrap(err, "Base configuration failed") | 		return reconcile.Result{}, err | ||||||
| 	} | 	} | ||||||
| 	if result.Requeue { | 	if result.Requeue { | ||||||
| 		return result, nil | 		return result, nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if jenkins.Status.UserConfigurationCompletedTime == nil { | 	if jenkins.Status.UserConfigurationCompletedTime == nil { | ||||||
| 		logger.Info("User configuration phase is complete") |  | ||||||
| 		now := metav1.Now() | 		now := metav1.Now() | ||||||
| 		jenkins.Status.UserConfigurationCompletedTime = &now | 		jenkins.Status.UserConfigurationCompletedTime = &now | ||||||
| 		err = r.client.Update(context.TODO(), jenkins) | 		err = r.client.Update(context.TODO(), jenkins) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return reconcile.Result{}, err | 			return reconcile.Result{}, err | ||||||
| 		} | 		} | ||||||
| 		logger.Info("User configuration completed time has been updated") | 		logger.Info("User configuration phase is complete") | ||||||
|  | 		r.events.Emit(jenkins, event.TypeNormal, reasonUserConfigurationSuccess, "User configuration completed") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return reconcile.Result{}, nil | 	return reconcile.Result{}, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (*ReconcileJenkins) buildLogger(jenkinsName string) logr.Logger { | func (r *ReconcileJenkins) buildLogger(jenkinsName string) logr.Logger { | ||||||
| 	return log.Log.WithValues("cr", jenkinsName) | 	return log.Log.WithValues("cr", jenkinsName) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -5,7 +5,6 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/VirtusLab/jenkins-operator/pkg/controller/jenkins/constants" | 	"github.com/VirtusLab/jenkins-operator/pkg/controller/jenkins/constants" | ||||||
| 
 | 
 | ||||||
| 	"github.com/golang/glog" |  | ||||||
| 	"k8s.io/api/core/v1" | 	"k8s.io/api/core/v1" | ||||||
| 	"k8s.io/apimachinery/pkg/runtime" | 	"k8s.io/apimachinery/pkg/runtime" | ||||||
| 	"k8s.io/client-go/kubernetes" | 	"k8s.io/client-go/kubernetes" | ||||||
|  | @ -16,15 +15,19 @@ import ( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const ( | const ( | ||||||
| 	// Information only and will not cause any problems
 | 	// TypeNormal is the information event type
 | ||||||
| 	TypeNormal = Type("Normal") | 	TypeNormal = Type("Normal") | ||||||
| 	// These events are to warn that something might go wrong
 | 	// TypeWarning is the warning event type, informs that something went wrong
 | ||||||
| 	TypeWarning = Type("Warning") | 	TypeWarning = Type("Warning") | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | // Type is the type of event
 | ||||||
| type Type string | type Type string | ||||||
|  | 
 | ||||||
|  | // Reason is the type of reason message, used in evant
 | ||||||
| type Reason string | type Reason string | ||||||
| 
 | 
 | ||||||
|  | // Recorder is the interface used to emit events
 | ||||||
| type Recorder interface { | type Recorder interface { | ||||||
| 	Emit(object runtime.Object, eventType Type, reason Reason, message string) | 	Emit(object runtime.Object, eventType Type, reason Reason, message string) | ||||||
| 	Emitf(object runtime.Object, eventType Type, reason Reason, format string, args ...interface{}) | 	Emitf(object runtime.Object, eventType Type, reason Reason, format string, args ...interface{}) | ||||||
|  | @ -34,6 +37,7 @@ type recorder struct { | ||||||
| 	recorder record.EventRecorder | 	recorder record.EventRecorder | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // New returns recorder used to emit events
 | ||||||
| func New(config *rest.Config) (Recorder, error) { | func New(config *rest.Config) (Recorder, error) { | ||||||
| 	eventRecorder, err := initializeEventRecorder(config) | 	eventRecorder, err := initializeEventRecorder(config) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | @ -51,7 +55,7 @@ func initializeEventRecorder(config *rest.Config) (record.EventRecorder, error) | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	eventBroadcaster := record.NewBroadcaster() | 	eventBroadcaster := record.NewBroadcaster() | ||||||
| 	eventBroadcaster.StartLogging(glog.Infof) | 	//eventBroadcaster.StartLogging(glog.Infof) TODO integrate with proper logger
 | ||||||
| 	eventBroadcaster.StartRecordingToSink( | 	eventBroadcaster.StartRecordingToSink( | ||||||
| 		&typedcorev1.EventSinkImpl{ | 		&typedcorev1.EventSinkImpl{ | ||||||
| 			Interface: client.CoreV1().Events("")}) | 			Interface: client.CoreV1().Events("")}) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue