func newFilter()

in pkg/scheduler/placement/filter.go [131:214]


func newFilter(conf configs.Filter) Filter {
	filter := Filter{
		userList:  map[string]bool{},
		groupList: map[string]bool{},
		empty:     true,
	}
	// type can only be '' , allow or deny.
	filter.allow = conf.Type != filterDeny

	var err error
	// create the user list or regexp
	if len(conf.Users) == 1 {
		user := conf.Users[0]
		// check for regexp characters that cannot be in a user
		if configs.SpecialRegExp.MatchString(user) {
			filter.userExp, err = regexp.Compile(user)
			if err != nil {
				log.Log(log.Config).Debug("Filter user expression does not compile", zap.Any("userFilter", conf.Users))
			}
		} else if configs.UserRegExp.MatchString(user) {
			// regexp not found consider this a user, sanity check the entry
			// single entry just a user
			filter.userList[user] = true
		}
		filter.empty = false
	}
	// if there are 2 or more users create a list
	if len(conf.Users) >= 2 {
		for _, user := range conf.Users {
			// sanity check the entry, do not add if it does not comply
			if configs.UserRegExp.MatchString(user) {
				filter.userList[user] = true
			}
		}
		if len(filter.userList) != len(conf.Users) {
			log.Log(log.Config).Info("Filter creation duplicate or invalid users found", zap.Any("userFilter", conf.Users))
		}
		filter.empty = false
	}

	// check what we have created
	if len(conf.Users) > 0 && filter.userExp == nil && len(filter.userList) == 0 {
		log.Log(log.Config).Info("Filter creation partially failed (user)", zap.Any("userFilter", conf.Users))
	}

	// create the group list or regexp
	if len(conf.Groups) == 1 {
		group := conf.Groups[0]
		// check for regexp characters that cannot be in a group
		if configs.SpecialRegExp.MatchString(group) {
			filter.groupExp, err = regexp.Compile(group)
			if err != nil {
				log.Log(log.Config).Debug("Filter group expression does not compile", zap.Any("groupFilter", conf.Groups))
			}
		} else if configs.GroupRegExp.MatchString(group) {
			// regexp not found consider this a group, sanity check the entry
			// single entry just a group
			filter.groupList[group] = true
		}
		filter.empty = false
	}
	// if there are 2 or more groups create a list
	if len(conf.Groups) >= 2 {
		for _, group := range conf.Groups {
			// sanity check the entry, do not add if it does not comply
			if configs.GroupRegExp.MatchString(group) {
				filter.groupList[group] = true
			}
		}
		if len(filter.groupList) != len(conf.Groups) {
			log.Log(log.Config).Info("Filter creation duplicate or invalid groups found", zap.Any("groupFilter", conf.Groups))
		}
		filter.empty = false
	}

	// check what we have created
	if len(conf.Groups) > 0 && filter.groupExp == nil && len(filter.groupList) == 0 {
		log.Log(log.Config).Info("Filter creation partially failed (groups)", zap.Any("groupFilter", conf.Groups))
	}

	// log the filter with all details (only at debug)
	logFilter(&filter)
	return filter
}