func (cs *clusterState) agents() map[string]*agent.AgentState { agents := make(map[string]*agent.AgentState, len(cs.machines)) for _, ms := range cs.machines { ms := ms agents[ms.ID] = agent.NewAgentState(ms) } for _, j := range cs.jobs { j := j if !j.Scheduled() || j.TargetState == job.JobStateInactive { continue } if as, ok := agents[j.TargetMachineID]; ok { u := &job.Unit{ Name: j.Name, Unit: j.Unit, TargetState: j.TargetState, } as.Units[j.Name] = u } } for _, gu := range cs.gUnits { gu := gu for _, a := range agents { if machine.HasMetadata(a.MState, gu.RequiredTargetMetadata()) { a.Units[gu.Name] = gu } } } return agents }
// AbleToRun determines if an Agent can run the provided Job based on // the Agent's current state. A boolean indicating whether this is the // case or not is returned. The following criteria is used: // - Agent must meet the Job's machine target requirement (if any) // - Agent must have all of the Job's required metadata (if any) // - Agent must have all required Peers of the Job scheduled locally (if any) // - Job must not conflict with any other Jobs scheduled to the agent func (as *AgentState) AbleToRun(j *job.Job) (bool, string) { if tgt, ok := j.RequiredTarget(); ok && !as.MState.MatchID(tgt) { return false, fmt.Sprintf("agent ID %q does not match required %q", as.MState.ID, tgt) } metadata := j.RequiredTargetMetadata() if len(metadata) != 0 { if !machine.HasMetadata(as.MState, metadata) { return false, "local Machine metadata insufficient" } } peers := j.Peers() if len(peers) != 0 { for _, peer := range peers { if !as.jobScheduled(peer) { return false, fmt.Sprintf("required peer Job(%s) is not scheduled locally", peer) } } } if cExists, cJobName := as.hasConflict(j.Name, j.Conflicts()); cExists { return false, fmt.Sprintf("found conflict with locally-scheduled Job(%s)", cJobName) } return true, "" }
// AbleToRun determines if an Agent can run the provided Job based on // the Agent's current state. A boolean indicating whether this is the // case or not is returned. The following criteria is used: // - Agent must meet the Job's machine target requirement (if any) // - Agent must have all of the Job's required metadata (if any) // - Agent must have all required Peers of the Job scheduled locally (if any) // - Job must not conflict with any other Units scheduled to the agent // - Job must specially handle replaced units to be rescheduled func (as *AgentState) AbleToRun(j *job.Job) (jobAction job.JobAction, errstr string) { if tgt, ok := j.RequiredTarget(); ok && !as.MState.MatchID(tgt) { return job.JobActionUnschedule, fmt.Sprintf("agent ID %q does not match required %q", as.MState.ID, tgt) } metadata := j.RequiredTargetMetadata() if len(metadata) != 0 { if !machine.HasMetadata(as.MState, metadata) { return job.JobActionUnschedule, "local Machine metadata insufficient" } } peers := j.Peers() if len(peers) != 0 { for _, peer := range peers { if !as.unitScheduled(peer) { return job.JobActionUnschedule, fmt.Sprintf("required peer Unit(%s) is not scheduled locally", peer) } } } if cExists, cJobName := as.HasConflict(j.Name, j.Conflicts()); cExists { return job.JobActionUnschedule, fmt.Sprintf("found conflict with locally-scheduled Unit(%s)", cJobName) } // Handle Replace option specially for rescheduling the unit if cExists, cJobName := as.hasReplace(j.Name, j.Replaces()); cExists { return job.JobActionReschedule, fmt.Sprintf("found replace with locally-scheduled Unit(%s)", cJobName) } return job.JobActionSchedule, "" }
// desiredAgentState builds an *AgentState object that represents what the // provided Agent should currently be doing. func desiredAgentState(a *Agent, reg registry.Registry) (*AgentState, error) { units, err := reg.Units() if err != nil { log.Errorf("Failed fetching Units from Registry: %v", err) return nil, err } sUnits, err := reg.Schedule() if err != nil { log.Errorf("Failed fetching schedule from Registry: %v", err) return nil, err } // fetch full machine state from registry instead of // using the local version to allow for dynamic metadata ms, err := reg.MachineState(a.Machine.State().ID) if err != nil { log.Errorf("Failed fetching machine state from Registry: %v", err) return nil, err } as := AgentState{ MState: &ms, Units: make(map[string]*job.Unit), } sUnitMap := make(map[string]*job.ScheduledUnit) for _, sUnit := range sUnits { sUnit := sUnit sUnitMap[sUnit.Name] = &sUnit } for _, u := range units { u := u md := u.RequiredTargetMetadata() if u.IsGlobal() { if !machine.HasMetadata(&ms, md) { log.Debugf("Agent unable to run global unit %s: missing required metadata", u.Name) continue } } if !u.IsGlobal() { sUnit, ok := sUnitMap[u.Name] if !ok || sUnit.TargetMachineID == "" || sUnit.TargetMachineID != ms.ID { continue } } if cExists, _ := as.HasConflict(u.Name, u.Conflicts()); cExists { continue } as.Units[u.Name] = &u } return &as, nil }
// Determine if the Agent can run the provided Job func (a *Agent) ableToRun(j *job.Job) bool { if !a.verifyJob(j) { log.V(1).Infof("Failed to verify Job(%s)", j.Name) return false } requirements := j.Requirements() if len(requirements) == 0 { log.V(1).Infof("Job(%s) has no requirements", j.Name) } log.Infof("Job(%s) has requirements: %s", j.Name, requirements) metadata := j.RequiredTargetMetadata() log.V(1).Infof("Job(%s) requires machine metadata: %v", j.Name, metadata) ms := a.Machine.State() if !machine.HasMetadata(&ms, metadata) { log.Infof("Unable to run Job(%s), local Machine metadata insufficient", j.Name) return false } if tgt, ok := j.RequiredTarget(); ok && !a.Machine.State().MatchID(tgt) { log.Infof("Agent does not meet machine target requirement for Job(%s)", j.Name) return false } peers := j.Peers() if len(peers) > 0 { log.V(1).Infof("Asserting required Peers %v of Job(%s) are scheduled locally", peers, j.Name) for _, peer := range peers { if !a.peerScheduledHere(j.Name, peer) { log.Infof("Required Peer(%s) of Job(%s) is not scheduled locally", peer, j.Name) return false } } } else { log.V(1).Infof("Job(%s) has no peers to worry about", j.Name) } if conflicted, conflictedJobName := a.HasConflict(j.Name, j.Conflicts()); conflicted { log.Infof("Job(%s) has conflict with Job(%s)", j.Name, conflictedJobName) return false } return true }
// ableToRun determines if the Agent can run the provided Job, and returns a boolean indicating // whether this is the case. There are five criteria for an Agent to be eligible to run a Job: // - Job must pass signature verification // - agent must have all of the Job's required metadata (if any) // - agent must meet the Job's machine target requirement (if any) // - agent must have all required Peers of the Job scheduled locally (if any) // - Job must not conflict with any other Jobs scheduled to the agent func (a *Agent) ableToRun(j *job.Job) bool { if !a.verifyJobSignature(j) { log.V(1).Infof("Failed to verify Job(%s)", j.Name) return false } log.Infof("Job(%s) has requirements: %s", j.Name, j.Requirements()) metadata := j.RequiredTargetMetadata() if len(metadata) == 0 { log.V(1).Infof("Job(%s) has no required machine metadata", j.Name) } else { log.V(1).Infof("Job(%s) requires machine metadata: %v", j.Name, metadata) ms := a.Machine.State() if !machine.HasMetadata(&ms, metadata) { log.Infof("Unable to run Job(%s): local Machine metadata insufficient", j.Name) return false } } if tgt, ok := j.RequiredTarget(); ok && !a.Machine.State().MatchID(tgt) { log.Infof("Unable to run Job(%s): agent does not meet machine target requirement (%s)", j.Name, tgt) return false } peers := j.Peers() if len(peers) == 0 { log.V(1).Infof("Job(%s) has no required peers", j.Name) } else { log.V(1).Infof("Job(%s) requires peers: %v", j.Name, peers) for _, peer := range peers { if !a.peerScheduledHere(j.Name, peer) { log.Infof("Unable to run Job(%s): required Peer(%s) is not scheduled locally", j.Name, peer) return false } } } if conflicted, conflictedJobName := a.HasConflict(j.Name, j.Conflicts()); conflicted { log.Infof("Unable to run Job(%s): conflict with Job(%s)", j.Name, conflictedJobName) return false } return true }
// desiredAgentState builds an *AgentState object that represents what the // provided Agent should currently be doing. func desiredAgentState(a *Agent, reg registry.Registry) (*AgentState, error) { units, err := reg.Units() if err != nil { log.Errorf("Failed fetching Units from Registry: %v", err) return nil, err } sUnits, err := reg.Schedule() if err != nil { log.Errorf("Failed fetching schedule from Registry: %v", err) return nil, err } ms := a.Machine.State() as := AgentState{ MState: &ms, Units: make(map[string]*job.Unit), } sUnitMap := make(map[string]*job.ScheduledUnit) for _, sUnit := range sUnits { sUnit := sUnit sUnitMap[sUnit.Name] = &sUnit } for _, u := range units { u := u md := u.RequiredTargetMetadata() if u.IsGlobal() && !machine.HasMetadata(&ms, md) { log.Debugf("Agent unable to run global unit %s: missing required metadata", u.Name) continue } if !u.IsGlobal() { sUnit, ok := sUnitMap[u.Name] if !ok || sUnit.TargetMachineID == "" || sUnit.TargetMachineID != ms.ID { continue } } as.Units[u.Name] = &u } return &as, nil }
// ableToRun determines if the Agent can run the provided Job based on // the Agent's desired state. A boolean indicating whether this is the // case or not is returned. The following criteria is used: // - Agent must meet the Job's machine target requirement (if any) // - Job must pass signature verification // - Agent must have all of the Job's required metadata (if any) // - Agent must have all required Peers of the Job scheduled locally (if any) // - Job must not conflict with any other Jobs scheduled to the agent func (ar *AgentReconciler) ableToRun(as *agentState, ms *machine.MachineState, j *job.Job) (bool, string) { log.V(1).Infof("Attempting to determine if able to run Job(%s)", j.Name) if tgt, ok := j.RequiredTarget(); ok && !ms.MatchID(tgt) { return false, fmt.Sprintf("Agent ID %q does not match required %q", ms.ID, tgt) } if !ar.verifyJobSignature(j) { return false, "unable to verify signature" } log.V(1).Infof("Job(%s) has requirements: %s", j.Name, j.Requirements()) metadata := j.RequiredTargetMetadata() if len(metadata) == 0 { log.V(1).Infof("Job(%s) has no required machine metadata", j.Name) } else { log.V(1).Infof("Job(%s) requires machine metadata: %v", j.Name, metadata) if !machine.HasMetadata(ms, metadata) { return false, "local Machine metadata insufficient" } } peers := j.Peers() if len(peers) == 0 { log.V(1).Infof("Job(%s) has no required peers", j.Name) } else { log.V(1).Infof("Job(%s) requires peers: %v", j.Name, peers) for _, peer := range peers { if !as.jobScheduled(peer) { return false, fmt.Sprintf("required peer Job(%s) is not scheduled locally", peer) } } } if cExists, cJobName := as.hasConflict(j.Name, j.Conflicts()); cExists { return false, fmt.Sprintf("found conflict with locally-scheduled Job(%s)", cJobName) } log.V(1).Infof("Determined local Agent is able to run Job(%s)", j.Name) return true, "" }