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
|
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.Tenant;
import com.yahoo.vespa.hosted.controller.api.application.v4.model.TenantType;
import com.yahoo.vespa.hosted.controller.api.identifiers.PropertyId;
import com.yahoo.vespa.hosted.controller.api.identifiers.TenantId;
import com.yahoo.vespa.hosted.controller.api.integration.organization.OwnershipIssues;
import com.yahoo.vespa.hosted.controller.api.integration.organization.IssueId;
import com.yahoo.vespa.hosted.controller.api.integration.organization.User;
import com.yahoo.vespa.hosted.controller.application.ApplicationList;
import java.time.Duration;
import java.util.NoSuchElementException;
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 jvenstad
*/
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();
}
/** File an ownership issue with the owners of all applications we know about. */
private void confirmApplicationOwnerships() {
ApplicationList.from(controller().applications().asList())
.notPullRequest()
.hasProductionDeployment()
.asList()
.forEach(application -> {
try {
Tenant tenant = ownerOf(application.id());
Optional<IssueId> ourIssueId = application.ownershipIssueId();
ourIssueId = tenant.tenantType() == TenantType.USER
? ownershipIssues.confirmOwnership(ourIssueId, application.id(), userFor(tenant))
: ownershipIssues.confirmOwnership(ourIssueId, application.id(), propertyIdFor(tenant));
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.WARNING, "Exception caught when attempting to file an issue for " + application.id(), 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 {
ownershipIssues.ensureResponse(issueId, ownerOf(application.id()).getPropertyId());
}
catch (RuntimeException e) {
log.log(Level.WARNING, "Exception caught when attempting to escalate issue with id " + issueId, e);
}
});
}
private Tenant ownerOf(ApplicationId applicationId) {
return controller().tenants().tenant(new TenantId(applicationId.tenant().value()))
.orElseThrow(() -> new IllegalStateException("No tenant found for application " + applicationId));
}
protected User userFor(Tenant tenant) {
return User.from(tenant.getId().id().replaceFirst("by-", ""));
}
protected PropertyId propertyIdFor(Tenant tenant) {
return tenant.getPropertyId()
.orElseThrow(() -> new NoSuchElementException("No PropertyId is listed for non-user tenant " + tenant));
}
protected void store(IssueId issueId, ApplicationId applicationId) {
controller().applications().lockIfPresent(applicationId, application ->
controller().applications().store(application.withOwnershipIssueId(issueId)));
}
}
|