-
Notifications
You must be signed in to change notification settings - Fork 747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: EventSource and Sensor HA without extra RBAC #1163
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package leaderelection | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
"github.com/nats-io/graft" | ||
nats "github.com/nats-io/nats.go" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/viper" | ||
"go.uber.org/zap" | ||
|
||
"github.com/argoproj/argo-events/common" | ||
"github.com/argoproj/argo-events/common/logging" | ||
eventbusdriver "github.com/argoproj/argo-events/eventbus/driver" | ||
apicommon "github.com/argoproj/argo-events/pkg/apis/common" | ||
eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" | ||
) | ||
|
||
type Elector interface { | ||
RunOrDie(context.Context, LeaderCallbacks) | ||
} | ||
|
||
type LeaderCallbacks struct { | ||
OnStartedLeading func(context.Context) | ||
OnStoppedLeading func() | ||
} | ||
|
||
func NewEventBusElector(ctx context.Context, eventBusConfig eventbusv1alpha1.BusConfig, clusterName string, clusterSize int) (Elector, error) { | ||
logger := logging.FromContext(ctx) | ||
var eventBusType apicommon.EventBusType | ||
var eventBusAuth *eventbusv1alpha1.AuthStrategy | ||
if eventBusConfig.NATS != nil { | ||
eventBusType = apicommon.EventBusNATS | ||
eventBusAuth = eventBusConfig.NATS.Auth | ||
} else { | ||
return nil, errors.New("invalid event bus") | ||
} | ||
var auth *eventbusdriver.Auth | ||
cred := &eventbusdriver.AuthCredential{} | ||
if eventBusAuth == nil || *eventBusAuth == eventbusv1alpha1.AuthStrategyNone { | ||
auth = &eventbusdriver.Auth{ | ||
Strategy: eventbusv1alpha1.AuthStrategyNone, | ||
} | ||
} else { | ||
v := viper.New() | ||
v.SetConfigName("auth") | ||
v.SetConfigType("yaml") | ||
v.AddConfigPath(common.EventBusAuthFileMountPath) | ||
err := v.ReadInConfig() | ||
if err != nil { | ||
return nil, errors.Errorf("failed to load auth.yaml. err: %+v", err) | ||
} | ||
err = v.Unmarshal(cred) | ||
if err != nil { | ||
logger.Errorw("failed to unmarshal auth.yaml", zap.Error(err)) | ||
return nil, err | ||
} | ||
v.WatchConfig() | ||
v.OnConfigChange(func(e fsnotify.Event) { | ||
logger.Info("eventbus auth config file changed.") | ||
err = v.Unmarshal(cred) | ||
if err != nil { | ||
logger.Errorw("failed to unmarshal auth.yaml after reloading", zap.Error(err)) | ||
} | ||
}) | ||
auth = &eventbusdriver.Auth{ | ||
Strategy: *eventBusAuth, | ||
Crendential: cred, | ||
} | ||
} | ||
var elector Elector | ||
switch eventBusType { | ||
case apicommon.EventBusNATS: | ||
elector = &natsEventBusElector{ | ||
clusterName: clusterName, | ||
size: clusterSize, | ||
url: eventBusConfig.NATS.URL, | ||
auth: auth, | ||
} | ||
default: | ||
return nil, errors.New("invalid eventbus type") | ||
} | ||
return elector, nil | ||
} | ||
|
||
type natsEventBusElector struct { | ||
clusterName string | ||
size int | ||
url string | ||
auth *eventbusdriver.Auth | ||
} | ||
|
||
func (e *natsEventBusElector) RunOrDie(ctx context.Context, callbacks LeaderCallbacks) { | ||
log := logging.FromContext(ctx) | ||
ci := graft.ClusterInfo{Name: e.clusterName, Size: e.size} | ||
opts := &nats.DefaultOptions | ||
opts.Url = e.url | ||
if e.auth.Strategy == eventbusv1alpha1.AuthStrategyToken { | ||
opts.Token = e.auth.Crendential.Token | ||
} | ||
rpc, err := graft.NewNatsRpc(opts) | ||
if err != nil { | ||
log.Fatalw("failed to new Nats Rpc", zap.Error(err)) | ||
} | ||
errChan := make(chan error) | ||
stateChangeChan := make(chan graft.StateChange) | ||
handler := graft.NewChanHandler(stateChangeChan, errChan) | ||
node, err := graft.New(ci, handler, rpc, "/tmp/graft.log") | ||
if err != nil { | ||
log.Fatalw("failed to new a node", zap.Error(err)) | ||
} | ||
defer node.Close() | ||
|
||
cctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
if node.State() == graft.LEADER { | ||
log.Info("I'm the LEADER, starting ...") | ||
go callbacks.OnStartedLeading(cctx) | ||
} else { | ||
log.Info("Not the LEADER, stand by ...") | ||
} | ||
|
||
handleStateChange := func(sc graft.StateChange) { | ||
switch sc.To { | ||
case graft.LEADER: | ||
log.Info("I'm the LEADER, starting ...") | ||
go callbacks.OnStartedLeading(cctx) | ||
case graft.FOLLOWER, graft.CANDIDATE: | ||
log.Infof("Becoming a %v, stand by ...", sc.To) | ||
if sc.From == graft.LEADER { | ||
cancel() | ||
callbacks.OnStoppedLeading() | ||
cctx, cancel = context.WithCancel(ctx) | ||
} | ||
case graft.CLOSED: | ||
if sc.From == graft.LEADER { | ||
cancel() | ||
callbacks.OnStoppedLeading() | ||
} | ||
log.Fatal("Leader elector connection was CLOSED") | ||
default: | ||
log.Fatalf("Unknown state: %s", sc.To) | ||
} | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
log.Info("exiting...") | ||
return | ||
case sc := <-stateChangeChan: | ||
handleStateChange(sc) | ||
case err := <-errChan: | ||
log.Errorw("Error happened", zap.Error(err)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,9 +185,14 @@ func buildDeployment(args *AdaptorArgs, eventBus *eventbusv1alpha1.EventBus) (*a | |
}, | ||
}, | ||
}) | ||
emptyDirVolName := "tmp" | ||
volumes = append(volumes, corev1.Volume{ | ||
Name: emptyDirVolName, VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}, | ||
}) | ||
deploymentSpec.Template.Spec.Volumes = volumes | ||
volumeMounts := deploymentSpec.Template.Spec.Containers[0].VolumeMounts | ||
volumeMounts = append(volumeMounts, corev1.VolumeMount{Name: "auth-volume", MountPath: common.EventBusAuthFileMountPath}) | ||
volumeMounts = append(volumeMounts, corev1.VolumeMount{Name: emptyDirVolName, MountPath: "/tmp"}) | ||
deploymentSpec.Template.Spec.Containers[0].VolumeMounts = volumeMounts | ||
} | ||
} else { | ||
|
@@ -270,10 +275,6 @@ func buildDeploymentSpec(args *AdaptorArgs) (*appv1.DeploymentSpec, error) { | |
MatchLabels: args.Labels, | ||
}, | ||
Replicas: &replicas, | ||
Strategy: appv1.DeploymentStrategy{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With leader election, sensor deployments can also run with |
||
// Event bus does not allow multiple clients with same clientID to connect to the server at the same time. | ||
Type: appv1.RecreateDeploymentStrategyType, | ||
}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: podTemplateLabels, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this any more, with leader election, all the event source deployments can run with
rolling update
strategy.