Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/flagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var (
ver bool
kubeconfigServiceMesh string
clusterName string
noCrossNamespaceRefs bool
)

func init() {
Expand Down Expand Up @@ -117,6 +118,7 @@ func init() {
flag.BoolVar(&ver, "version", false, "Print version")
flag.StringVar(&kubeconfigServiceMesh, "kubeconfig-service-mesh", "", "Path to a kubeconfig for the service mesh control plane cluster.")
flag.StringVar(&clusterName, "cluster-name", "", "Cluster name to be included in alert msgs.")
flag.BoolVar(&noCrossNamespaceRefs, "no-cross-namespace-refs", false, "When set to true, Flagger can only refer to resources in the same namespace.")
}

func main() {
Expand Down Expand Up @@ -241,6 +243,7 @@ func main() {
version.VERSION,
fromEnv("EVENT_WEBHOOK_URL", eventWebhook),
clusterName,
noCrossNamespaceRefs,
)

// leader election context
Expand Down
21 changes: 18 additions & 3 deletions pkg/apis/flagger/v1beta1/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ type CanarySpec struct {
MetricsServer string `json:"metricsServer,omitempty"`

// TargetRef references a target resource
TargetRef CrossNamespaceObjectReference `json:"targetRef"`
TargetRef LocalObjectReference `json:"targetRef"`

// AutoscalerRef references an autoscaling resource
// +optional
AutoscalerRef *CrossNamespaceObjectReference `json:"autoscalerRef,omitempty"`
AutoscalerRef *LocalObjectReference `json:"autoscalerRef,omitempty"`

// Reference to NGINX ingress resource
// +optional
IngressRef *CrossNamespaceObjectReference `json:"ingressRef,omitempty"`
IngressRef *LocalObjectReference `json:"ingressRef,omitempty"`

// Reference to Gloo Upstream resource. Upstream config is copied from
// the referenced upstream to the upstreams generated by flagger.
Expand Down Expand Up @@ -393,6 +393,21 @@ type CrossNamespaceObjectReference struct {
Namespace string `json:"namespace,omitempty"`
}

// LocalObjectReference contains enough information to let you locate the typed
// referenced object in the same namespace.
type LocalObjectReference struct {
// API version of the referent
// +optional
APIVersion string `json:"apiVersion,omitempty"`

// Kind of the referent
// +optional
Kind string `json:"kind,omitempty"`

// Name of the referent
Name string `json:"name"`
}

// CustomMetadata holds labels and annotations to set on generated objects.
type CustomMetadata struct {
Labels map[string]string `json:"labels,omitempty"`
Expand Down
20 changes: 18 additions & 2 deletions pkg/apis/flagger/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/canary/daemonset_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func newDaemonSetControllerTestCanary(dc daemonsetConfigs) *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: dc.name,
APIVersion: "apps/v1",
Kind: "DaemonSet",
Expand Down
4 changes: 2 additions & 2 deletions pkg/canary/deployment_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,12 @@ func newDeploymentControllerTestCanary(cc canaryConfigs) *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: cc.targetName,
APIVersion: "apps/v1",
Kind: "Deployment",
},
AutoscalerRef: &flaggerv1.CrossNamespaceObjectReference{
AutoscalerRef: &flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "autoscaling/v2beta2",
Kind: "HorizontalPodAutoscaler",
Expand Down
75 changes: 39 additions & 36 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,25 @@ const controllerAgentName = "flagger"

// Controller is managing the canary objects and schedules canary deployments
type Controller struct {
kubeClient kubernetes.Interface
flaggerClient clientset.Interface
flaggerInformers Informers
flaggerSynced cache.InformerSynced
flaggerWindow time.Duration
workqueue workqueue.RateLimitingInterface
eventRecorder record.EventRecorder
logger *zap.SugaredLogger
canaries *sync.Map
jobs map[string]CanaryJob
recorder metrics.Recorder
notifier notifier.Interface
canaryFactory *canary.Factory
routerFactory *router.Factory
observerFactory *observers.Factory
meshProvider string
eventWebhook string
clusterName string
kubeClient kubernetes.Interface
flaggerClient clientset.Interface
flaggerInformers Informers
flaggerSynced cache.InformerSynced
flaggerWindow time.Duration
workqueue workqueue.RateLimitingInterface
eventRecorder record.EventRecorder
logger *zap.SugaredLogger
canaries *sync.Map
jobs map[string]CanaryJob
recorder metrics.Recorder
notifier notifier.Interface
canaryFactory *canary.Factory
routerFactory *router.Factory
observerFactory *observers.Factory
meshProvider string
eventWebhook string
clusterName string
noCrossNamespaceRefs bool
}

type Informers struct {
Expand All @@ -89,6 +90,7 @@ func NewController(
version string,
eventWebhook string,
clusterName string,
noCrossNamespaceRefs bool,
) *Controller {
logger.Debug("Creating event broadcaster")
flaggerscheme.AddToScheme(scheme.Scheme)
Expand All @@ -103,24 +105,25 @@ func NewController(
recorder.SetInfo(version, meshProvider)

ctrl := &Controller{
kubeClient: kubeClient,
flaggerClient: flaggerClient,
flaggerInformers: flaggerInformers,
flaggerSynced: flaggerInformers.CanaryInformer.Informer().HasSynced,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), controllerAgentName),
eventRecorder: eventRecorder,
logger: logger,
canaries: new(sync.Map),
jobs: map[string]CanaryJob{},
flaggerWindow: flaggerWindow,
observerFactory: observerFactory,
recorder: recorder,
notifier: notifier,
canaryFactory: canaryFactory,
routerFactory: routerFactory,
meshProvider: meshProvider,
eventWebhook: eventWebhook,
clusterName: clusterName,
kubeClient: kubeClient,
flaggerClient: flaggerClient,
flaggerInformers: flaggerInformers,
flaggerSynced: flaggerInformers.CanaryInformer.Informer().HasSynced,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), controllerAgentName),
eventRecorder: eventRecorder,
logger: logger,
canaries: new(sync.Map),
jobs: map[string]CanaryJob{},
flaggerWindow: flaggerWindow,
observerFactory: observerFactory,
recorder: recorder,
notifier: notifier,
canaryFactory: canaryFactory,
routerFactory: routerFactory,
meshProvider: meshProvider,
eventWebhook: eventWebhook,
clusterName: clusterName,
noCrossNamespaceRefs: noCrossNamespaceRefs,
}

flaggerInformers.CanaryInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ func (c *Controller) alert(canary *flaggerv1.Canary, message string, metadata bo

// determine alert provider namespace
providerNamespace := canary.GetNamespace()
if alert.ProviderRef.Namespace != "" {
if alert.ProviderRef.Namespace != canary.Namespace {
if c.noCrossNamespaceRefs {
c.logger.With("canary", fmt.Sprintf("%s.%s", canary.Name, canary.Namespace)).
Errorf("can't access alert provider ref %s.%s, cross-namespace references are blocked", alert.ProviderRef.Name, alert.ProviderRef.Namespace)
return
}
providerNamespace = alert.ProviderRef.Namespace
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/scheduler_daemonset_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func newDaemonSetTestCanary() *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "apps/v1",
Kind: "DaemonSet",
Expand Down Expand Up @@ -318,7 +318,7 @@ func newDaemonSetTestCanaryAB() *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "apps/v1",
Kind: "DaemonSet",
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/scheduler_deployment_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ func newDeploymentTestCanary() *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "apps/v1",
Kind: "Deployment",
},
AutoscalerRef: &flaggerv1.CrossNamespaceObjectReference{
AutoscalerRef: &flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "autoscaling/v2beta2",
Kind: "HorizontalPodAutoscaler",
Expand Down Expand Up @@ -351,12 +351,12 @@ func newDeploymentTestCanaryAB() *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "apps/v1",
Kind: "Deployment",
},
AutoscalerRef: &flaggerv1.CrossNamespaceObjectReference{
AutoscalerRef: &flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "autoscaling/v2beta2",
Kind: "HorizontalPodAutoscaler",
Expand Down
11 changes: 9 additions & 2 deletions pkg/controller/scheduler_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func (c *Controller) checkMetricProviderAvailability(canary *flaggerv1.Canary) e

if metric.TemplateRef != nil {
namespace := canary.Namespace
if metric.TemplateRef.Namespace != "" {
if metric.TemplateRef.Namespace != canary.Namespace {
if c.noCrossNamespaceRefs {
return fmt.Errorf("can't access metric template ref %s.%s, cross-namespace references are blocked", metric.TemplateRef.Name, metric.TemplateRef.Namespace)
}
namespace = metric.TemplateRef.Namespace
}

Expand Down Expand Up @@ -238,7 +241,11 @@ func (c *Controller) runMetricChecks(canary *flaggerv1.Canary) bool {
for _, metric := range canary.GetAnalysis().Metrics {
if metric.TemplateRef != nil {
namespace := canary.Namespace
if metric.TemplateRef.Namespace != "" {
if metric.TemplateRef.Namespace != canary.Namespace {
if c.noCrossNamespaceRefs {
c.recordEventErrorf(canary, "Metric template %s.%s error: cross-namespace references are blocked", metric.TemplateRef.Name, metric.TemplateRef.Namespace)
return false
}
namespace = metric.TemplateRef.Namespace
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/scheduler_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@ func TestController_checkMetricProviderAvailability(t *testing.T) {
Namespace: "default",
}
require.NoError(t, ctrl.checkMetricProviderAvailability(canary))

ctrl.noCrossNamespaceRefs = true
canary.Namespace = "test"
require.Error(t, ctrl.checkMetricProviderAvailability(canary))
})
}
8 changes: 4 additions & 4 deletions pkg/controller/scheduler_svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func newTestServiceCanary() *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "core/v1",
Kind: "Service",
Expand Down Expand Up @@ -161,7 +161,7 @@ func newTestServiceCanaryMaxWeight() *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "core/v1",
Kind: "Service",
Expand Down Expand Up @@ -199,7 +199,7 @@ func newTestServiceCanaryWithWeightsHappyCase() *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "core/v1",
Kind: "Service",
Expand Down Expand Up @@ -236,7 +236,7 @@ func newTestServiceCanaryWithWeightsOverflow() *flaggerv1.Canary {
Name: "podinfo",
},
Spec: flaggerv1.CanarySpec{
TargetRef: flaggerv1.CrossNamespaceObjectReference{
TargetRef: flaggerv1.LocalObjectReference{
Name: "podinfo",
APIVersion: "core/v1",
Kind: "Service",
Expand Down
9 changes: 0 additions & 9 deletions pkg/router/gateway_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ func (gwr *GatewayAPIRouter) Reconcile(canary *flaggerv1.Canary) error {
apexSvcName, primarySvcName, canarySvcName := canary.GetServiceNames()

hrNamespace := canary.Namespace
if canary.Spec.TargetRef.Namespace != "" {
hrNamespace = canary.Spec.TargetRef.Namespace
}

hostNames := []v1alpha2.Hostname{}
for _, host := range canary.Spec.Service.Hosts {
Expand Down Expand Up @@ -193,9 +190,6 @@ func (gwr *GatewayAPIRouter) GetRoutes(canary *flaggerv1.Canary) (
) {
apexSvcName, primarySvcName, canarySvcName := canary.GetServiceNames()
hrNamespace := canary.Namespace
if canary.Spec.TargetRef.Namespace != "" {
hrNamespace = canary.Spec.TargetRef.Namespace
}
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1alpha2().HTTPRoutes(hrNamespace).Get(context.TODO(), apexSvcName, metav1.GetOptions{})
if err != nil {
err = fmt.Errorf("HTTPRoute %s.%s get error: %w", apexSvcName, hrNamespace, err)
Expand Down Expand Up @@ -228,9 +222,6 @@ func (gwr *GatewayAPIRouter) SetRoutes(
cWeight := int32(canaryWeight)
apexSvcName, primarySvcName, canarySvcName := canary.GetServiceNames()
hrNamespace := canary.Namespace
if canary.Spec.TargetRef.Namespace != "" {
hrNamespace = canary.Spec.TargetRef.Namespace
}
httpRoute, err := gwr.gatewayAPIClient.GatewayapiV1alpha2().HTTPRoutes(hrNamespace).Get(context.TODO(), apexSvcName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("HTTPRoute %s.%s get error: %w", apexSvcName, hrNamespace, err)
Expand Down
Loading