diff --git a/api/vars.go b/api/vars.go index f4301bd..a2fa0ce 100644 --- a/api/vars.go +++ b/api/vars.go @@ -263,6 +263,7 @@ const ( // Headers and query params. const ( HeaderAdapter = "kubefox-adapter" + HeaderApp = "kubefox-app" HeaderAppDep = "kubefox-app-deployment" HeaderAppDepAbbrv = "kf-dep" HeaderAppDepShort = "kfd" @@ -272,6 +273,7 @@ const ( HeaderEventTypeShort = "kft" HeaderHost = "Host" HeaderPlatform = "kubefox-platform" + HeaderReleaseManifest = "kubefox-release-manifest" HeaderTraceId = "kubefox-trace-id" HeaderVirtualEnvironment = "kubefox-virtual-environment" HeaderVirtualEnvironmentAbbrv = "kf-env" diff --git a/components/broker/engine/broker.go b/components/broker/engine/broker.go index c581450..1e38c20 100644 --- a/components/broker/engine/broker.go +++ b/components/broker/engine/broker.go @@ -430,7 +430,6 @@ func (brk *broker) checkEvent(evt *BrokerEvent) error { (evt.Context.VirtualEnvironment != "" && evt.Context.AppDeployment == "" && evt.Context.ReleaseManifest == "") { - brk.log.DebugInterface("invalid context:", evt.Context) return core.ErrInvalid(fmt.Errorf("event context is invalid")) } } @@ -492,14 +491,14 @@ func (brk *broker) checkComponents(ctx context.Context, evt *BrokerEvent) error adapter, _ = evt.Adapters.GetByComponent(evt.Target) } - depComp := evt.Spec.Components[evt.Target.Name] + appComp := evt.Spec.Components[evt.Target.Name] switch { - case depComp == nil && adapter == nil: + case appComp == nil && adapter == nil: if !brk.store.IsGenesisAdapter(evt.Target) { return core.ErrComponentMismatch(fmt.Errorf("target component not part of app")) } - case depComp == nil && adapter != nil: + case appComp == nil && adapter != nil: if adapter.GetComponentType() != api.ComponentTypeHTTPAdapter { return core.ErrUnsupportedAdapter( fmt.Errorf("adapter type '%s' is not supported", adapter.GetComponentType())) @@ -507,12 +506,12 @@ func (brk *broker) checkComponents(ctx context.Context, evt *BrokerEvent) error evt.TargetAdapter = adapter case evt.Target.Commit == "" && evt.RouteId == api.DefaultRouteId: - evt.Target.Commit = depComp.Commit - if !depComp.DefaultHandler { + evt.Target.Commit = appComp.Commit + if !appComp.DefaultHandler { return core.ErrRouteNotFound(fmt.Errorf("target component does not have default handler")) } - case evt.Target.Commit != depComp.Commit: + case evt.Target.Commit != appComp.Commit: return core.ErrComponentMismatch(fmt.Errorf("target component commit does not match app")) } @@ -520,19 +519,20 @@ func (brk *broker) checkComponents(ctx context.Context, evt *BrokerEvent) error if evt.Adapters != nil { adapter, _ = evt.Adapters.GetByComponent(evt.Source) } - depComp = evt.Spec.Components[evt.Source.Name] + + appComp = evt.Spec.Components[evt.Source.Name] switch { - case depComp == nil && adapter == nil: + case appComp == nil && adapter == nil: if !brk.store.IsGenesisAdapter(evt.Source) { return core.ErrComponentMismatch(fmt.Errorf("source component not part of app")) } - case depComp == nil && adapter != nil: + case appComp == nil && adapter != nil: if evt.Source.BrokerId != brk.comp.BrokerId { return core.ErrBrokerMismatch(fmt.Errorf("source component is adapter but does not match broker")) } - case evt.Source.Commit != depComp.Commit: + case evt.Source.Commit != appComp.Commit: return core.ErrComponentMismatch(fmt.Errorf("source component commit does not match app")) } diff --git a/components/broker/engine/store.go b/components/broker/engine/store.go index 9dceedd..d114c86 100644 --- a/components/broker/engine/store.go +++ b/components/broker/engine/store.go @@ -340,17 +340,16 @@ func (str *Store) OnDelete(obj interface{}) { func (str *Store) onChange(obj interface{}, op string) { str.log.Debugf("%T %s", obj, op) - isAppDep := false + updateComps := false switch obj.(type) { - case *v1alpha1.AppDeployment: - isAppDep = true - + case *v1alpha1.AppDeployment, *v1alpha1.ReleaseManifest: + updateComps = true case *v1alpha1.HTTPAdapter: // Clear adapters to force reload on next use. str.adapters = nil } - go str.updateCaches(context.Background(), isAppDep) + go str.updateCaches(context.Background(), updateComps) } func (str *Store) updateCaches(ctx context.Context, updateComps bool) error { @@ -381,12 +380,12 @@ func (str *Store) updateCaches(ctx context.Context, updateComps bool) error { func (str *Store) buildComponentCache(ctx context.Context) (cache.Cache[*api.ComponentDefinition], error) { compCache := cache.New[*api.ComponentDefinition](time.Hour * 24) - list := &v1alpha1.AppDeploymentList{} - if err := str.resCache.List(ctx, list); err != nil { + appDepList := &v1alpha1.AppDeploymentList{} + if err := str.resCache.List(ctx, appDepList); err != nil { return nil, err } - for _, appDep := range list.Items { + for _, appDep := range appDepList.Items { for compName, compSpec := range appDep.Spec.Components { comp := &core.Component{ Type: string(compSpec.Type), @@ -397,6 +396,35 @@ func (str *Store) buildComponentCache(ctx context.Context) (cache.Cache[*api.Com } } + envList := &v1alpha1.VirtualEnvironmentList{} + if err := str.resCache.List(ctx, envList); err != nil { + return nil, err + } + + for _, env := range envList.Items { + active := env.Status.ActiveRelease + if active == nil { + continue + } + + manifest := &v1alpha1.ReleaseManifest{} + if err := str.get(active.ReleaseManifest, manifest, true); err != nil { + str.log.Warn(err) + continue + } + + for _, app := range manifest.Spec.Apps { + for compName, compSpec := range app.AppDeployment.Spec.Components { + comp := &core.Component{ + Type: string(compSpec.Type), + Name: compName, + Commit: compSpec.Commit, + } + compCache.Set(comp.GroupKey(), compSpec) + } + } + } + p := &v1alpha1.Platform{} if err := str.resCache.Get(ctx, k8s.Key(config.Namespace, config.Platform), p); err != nil { return nil, err @@ -455,18 +483,13 @@ func (str *Store) buildReleaseMatcher(ctx context.Context) (*matcher.EventMatche } for _, app := range manifest.Spec.Apps { - appDep, err := str.AppDeployment(ctx, app.AppDeployment.Name) - if err != nil { - str.log.Warn(err) - continue - } data, err := str.Data(ctx, evtCtx) if err != nil { str.log.Warn(err) continue } - routes, err := str.buildRoutes(ctx, &appDep.Spec, data, evtCtx) + routes, err := str.buildRoutes(ctx, &app.AppDeployment.Spec, data, evtCtx) if err != nil { str.log.Warn(err) continue diff --git a/components/httpsrv/server/server.go b/components/httpsrv/server/server.go index 1b6e4a0..e7ad33b 100644 --- a/components/httpsrv/server/server.go +++ b/components/httpsrv/server/server.go @@ -138,10 +138,10 @@ func (srv *Server) ServeHTTP(resWriter http.ResponseWriter, httpReq *http.Reques if resp != nil && resp.Context != nil { setHeader(resWriter, api.HeaderPlatform, resp.Context.Platform) setHeader(resWriter, api.HeaderVirtualEnvironment, resp.Context.VirtualEnvironment) + setHeader(resWriter, api.HeaderApp, resp.Context.App) setHeader(resWriter, api.HeaderAppDep, resp.Context.AppDeployment) - if resp.TraceId() != "" { - resWriter.Header().Set(api.HeaderTraceId, resp.TraceId()) - } + setHeader(resWriter, api.HeaderReleaseManifest, resp.Context.ReleaseManifest) + setHeader(resWriter, api.HeaderTraceId, resp.TraceId()) } switch { diff --git a/components/operator/controller/component_mgr.go b/components/operator/controller/component_mgr.go index d3a6e61..08de9bd 100644 --- a/components/operator/controller/component_mgr.go +++ b/components/operator/controller/component_mgr.go @@ -483,6 +483,10 @@ func availableCondition( condition.Status = metav1.ConditionTrue condition.Reason = api.ConditionReasonComponentsAvailable condition.Message = "Component Deployments have minimum required Pods available." + default: + condition.Status = metav1.ConditionFalse + condition.Reason = api.ConditionReasonProblemsFound + condition.Message = "One or more problems found, see `status.problems` for details." } return condition, problems @@ -547,6 +551,10 @@ func progressingCondition( condition.Status = metav1.ConditionFalse condition.Reason = api.ConditionReasonComponentsDeployed condition.Message = "Component Deployments completed successfully." + default: + condition.Status = metav1.ConditionFalse + condition.Reason = api.ConditionReasonProblemsFound + condition.Message = "One or more problems found, see `status.problems` for details." } return condition, problems