Skip to content

Commit

Permalink
Add flag for maxReconciles
Browse files Browse the repository at this point in the history
  • Loading branch information
dviejokfs committed Dec 29, 2023
1 parent 4cdab0e commit 54a6a24
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 70 deletions.
32 changes: 20 additions & 12 deletions controllers/ca/ca_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
16 changes: 0 additions & 16 deletions controllers/mainchannel/mainchannel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 16 additions & 6 deletions controllers/ordnode/ordnode_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type FabricOrdererNodeReconciler struct {
AutoRenewCertificatesDelta time.Duration
Wait bool
Timeout time.Duration
MaxHistory int
}

const ordererNodeFinalizer = "finalizer.orderernode.hlf.kungfusoftware.es"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 15 additions & 6 deletions controllers/peer/peer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion dashboards/hlf-operator.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 26 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -75,34 +74,32 @@ 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.")
flag.DurationVar(&autoRenewIdentityCertificatesDelta, "auto-renew-identity-certificates-delta", 15*24*time.Hour, "The delta to renew FabricIdentity certificates before expiration. Default is 15 days.")
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)
log.Infof("Auto renew orderer certificates delta: %s", autoRenewOrdererCertificatesDelta)
// 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")
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 54a6a24

Please sign in to comment.