Skip to content

Commit

Permalink
bug fix for missing comps from release manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Jan 20, 2024
1 parent 4960745 commit 8c156e4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 28 deletions.
2 changes: 2 additions & 0 deletions api/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
22 changes: 11 additions & 11 deletions components/broker/engine/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
Expand Down Expand Up @@ -492,47 +491,48 @@ 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()))
}
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"))
}

// Check if source is part of deployment spec.
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"))
}

Expand Down
51 changes: 37 additions & 14 deletions components/broker/engine/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions components/httpsrv/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions components/operator/controller/component_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8c156e4

Please sign in to comment.