-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for traffic router plugins
Signed-off-by: zachaller <[email protected]>
- Loading branch information
Showing
25 changed files
with
1,981 additions
and
526 deletions.
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"sync" | ||
|
||
"github.com/argoproj/argo-rollouts/rollout/trafficrouting/plugin/rpc" | ||
"github.com/argoproj/argo-rollouts/utils/plugin" | ||
goPlugin "github.com/hashicorp/go-plugin" | ||
) | ||
|
||
type trafficPlugin struct { | ||
pluginClient map[string]*goPlugin.Client | ||
plugin map[string]rpc.TrafficRouterPlugin | ||
} | ||
|
||
var pluginClients *trafficPlugin | ||
var once sync.Once | ||
|
||
func GetTrafficPlugin(pluginName string) (rpc.TrafficRouterPlugin, error) { | ||
once.Do(func() { | ||
pluginClients = &trafficPlugin{ | ||
pluginClient: make(map[string]*goPlugin.Client), | ||
plugin: make(map[string]rpc.TrafficRouterPlugin), | ||
} | ||
}) | ||
plugin, err := pluginClients.startPlugin(pluginName) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to start plugin system: %w", err) | ||
} | ||
return plugin, nil | ||
} | ||
|
||
func (t *trafficPlugin) startPlugin(pluginName string) (rpc.TrafficRouterPlugin, error) { | ||
var handshakeConfig = goPlugin.HandshakeConfig{ | ||
ProtocolVersion: 1, | ||
MagicCookieKey: "ARGO_ROLLOUTS_RPC_PLUGIN", | ||
MagicCookieValue: "trafficrouter", | ||
} | ||
|
||
// pluginMap is the map of plugins we can dispense. | ||
var pluginMap = map[string]goPlugin.Plugin{ | ||
"RpcTrafficRouterPlugin": &rpc.RpcTrafficRouterPlugin{}, | ||
} | ||
|
||
if t.pluginClient[pluginName] == nil || t.pluginClient[pluginName].Exited() { | ||
pluginPath, err := plugin.GetPluginLocation(pluginName) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to find plugin (%s): %w", pluginName, err) | ||
} | ||
|
||
t.pluginClient[pluginName] = goPlugin.NewClient(&goPlugin.ClientConfig{ | ||
HandshakeConfig: handshakeConfig, | ||
Plugins: pluginMap, | ||
Cmd: exec.Command(pluginPath), | ||
Managed: true, | ||
}) | ||
|
||
rpcClient, err := t.pluginClient[pluginName].Client() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Request the plugin | ||
plugin, err := rpcClient.Dispense("RpcTrafficRouterPlugin") | ||
if err != nil { | ||
return nil, err | ||
} | ||
t.plugin[pluginName] = plugin.(rpc.TrafficRouterPlugin) | ||
|
||
err = t.plugin[pluginName].NewTrafficRouterPlugin() | ||
if err.Error() != "" { | ||
return nil, err | ||
} | ||
} | ||
|
||
client, err := t.pluginClient[pluginName].Client() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := client.Ping(); err != nil { | ||
t.pluginClient[pluginName].Kill() | ||
t.pluginClient[pluginName] = nil | ||
return nil, fmt.Errorf("could not ping plugin will cleanup process so we can restart it next reconcile (%w)", err) | ||
} | ||
|
||
return t.plugin[pluginName], nil | ||
} |
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,102 @@ | ||
package plugin | ||
|
||
import ( | ||
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" | ||
"github.com/argoproj/argo-rollouts/rollout/trafficrouting/plugin/client" | ||
"github.com/argoproj/argo-rollouts/rollout/trafficrouting/plugin/rpc" | ||
"github.com/argoproj/argo-rollouts/utils/record" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
const ErrNotImplemented = "not-implemented" | ||
|
||
type ReconcilerConfig struct { | ||
Rollout *v1alpha1.Rollout | ||
PluginName string | ||
Client kubernetes.Interface | ||
Recorder record.EventRecorder | ||
} | ||
|
||
type Reconciler struct { | ||
Rollout *v1alpha1.Rollout | ||
PluginName string | ||
Client kubernetes.Interface | ||
Recorder record.EventRecorder | ||
rpc.TrafficRouterPlugin | ||
} | ||
|
||
func NewReconciler(cfg *ReconcilerConfig) (*Reconciler, error) { | ||
pluginClient, err := client.GetTrafficPlugin(cfg.PluginName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reconciler := &Reconciler{ | ||
Rollout: cfg.Rollout, | ||
Client: cfg.Client, | ||
Recorder: cfg.Recorder, | ||
PluginName: cfg.PluginName, | ||
TrafficRouterPlugin: pluginClient, | ||
} | ||
return reconciler, nil | ||
} | ||
|
||
// UpdateHash informs a traffic routing reconciler about new canary, stable, and additionalDestination(s) pod hashes | ||
func (r *Reconciler) UpdateHash(canaryHash, stableHash string, additionalDestinations ...v1alpha1.WeightDestination) error { | ||
err := r.TrafficRouterPlugin.UpdateHash(r.Rollout, canaryHash, stableHash, additionalDestinations) | ||
if err.Error() != "" { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// SetWeight sets the canary weight to the desired weight | ||
func (r *Reconciler) SetWeight(desiredWeight int32, additionalDestinations ...v1alpha1.WeightDestination) error { | ||
err := r.TrafficRouterPlugin.SetWeight(r.Rollout, desiredWeight, additionalDestinations) | ||
if err.Error() != "" { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// SetHeaderRoute sets the header routing step | ||
func (r *Reconciler) SetHeaderRoute(headerRouting *v1alpha1.SetHeaderRoute) error { | ||
err := r.TrafficRouterPlugin.SetHeaderRoute(r.Rollout, headerRouting) | ||
if err.Error() != "" { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// VerifyWeight returns true if the canary is at the desired weight and additionalDestinations are at the weights specified | ||
// Returns nil if weight verification is not supported or not applicable | ||
func (r *Reconciler) VerifyWeight(desiredWeight int32, additionalDestinations ...v1alpha1.WeightDestination) (*bool, error) { | ||
verified, err := r.TrafficRouterPlugin.VerifyWeight(r.Rollout, desiredWeight, additionalDestinations) | ||
if err.Error() != "" { | ||
// We do this to keep sematics with local implementations, rpc calls can not send a nil back in a *bool so they | ||
// send a *true with an error of ErrNotImplemented then we can wrap the response. | ||
if err.Error() == ErrNotImplemented { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
return verified, nil | ||
} | ||
|
||
// SetMirrorRoute sets up the traffic router to mirror traffic to a service | ||
func (r *Reconciler) SetMirrorRoute(setMirrorRoute *v1alpha1.SetMirrorRoute) error { | ||
err := r.TrafficRouterPlugin.SetMirrorRoute(r.Rollout, setMirrorRoute) | ||
if err.Error() != "" { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// RemoveManagedRoutes Removes all routes that are managed by rollouts by looking at spec.strategy.canary.trafficRouting.managedRoutes | ||
func (r *Reconciler) RemoveManagedRoutes() error { | ||
err := r.TrafficRouterPlugin.RemoveManagedRoutes(r.Rollout) | ||
if err.Error() != "" { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.