1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.maintenance;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.organization.Contact;
import com.yahoo.vespa.hosted.controller.api.integration.organization.IssueId;
import com.yahoo.vespa.hosted.controller.api.integration.organization.OwnershipIssues;
import com.yahoo.vespa.hosted.controller.api.integration.organization.User;
import com.yahoo.vespa.hosted.controller.application.ApplicationList;
import com.yahoo.vespa.hosted.controller.tenant.AthenzTenant;
import com.yahoo.vespa.hosted.controller.tenant.Tenant;
import com.yahoo.vespa.hosted.controller.tenant.UserTenant;
import com.yahoo.yolean.Exceptions;
import java.time.Duration;
import java.util.Optional;
import java.util.logging.Level;
/**
* Periodically request application ownership confirmation through filing issues.
*
* When to file new issues, escalate inactive ones, etc., is handled by the enclosed OwnershipIssues.
*
* @author jonmv
*/
public class ApplicationOwnershipConfirmer extends Maintainer {
private final OwnershipIssues ownershipIssues;
public ApplicationOwnershipConfirmer(Controller controller, Duration interval, JobControl jobControl, OwnershipIssues ownershipIssues) {
super(controller, interval, jobControl);
this.ownershipIssues = ownershipIssues;
}
@Override
protected void maintain() {
confirmApplicationOwnerships();
ensureConfirmationResponses();
updateConfirmedApplicationOwners();
}
/** File an ownership issue with the owners of all applications we know about. */
private void confirmApplicationOwnerships() {
ApplicationList.from(controller().applications().asList())
.withProjectId()
.hasProductionDeployment()
.asList()
.stream()
.filter(application -> application.createdAt().isBefore(controller().clock().instant().minus(Duration.ofDays(90))))
.forEach(application -> {
try {
Tenant tenant = tenantOf(application.id());
Optional<IssueId> ourIssueId = application.ownershipIssueId();
Contact contact = tenant.contact().orElseThrow(RuntimeException::new);
User assignee = determineAssignee(tenant, application);
ourIssueId = ownershipIssues.confirmOwnership(ourIssueId, application.id(), assignee, contact);
ourIssueId.ifPresent(issueId -> store(issueId, application.id()));
}
catch (RuntimeException e) { // Catch errors due to wrong data in the controller, or issues client timeout.
log.log(Level.INFO, "Exception caught when attempting to file an issue for '" + application.id() + "': " + Exceptions.toMessageString(e));
}
});
}
/** Escalate ownership issues which have not been closed before a defined amount of time has passed. */
private void ensureConfirmationResponses() {
for (Application application : controller().applications().asList())
application.ownershipIssueId().ifPresent(issueId -> {
try {
Optional<Contact> contact = Optional.of(application.id())
.map(this::tenantOf)
.filter(t -> t instanceof AthenzTenant)
.flatMap(Tenant::contact);
ownershipIssues.ensureResponse(issueId, contact);
}
catch (RuntimeException e) {
log.log(Level.INFO, "Exception caught when attempting to escalate issue with id '" + issueId + "': " + Exceptions.toMessageString(e));
}
});
}
private void updateConfirmedApplicationOwners() {
ApplicationList.from(controller().applications().asList())
.withProjectId()
.hasProductionDeployment()
.asList()
.stream()
.filter(application -> application.ownershipIssueId().isPresent())
.forEach(application -> {
IssueId ownershipIssueId = application.ownershipIssueId().get();
ownershipIssues.getConfirmedOwner(ownershipIssueId).ifPresent(owner -> {
controller().applications().lockIfPresent(application.id(), lockedApplication ->
controller().applications().store(lockedApplication.withOwner(owner)));
});
});
}
private User determineAssignee(Tenant tenant, Application application) {
return application.owner().orElse(
tenant instanceof UserTenant ? userFor(tenant) : null
);
}
private Tenant tenantOf(ApplicationId applicationId) {
return controller().tenants().tenant(applicationId.tenant())
.orElseThrow(() -> new IllegalStateException("No tenant found for application " + applicationId));
}
protected User userFor(Tenant tenant) {
return User.from(tenant.name().value().replaceFirst(Tenant.userPrefix, ""));
}
protected void store(IssueId issueId, ApplicationId applicationId) {
controller().applications().lockIfPresent(applicationId, application ->
controller().applications().store(application.withOwnershipIssueId(issueId)));
}
}
|