diff --git a/controllers/ca/ca_controller.go b/controllers/ca/ca_controller.go index d7f9498f..2c90e72a 100644 --- a/controllers/ca/ca_controller.go +++ b/controllers/ca/ca_controller.go @@ -19,6 +19,7 @@ import ( "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/release" "k8s.io/kubernetes/pkg/api/v1/pod" + "sigs.k8s.io/controller-runtime/pkg/controller" "sort" "math/big" @@ -51,13 +52,14 @@ import ( // FabricCAReconciler reconciles a FabricCA object type FabricCAReconciler struct { client.Client - ChartPath string - Log logr.Logger - Scheme *runtime.Scheme - Config *rest.Config - ClientSet *kubernetes.Clientset - Wait bool - Timeout time.Duration + ChartPath string + Log logr.Logger + Scheme *runtime.Scheme + Config *rest.Config + ClientSet *kubernetes.Clientset + Wait bool + Timeout time.Duration + MaxHistory int } func parseECDSAPrivateKey(contents []byte) (*ecdsa.PrivateKey, error) { @@ -844,6 +846,8 @@ func (r *FabricCAReconciler) finalizeCA(reqLogger logr.Logger, m *hlfv1alpha1.Fa releaseName := m.Name reqLogger.Info("Successfully finalized ca") cmd := action.NewUninstall(cfg) + cmd.Wait = r.Wait + cmd.Timeout = r.Timeout resp, err := cmd.Run(releaseName) if err != nil { if strings.Compare("Release not loaded", err.Error()) != 0 { @@ -979,9 +983,9 @@ func Reconcile( return ctrl.Result{}, err } cmd := action.NewUpgrade(cfg) - cmd.Timeout = 5 * time.Minute - cmd.Wait = false - cmd.MaxHistory = 10 + cmd.Timeout = r.Timeout + cmd.Wait = r.Wait + cmd.MaxHistory = r.MaxHistory settings := cli.New() chartPath, err := cmd.LocateChart(r.ChartPath, settings) ch, err := loader.Load(chartPath) @@ -1020,7 +1024,8 @@ func Reconcile( return ctrl.Result{}, err } cmd.ReleaseName = name - cmd.Wait = false + cmd.Wait = r.Wait + cmd.Timeout = r.Timeout ch, err := loader.Load(chart) if err != nil { return ctrl.Result{}, err @@ -1126,9 +1131,12 @@ func (r *FabricCAReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } -func (r *FabricCAReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *FabricCAReconciler) SetupWithManager(mgr ctrl.Manager, maxConcurrentReconciles int) error { return ctrl.NewControllerManagedBy(mgr). For(&hlfv1alpha1.FabricCA{}). Owns(&appsv1.Deployment{}). + WithOptions(controller.Options{ + MaxConcurrentReconciles: maxConcurrentReconciles, + }). Complete(r) } diff --git a/controllers/mainchannel/mainchannel_controller.go b/controllers/mainchannel/mainchannel_controller.go index 600cece9..ac62979b 100644 --- a/controllers/mainchannel/mainchannel_controller.go +++ b/controllers/mainchannel/mainchannel_controller.go @@ -16,7 +16,6 @@ import ( "github.com/hyperledger/fabric-config/protolator" "github.com/hyperledger/fabric-protos-go/common" cb "github.com/hyperledger/fabric-protos-go/common" - "github.com/hyperledger/fabric-protos-go/orderer/smartbft" "github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt" fab2 "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp" @@ -933,21 +932,6 @@ func (r *FabricMainChannelReconciler) mapToConfigTX(channel *hlfv1alpha1.FabricM ordConfigtx := configtx.Orderer{ OrdererType: "etcdraft", Organizations: ordererOrgs, - SmartBFT: &smartbft.Options{ - RequestBatchMaxCount: 100, - RequestBatchMaxInterval: "50ms", - RequestForwardTimeout: "2s", - RequestComplainTimeout: "20s", - RequestAutoRemoveTimeout: "3m0s", - ViewChangeResendInterval: "5s", - ViewChangeTimeout: "20s", - LeaderHeartbeatTimeout: "1m0s", - CollectTimeout: "1s", - RequestBatchMaxBytes: 10485760, - IncomingMessageBufferSize: 200, - RequestPoolSize: 100000, - LeaderHeartbeatCount: 10, - }, EtcdRaft: orderer.EtcdRaft{ Consenters: consenters, Options: etcdRaftOptions, diff --git a/controllers/ordnode/ordnode_controller.go b/controllers/ordnode/ordnode_controller.go index 1f0db77d..529558e6 100644 --- a/controllers/ordnode/ordnode_controller.go +++ b/controllers/ordnode/ordnode_controller.go @@ -54,6 +54,7 @@ type FabricOrdererNodeReconciler struct { AutoRenewCertificatesDelta time.Duration Wait bool Timeout time.Duration + MaxHistory int } const ordererNodeFinalizer = "finalizer.orderernode.hlf.kungfusoftware.es" @@ -70,6 +71,8 @@ func (r *FabricOrdererNodeReconciler) finalizeOrderer(reqLogger logr.Logger, m * releaseName := m.Name reqLogger.Info("Successfully finalized orderer") cmd := action.NewUninstall(cfg) + cmd.Wait = r.Wait + cmd.Timeout = r.Timeout resp, err := cmd.Run(releaseName) if err != nil { if strings.Compare("Release not loaded", err.Error()) != 0 { @@ -280,6 +283,9 @@ func (r *FabricOrdererNodeReconciler) Reconcile(ctx context.Context, req ctrl.Re } } else { cmd := action.NewInstall(cfg) + cmd.Wait = r.Wait + cmd.Timeout = r.Timeout + cmd.ReleaseName = releaseName name, chart, err := cmd.NameAndChart([]string{releaseName, r.ChartPath}) if err != nil { return ctrl.Result{}, err @@ -325,6 +331,11 @@ func (r *FabricOrdererNodeReconciler) Reconcile(ctx context.Context, req ctrl.Re Status: "True", LastTransitionTime: v1.Time{}, }) + err = r.Get(ctx, req.NamespacedName, fabricOrdererNode) + if err != nil { + reqLogger.Error(err, "Failed to get Orderer before updating it.") + return ctrl.Result{}, err + } if err := r.Status().Update(ctx, fabricOrdererNode); err != nil { return ctrl.Result{}, err } @@ -393,12 +404,12 @@ func (r *FabricOrdererNodeReconciler) setConditionStatus( return p.Status.Conditions.SetCondition(condition()) } -func (r *FabricOrdererNodeReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *FabricOrdererNodeReconciler) SetupWithManager(mgr ctrl.Manager, maxReconciles int) error { return ctrl.NewControllerManagedBy(mgr). For(&hlfv1alpha1.FabricOrdererNode{}). Owns(&appsv1.Deployment{}). WithOptions(controller.Options{ - MaxConcurrentReconciles: 1, + MaxConcurrentReconciles: maxReconciles, }). Complete(r) } @@ -451,7 +462,6 @@ func (r *FabricOrdererNodeReconciler) upgradeChart( return err } cmd := action.NewUpgrade(cfg) - cmd.MaxHistory = 5 err = os.Setenv("HELM_NAMESPACE", ns) if err != nil { return err @@ -465,9 +475,9 @@ func (r *FabricOrdererNodeReconciler) upgradeChart( if err != nil { return err } - cmd.Wait = false - cmd.MaxHistory = 10 - cmd.Timeout = time.Minute * 5 + cmd.Wait = r.Wait + cmd.Timeout = r.Timeout + cmd.MaxHistory = r.MaxHistory release, err := cmd.Run(releaseName, ch, inInterface) if err != nil { return err diff --git a/controllers/peer/peer_controller.go b/controllers/peer/peer_controller.go index d743d152..4ac23945 100644 --- a/controllers/peer/peer_controller.go +++ b/controllers/peer/peer_controller.go @@ -61,6 +61,7 @@ type FabricPeerReconciler struct { AutoRenewCertificatesDelta time.Duration Wait bool Timeout time.Duration + MaxHistory int } func (r *FabricPeerReconciler) addFinalizer(reqLogger logr.Logger, m *hlfv1alpha1.FabricPeer) error { @@ -509,6 +510,8 @@ func (r *FabricPeerReconciler) Reconcile(ctx context.Context, req ctrl.Request) } } else { cmd := action.NewInstall(cfg) + cmd.Wait = r.Wait + cmd.Timeout = r.Timeout name, chart, err := cmd.NameAndChart([]string{releaseName, r.ChartPath}) if err != nil { r.setConditionStatus(ctx, fabricPeer, hlfv1alpha1.FailedStatus, false, err, false) @@ -551,6 +554,11 @@ func (r *FabricPeerReconciler) Reconcile(ctx context.Context, req ctrl.Request) return r.updateCRStatusOrFailReconcile(ctx, r.Log, fabricPeer) } log.Infof("Chart installed %s", release.Name) + err = r.Get(ctx, req.NamespacedName, fabricPeer) + if err != nil { + r.setConditionStatus(ctx, fabricPeer, hlfv1alpha1.FailedStatus, false, err, false) + return r.updateCRStatusOrFailReconcile(ctx, r.Log, fabricPeer) + } fabricPeer.Status.Status = hlfv1alpha1.PendingStatus fabricPeer.Status.Conditions.SetCondition(status.Condition{ Type: "DEPLOYED", @@ -616,7 +624,6 @@ func (r *FabricPeerReconciler) upgradeChart( return err } cmd := action.NewUpgrade(cfg) - cmd.MaxHistory = 5 err = os.Setenv("HELM_NAMESPACE", ns) if err != nil { return err @@ -627,9 +634,9 @@ func (r *FabricPeerReconciler) upgradeChart( if err != nil { return err } - cmd.Wait = false - cmd.MaxHistory = 10 - cmd.Timeout = time.Minute * 5 + cmd.Wait = r.Wait + cmd.MaxHistory = r.MaxHistory + cmd.Timeout = r.Timeout log.Infof("Upgrading chart %s", inrec) release, err := cmd.Run(releaseName, ch, inInterface) if err != nil { @@ -1496,12 +1503,12 @@ func GetConfig( return &c, nil } -func (r *FabricPeerReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *FabricPeerReconciler) SetupWithManager(mgr ctrl.Manager, maxConcurrentReconciles int) error { return ctrl.NewControllerManagedBy(mgr). For(&hlfv1alpha1.FabricPeer{}). Owns(&appsv1.Deployment{}). WithOptions(controller.Options{ - MaxConcurrentReconciles: 1, + MaxConcurrentReconciles: maxConcurrentReconciles, }). Complete(r) } @@ -1534,6 +1541,8 @@ func (r *FabricPeerReconciler) finalizePeer(reqLogger logr.Logger, peer *hlfv1al releaseName := peer.Name reqLogger.Info("Successfully finalized peer") cmd := action.NewUninstall(cfg) + cmd.Wait = r.Wait + cmd.Timeout = r.Timeout resp, err := cmd.Run(releaseName) if err != nil { if strings.Compare("Release not loaded", err.Error()) != 0 { diff --git a/dashboards/hlf-operator.json b/dashboards/hlf-operator.json index 1fbd9228..4645a1c5 100644 --- a/dashboards/hlf-operator.json +++ b/dashboards/hlf-operator.json @@ -95,7 +95,7 @@ "targets": [ { "exemplar": true, - "expr": "hlf_operator_certificate_expiration_timestamp_seconds{exported_namespace=\"default\"} - time()", + "expr": "hlf_operator_certificate_expiration_timestamp_seconds{} - time()", "interval": "", "legendFormat": "{{exported_namespace}} / {{name}}", "refId": "A" diff --git a/go.mod b/go.mod index ee6250e3..81aea862 100644 --- a/go.mod +++ b/go.mod @@ -215,9 +215,6 @@ require ( sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect ) -replace ( - github.com/hyperledger/fabric-config => /Users/davidviejo/projects/kfs/fabric-config -) replace ( github.com/Azure/go-autorest => github.com/Azure/go-autorest v14.2.0+incompatible diff --git a/main.go b/main.go index 0de435ee..636cfb82 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,6 @@ import ( "path/filepath" "time" - "github.com/amplitude/analytics-go/amplitude" "github.com/kfsoftware/hlf-operator/controllers/chaincode" "github.com/kfsoftware/hlf-operator/controllers/console" "github.com/kfsoftware/hlf-operator/controllers/followerchannel" @@ -75,6 +74,10 @@ func main() { var autoRenewOrdererCertificatesDelta time.Duration var autoRenewPeerCertificatesDelta time.Duration var autoRenewIdentityCertificatesDelta time.Duration + var helmChartWait bool + var helmChartTimeout time.Duration + var maxHistory int + var maxReconciles int flag.StringVar(&metricsAddr, "metrics-addr", ":8090", "The address the metric endpoint binds to.") flag.DurationVar(&autoRenewOrdererCertificatesDelta, "auto-renew-orderer-certificates-delta", 15*24*time.Hour, "The delta to renew orderer certificates before expiration. Default is 15 days.") flag.DurationVar(&autoRenewPeerCertificatesDelta, "auto-renew-peer-certificates-delta", 15*24*time.Hour, "The delta to renew peer certificates before expiration. Default is 15 days.") @@ -82,11 +85,14 @@ func main() { flag.BoolVar(&autoRenewCertificatesPeerEnabled, "auto-renew-peer-certificates", false, "Enable auto renew certificates for orderer and peer nodes. Default is false.") flag.BoolVar(&autoRenewCertificatesOrdererEnabled, "auto-renew-orderer-certificates", false, "Enable auto renew certificates for orderer and peer nodes. Default is false.") flag.BoolVar(&autoRenewCertificatesIdentityEnabled, "auto-renew-identity-certificates", true, "Enable auto renew certificates for FabricIdentity. Default is true.") + flag.IntVar(&maxReconciles, "max-reconciles", 10, "Max reconciles for a resource. Default is 10.") + flag.BoolVar(&helmChartWait, "helm-chart-wait", false, "Wait for helm chart to be deployed. Default is false.") + flag.IntVar(&maxHistory, "helm-max-history", 10, "Max history for helm chart. Default is 10.") + flag.DurationVar(&helmChartTimeout, "helm-chart-timeout", 5*time.Minute, "Timeout for helm chart to be deployed. Default is 5 minutes.") flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") flag.Parse() - log.Infof("Auto renew peer certificates enabled: %t", autoRenewCertificatesPeerEnabled) log.Infof("Auto renew orderer certificates enabled: %t", autoRenewCertificatesOrdererEnabled) log.Infof("Auto renew peer certificates delta: %s", autoRenewPeerCertificatesDelta) @@ -94,15 +100,6 @@ func main() { // Pass a Config struct // to initialize a Client struct // which implements Client interface - analytics := amplitude.NewClient( - amplitude.NewConfig("569cfca546698061cf130f97745afca6"), - ) - // Track events in your application - analytics.Track(amplitude.Event{ - UserID: "user-id", - EventType: "Start operator", - EventProperties: map[string]interface{}{"source": "notification"}, - }) ctrl.SetLogger(zap.New(zap.UseDevMode(true))) kubeContext, exists := os.LookupEnv("KUBECONTEXT") @@ -142,9 +139,10 @@ func main() { Config: mgr.GetConfig(), AutoRenewCertificates: autoRenewCertificatesPeerEnabled, AutoRenewCertificatesDelta: autoRenewPeerCertificatesDelta, - Wait: false, - Timeout: 0, - }).SetupWithManager(mgr); err != nil { + Wait: helmChartWait, + Timeout: helmChartTimeout, + MaxHistory: maxHistory, + }).SetupWithManager(mgr, maxReconciles); err != nil { setupLog.Error(err, "unable to create controller", "controller", "FabricPeer") os.Exit(1) } @@ -159,15 +157,16 @@ func main() { os.Exit(1) } if err = (&ca.FabricCAReconciler{ - Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("FabricCA"), - Scheme: mgr.GetScheme(), - Config: mgr.GetConfig(), - ClientSet: clientSet, - ChartPath: caChartPath, - Wait: false, - Timeout: 0, - }).SetupWithManager(mgr); err != nil { + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("controllers").WithName("FabricCA"), + Scheme: mgr.GetScheme(), + Config: mgr.GetConfig(), + ClientSet: clientSet, + ChartPath: caChartPath, + Wait: helmChartWait, + Timeout: helmChartTimeout, + MaxHistory: maxHistory, + }).SetupWithManager(mgr, maxReconciles); err != nil { setupLog.Error(err, "unable to create controller", "controller", "FabricCA") os.Exit(1) } @@ -200,9 +199,10 @@ func main() { ChartPath: ordNodeChartPath, AutoRenewCertificates: autoRenewCertificatesOrdererEnabled, AutoRenewCertificatesDelta: autoRenewOrdererCertificatesDelta, - Wait: false, - Timeout: 0, - }).SetupWithManager(mgr); err != nil { + Wait: helmChartWait, + Timeout: helmChartTimeout, + MaxHistory: maxHistory, + }).SetupWithManager(mgr, maxReconciles); err != nil { setupLog.Error(err, "unable to create controller", "controller", "FabricOrdererNode") os.Exit(1) }