func containerImageRecurser()

in pkg/find/images.go [147:192]


func containerImageRecurser(fieldName string, parentFieldName string, elem interface{}, filter Filter, emit func(img string) string) *imageMod {
	switch elem := elem.(type) {
	case map[string]interface{}:
		if elem == nil {
			return nil
		}
		var changes []*imageMod
		for key, val := range elem {
			if o := containerImageRecurser(key, fieldName, val, filter, emit); o != nil {
				changes = append(changes, o)
			}
		}
		// replace the image-keys
		for _, c := range changes {
			elem[c.key] = c.img
		}
		return nil
	case string:
		// It looks like it's frequently true that the parent name for the
		// container object is 'container', 'containers' or
		// 'somethingContainer[s]'.
		if fieldName == "image" && (strings.Contains(parentFieldName, "container") || strings.Contains(parentFieldName, "Container")) ||
				fieldName == "url" && parentFieldName == "osImage" { // hack to make finding images work with NodeConfigs
			if filter == nil || filter(fieldName, parentFieldName, elem) {
				ret := emit(elem)
				if ret != elem {
					return &imageMod{fieldName, ret}
				}
			}
		}
		return nil
	case []interface{}:
		if elem == nil {
			return nil
		}
		for _, val := range elem {
			// Ignore any result here. image-fields should be singletons in maps.
			containerImageRecurser(fieldName, parentFieldName, val, filter, emit)
		}
		return nil
	case int64, bool, float64, nil, json.Number:
		return nil
	default:
		panic(fmt.Errorf("unexpected type; cannot tranverse %T", elem))
	}
}