func()

in pkg/apis/camel/v1/build_types_support.go [255:308]


func (bl BuildList) HasMatchingBuild(build *Build) (bool, *Build) {
	required := build.BuilderDependencies()
	if len(required) == 0 {
		return false, nil
	}

	for _, b := range bl.Items {
		if b.Name == build.Name || b.Status.IsFinished() {
			continue
		}

		dependencies := b.BuilderDependencies()
		dependencyMap := make(map[string]int, len(dependencies))
		for i, item := range dependencies {
			dependencyMap[item] = i
		}

		allMatching := true
		missing := 0
		for _, item := range required {
			if _, ok := dependencyMap[item]; !ok {
				allMatching = false
				missing++
			}
		}

		// Heuristic approach: if there are too many unrelated libraries then this image is
		// not suitable to be used as base image
		if !allMatching && missing >= len(required)/2 {
			continue
		}

		// handle suitable build that has started already
		if b.Status.Phase == BuildPhasePending || b.Status.Phase == BuildPhaseRunning {
			return true, &b
		}

		// handle suitable scheduled build
		if b.Status.Phase == BuildPhaseInitialization || b.Status.Phase == BuildPhaseScheduling {
			if allMatching && len(required) == len(dependencies) {
				// seems like both builds require exactly the same list of dependencies
				// additionally check for the creation timestamp
				if b.CreationTimestamp.Before(&build.CreationTimestamp) {
					return true, &b
				}
			} else if missing > 0 {
				// found another suitable scheduled build with fewer dependencies that should build first in order to reuse the produced image
				return true, &b
			}
		}
	}

	return false, nil
}